Added ParseFunctionCall
This commit is contained in:
@@ -10,6 +10,13 @@ namespace LXC::Internal
|
||||
{
|
||||
return Util::ReturnVal<AST::NodePtr, Parser::ParserError>(std::make_unique<NodeType>(std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
template<typename NodeType, typename... Args>
|
||||
requires std::is_base_of_v<AST::NodeValue, NodeType> && std::is_constructible_v<NodeType, Args...>
|
||||
static const inline Util::ReturnVal<AST::NodeValuePtr, Parser::ParserError> CreateNodeV(Args&&... args)
|
||||
{
|
||||
return Util::ReturnVal<AST::NodeValuePtr, Parser::ParserError>(std::make_unique<NodeType>(std::forward<Args>(args)...));
|
||||
}
|
||||
}
|
||||
|
||||
namespace LXC::Parser
|
||||
@@ -73,7 +80,7 @@ namespace LXC::Parser
|
||||
const size_t length;
|
||||
};
|
||||
|
||||
static Util::ReturnVal<AST::NodePtr, ParserError> ParsePrimary(ParserContext& ctx)
|
||||
static Util::ReturnVal<AST::NodeValuePtr, ParserError> 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<AST::VarAccess>(current->Str());
|
||||
|
||||
// Returns an integer literal node //
|
||||
case Lexer::Token::NumLiteral:
|
||||
ctx.Advance();
|
||||
return Internal::CreateNode<AST::IntLiteral>(current->Str());
|
||||
|
||||
default:
|
||||
@@ -97,6 +106,38 @@ namespace LXC::Parser
|
||||
}
|
||||
}
|
||||
|
||||
static Util::ReturnVal<AST::NodeValuePtr, ParserError> 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<AST::FunctionCall>(functionNameToken->Str(), arguments);
|
||||
|
||||
// Checks for seperating comma //
|
||||
if (!ctx.Expect(std::array{ Lexer::Token::Comma }))
|
||||
return Util::FunctionFail<ParserError>(); // <- 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<AST::NodePtr, ParserError> ParseOperation(ParserContext& ctx)
|
||||
{}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user