AST - Added superclass field

This commit is contained in:
Emmanuel BENOîT 2023-01-15 17:47:52 +01:00
parent 680c4b249d
commit 164e7952bd
3 changed files with 12 additions and 2 deletions

View file

@ -37,6 +37,8 @@ pub struct ClassMemberDecl {
pub struct ClassDecl { 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.
pub superclass: Option<Token>,
/// The list of class members. /// The list of class members.
pub members: Vec<ClassMemberDecl>, pub members: Vec<ClassMemberDecl>,
} }

View file

@ -95,7 +95,11 @@ 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 {
dumper.current_line().push_str(&format!(" < {}", superclass.lexeme));
}
dumper.current_line().push_str(" {");
if !decl.members.is_empty() { if !decl.members.is_empty() {
dumper.depth += 1; dumper.depth += 1;
for member in decl.members.iter() { for member in decl.members.iter() {

View file

@ -257,7 +257,11 @@ impl Parser {
} }
self.consume(&TokenType::RightBrace, "'}' expected")?; self.consume(&TokenType::RightBrace, "'}' expected")?;
Ok(StmtNode::ClassDecl(ClassDecl { name, members })) Ok(StmtNode::ClassDecl(ClassDecl {
name,
superclass: None,
members,
}))
} }
/// Parse the following rule: /// Parse the following rule: