AST - Store superclass as a var expr

This commit is contained in:
Emmanuel BENOîT 2023-01-15 17:57:29 +01:00
parent 33c28db780
commit 6b384cda5d
3 changed files with 8 additions and 3 deletions

View file

@ -38,7 +38,7 @@ pub struct ClassDecl {
/// The token that represents the name of the class. /// The token that represents the name of the class.
pub name: Token, pub name: Token,
/// The token indicating the name of the parent class, if any. /// The token indicating the name of the parent class, if any.
pub superclass: Option<Token>, pub superclass: Option<VariableExpr>,
/// The list of class members. /// The list of class members.
pub members: Vec<ClassMemberDecl>, pub members: Vec<ClassMemberDecl>,
} }

View file

@ -97,7 +97,9 @@ fn dump_statement(dumper: &mut Dumper, stmt: &StmtNode) {
StmtNode::ClassDecl(decl) => { StmtNode::ClassDecl(decl) => {
dumper.add_line(format!("class {}", decl.name.lexeme)); dumper.add_line(format!("class {}", decl.name.lexeme));
if let Some(superclass) = &decl.superclass { 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(" {"); dumper.current_line().push_str(" {");
if !decl.members.is_empty() { if !decl.members.is_empty() {

View file

@ -227,7 +227,10 @@ impl Parser {
let name = self.consume_identifier("expected class name")?; let name = self.consume_identifier("expected class name")?;
let superclass = match self.expect(&[TokenType::Less]) { let superclass = match self.expect(&[TokenType::Less]) {
None => None, 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")?; self.consume(&TokenType::LeftBrace, "'{' expected")?;