Added ParseFunctionCall
This commit is contained in:
@@ -10,7 +10,7 @@ namespace LXC::AST
|
|||||||
class FunctionCall final : public NodeValue
|
class FunctionCall final : public NodeValue
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
FunctionCall(Identifier& functionName, ValueList& arguments);
|
FunctionCall(const Identifier& functionName, ValueList& arguments);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// The name of the function //
|
// The name of the function //
|
||||||
|
|||||||
@@ -4,8 +4,8 @@
|
|||||||
|
|
||||||
namespace LXC::AST
|
namespace LXC::AST
|
||||||
{
|
{
|
||||||
FunctionCall::FunctionCall (Identifier& functionName, ValueList& arguments)
|
FunctionCall::FunctionCall (const Identifier& functionName, ValueList& arguments)
|
||||||
: NodeValue(NodeType::FunctionCall), m_FuncName(std::move(functionName)), m_Arguments(std::move(arguments))
|
: NodeValue(NodeType::FunctionCall), m_FuncName(functionName), m_Arguments(std::move(arguments))
|
||||||
{}
|
{}
|
||||||
|
|
||||||
IntLiteral::IntLiteral(const std::string_view& value)
|
IntLiteral::IntLiteral(const std::string_view& value)
|
||||||
|
|||||||
@@ -10,6 +10,13 @@ namespace LXC::Internal
|
|||||||
{
|
{
|
||||||
return Util::ReturnVal<AST::NodePtr, Parser::ParserError>(std::make_unique<NodeType>(std::forward<Args>(args)...));
|
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
|
namespace LXC::Parser
|
||||||
@@ -73,7 +80,7 @@ namespace LXC::Parser
|
|||||||
const size_t length;
|
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 //
|
// Gets the current token and returns if out of bounds //
|
||||||
const Lexer::Token* current = ctx.At();
|
const Lexer::Token* current = ctx.At();
|
||||||
@@ -85,10 +92,12 @@ namespace LXC::Parser
|
|||||||
{
|
{
|
||||||
// Fowards to the ParseIdentifier and returns the result/error //
|
// Fowards to the ParseIdentifier and returns the result/error //
|
||||||
case Lexer::Token::Identifier:
|
case Lexer::Token::Identifier:
|
||||||
|
ctx.Advance();
|
||||||
return Internal::CreateNode<AST::VarAccess>(current->Str());
|
return Internal::CreateNode<AST::VarAccess>(current->Str());
|
||||||
|
|
||||||
// Returns an integer literal node //
|
// Returns an integer literal node //
|
||||||
case Lexer::Token::NumLiteral:
|
case Lexer::Token::NumLiteral:
|
||||||
|
ctx.Advance();
|
||||||
return Internal::CreateNode<AST::IntLiteral>(current->Str());
|
return Internal::CreateNode<AST::IntLiteral>(current->Str());
|
||||||
|
|
||||||
default:
|
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)
|
static Util::ReturnVal<AST::NodePtr, ParserError> ParseOperation(ParserContext& ctx)
|
||||||
{}
|
{}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user