From e3ed34c205ee2581f496439af92ca2918f681bb2 Mon Sep 17 00:00:00 2001 From: Pasha Bibko <156938226+PashaBibko@users.noreply.github.com> Date: Mon, 4 Aug 2025 16:07:35 +0100 Subject: [PATCH] Added ParseFunctionCall --- ast/inc/NodeTypes.h | 2 +- ast/src/NodeTypes.cpp | 4 ++-- parser/src/Parser.cpp | 43 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/ast/inc/NodeTypes.h b/ast/inc/NodeTypes.h index af24b92..5a8bd31 100644 --- a/ast/inc/NodeTypes.h +++ b/ast/inc/NodeTypes.h @@ -10,7 +10,7 @@ namespace LXC::AST class FunctionCall final : public NodeValue { public: - FunctionCall(Identifier& functionName, ValueList& arguments); + FunctionCall(const Identifier& functionName, ValueList& arguments); private: // The name of the function // diff --git a/ast/src/NodeTypes.cpp b/ast/src/NodeTypes.cpp index 952bf55..d847a3e 100644 --- a/ast/src/NodeTypes.cpp +++ b/ast/src/NodeTypes.cpp @@ -4,8 +4,8 @@ namespace LXC::AST { - FunctionCall::FunctionCall (Identifier& functionName, ValueList& arguments) - : NodeValue(NodeType::FunctionCall), m_FuncName(std::move(functionName)), m_Arguments(std::move(arguments)) + FunctionCall::FunctionCall (const Identifier& functionName, ValueList& arguments) + : NodeValue(NodeType::FunctionCall), m_FuncName(functionName), m_Arguments(std::move(arguments)) {} IntLiteral::IntLiteral(const std::string_view& value) diff --git a/parser/src/Parser.cpp b/parser/src/Parser.cpp index eee7b0b..e8b96bd 100644 --- a/parser/src/Parser.cpp +++ b/parser/src/Parser.cpp @@ -10,6 +10,13 @@ namespace LXC::Internal { return Util::ReturnVal(std::make_unique(std::forward(args)...)); } + + template + requires std::is_base_of_v && std::is_constructible_v + static const inline Util::ReturnVal CreateNodeV(Args&&... args) + { + return Util::ReturnVal(std::make_unique(std::forward(args)...)); + } } namespace LXC::Parser @@ -73,7 +80,7 @@ namespace LXC::Parser const size_t length; }; - static Util::ReturnVal ParsePrimary(ParserContext& ctx) + static Util::ReturnVal ParsePrimary(ParserContext& ctx) { // Gets the current token and returns if out of bounds // const Lexer::Token* current = ctx.At(); @@ -85,10 +92,12 @@ namespace LXC::Parser { // Fowards to the ParseIdentifier and returns the result/error // case Lexer::Token::Identifier: + ctx.Advance(); return Internal::CreateNode(current->Str()); // Returns an integer literal node // case Lexer::Token::NumLiteral: + ctx.Advance(); return Internal::CreateNode(current->Str()); default: @@ -97,6 +106,38 @@ namespace LXC::Parser } } + static Util::ReturnVal ParseFunctionCall(ParserContext& ctx) + { + // Checks if the upcoming pattern matches a function signature: [Identifier(function name), OpenBracket(Start of params)] // + if (!ctx.Expect(std::array{ Lexer::Token::Identifier, Lexer::Token::OpenParen })) + { + // Captures the function name and advances over it and the start paren // + const Lexer::Token* functionNameToken = ctx.At(); + ctx.Advance(2); + + // Captures the arguments of the function call // + AST::ValueList arguments; + const Lexer::Token* current = ctx.At(); + while (current != nullptr) + { + // End of the function call // + if (current->type == Lexer::Token::CloseParen) + return Internal::CreateNodeV(functionNameToken->Str(), arguments); + + // Checks for seperating comma // + if (!ctx.Expect(std::array{ Lexer::Token::Comma })) + return Util::FunctionFail(); // <- TODO: Make an actual error + ctx.Advance(); + + // Adds the current argument to the ValueList // + arguments.push_back(ParsePrimary(ctx)); + } + } + + // Else fowards to the next function in the call stack // + return ParsePrimary(ctx); + } + static Util::ReturnVal ParseOperation(ParserContext& ctx) {}