Changed how functions are created

This commit is contained in:
Pasha Bibko
2025-07-23 21:58:23 +01:00
parent 004f859cc7
commit 28049ece94
6 changed files with 144 additions and 118 deletions

View File

@@ -54,6 +54,8 @@ namespace LXC::Lexer
Else,
Return,
FunctionDef,
// === User defined === //
StringLiteral = TokenClass::UserDefined,
@@ -73,6 +75,9 @@ namespace LXC::Lexer
CloseParen,
OpenParen,
CloseCrocodile,
OpenCrocodile,
Comma,
// === Misc === //

View File

@@ -29,7 +29,8 @@ namespace LXC::Internal
c == ',' || c == '[' ||
c == ']' || c == '{' ||
c == '}' || c == '(' ||
c == ')';
c == ')' || c == '<' ||
c == '>';
}
static const std::unordered_map<std::string_view, Lexer::Token::TokenType> symbolAndOpMap =
@@ -52,7 +53,10 @@ namespace LXC::Internal
{ "{", Lexer::Token::OpenBrace },
{ ")", Lexer::Token::CloseParen },
{ "(", Lexer::Token::OpenParen }
{ "(", Lexer::Token::OpenParen },
{ ">", Lexer::Token::CloseCrocodile },
{ "<", Lexer::Token::OpenCrocodile }
};
static const std::unordered_map<std::string_view, Lexer::Token::TokenType> keywords =
@@ -62,7 +66,8 @@ namespace LXC::Internal
{ "if", Lexer::Token::If },
{ "elif", Lexer::Token::ElseIf },
{ "else", Lexer::Token::Else },
{ "return", Lexer::Token::Return }
{ "return", Lexer::Token::Return },
{ "func", Lexer::Token::FunctionDef },
};
}

View File

@@ -75,6 +75,8 @@ namespace LXC::Lexer
TOKEN_TYPE_CASE(Token::Else);
TOKEN_TYPE_CASE(Token::Return);
TOKEN_TYPE_CASE(Token::FunctionDef);
TOKEN_TYPE_CASE(Token::StringLiteral);
TOKEN_TYPE_CASE(Token::NumLiteral);
TOKEN_TYPE_CASE(Token::Identifier);
@@ -86,6 +88,8 @@ namespace LXC::Lexer
TOKEN_TYPE_CASE(Token::OpenBrace);
TOKEN_TYPE_CASE(Token::CloseParen);
TOKEN_TYPE_CASE(Token::OpenParen);
TOKEN_TYPE_CASE(Token::CloseCrocodile);
TOKEN_TYPE_CASE(Token::OpenCrocodile);
TOKEN_TYPE_CASE(Token::Comma);
TOKEN_TYPE_CASE(Token::End_of_file);

View File

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

View File

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

View File

@@ -123,7 +123,10 @@ namespace LXC::Lexer
Util::ReturnVal tokens = TokenizeFile(fileContents);
Internal::ExpectTokens(tokens,
{
Token::FunctionDef, // func
Token::OpenCrocodile, // <
Token::Identifier, // int
Token::CloseCrocodile, // >
Token::Identifier, // add
Token::OpenParen, // (
Token::Identifier, // int
@@ -139,7 +142,10 @@ namespace LXC::Lexer
Token::Identifier, // b
Token::CloseBrace, // }
Token::FunctionDef, // func
Token::OpenCrocodile, // <
Token::Identifier, // int
Token::CloseCrocodile, // >
Token::Identifier, // main
Token::OpenParen, // (
Token::Identifier, // void
@@ -180,7 +186,10 @@ namespace LXC::Lexer
Util::ReturnVal tokens = TokenizeFile(fileContents);
Internal::ExpectTokens(tokens,
{
Token::FunctionDef, // func
Token::OpenCrocodile, // <
Token::Identifier, // int
Token::CloseCrocodile, // >
Token::Identifier, // fib
Token::OpenParen, // (
Token::Identifier, // int
@@ -228,7 +237,10 @@ namespace LXC::Lexer
Token::CloseParen, // )
Token::CloseBrace, // }
Token::FunctionDef, // func
Token::OpenCrocodile, // <
Token::Identifier, // int
Token::CloseCrocodile, // >
Token::Identifier, // main
Token::OpenParen, // (
Token::Identifier, // void