From bb4720feac00710c499acf61b98c8da4d4351d1e Mon Sep 17 00:00:00 2001 From: LeonardoBizzoni Date: Sun, 29 Oct 2023 18:17:31 +0100 Subject: [PATCH] Forgot to interpret "this" expresions --- lib/expressions.h | 9 +++++++-- src/interpreter.cpp | 2 +- src/parser.cpp | 2 +- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/expressions.h b/lib/expressions.h index 3b7f324..81fa7ee 100755 --- a/lib/expressions.h +++ b/lib/expressions.h @@ -2,6 +2,7 @@ #define EXPRESSIONS_H #include "common.h" +#include "token.h" #include "visitor.h" #include @@ -95,8 +96,12 @@ struct SuperExpr : public Expr { }; struct ThisExpr : public Expr { - ThisExpr(int line, int column, const std::string &file) - : Expr(line, column, file) {} +public: + std::shared_ptr keyword; + + ThisExpr(int line, int column, const std::string &file, std::shared_ptr& keyword) + : keyword(keyword), Expr(line, column, file) {} + LBPLType accept(Expression::Visitor *visitor) { return visitor->visitThisExpr(this); } diff --git a/src/interpreter.cpp b/src/interpreter.cpp index ff68f4a..58fc6b2 100644 --- a/src/interpreter.cpp +++ b/src/interpreter.cpp @@ -185,7 +185,7 @@ LBPLType Interpreter::visitGroupExpr(GroupingExpr *expr) { } LBPLType Interpreter::visitSuperExpr(SuperExpr *) { return nullptr; } -LBPLType Interpreter::visitThisExpr(ThisExpr *) { return nullptr; } +LBPLType Interpreter::visitThisExpr(ThisExpr *expr) { return lookupVariable(expr->keyword, expr); } LBPLType Interpreter::visitCallExpr(FnCallExpr *expr) { LBPLType callee = expr->callee->accept(this); diff --git a/src/parser.cpp b/src/parser.cpp index d94e9e6..83a80de 100755 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -455,7 +455,7 @@ std::unique_ptr Parser::primary() { return std::make_unique(line, col, lexer->getFilename(), field); } else if (match(TokenType::This)) { - return std::make_unique(line, col, lexer->getFilename()); + return std::make_unique(line, col, lexer->getFilename(), previous); } else if (match(TokenType::Identifier)) { return std::make_unique(line, col, lexer->getFilename(), previous); -- 2.52.0