From 2a84b6d41a16c74f166039f6f19862ce9ae2b32f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludwig=20Mostr=C3=B6m?= <ludmo578@student.liu.se> Date: Sun, 6 Apr 2025 19:37:14 +0200 Subject: [PATCH] Identifiers can now reach variables in the scope. However not currently working with arithmetic operations. Think the AST for that one has priority before variable lookup --- examples/var.funk | 2 ++ include/ast/declaration/VariableNode.h | 2 ++ source/ast/declaration/VariableNode.cc | 12 ++++++++++++ source/parser/Parser.cc | 2 +- 4 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 examples/var.funk diff --git a/examples/var.funk b/examples/var.funk new file mode 100644 index 0000000..d6e8689 --- /dev/null +++ b/examples/var.funk @@ -0,0 +1,2 @@ +numb test = 5; +print(test); \ No newline at end of file diff --git a/include/ast/declaration/VariableNode.h b/include/ast/declaration/VariableNode.h index 1e5b899..f26d97e 100644 --- a/include/ast/declaration/VariableNode.h +++ b/include/ast/declaration/VariableNode.h @@ -2,6 +2,7 @@ #include "ast/Node.h" #include "ast/expression/ExpressionNode.h" +#include "parser/Scope.h" #include "token/TokenType.h" namespace funk @@ -12,6 +13,7 @@ class VariableNode : public ExpressionNode public: VariableNode(const SourceLocation& location, const String& identifier, bool is_mutable, TokenType type, ExpressionNode* value); + VariableNode(const SourceLocation& location, const String& identifier); ~VariableNode() override; Node* evaluate() const override; diff --git a/source/ast/declaration/VariableNode.cc b/source/ast/declaration/VariableNode.cc index 0639e34..69958e1 100644 --- a/source/ast/declaration/VariableNode.cc +++ b/source/ast/declaration/VariableNode.cc @@ -7,6 +7,12 @@ VariableNode::VariableNode( ExpressionNode{location}, identifier{identifier}, is_mutable{is_mutable}, type{type}, value{value} { } + +VariableNode::VariableNode(const SourceLocation& location, const String& identifier) : + ExpressionNode{location}, identifier{identifier} +{ +} + VariableNode::~VariableNode() { if (value) { delete value; } @@ -15,6 +21,12 @@ VariableNode::~VariableNode() Node* VariableNode::evaluate() const { + if (value == nullptr) + { + Node* result = Scope::instance().get(identifier); + if (result == nullptr) { throw RuntimeError(get_location(), "Undefined variable '" + identifier + "'"); } + return result; + } return new VariableNode(get_location(), identifier, is_mutable, type, value); } diff --git a/source/parser/Parser.cc b/source/parser/Parser.cc index eb66ad0..4933598 100644 --- a/source/parser/Parser.cc +++ b/source/parser/Parser.cc @@ -328,7 +328,7 @@ Node* Parser::parse_identifier() new VariableNode(identifier.get_location(), identifier.get_lexeme(), false, TokenType::NONE, nullptr)); } - return new VariableNode(identifier.get_location(), identifier.get_lexeme(), false, TokenType::NONE, nullptr); + return new VariableNode(identifier.get_location(), identifier.get_lexeme()); } Node* Parser::parse_call(const Token& identifier) -- GitLab