]> git.leonardobizzoni.com Git - LBPL/commitdiff
Forgot to interpret "this" expresions
authorLeonardoBizzoni <leo2002714@gmail.com>
Sun, 29 Oct 2023 17:17:31 +0000 (18:17 +0100)
committerLeonardoBizzoni <leo2002714@gmail.com>
Sun, 29 Oct 2023 17:17:31 +0000 (18:17 +0100)
lib/expressions.h
src/interpreter.cpp
src/parser.cpp

index 3b7f324219a6b2d3a23dcb428b13fa799f9a4ba7..81fa7ee5b53995488b7b8915930365817fef0369 100755 (executable)
@@ -2,6 +2,7 @@
 #define EXPRESSIONS_H
 
 #include "common.h"
+#include "token.h"
 #include "visitor.h"
 
 #include <cstdint>
@@ -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<const Token> keyword;
+
+  ThisExpr(int line, int column, const std::string &file, std::shared_ptr<const Token>& keyword)
+      : keyword(keyword), Expr(line, column, file) {}
+
   LBPLType accept(Expression::Visitor *visitor) {
     return visitor->visitThisExpr(this);
   }
index ff68f4ab238a618d8aed904e86ca8ecbde6bb16a..58fc6b218f687dcc09e80c03a3decba0a3c14db1 100644 (file)
@@ -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);
index d94e9e606df09e0898f03b8e1d2a5187001decd2..83a80de977047b2afa140916b809802e108ba038 100755 (executable)
@@ -455,7 +455,7 @@ std::unique_ptr<Expr> Parser::primary() {
 
     return std::make_unique<SuperExpr>(line, col, lexer->getFilename(), field);
   } else if (match(TokenType::This)) {
-    return std::make_unique<ThisExpr>(line, col, lexer->getFilename());
+    return std::make_unique<ThisExpr>(line, col, lexer->getFilename(), previous);
   } else if (match(TokenType::Identifier)) {
     return std::make_unique<VariableExpr>(line, col, lexer->getFilename(),
                                           previous);