Added ParsePrimary (base of call stack)
This commit is contained in:
@@ -23,7 +23,7 @@ namespace LXC::AST
|
||||
class IntLiteral final : public NodeValue
|
||||
{
|
||||
public:
|
||||
IntLiteral(std::string& value);
|
||||
IntLiteral(const std::string_view& value);
|
||||
|
||||
private:
|
||||
// Yes numbers are stored as strings //
|
||||
@@ -73,7 +73,7 @@ namespace LXC::AST
|
||||
class VarAccess final : public NodeValue
|
||||
{
|
||||
public:
|
||||
VarAccess(Identifier& name);
|
||||
VarAccess(const Identifier& name);
|
||||
|
||||
private:
|
||||
// The name of the variable //
|
||||
|
||||
@@ -8,8 +8,8 @@ namespace LXC::AST
|
||||
: NodeValue(NodeType::FunctionCall), m_FuncName(std::move(functionName)), m_Arguments(std::move(arguments))
|
||||
{}
|
||||
|
||||
IntLiteral::IntLiteral(std::string& value)
|
||||
: NodeValue(NodeType::IntLiteral), m_NumberValue(std::move(value))
|
||||
IntLiteral::IntLiteral(const std::string_view& value)
|
||||
: NodeValue(NodeType::IntLiteral), m_NumberValue(value)
|
||||
{}
|
||||
|
||||
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))
|
||||
{}
|
||||
|
||||
VarAccess::VarAccess(Identifier& name)
|
||||
: NodeValue(NodeType::Var_Access), m_Name(std::move(m_Name))
|
||||
VarAccess::VarAccess(const Identifier& name)
|
||||
: NodeValue(NodeType::Var_Access), m_Name(name)
|
||||
{}
|
||||
|
||||
IfBranch::IfBranch(NodeValuePtr& condition, SyntaxBranch& body)
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user