Inheritance 13

This commit is contained in:
nazrin 2025-06-02 23:44:23 +00:00
parent d8ac625429
commit 848c846e09
12 changed files with 117 additions and 7 deletions

View file

@ -312,6 +312,12 @@ class Parser{
consume(TokenType.RIGHT_PAREN, "Expect ')' after expression.");
return new Expr.Grouping(expr);
}
if(match(TokenType.SUPER)){
Token keyword = previous();
consume(TokenType.DOT, "Expect '.' after 'super'.");
Token method = consume(TokenType.IDENTIFIER, "Expect superclass method name.");
return new Expr.Super(keyword, method);
}
throw error(peek(), "Expect expression.");
}
private Stmt varDeclaration(){
@ -324,12 +330,17 @@ class Parser{
}
private Stmt classDeclaration() {
Token name = consume(TokenType.IDENTIFIER, "Expect class name.");
Expr.Variable superclass;
if(match(TokenType.LESS)){
consume(TokenType.IDENTIFIER, "Expect superlcass name.");
superclass = new Expr.Variable(previous);
}
consume(TokenType.LEFT_BRACE, "Expect '{' before class body.");
Stmt.Function[] methods;
while(!check(TokenType.RIGHT_BRACE) && !isAtEnd)
methods ~= fun("method");
consume(TokenType.RIGHT_BRACE, "Expect '}' after class body.");
return new Stmt.Class(name, methods);
return new Stmt.Class(name, superclass, methods);
}
private Stmt declaration(){
try {