From 6b384cda5da84c2373667eab12f1037fd8d42878 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sun, 15 Jan 2023 17:57:29 +0100 Subject: [PATCH] AST - Store superclass as a var expr --- src/ast.rs | 2 +- src/dumper.rs | 4 +++- src/parser.rs | 5 ++++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/ast.rs b/src/ast.rs index 8db7572..7f9d790 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -38,7 +38,7 @@ pub struct ClassDecl { /// The token that represents the name of the class. pub name: Token, /// The token indicating the name of the parent class, if any. - pub superclass: Option, + pub superclass: Option, /// The list of class members. pub members: Vec, } diff --git a/src/dumper.rs b/src/dumper.rs index 038cce8..0c15ef5 100644 --- a/src/dumper.rs +++ b/src/dumper.rs @@ -97,7 +97,9 @@ fn dump_statement(dumper: &mut Dumper, stmt: &StmtNode) { StmtNode::ClassDecl(decl) => { dumper.add_line(format!("class {}", decl.name.lexeme)); if let Some(superclass) = &decl.superclass { - dumper.current_line().push_str(&format!(" < {}", superclass.lexeme)); + dumper + .current_line() + .push_str(&format!(" < {}", superclass.token.lexeme)); } dumper.current_line().push_str(" {"); if !decl.members.is_empty() { diff --git a/src/parser.rs b/src/parser.rs index eaf97fd..b9c8407 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -227,7 +227,10 @@ impl Parser { let name = self.consume_identifier("expected class name")?; let superclass = match self.expect(&[TokenType::Less]) { None => None, - Some(_) => Some(self.consume_identifier("expected superclass name")?), + Some(_) => Some(VariableExpr { + token: self.consume_identifier("expected superclass name")?.clone(), + id: self.make_id(), + }), }; self.consume(&TokenType::LeftBrace, "'{' expected")?;