From 794173e14f9079b87a08d188a1d2a225eea72c07 Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Thu, 8 May 2025 15:04:01 +0100 Subject: [PATCH] Variables can now have an initializer --- Parser/src/Parser.cpp | 13 +++++++++---- example/main.lx | 5 +++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Parser/src/Parser.cpp b/Parser/src/Parser.cpp index 2d50b61..1dd0901 100644 --- a/Parser/src/Parser.cpp +++ b/Parser/src/Parser.cpp @@ -129,10 +129,15 @@ namespace LX p.index++; // Skips over Token::ASSIGN // Gets the value to be assigned to the variable // - std::unique_ptr defaultVal = ParsePrimary(p); + std::unique_ptr defaultVal = ParseOperation(p); ThrowIf(defaultVal.get() == nullptr, Token::UNDEFINED, p.tokens[p.index - 1], "value", p); - return std::make_unique(name); + // Creates a multi-node of the variable creation and assignment // + std::unique_ptr node = std::make_unique(); + node->nodes.push_back(std::make_unique(name)); + node->nodes.push_back(std::make_unique(name, std::move(defaultVal))); + + return node; } // Else goes down the call stack // @@ -181,7 +186,7 @@ namespace LX FileAST TurnTokensIntoAbstractSyntaxTree(std::vector& tokens, const std::filesystem::path& path) { // Logs the start of the parsing - Log::LogNewSection("Started parsing tokens"); + Log::LogNewSection("Started parsing tokens"); // Creates the output storer and the parser // FileAST output; @@ -236,7 +241,7 @@ namespace LX for (std::unique_ptr& containedNode : ((AST::MultiNode*)node.get())->nodes) { // Logs the node to the log // - node->Log(0); + containedNode->Log(0); // Adds it to the vector // func.body.push_back(std::move(containedNode)); diff --git a/example/main.lx b/example/main.lx index 672959f..b2e1b8d 100644 --- a/example/main.lx +++ b/example/main.lx @@ -1,8 +1,9 @@ func main() { - int a = 65465 + int a + a = 5 + 2 - int b = 6 + int b = 6 + 1 return a + b }