Updated function signatures
This commit is contained in:
@@ -12,15 +12,33 @@ namespace LXC::Parser
|
||||
struct FunctionAST
|
||||
{
|
||||
FunctionAST() :
|
||||
name{}, contents{}
|
||||
name{}, contents{}, funcParams{}
|
||||
{}
|
||||
|
||||
FunctionAST(FunctionAST&& other) noexcept :
|
||||
name{}, contents{}
|
||||
name(std::move(other.name)), contents(std::move(other.contents)), funcParams(std::move(other.funcParams))
|
||||
{}
|
||||
|
||||
std::string name;
|
||||
std::vector<std::pair<std::string, std::string>> funcParams;
|
||||
|
||||
AST::SyntaxBranch contents;
|
||||
|
||||
std::string LogStr() const
|
||||
{
|
||||
std::ostringstream os;
|
||||
os << name << " (";
|
||||
|
||||
for (size_t i = 0; i < funcParams.size(); i++)
|
||||
{
|
||||
os << funcParams[i].first << ' ' << funcParams[i].second;
|
||||
if (i != funcParams.size() - 1)
|
||||
os << ", ";
|
||||
}
|
||||
|
||||
os << ")";
|
||||
return os.str();
|
||||
}
|
||||
};
|
||||
|
||||
Util::ReturnVal<std::vector<FunctionAST>, ParserError> TurnTokensIntoAST(const Lexer::LexerOutput& input);
|
||||
|
||||
@@ -63,39 +63,6 @@ namespace LXC::Parser
|
||||
const size_t length;
|
||||
};
|
||||
|
||||
static void ParseBlock(ParserContext& ctx)
|
||||
{
|
||||
// Local lamdba for working out if at the end (static to avoid double initalisation) //
|
||||
static const std::function<bool(const ParserContext&)> AtEnd = [](const ParserContext& context)
|
||||
{
|
||||
// Peeks the current tokens type //
|
||||
const Lexer::Token* current = context.Peek();
|
||||
if (current != nullptr)
|
||||
{
|
||||
return current->type == Lexer::Token::CloseBrace;
|
||||
}
|
||||
|
||||
// If nullptr means it has overflowed so it is at the end //
|
||||
return true;
|
||||
};
|
||||
|
||||
// Loops over the body until it reaches the end //
|
||||
while (!AtEnd(ctx))
|
||||
{
|
||||
// Force ends if it has gone over the end of the tokens //
|
||||
const Lexer::Token* current = ctx.Advance();
|
||||
if (current != nullptr)
|
||||
{
|
||||
// Recurses if at the start of another block //
|
||||
if (current->type == Lexer::Token::OpenBrace)
|
||||
ParseBlock(ctx);
|
||||
}
|
||||
}
|
||||
|
||||
// Advances over the ending close brace //
|
||||
ctx.Advance();
|
||||
}
|
||||
|
||||
static Util::ReturnVal<FunctionAST, ParserError> ParseFunction(ParserContext& ctx)
|
||||
{
|
||||
// Checks for the sequence of: func<T> funcName( //
|
||||
@@ -130,6 +97,8 @@ namespace LXC::Parser
|
||||
const Lexer::Token* paramType = ctx.At();
|
||||
const Lexer::Token* paramName = ctx.Advance();
|
||||
|
||||
currentFunction.funcParams.emplace_back(paramType->Str(), paramName->Str());
|
||||
|
||||
// Expects a comma or close bracket for the next token //
|
||||
const Lexer::Token* end = ctx.Advance();
|
||||
if (end == nullptr)
|
||||
@@ -143,10 +112,21 @@ namespace LXC::Parser
|
||||
}
|
||||
|
||||
// Parses the function body //
|
||||
ParseBlock(ctx);
|
||||
ctx.Advance(); // <- Goes over the closing brace
|
||||
|
||||
return currentFunction;
|
||||
const Lexer::Token* current = ctx.At();
|
||||
while (current != nullptr)
|
||||
{
|
||||
if (current->type == Lexer::Token::CloseBrace)
|
||||
{
|
||||
// Advances over closing brace before returning the function //
|
||||
ctx.Advance();
|
||||
return currentFunction;
|
||||
}
|
||||
|
||||
// Advances to the next token //
|
||||
current = ctx.Advance();
|
||||
}
|
||||
|
||||
return Util::FunctionFail<ParserError>(); // <- TODO: Make an actual error
|
||||
}
|
||||
|
||||
Util::ReturnVal<std::vector<FunctionAST>, ParserError> TurnTokensIntoAST(const Lexer::LexerOutput& input)
|
||||
@@ -171,6 +151,8 @@ namespace LXC::Parser
|
||||
if (func.Failed())
|
||||
return Util::FunctionFail<ParserError>(func.Error());
|
||||
|
||||
ctx.output.emplace_back(std::move(func.Result()));
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user