From 2a8230ac8027ec5feee7961e84ad68e53510d35e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Sun, 15 Jan 2023 18:50:16 +0100 Subject: [PATCH] AST - "Super expressions" (references to superclass methods) --- src/ast.rs | 11 +++++++++++ src/dumper.rs | 5 +++++ src/interpreter/interpretable.rs | 1 + src/resolver.rs | 2 ++ 4 files changed, 19 insertions(+) diff --git a/src/ast.rs b/src/ast.rs index 7f9d790..ec75403 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -134,6 +134,15 @@ pub struct VariableExpr { pub id: usize, } +/// A reference to a method in the superclass. +#[derive(Debug, Clone)] +pub struct SuperExpr { + /// The 'super' token itself + pub keyword: Token, + /// The identifier that contains the method name + pub method: Token, +} + /// An AST node that represents an expression. #[derive(Debug, Clone)] pub enum ExprNode { @@ -189,4 +198,6 @@ pub enum ExprNode { Get(GetExpr), /// A set expression. Set(SetExpr), + /// A reference to a method in the superclass + Super(SuperExpr), } diff --git a/src/dumper.rs b/src/dumper.rs index 0c15ef5..c3d0ade 100644 --- a/src/dumper.rs +++ b/src/dumper.rs @@ -283,6 +283,11 @@ fn dump_expr_node(dumper: &mut Dumper, expr: &ExprNode) { dumper.current_line().push_str(" = "); dump_expr_node(dumper, &set_expr.value); } + + ExprNode::Super(super_expr) => { + dumper.current_line().push_str("super."); + dumper.current_line().push_str(&super_expr.method.lexeme); + } } } diff --git a/src/interpreter/interpretable.rs b/src/interpreter/interpretable.rs index 0a11f98..b3d229d 100644 --- a/src/interpreter/interpretable.rs +++ b/src/interpreter/interpretable.rs @@ -389,6 +389,7 @@ impl Interpretable for ExprNode { } ExprNode::Get(get_expr) => self.on_get_expression(es, get_expr), ExprNode::Set(set_expr) => self.on_set_expression(es, set_expr), + ExprNode::Super(_) => todo!(), } } } diff --git a/src/resolver.rs b/src/resolver.rs index 403d637..9a8bcbf 100644 --- a/src/resolver.rs +++ b/src/resolver.rs @@ -485,6 +485,8 @@ impl VarResolver for ExprNode { .instance .resolve(rs) .and_then(|_| set_expr.value.resolve(rs)), + + ExprNode::Super(_) => todo!(), } } }