Added ParsePrimary (base of call stack)

This commit is contained in:
Pasha Bibko
2025-08-04 15:46:43 +01:00
parent 7e95b7157e
commit 820d374fa0
3 changed files with 63 additions and 8 deletions

View File

@@ -2,6 +2,16 @@
#include <Parser.h>
namespace LXC::Internal
{
template<typename NodeType, typename... Args>
requires std::is_base_of_v<AST::Node, NodeType> && std::is_constructible_v<NodeType, Args...>
static const inline Util::ReturnVal<AST::NodePtr, Parser::ParserError> CreateNode(Args&&... args)
{
return Util::ReturnVal<AST::NodePtr, Parser::ParserError>(std::make_unique<NodeType>(std::forward<Args>(args)...));
}
}
namespace LXC::Parser
{
class ParserContext final
@@ -63,6 +73,44 @@ namespace LXC::Parser
const size_t length;
};
static Util::ReturnVal<AST::NodePtr, ParserError> ParsePrimary(ParserContext& ctx)
{
// Gets the current token and returns if out of bounds //
const Lexer::Token* current = ctx.At();
if (current == nullptr) _UNLIKELY
return Util::FunctionFail<ParserError>(); // <- TODO: Make an actual error
// Creates a designated node for the current token type //
switch (current->type)
{
// Fowards to the ParseIdentifier and returns the result/error //
case Lexer::Token::Identifier:
return Internal::CreateNode<AST::VarAccess>(current->Str());
// Returns an integer literal node //
case Lexer::Token::NumLiteral:
return Internal::CreateNode<AST::IntLiteral>(current->Str());
default:
// Unknown token type for this section //
return Util::FunctionFail<ParserError>(); // <- TODO: Make an actual error
}
}
static Util::ReturnVal<AST::NodePtr, ParserError> ParseOperation(ParserContext& ctx)
{}
static Util::ReturnVal<AST::NodePtr, ParserError> ParseReturn(ParserContext& ctx)
{}
static Util::ReturnVal<AST::NodePtr, ParserError> ParseVarDeclaration(ParserContext& ctx)
{}
static Util::ReturnVal<AST::NodePtr, ParserError> Parse(ParserContext& ctx)
{
return (AST::NodePtr)nullptr;
}
static Util::ReturnVal<FunctionAST, ParserError> ParseFunction(ParserContext& ctx)
{
// Checks for the sequence of: func<T> funcName( //
@@ -91,11 +139,11 @@ namespace LXC::Parser
while (ctx.At()->type != Lexer::Token::CloseParen)
{
// Checks for parameter pattern: identifier, identifier //
if (!ctx.Expect(std::array{ Lexer::Token::Identifier, Lexer::Token::Identifier }))
if (!ctx.Expect(std::array{ Lexer::Token::Identifier, Lexer::Token::Colon, Lexer::Token::Identifier }))
return Util::FunctionFail<ParserError>(); // <- TODO: Make an actual error
const Lexer::Token* paramType = ctx.At();
const Lexer::Token* paramName = ctx.Advance();
const Lexer::Token* paramName = ctx.Advance(2); // Skips over seperating colon
currentFunction.funcParams.emplace_back(paramType->Str(), paramName->Str());
@@ -122,6 +170,13 @@ namespace LXC::Parser
return currentFunction;
}
// Parses the current token //
Util::ReturnVal node = Parse(ctx);
if (node.Failed()) _UNLIKELY
return Util::FunctionFail<ParserError>(node.Error()); // Fowards the error
currentFunction.contents.emplace_back(std::move(node.Result()));
// Advances to the next token //
current = ctx.Advance();
}