Updated function signatures

This commit is contained in:
Pasha Bibko
2025-08-04 13:16:08 +01:00
parent 5bb9f2c28a
commit a77d5bd129
7 changed files with 83 additions and 66 deletions

View File

@@ -71,6 +71,10 @@ int main(int argc, char** argv)
{ {
Util::Stop(); Util::Stop();
} }
else
{
Util::PrintContainer("Function AST", functionsAST.Result());
}
return 0; return 0;
} }

View File

@@ -15,17 +15,22 @@ namespace LXC::Internal
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
} }
bool IsWhitespace(const char c) static constexpr bool IsWhitespace(const char c)
{ {
return c == ' ' || c == '\t' || c == '\n' || c == '\r'; return c == ' ' || c == '\t' || c == '\n' || c == '\r';
} }
static constexpr bool IsSymbolOrOperator(const char c) static constexpr bool IsOperator(const char c)
{ {
return return
c == '+' || c == '-' || c == '+' || c == '-' ||
c == '*' || c == '/' || c == '*' || c == '/' ||
c == '%' || c == '=' || c == '%' || c == '=';
}
static constexpr bool IsSymbol(const char c)
{
return
c == ',' || c == '[' || c == ',' || c == '[' ||
c == ']' || c == '{' || c == ']' || c == '{' ||
c == '}' || c == '(' || c == '}' || c == '(' ||
@@ -33,7 +38,7 @@ namespace LXC::Internal
c == '>'; c == '>';
} }
static const std::unordered_map<std::string_view, Lexer::Token::TokenType> symbolAndOpMap = static const std::unordered_map<std::string_view, Lexer::Token::TokenType> operatorMap =
{ {
{ "+", Lexer::Token::Add }, { "+", Lexer::Token::Add },
{ "-", Lexer::Token::Sub }, { "-", Lexer::Token::Sub },
@@ -43,20 +48,24 @@ namespace LXC::Internal
{ "==", Lexer::Token::Eql }, { "==", Lexer::Token::Eql },
{ "=", Lexer::Token::Assign }, { "=", Lexer::Token::Assign }
{ ",", Lexer::Token::Comma }, };
{ "[", Lexer::Token::CloseBracket }, static const std::unordered_map<char, Lexer::Token::TokenType> symbolMap =
{ "]", Lexer::Token::OpenBracket }, {
{ ',', Lexer::Token::Comma },
{ "}", Lexer::Token::CloseBrace }, { '[', Lexer::Token::CloseBracket },
{ "{", Lexer::Token::OpenBrace }, { ']', Lexer::Token::OpenBracket },
{ ")", Lexer::Token::CloseParen }, { '}', Lexer::Token::CloseBrace },
{ "(", Lexer::Token::OpenParen }, { '{', Lexer::Token::OpenBrace },
{ ">", Lexer::Token::CloseCrocodile }, { ')', Lexer::Token::CloseParen },
{ "<", Lexer::Token::OpenCrocodile } { '(', Lexer::Token::OpenParen },
{ '>', Lexer::Token::CloseCrocodile },
{ '<', Lexer::Token::OpenCrocodile }
}; };
static const std::unordered_map<std::string_view, Lexer::Token::TokenType> keywords = static const std::unordered_map<std::string_view, Lexer::Token::TokenType> keywords =
@@ -87,7 +96,7 @@ namespace LXC::Lexer
bool inStrLiteral = false; bool inStrLiteral = false;
bool inIdentifier = false; bool inIdentifier = false;
bool inNumLiteral = false; bool inNumLiteral = false;
bool inSymbolOrOp = false; bool inOperator = false;
bool inComment = false; bool inComment = false;
@@ -155,22 +164,22 @@ namespace LXC::Lexer
} }
} }
// === Symbols/Operators === // // === Operators === //
else if (Internal::IsSymbolOrOperator(current)) else if (Internal::IsOperator(current))
{ {
// Updates trackers // // Updates trackers //
trackers.sectionStart = trackers.inSymbolOrOp ? trackers.sectionStart : ctx.index; trackers.sectionStart = trackers.inOperator ? trackers.sectionStart : ctx.index;
trackers.inSymbolOrOp = true; trackers.inOperator = true;
// Checks for the end of the symbol or operator // // Checks for the end of the symbol or operator //
if (!Internal::IsSymbolOrOperator(next)) _LIKELY if (!Internal::IsOperator(next)) _LIKELY
{ {
trackers.inSymbolOrOp = false; trackers.inOperator = false;
// Finds the operator/symbol if it can // // Finds the operator/symbol if it can //
std::string_view fullSymbol(ctx.source.data() + trackers.sectionStart, ctx.index - trackers.sectionStart + 1); std::string_view fullSymbol(ctx.source.data() + trackers.sectionStart, ctx.index - trackers.sectionStart + 1);
auto it = Internal::symbolAndOpMap.find(fullSymbol); auto it = Internal::operatorMap.find(fullSymbol);
if (it != Internal::symbolAndOpMap.end()) if (it != Internal::operatorMap.end())
ctx.out.emplace_back(ctx, trackers.sectionStart, (unsigned short)(ctx.index - trackers.sectionStart + 1), it->second); ctx.out.emplace_back(ctx, trackers.sectionStart, (unsigned short)(ctx.index - trackers.sectionStart + 1), it->second);
else else
@@ -178,6 +187,12 @@ namespace LXC::Lexer
} }
} }
// === Symbols === //
else if (Internal::IsSymbol(current))
{
ctx.out.emplace_back(ctx, ctx.index, 1, Internal::symbolMap.at(current));
}
// === Whitespace === // // === Whitespace === //
else if (Internal::IsWhitespace(current)) _LIKELY {} else if (Internal::IsWhitespace(current)) _LIKELY {}

