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

@@ -23,7 +23,7 @@ namespace LXC::AST
class IntLiteral final : public NodeValue class IntLiteral final : public NodeValue
{ {
public: public:
IntLiteral(std::string& value); IntLiteral(const std::string_view& value);
private: private:
// Yes numbers are stored as strings // // Yes numbers are stored as strings //
@@ -73,7 +73,7 @@ namespace LXC::AST
class VarAccess final : public NodeValue class VarAccess final : public NodeValue
{ {
public: public:
VarAccess(Identifier& name); VarAccess(const Identifier& name);
private: private:
// The name of the variable // // The name of the variable //

View File

@@ -8,8 +8,8 @@ namespace LXC::AST
: NodeValue(NodeType::FunctionCall), m_FuncName(std::move(functionName)), m_Arguments(std::move(arguments)) : NodeValue(NodeType::FunctionCall), m_FuncName(std::move(functionName)), m_Arguments(std::move(arguments))
{} {}
IntLiteral::IntLiteral(std::string& value) IntLiteral::IntLiteral(const std::string_view& value)
: NodeValue(NodeType::IntLiteral), m_NumberValue(std::move(value)) : NodeValue(NodeType::IntLiteral), m_NumberValue(value)
{} {}
Operation::Operation(NodeValuePtr& left, Lexer::Token::TokenType operand, NodeValuePtr& right) Operation::Operation(NodeValuePtr& left, Lexer::Token::TokenType operand, NodeValuePtr& right)
@@ -28,8 +28,8 @@ namespace LXC::AST
: Node(NodeType::Var_Assign), m_Value(std::move(value)) : Node(NodeType::Var_Assign), m_Value(std::move(value))
{} {}
VarAccess::VarAccess(Identifier& name) VarAccess::VarAccess(const Identifier& name)
: NodeValue(NodeType::Var_Access), m_Name(std::move(m_Name)) : NodeValue(NodeType::Var_Access), m_Name(name)
{} {}
IfBranch::IfBranch(NodeValuePtr& condition, SyntaxBranch& body) IfBranch::IfBranch(NodeValuePtr& condition, SyntaxBranch& body)

View File

@@ -2,6 +2,16 @@
#include <Parser.h> #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 namespace LXC::Parser
{ {
class ParserContext final class ParserContext final
@@ -63,6 +73,44 @@ namespace LXC::Parser
const size_t length; 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) static Util::ReturnVal<FunctionAST, ParserError> ParseFunction(ParserContext& ctx)
{ {
// Checks for the sequence of: func<T> funcName( // // Checks for the sequence of: func<T> funcName( //
@@ -91,11 +139,11 @@ namespace LXC::Parser
while (ctx.At()->type != Lexer::Token::CloseParen) while (ctx.At()->type != Lexer::Token::CloseParen)
{ {
// Checks for parameter pattern: identifier, identifier // // 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 return Util::FunctionFail<ParserError>(); // <- TODO: Make an actual error
const Lexer::Token* paramType = ctx.At(); 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()); currentFunction.funcParams.emplace_back(paramType->Str(), paramName->Str());
@@ -122,6 +170,13 @@ namespace LXC::Parser
return currentFunction; 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 // // Advances to the next token //
current = ctx.Advance(); current = ctx.Advance();
} }