From 935cb35715ffb46e161b90a67d19781e96c2ad3d Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Sun, 27 Apr 2025 21:58:23 +0100 Subject: [PATCH] Allowed parser to parse basic variable declarations --- Parser/src/Parser.cpp | 24 +++++++++++++++++++++++- example/main.lx | 2 +- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/Parser/src/Parser.cpp b/Parser/src/Parser.cpp index 75a448c..4fa0cd8 100644 --- a/Parser/src/Parser.cpp +++ b/Parser/src/Parser.cpp @@ -108,12 +108,33 @@ namespace LX // Else goes down the call stack // return ParseOperation(p); } + + // Handles variable declarations, if not calls ParseReturn // + static std::unique_ptr ParseVarDeclaration(Parser& p) + { + // Checks if the current token is a declaration // + if (p.tokens[p.index].type == Token::INT_DEC) + { + // Skips over the dec token // + p.index++; + + // Checks for the variable name // + ThrowIf(p.tokens[p.index].type != Token::IDENTIFIER, Token::IDENTIFIER, "", p.tokens[p.index]); + p.index++; // <- Goes over the identifier token + + // Returns the variable declaration as an AST node by creating it with it's name // + return std::make_unique(p.tokens[p.index - 1].GetContents()); + } + + // Else goes down the call stack // + return ParseReturn(p); + } // Helper function to call the top of the Parse-Call-Stack // static inline std::unique_ptr Parse(Parser& p) { // Parses the current token // - std::unique_ptr out = ParseReturn(p); + std::unique_ptr out = ParseVarDeclaration(p); // Checks it is valid before returning // ThrowIf(out == nullptr, Token::UNDEFINED, "top level statement", p.tokens[p.index - 1]); @@ -154,6 +175,7 @@ namespace LX p.index++; // Loops over all the arguments of the function // + // TODO: Do something with the parameters while (p.index < p.len && (p.tokens[p.index].type == Token::CLOSE_PAREN) == false) { p.index++; diff --git a/example/main.lx b/example/main.lx index 50cf183..ad87d1d 100644 --- a/example/main.lx +++ b/example/main.lx @@ -1,4 +1,4 @@ func main() { - int a + int result }