View File

@@ -8,7 +8,7 @@ func<int> fib(int num)
return fib(n - 1) + fib(n - 2) return fib(n - 1) + fib(n - 2)
} }
func<int> main(void) func<int> main()
{ {
int res = fib(8) int res = fib(8)
return res == 21 return res == 21

View File

@@ -3,7 +3,7 @@ func<int> add(int a, int b)
return a + b return a + b
} }
func<int> main(void) func<int> main()
{ {
int c = add(3, 4) int c = add(3, 4)
if (c == 7) if (c == 7)

View File

@@ -12,15 +12,33 @@ namespace LXC::Parser
struct FunctionAST struct FunctionAST
{ {
FunctionAST() : FunctionAST() :
name{}, contents{} name{}, contents{}, funcParams{}
{} {}
FunctionAST(FunctionAST&& other) noexcept : 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::string name;
std::vector<std::pair<std::string, std::string>> funcParams;
AST::SyntaxBranch contents; 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); Util::ReturnVal<std::vector<FunctionAST>, ParserError> TurnTokensIntoAST(const Lexer::LexerOutput& input);

View File

@@ -63,39 +63,6 @@ namespace LXC::Parser
const size_t length; 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) static Util::ReturnVal<FunctionAST, ParserError> ParseFunction(ParserContext& ctx)
{ {
// Checks for the sequence of: func<T> funcName( // // Checks for the sequence of: func<T> funcName( //
@@ -130,6 +97,8 @@ namespace LXC::Parser
const Lexer::Token* paramType = ctx.At(); const Lexer::Token* paramType = ctx.At();
const Lexer::Token* paramName = ctx.Advance(); const Lexer::Token* paramName = ctx.Advance();
currentFunction.funcParams.emplace_back(paramType->Str(), paramName->Str());
// Expects a comma or close bracket for the next token // // Expects a comma or close bracket for the next token //
const Lexer::Token* end = ctx.Advance(); const Lexer::Token* end = ctx.Advance();
if (end == nullptr) if (end == nullptr)
@@ -143,10 +112,21 @@ namespace LXC::Parser
} }
// Parses the function body // // Parses the function body //
ParseBlock(ctx); const Lexer::Token* current = ctx.At();
ctx.Advance(); // <- Goes over the closing brace while (current != nullptr)
{
return currentFunction; 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) Util::ReturnVal<std::vector<FunctionAST>, ParserError> TurnTokensIntoAST(const Lexer::LexerOutput& input)
@@ -171,6 +151,8 @@ namespace LXC::Parser
if (func.Failed()) if (func.Failed())
return Util::FunctionFail<ParserError>(func.Error()); return Util::FunctionFail<ParserError>(func.Error());
ctx.output.emplace_back(std::move(func.Result()));
break; break;
} }

View File

@@ -148,7 +148,6 @@ namespace LXC::Lexer
Token::CloseCrocodile, // > Token::CloseCrocodile, // >
Token::Identifier, // main Token::Identifier, // main
Token::OpenParen, // ( Token::OpenParen, // (
Token::Identifier, // void
Token::CloseParen, // ) Token::CloseParen, // )
Token::OpenBrace, // { Token::OpenBrace, // {
Token::Identifier, // int Token::Identifier, // int
@@ -243,7 +242,6 @@ namespace LXC::Lexer
Token::CloseCrocodile, // > Token::CloseCrocodile, // >
Token::Identifier, // main Token::Identifier, // main
Token::OpenParen, // ( Token::OpenParen, // (
Token::Identifier, // void
Token::CloseParen, // ) Token::CloseParen, // )
Token::OpenBrace, // { Token::OpenBrace, // {