From 41609ba3d68c0cc4db82ed3ebbdc880b807ac57b Mon Sep 17 00:00:00 2001 From: Weslley Neves <85376122+wesuRage@users.noreply.github.com> Date: Tue, 10 Dec 2024 12:31:24 -0300 Subject: [PATCH 01/21] Update LLVM link --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 102facd..288e835 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Install all the dependencies: sudo apt install clang cmake make llvm llvm-dev zlib1g-dev ``` -Or you can [compile LLVM from source](https://github.com/llvm/llvm-project). +Or you can [download the built LLVM](https://github.com/llvm/llvm-project/releases/tag/llvmorg-18.1.8). Clone the repo and compile the code: From f66269af55614f99b9c3274d40d600cb91c0087f Mon Sep 17 00:00:00 2001 From: Weslley Neves <85376122+wesuRage@users.noreply.github.com> Date: Tue, 10 Dec 2024 13:41:55 -0300 Subject: [PATCH 02/21] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 288e835..1cef8c1 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ Take a look at our [contributors guide](https://github.com/galaxy-lang/galaxy/bl --- -### Installation +### Build the project Install all the dependencies: ```bash From 15d4dbc87152f953e70d037acc9f92a14870a9e1 Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:13:51 -0300 Subject: [PATCH 03/21] feat(symbols): added symbol tracking on generation --- .../symbols/function_symbol_table.hpp | 10 ++++++++++ .../symbols/identifier_symbol_table.hpp | 11 +++++++++++ .../backend/generator/symbols/symbol_stack.hpp | 15 +++++++++++++++ .../symbols/function_symbol_table.cpp | 3 +++ .../symbols/identifier_symbol_table.cpp | 18 ++++++++++++++++++ src/backend/generator/symbols/symbol_stack.cpp | 17 +++++++++++++++++ 6 files changed, 74 insertions(+) create mode 100644 include/backend/generator/symbols/function_symbol_table.hpp create mode 100644 include/backend/generator/symbols/identifier_symbol_table.hpp create mode 100644 include/backend/generator/symbols/symbol_stack.hpp create mode 100644 src/backend/generator/symbols/function_symbol_table.cpp create mode 100644 src/backend/generator/symbols/identifier_symbol_table.cpp create mode 100644 src/backend/generator/symbols/symbol_stack.cpp diff --git a/include/backend/generator/symbols/function_symbol_table.hpp b/include/backend/generator/symbols/function_symbol_table.hpp new file mode 100644 index 0000000..233763b --- /dev/null +++ b/include/backend/generator/symbols/function_symbol_table.hpp @@ -0,0 +1,10 @@ +#ifndef FUNCTION_SYMBOL_TABLE_H +#define FUNCTION_SYMBOL_TABLE_H + +#include +#include +#include + +extern std::unordered_map function_symbol_table; + +#endif // FUNCTION_SYMBOL_TABLE_H \ No newline at end of file diff --git a/include/backend/generator/symbols/identifier_symbol_table.hpp b/include/backend/generator/symbols/identifier_symbol_table.hpp new file mode 100644 index 0000000..2be5790 --- /dev/null +++ b/include/backend/generator/symbols/identifier_symbol_table.hpp @@ -0,0 +1,11 @@ +#ifndef IDENTIFIER_SYMBOL_TABLE_H +#define IDENTIFIER_SYMBOL_TABLE_H + +#include +#include +#include + +llvm::Value* find_identifier(const std::string &name); +void add_identifier(const std::string &name, llvm::Value *value); + +#endif // IDENTIFIER_SYMBOL_TABLE_H \ No newline at end of file diff --git a/include/backend/generator/symbols/symbol_stack.hpp b/include/backend/generator/symbols/symbol_stack.hpp new file mode 100644 index 0000000..cadcccc --- /dev/null +++ b/include/backend/generator/symbols/symbol_stack.hpp @@ -0,0 +1,15 @@ +#ifndef SYMBOL_STACK_H +#define SYMBOL_STACK_H + +#include +#include +#include +#include + +using SymbolTable = std::unordered_map; + +extern std::stack symbol_stack; +void enter_scope(void); +void exit_scope(void); + +#endif // SYMBOL_STACK_H \ No newline at end of file diff --git a/src/backend/generator/symbols/function_symbol_table.cpp b/src/backend/generator/symbols/function_symbol_table.cpp new file mode 100644 index 0000000..6a874a4 --- /dev/null +++ b/src/backend/generator/symbols/function_symbol_table.cpp @@ -0,0 +1,3 @@ +#include "backend/generator/symbols/function_symbol_table.hpp" + +std::unordered_map function_symbol_table; \ No newline at end of file diff --git a/src/backend/generator/symbols/identifier_symbol_table.cpp b/src/backend/generator/symbols/identifier_symbol_table.cpp new file mode 100644 index 0000000..2f6abb5 --- /dev/null +++ b/src/backend/generator/symbols/identifier_symbol_table.cpp @@ -0,0 +1,18 @@ +#include "backend/generator/symbols/identifier_symbol_table.hpp" +#include "backend/generator/symbols/symbol_stack.hpp" + +llvm::Value* find_identifier(const std::string &name) { + for (const auto& entry : symbol_stack.top()) { + if (entry.first == name) { // A chave é 'entry.first' + return entry.second; // O valor é 'entry.second' + } + } + return nullptr; // Not found +} + +void add_identifier(const std::string &name, llvm::Value *value) { + if (symbol_stack.empty()) { + throw std::runtime_error("Error: No active scope to add identifier."); + } + symbol_stack.top()[name] = value; +} \ No newline at end of file diff --git a/src/backend/generator/symbols/symbol_stack.cpp b/src/backend/generator/symbols/symbol_stack.cpp new file mode 100644 index 0000000..cc7c9b5 --- /dev/null +++ b/src/backend/generator/symbols/symbol_stack.cpp @@ -0,0 +1,17 @@ +#include "backend/generator/symbols/symbol_stack.hpp" + +std::stack symbol_stack; + + +void enter_scope() { + // Adiciona uma nova tabela de símbolos ao topo da pilha + symbol_stack.push(SymbolTable{}); +} + +void exit_scope() { + // Remove a tabela de símbolos do topo da pilha + if (symbol_stack.empty()) { + throw std::runtime_error("Exiting scope when no scope exists!"); + } + symbol_stack.pop(); +} \ No newline at end of file From 8d88bfe958c8cc0a4181ef436a5230244a85a91d Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:15:08 -0300 Subject: [PATCH 04/21] refactor(add_id): created add_identifer for searching on scopes --- .../statements/generate_variable_declaration_stmt.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/backend/generator/statements/generate_variable_declaration_stmt.cpp b/src/backend/generator/statements/generate_variable_declaration_stmt.cpp index 1e3bcca..03d7f47 100644 --- a/src/backend/generator/statements/generate_variable_declaration_stmt.cpp +++ b/src/backend/generator/statements/generate_variable_declaration_stmt.cpp @@ -1,6 +1,7 @@ #include "backend/generator/statements/generate_variable_declaration_stmt.hpp" #include "backend/generator/expressions/generate_expr.hpp" #include "backend/generator/types/generate_type.hpp" +#include "backend/generator/symbols/identifier_symbol_table.hpp" llvm::Value* generate_variable_declaration_stmt(VariableNode *node, llvm::LLVMContext &Context, llvm::IRBuilder<> &Builder, llvm::Module &Module) { llvm::Type *var_type = generate_type(nullptr, Context, node->varType); @@ -17,6 +18,9 @@ llvm::Value* generate_variable_declaration_stmt(VariableNode *node, llvm::LLVMCo Builder.CreateStore(init_value, alloca); } + // Stores the allocated variable in the identifier symbol table + add_identifier(node->name, alloca); + // Return the AllocaInst, which represents the variable's storage in memory return alloca; } From 03a71c78804bca6b727404b81d064ccb6cfbe86c Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:15:42 -0300 Subject: [PATCH 05/21] feat(return): added return case to the function generation --- .../generate_function_declaration_stmt.cpp | 97 ++++++++++++++----- 1 file changed, 71 insertions(+), 26 deletions(-) diff --git a/src/backend/generator/statements/generate_function_declaration_stmt.cpp b/src/backend/generator/statements/generate_function_declaration_stmt.cpp index a0e803e..3adaef2 100644 --- a/src/backend/generator/statements/generate_function_declaration_stmt.cpp +++ b/src/backend/generator/statements/generate_function_declaration_stmt.cpp @@ -2,56 +2,101 @@ #include "backend/generator/statements/generate_stmt.hpp" #include "backend/generator/expressions/generate_expr.hpp" #include "backend/generator/types/generate_type.hpp" +#include "backend/generator/symbols/function_symbol_table.hpp" +#include "backend/generator/symbols/symbol_stack.hpp" +#include "backend/generator/symbols/identifier_symbol_table.hpp" llvm::Value* generate_function_declaration_stmt(FunctionNode *node, llvm::LLVMContext &Context, llvm::IRBuilder<> &Builder, llvm::Module &Module) { + if (!node || !node->name || !node->parameters) { + throw std::runtime_error("Invalid function: node, name, or parameters are null."); + } + + + // Generate the return type for the function llvm::Type *return_type = generate_type(nullptr, Context, node->type); - // Create a vector of LLVM Types for the function parameters + // Generate parameter types std::vector param_types; - - for (size_t i = 0; i < node->parameters->parameter_count; ++i) { - // Retrieve each parameter node - AstNode *param_node = node->parameters->parameters[i]; - ParameterNode *param = static_cast(param_node->data); - - // Generate the type for each parameter and add it to the param_types vector - llvm::Type* param_type = generate_type(nullptr, Context, param->type); + for (int i = 0; i < node->parameters->parameter_count; ++i) { + ParameterNode *param = static_cast(node->parameters->parameters[i]->data); + llvm::Type *param_type = generate_type(nullptr, Context, param->type); param_types.push_back(param_type); } - // Create the function type (specifying return type and parameter types) + // Create the function type and function llvm::FunctionType *func_type = llvm::FunctionType::get(return_type, param_types, false); - - // Create the function in the LLVM module with external linkage (the function's symbol is externally visible) llvm::Function *function = llvm::Function::Create(func_type, llvm::Function::ExternalLinkage, node->name, &Module); - // Set the names of the function parameters based on the parameter nodes - size_t idx = 0; + // Saves the function on a symbol table + function_symbol_table[node->name] = function; + + // Assign parameter names + enter_scope(); + int idx = 0; for (auto &arg : function->args()) { - AstNode *param_node = node->parameters->parameters[idx]; - ParameterNode *param = static_cast(param_node->data); - // Set the name for the parameter argument + ParameterNode *param = static_cast(node->parameters->parameters[idx]->data); arg.setName(param->name); + + add_identifier(param->name, &arg); // Stores in the identifier symbol table ++idx; } - // If the function has a body, create the corresponding LLVM IR for the function body if (node->body != nullptr) { - // Create the entry block for the function (this is where instructions will be inserted) llvm::BasicBlock *entry = llvm::BasicBlock::Create(Context, "entry", function); - Builder.SetInsertPoint(entry); // Set the builder to insert instructions into the entry block + Builder.SetInsertPoint(entry); + + // Map function parameters to LLVM variables + std::map variable_map; + idx = 0; + for (auto &arg : function->args()) { + llvm::AllocaInst *alloca = Builder.CreateAlloca(arg.getType(), nullptr, arg.getName()); + Builder.CreateStore(&arg, alloca); + variable_map[arg.getName().str()] = alloca; + } + + llvm::Value *return_value = nullptr; - // Generate the statements in the function body for (size_t i = 0; i < node->body_count; ++i) { - generate_stmt(node->body[i], Context, Module, Builder); // Generate each statement + AstNode *statement = node->body[i]; + + if (statement->kind == NODE_RETURN) { + ReturnNode *return_node = static_cast(statement->data); + AstNode *value_node = return_node->value; + + // Generate the return expression + return_value = generate_expr(value_node, Context, Builder, Module); + + if (!return_value) { + llvm::errs() << "Error: generate_expr returned null for return expression!\n"; + if (return_type->isVoidTy()) { + Builder.CreateRetVoid(); + } else { + return_value = llvm::Constant::getNullValue(return_type); + Builder.CreateRet(return_value); + return function; + } + } else { + Builder.CreateRet(return_value); + return function; + } + + } else { + // Handle other statement types + generate_stmt(statement, Context, Module, Builder); + } } - // If the function has a void return type and the body doesn't end with a return statement, add a return statement - if (return_type->isVoidTy() && Builder.GetInsertBlock()->getTerminator() == nullptr) { - Builder.CreateRetVoid(); // Create a return with no value (void) + // Add a default return if no explicit return is provided + if (!return_value) { + if (return_type->isVoidTy()) { + Builder.CreateRetVoid(); + } else { + Builder.CreateRet(llvm::Constant::getNullValue(return_type)); + } } } - // Return the generated function (LLVM Function object) + exit_scope(); + return function; } From 842b867cadb34593282a198ebbabf3399495c1ed Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:16:30 -0300 Subject: [PATCH 06/21] fix(function): removed pre-created main function --- src/backend/generator/generator.test.cpp | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/backend/generator/generator.test.cpp b/src/backend/generator/generator.test.cpp index 816b70d..bdd7398 100644 --- a/src/backend/generator/generator.test.cpp +++ b/src/backend/generator/generator.test.cpp @@ -77,14 +77,6 @@ int main(int argc, char **argv) { llvm::Module TheModule("GalaxyJIT", TheContext); llvm::IRBuilder<> Builder(TheContext); - // Create the main function in the LLVM module (returns void) - llvm::FunctionType* funcType = llvm::FunctionType::get(llvm::Type::getVoidTy(TheContext), false); - llvm::Function* mainFunc = llvm::Function::Create(funcType, llvm::Function::ExternalLinkage, "main", &TheModule); - - // Create the entry basic block for the main function - llvm::BasicBlock* entry = llvm::BasicBlock::Create(TheContext, "entry", mainFunc); - Builder.SetInsertPoint(entry); - // Generate the LLVM IR from the AST std::vector values = generate_ir(ast, TheContext, TheModule, Builder); @@ -98,9 +90,6 @@ int main(int argc, char **argv) { } } - // End the main function with a return statement (for void functions) - Builder.CreateRetVoid(); - // Verify the generated module for correctness std::string errorMsg; llvm::raw_string_ostream errorStream(errorMsg); From f6af7c727457b9c11e095990b1bdf44a8582d5e2 Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:17:03 -0300 Subject: [PATCH 07/21] feat(isDecimal): added verification for decimal values --- src/frontend/lexer/lexer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/frontend/lexer/lexer.c b/src/frontend/lexer/lexer.c index a7ef255..d7df51d 100644 --- a/src/frontend/lexer/lexer.c +++ b/src/frontend/lexer/lexer.c @@ -283,7 +283,7 @@ Token getNextToken() { position - i, position, strdup(filename), - strdup("") + isDecimal ? strdup("decimal") : strdup("integer") }; } From eef230715ac66489f999a976d83ef5dbec19214a Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:18:32 -0300 Subject: [PATCH 08/21] feat(recursion): added multiple members recursion --- .../expressions/parse_call_member_expr.c | 119 +++++++++--------- 1 file changed, 60 insertions(+), 59 deletions(-) diff --git a/src/frontend/parser/expressions/parse_call_member_expr.c b/src/frontend/parser/expressions/parse_call_member_expr.c index 3ed89f2..3f3d5b6 100644 --- a/src/frontend/parser/expressions/parse_call_member_expr.c +++ b/src/frontend/parser/expressions/parse_call_member_expr.c @@ -16,75 +16,76 @@ AstNode *parse_call_member_expr(Parser *parser, AstNode *statement) { expr = parse_primary_expr(parser); } - switch (at(parser).type) { - case TOKEN_DOT: { - int line = at(parser).line; - int column_start_member = at(parser).column_start; - int column_end_member = at(parser).column_end; - int position_start_member = at(parser).position_start; - int position_end_member = at(parser).position_end; + while (true) { + switch (at(parser).type) { + case TOKEN_DOT: { + int line = at(parser).line; + int column_start_member = at(parser).column_start; + int column_end_member = at(parser).column_end; + int position_start_member = at(parser).position_start; + int position_end_member = at(parser).position_end; - eat(parser); + eat(parser); - int column_start_val = at(parser).column_start; - int column_end_val = at(parser).column_end; + int column_start_val = at(parser).column_start; + int column_end_val = at(parser).column_end; - if (at(parser).type != TOKEN_IDENTIFIER && at(parser).type != TOKEN_NUMBER) { - error(parser, "Expected identifier or integer on member expression."); - exit(EXIT_FAILURE); - } - - char *property = strdup(at(parser).lexeme); - TokenType prop_type = eat(parser).type; - - int position_start_val = at(parser).position_start; - int position_end_val = at(parser).position_end; - - MemberPropertyNode *prop_data = MALLOC_S(sizeof(MemberPropertyNode)); - prop_data->reference = property; - prop_data->type = prop_type; + if (at(parser).type != TOKEN_IDENTIFIER && at(parser).type != TOKEN_NUMBER) { + error(parser, "Expected identifier or integer on member expression."); + exit(EXIT_FAILURE); + } + + char *property = strdup(at(parser).lexeme); + TokenType prop_type = eat(parser).type; - AstNode *prop_node = create_ast_node( - NODE_MEMBER_PROPERTY, - prop_data, - line, - column_start_val, - position_start_val, - column_end_val, - position_end_val - ); + int position_start_val = at(parser).position_start; + int position_end_val = at(parser).position_end; + + MemberPropertyNode *prop_data = MALLOC_S(sizeof(MemberPropertyNode)); + prop_data->reference = property; + prop_data->type = prop_type; - MemberNode *member_data = MALLOC_S(sizeof(MemberNode)); - member_data->member = expr; - member_data->property = prop_node; + AstNode *prop_node = create_ast_node( + NODE_MEMBER_PROPERTY, + prop_data, + line, + column_start_val, + position_start_val, + column_end_val, + position_end_val + ); - AstNode *member_node = create_ast_node( - NODE_MEMBER, - member_data, - line, - column_start_member, - position_start_member, - column_end_member, - position_end_member - ); + MemberNode *member_data = MALLOC_S(sizeof(MemberNode)); + member_data->member = expr; + member_data->property = prop_node; - return member_node; - } + AstNode *member_node = create_ast_node( + NODE_MEMBER, + member_data, + line, + column_start_member, + position_start_member, + column_end_member, + position_end_member + ); - case TOKEN_OPAREN: { - expr = parse_call_expr(parser, expr); - } break; - - case TOKEN_OBRACKET: { - expr = parse_array_access_expr(parser, expr); - } break; + return member_node; + } - case TOKEN_SEMICOLON: { - return expr; - } + case TOKEN_OPAREN: { + expr = parse_call_expr(parser, expr); + } break; + + case TOKEN_OBRACKET: { + expr = parse_array_access_expr(parser, expr); + } break; - default: { - return expr; + case TOKEN_SEMICOLON: { + return expr; + } + default: break; } } + + return expr; } \ No newline at end of file From a425af8739de1444944647b6b7c740b933ebfbd7 Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:18:54 -0300 Subject: [PATCH 09/21] feat(arg_count): added counter for args --- src/frontend/parser/expressions/parse_call_expr.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/frontend/parser/expressions/parse_call_expr.c b/src/frontend/parser/expressions/parse_call_expr.c index 5fd3f68..3b3ca7e 100644 --- a/src/frontend/parser/expressions/parse_call_expr.c +++ b/src/frontend/parser/expressions/parse_call_expr.c @@ -9,17 +9,18 @@ AstNode *parse_call_expr(Parser *parser, AstNode *caller){ int column_start = at(parser).column_start; int position_start = at(parser).position_start; - expect(parser, TOKEN_OPAREN, "Expected \")\"."); + expect(parser, TOKEN_OPAREN, "Expected \"(\"."); int arg_count = 0; AstNode **args = parse_args(parser, &arg_count); - + int column_end = at(parser).column_end; int position_end = at(parser).position_end; CallNode *call_data = MALLOC_S(sizeof(CallNode)); call_data->caller = caller; call_data->args = args; + call_data->arg_count = arg_count; AstNode *call_node = create_ast_node( NODE_CALL, From b8fac10fb7b6f286deb0fd98081dc35a3c66b1ea Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:19:20 -0300 Subject: [PATCH 10/21] feat(tokens): added parsing for return and boolean tokens --- .../parser/expressions/parse_primary_expr.c | 57 +++++++++++++++---- 1 file changed, 47 insertions(+), 10 deletions(-) diff --git a/src/frontend/parser/expressions/parse_primary_expr.c b/src/frontend/parser/expressions/parse_primary_expr.c index 780c872..521ae2a 100644 --- a/src/frontend/parser/expressions/parse_primary_expr.c +++ b/src/frontend/parser/expressions/parse_primary_expr.c @@ -35,16 +35,7 @@ AstNode *parse_primary_expr(Parser *parser) { switch (token.type) { case TOKEN_NUMBER: { - if (token.lexeme == NULL || strlen(token.lexeme) == 0) { - error(parser, "Invalid number lexeme"); - return NULL; - } - NumericLiteralNode *numeric_data = MALLOC_S(sizeof(NumericLiteralNode)); - if (!numeric_data) { - fprintf(stderr, "Error: Memory allocation failed for NumericLiteralNode\n"); - exit(EXIT_FAILURE); - } if (strcmp(token.message, "decimal") == 0) { numeric_data->value = strtod(token.lexeme, NULL); @@ -117,9 +108,55 @@ AstNode *parse_primary_expr(Parser *parser) { return node; } + case TOKEN_FALSE: + case TOKEN_TRUE: { + char *value = eat(parser).lexeme; + column_end = at(parser).column_end - 1; + position_end = at(parser).position_end - 1; + + BooleanLiteralNode *boolean_data = MALLOC_S(sizeof(BooleanLiteralNode)); + boolean_data->value = value; + + AstNode *node = create_ast_node( + NODE_BOOLEAN_LITERAL, + boolean_data, + line, + column_start, + position_start, + column_end, + position_end + ); + + return node; + } + + case TOKEN_RETURN: { + AstNode *value = parse_expr(parser); + + expect(parser, TOKEN_SEMICOLON, "Expected \";\"."); + + column_end = at(parser).column_end - 1; + position_end = at(parser).position_end - 1; + + ReturnNode *return_data = MALLOC_S(sizeof(ReturnNode)); + return_data->value = value; + + AstNode *node = create_ast_node( + NODE_RETURN, + return_data, + line, + column_start, + position_start, + column_end, + position_end + ); + + return node; + } + default: error(parser, "Unexpected token in primary expression"); - exit(EXIT_FAILURE); + eat(parser); return NULL; } } From 2b3828e820feb3bc62089de3700bf2ba1c7598a2 Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:20:00 -0300 Subject: [PATCH 11/21] feat(increment): put increment inside array index --- .../parser/statements/parse_function_declaration_stmt.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/frontend/parser/statements/parse_function_declaration_stmt.c b/src/frontend/parser/statements/parse_function_declaration_stmt.c index 19e7f39..dc2f8ed 100644 --- a/src/frontend/parser/statements/parse_function_declaration_stmt.c +++ b/src/frontend/parser/statements/parse_function_declaration_stmt.c @@ -104,12 +104,10 @@ AstNode *parse_function_declaration_stmt(Parser *parser) { while (not_eof(parser) && at(parser).type != TOKEN_END) { function_data->body = realloc( - function_data->body, - sizeof(AstNode *) * (function_data->body_count + 1) + function_data->body, + sizeof(AstNode *) * (function_data->body_count + 1) ); - - function_data->body[function_data->body_count] = parse_stmt(parser); - function_data->body_count++; + function_data->body[function_data->body_count++] = parse_stmt(parser); } expect(parser, TOKEN_END, "Expected end."); From 8163e2640dae8c30e6afd07139381eebd32f43b5 Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:20:26 -0300 Subject: [PATCH 12/21] misc(comments): only comments --- .../parser/statements/parse_variable_declaration_stmt.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/src/frontend/parser/statements/parse_variable_declaration_stmt.c b/src/frontend/parser/statements/parse_variable_declaration_stmt.c index e7a1e21..c6e7b0b 100644 --- a/src/frontend/parser/statements/parse_variable_declaration_stmt.c +++ b/src/frontend/parser/statements/parse_variable_declaration_stmt.c @@ -17,7 +17,7 @@ AstNode *parse_variable_declaration_stmt( char *name = expect(parser, TOKEN_IDENTIFIER, "Expected identifier.").lexeme; - // Type *name; + // Type *name; - Uninitialized variable declaration if (at(parser).type == TOKEN_SEMICOLON) { eat(parser); @@ -42,7 +42,7 @@ AstNode *parse_variable_declaration_stmt( return variable_node; } - // Type *name := value; + // Type *name := value; - Initialized variable declaration expect(parser, TOKEN_ASSIGN, "Expected \":=\"."); AstNode *value = parse_expr(parser); @@ -67,11 +67,6 @@ AstNode *parse_variable_declaration_stmt( at(parser).position_end - 1 ); - if (!variable_node) { - printf("Error: Failed to create variable AST node. Aborting."); - exit(EXIT_FAILURE); - } - return variable_node; } From dc9c4c68bc8689b593eb05d310f5b3999fe65f48 Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:21:05 -0300 Subject: [PATCH 13/21] feat(printing): added printing for return and call expr --- .../printer/nodes/expressions/print_boolean.c | 13 +++ .../printer/nodes/expressions/print_call.c | 24 +++++ .../printer/nodes/expressions/print_return.c | 18 ++++ .../printer/nodes/statements/print_function.c | 13 ++- src/frontend/parser/printer/print_ast.c | 93 ++++++++++--------- 5 files changed, 115 insertions(+), 46 deletions(-) create mode 100644 src/frontend/parser/printer/nodes/expressions/print_boolean.c create mode 100644 src/frontend/parser/printer/nodes/expressions/print_call.c create mode 100644 src/frontend/parser/printer/nodes/expressions/print_return.c diff --git a/src/frontend/parser/printer/nodes/expressions/print_boolean.c b/src/frontend/parser/printer/nodes/expressions/print_boolean.c new file mode 100644 index 0000000..dcded9e --- /dev/null +++ b/src/frontend/parser/printer/nodes/expressions/print_boolean.c @@ -0,0 +1,13 @@ +#include "frontend/parser/printer/nodes/expressions/print_boolean.h" +#include "frontend/ast/definitions.h" +#include "frontend/parser/printer/print_indent.h" + +void print_boolean(const AstNode *node, int depth){ + BooleanLiteralNode *boolean_data = (BooleanLiteralNode *)node->data; + if (boolean_data) { + print_indent(depth + 1); + printf("Value: %s\n", boolean_data->value); + } else { + printf("Value: (NULL data)\n"); + } +} \ No newline at end of file diff --git a/src/frontend/parser/printer/nodes/expressions/print_call.c b/src/frontend/parser/printer/nodes/expressions/print_call.c new file mode 100644 index 0000000..7d1b61d --- /dev/null +++ b/src/frontend/parser/printer/nodes/expressions/print_call.c @@ -0,0 +1,24 @@ +#include "frontend/parser/printer/nodes/expressions/print_call.h" +#include "frontend/ast/definitions.h" +#include "frontend/parser/printer/print_indent.h" +#include "frontend/parser/printer/print_ast.h" +#include "frontend/parser/printer/visited.h" + +void print_call(const AstNode *node, int depth, VisitedNodes *visited){ + CallNode *call_data = (CallNode *)node->data; + print_indent(depth + 1); + + printf("Caller:\n"); + print_ast_node(call_data->caller, depth + 2, visited); + + print_indent(depth + 1); + printf("Arguments:\n"); + if (call_data->args) { + for (size_t i = 0; i < call_data->arg_count; i++) { + print_ast_node(call_data->args[i], depth + 2, visited); + } + } else { + print_indent(depth + 2); + printf("No arguments\n"); + } +} \ No newline at end of file diff --git a/src/frontend/parser/printer/nodes/expressions/print_return.c b/src/frontend/parser/printer/nodes/expressions/print_return.c new file mode 100644 index 0000000..f6797eb --- /dev/null +++ b/src/frontend/parser/printer/nodes/expressions/print_return.c @@ -0,0 +1,18 @@ +#include "frontend/parser/printer/nodes/expressions/print_return.h" +#include "frontend/ast/definitions.h" +#include "frontend/parser/printer/print_indent.h" +#include "frontend/parser/printer/print_ast.h" +#include "frontend/parser/printer/visited.h" + +void print_return(const AstNode *node, int depth, VisitedNodes *visited){ + ReturnNode *return_data = (ReturnNode *)node->data; + + print_indent(depth + 1); + printf("Value:\n"); + if (return_data->value) { + print_ast_node(return_data->value, depth + 2, visited); + } else { + print_indent(depth + 2); + printf("No Value\n"); + } +} \ No newline at end of file diff --git a/src/frontend/parser/printer/nodes/statements/print_function.c b/src/frontend/parser/printer/nodes/statements/print_function.c index 3f28f71..fe7d10e 100644 --- a/src/frontend/parser/printer/nodes/statements/print_function.c +++ b/src/frontend/parser/printer/nodes/statements/print_function.c @@ -16,7 +16,7 @@ void print_function(const AstNode *node, int depth, VisitedNodes *visited) { printf("Name: %s\n", func_data->name); print_indent(depth + 2); - printf("Return Type: %s", func_data->type); + printf("Return Type: %s\n", func_data->type); print_indent(depth + 2); printf("Is Pointer: %s\n", func_data->isPtr ? "true" : "false"); @@ -37,7 +37,7 @@ void print_function(const AstNode *node, int depth, VisitedNodes *visited) { printf("Parameter Name: %s\n", param_data->name); print_indent(depth + 4); - printf("Type: %s", param_data->type); + printf("Type: %s\n", param_data->type); print_indent(depth + 4); printf("Is Constant: %s\n", param_data->isConst ? "true" : "false"); @@ -49,7 +49,12 @@ void print_function(const AstNode *node, int depth, VisitedNodes *visited) { print_indent(depth + 2); printf("Body:\n"); - for (int i = 0; i < func_data->body_count; i++) { - print_ast_node(func_data->body[i], depth + 3, visited); + if (func_data->body) { + for (int i = 0; i < func_data->body_count; i++) { + print_ast_node(func_data->body[i], depth + 3, visited); + } + } else { + print_indent(depth + 3); + printf("Body Null\n"); } } diff --git a/src/frontend/parser/printer/print_ast.c b/src/frontend/parser/printer/print_ast.c index b0bc4d9..0316db8 100644 --- a/src/frontend/parser/printer/print_ast.c +++ b/src/frontend/parser/printer/print_ast.c @@ -5,6 +5,7 @@ #include "frontend/parser/printer/print_indent.h" #include "frontend/parser/printer/visited.h" #include "frontend/parser/printer/nodes/print_program.h" + #include "frontend/parser/printer/nodes/expressions/print_assignment.h" #include "frontend/parser/printer/nodes/expressions/print_binary_expr.h" #include "frontend/parser/printer/nodes/expressions/print_identifier.h" @@ -17,6 +18,10 @@ #include "frontend/parser/printer/nodes/expressions/print_pre_decrement.h" #include "frontend/parser/printer/nodes/expressions/print_pre_increment.h" #include "frontend/parser/printer/nodes/expressions/print_string.h" +#include "frontend/parser/printer/nodes/expressions/print_boolean.h" +#include "frontend/parser/printer/nodes/expressions/print_call.h" +#include "frontend/parser/printer/nodes/expressions/print_return.h" + #include "frontend/parser/printer/nodes/statements/print_import.h" #include "frontend/parser/printer/nodes/statements/print_package.h" #include "frontend/parser/printer/nodes/statements/print_variable.h" @@ -41,24 +46,34 @@ const char* returnASTNodeName(NodeType node_type) { case NODE_NUMERIC_LITERAL: return "Numeric Literal"; case NODE_IDENTIFIER: return "Identifier"; case NODE_BINARY_EXPR: return "Binary Expression"; - case NODE_PACKAGE: return "Package Statement"; case NODE_IMPORT: return "Import Statement"; + case NODE_PACKAGE: return "Package Statement"; case NODE_ASSIGNMENT: return "Assignment Expression"; case NODE_OBJECT: return "Object Expression"; case NODE_PROPERTY: return "Property"; - case NODE_PRE_INCREMENT: return "Pre-Increment"; - case NODE_PRE_DECREMENT: return "Pre-Decrement"; case NODE_UNARY_MINUS: return "Unary Minus"; case NODE_LOGICAL_NOT: return "Logical Not"; case NODE_UNARY_BITWISE_NOT: return "Unary Bitwise Not"; + case NODE_PRE_INCREMENT: return "Pre-Increment"; + case NODE_PRE_DECREMENT: return "Pre-Decrement"; case NODE_VARIABLE: return "Variable Declaration"; case NODE_FUNCTION: return "Function Declaration"; + case NODE_PARAMETER: return "Function Parameter"; case NODE_FOR: return "For Statement"; - case NODE_STRING: return "String"; + case NODE_DECORATOR: return "Decorator"; + case NODE_MEMBER: return "Member Access"; + case NODE_MEMBER_PROPERTY: return "Member Property"; + case NODE_CALL: return "Call Expression"; + case NODE_ARRAY_ACCESS: return "Array Access"; + case NODE_TERNARY: return "Ternary Expression"; + case NODE_STRING: return "String Literal"; + case NODE_RETURN: return "Return Statement"; + case NODE_BOOLEAN_LITERAL: return "Boolean Literal"; default: return "Unknown"; } } + /** * @brief Prints an AST node and its details. * @@ -80,98 +95,92 @@ void print_ast_node(const AstNode *node, int depth, VisitedNodes *visited) { switch (node->kind) { case NODE_PROGRAM: { print_program(node, depth, visited); - break; - } + } break; case NODE_ASSIGNMENT: { print_assignment(node, depth, visited); - break; - } + } break; case NODE_VARIABLE: { print_variable(node, depth, visited); - break; - } + } break; case NODE_FUNCTION: { print_function(node, depth, visited); - break; - } + } break; + + case NODE_BOOLEAN_LITERAL: { + print_boolean(node, depth); + } break; + + case NODE_RETURN: { + print_return(node, depth, visited); + } break; + + case NODE_CALL: { + print_call(node, depth, visited); + } break; case NODE_FOR: { print_for(node, depth, visited); - break; - } + } break; case NODE_STRING: { print_string(node, depth); - break; - } + } break; case NODE_OBJECT: { print_object(node, depth, visited); - break; - } + } break; case NODE_PROPERTY: { print_property(node, depth, visited); - break; - } + } break; case NODE_UNARY_MINUS:{ print_unary_minus(node, depth, visited); - break; - } + } break; case NODE_PRE_DECREMENT: { print_pre_decrement(node, depth, visited); - break; - } + } break; case NODE_PRE_INCREMENT: { print_pre_increment(node, depth, visited); - break; - } + } break; case NODE_LOGICAL_NOT: { print_logical_not(node, depth, visited); - break; - } + } break; case NODE_UNARY_BITWISE_NOT: { print_unary_bitwise_not(node, depth, visited); - break; - } + } break; case NODE_PACKAGE: { print_package(node, depth); - break; - } + } break; case NODE_IMPORT: { print_import(node, depth); - break; - } + } break; case NODE_NUMERIC_LITERAL: { print_numeric_literal(node, depth); - break; - } + } break; case NODE_IDENTIFIER: { print_identifier(node, depth); - break; - } + } break; case NODE_BINARY_EXPR: { print_binary_expr(node, depth, visited); - break; - } + } break; - default: + default: { print_indent(depth + 1); printf("Value: Unknown or no data\n"); - break; + } break; } if (node->children) { From 7da6801ffff470cba7ecbf4a70d486d0735fc4c1 Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:21:40 -0300 Subject: [PATCH 14/21] feat(call): added call expr generation --- .../generator/expressions/generate_call.cpp | 50 +++++++++++++++++++ .../generator/expressions/generate_expr.cpp | 5 ++ 2 files changed, 55 insertions(+) create mode 100644 src/backend/generator/expressions/generate_call.cpp diff --git a/src/backend/generator/expressions/generate_call.cpp b/src/backend/generator/expressions/generate_call.cpp new file mode 100644 index 0000000..2cdfc6f --- /dev/null +++ b/src/backend/generator/expressions/generate_call.cpp @@ -0,0 +1,50 @@ +#include "backend/generator/statements/generate_function_declaration_stmt.hpp" +#include "backend/generator/statements/generate_stmt.hpp" +#include "backend/generator/expressions/generate_expr.hpp" +#include "backend/generator/types/generate_type.hpp" +#include "backend/generator/symbols/function_symbol_table.hpp" + +llvm::Value *generate_call(CallNode *call_node, llvm::LLVMContext &Context, llvm::IRBuilder<> &Builder, llvm::Module &Module) { + if (!call_node || !call_node->caller) { + throw std::runtime_error("Invalid call expression: missing caller."); + } + + // Assume the caller is a function name (e.g., NODE_IDENTIFIER) + IdentifierNode *identifier = static_cast(call_node->caller->data); + const std::string &function_name = identifier->symbol; + + // Lookup the function in the symbol table + auto it = function_symbol_table.find(function_name); + if (it == function_symbol_table.end()) { + throw std::runtime_error("Function not found in symbol table: " + function_name); + } + + llvm::Function *function = it->second; + llvm::FunctionType *func_type = function->getFunctionType(); + + // Generate arguments + std::vector args; + for (size_t i = 0; i < call_node->arg_count; ++i) { + llvm::Value *arg = generate_expr(call_node->args[i], Context, Builder, Module); + if (!arg) { + throw std::runtime_error("Failed to generate call argument."); + } + + // Ensure the argument type matches the function parameter type + llvm::Type *expected_type = func_type->getParamType(i); + if (arg->getType() != expected_type) { + if (arg->getType()->isIntegerTy() && expected_type->isIntegerTy()) { + arg = Builder.CreateIntCast(arg, expected_type, true); + } else if (arg->getType()->isFloatingPointTy() && expected_type->isFloatingPointTy()) { + arg = Builder.CreateFPCast(arg, expected_type); + } else { + throw std::runtime_error("Argument type mismatch."); + } + } + + args.push_back(arg); + } + + // Create the call instruction + return Builder.CreateCall(function, args); +} diff --git a/src/backend/generator/expressions/generate_expr.cpp b/src/backend/generator/expressions/generate_expr.cpp index 7d83da8..559319f 100644 --- a/src/backend/generator/expressions/generate_expr.cpp +++ b/src/backend/generator/expressions/generate_expr.cpp @@ -8,6 +8,7 @@ #include "backend/generator/expressions/generate_pre_increment.hpp" #include "backend/generator/expressions/generate_pre_decrement.hpp" #include "backend/generator/expressions/generate_assignment_expr.hpp" +#include "backend/generator/expressions/generate_call.hpp" llvm::Value *generate_expr(AstNode *node, llvm::LLVMContext &Context, llvm::IRBuilder<> &Builder, llvm::Module &Module) { // Checks the node kind, casts the node data into @@ -17,6 +18,10 @@ llvm::Value *generate_expr(AstNode *node, llvm::LLVMContext &Context, llvm::IRBu NumericLiteralNode *num_node = (NumericLiteralNode *)node->data; return generate_numeric_literal(num_node, Context); } + case NODE_CALL: { + CallNode *call_node = (CallNode *)node->data; + return generate_call(call_node, Context, Builder, Module); + } case NODE_IDENTIFIER: { IdentifierNode *id_node = (IdentifierNode *)node->data; return generate_identifier(id_node); From c5d1e1c9ffcf03a5b8b8210e7df48d9e9d11b806 Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:22:09 -0300 Subject: [PATCH 15/21] fix(logic): fixed logic for identifier look up --- .../expressions/generate_identifier.cpp | 20 +++++++++---------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/backend/generator/expressions/generate_identifier.cpp b/src/backend/generator/expressions/generate_identifier.cpp index 22a664a..23f185a 100644 --- a/src/backend/generator/expressions/generate_identifier.cpp +++ b/src/backend/generator/expressions/generate_identifier.cpp @@ -1,17 +1,15 @@ #include "backend/generator/expressions/generate_identifier.hpp" - -// A map that associates variable names (symbols) with their corresponding LLVM values. -std::map NamedValues; +#include "backend/generator/symbols/identifier_symbol_table.hpp" llvm::Value *generate_identifier(IdentifierNode *node) { - // Search for the identifier in the NamedValues map using the symbol name - auto it = NamedValues.find(node->symbol); - - // If the identifier is not found in the map - if (it == NamedValues.end()) { - // TODO: Handle the case where the identifier is not found (e.g., variable not declared) + // Usa find_identifier para buscar o valor associado ao símbolo no escopo atual + llvm::Value *value = find_identifier(node->symbol); + + // Verifica se o identificador foi encontrado + if (!value) { + throw std::runtime_error("Error: identifier not found!"); } - // Return the LLVM Value associated with the found identifier - return it->second; + // Retorna o LLVM Value associado ao identificador + return value; } From 2a926d966434225c90867ecb8a7a8184a6736da8 Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:22:45 -0300 Subject: [PATCH 16/21] feat(headers): added headers for boolean, return and call printing --- .../parser/printer/nodes/expressions/print_boolean.h | 8 ++++++++ .../parser/printer/nodes/expressions/print_call.h | 9 +++++++++ .../parser/printer/nodes/expressions/print_return.h | 9 +++++++++ 3 files changed, 26 insertions(+) create mode 100644 include/frontend/parser/printer/nodes/expressions/print_boolean.h create mode 100644 include/frontend/parser/printer/nodes/expressions/print_call.h create mode 100644 include/frontend/parser/printer/nodes/expressions/print_return.h diff --git a/include/frontend/parser/printer/nodes/expressions/print_boolean.h b/include/frontend/parser/printer/nodes/expressions/print_boolean.h new file mode 100644 index 0000000..24152b4 --- /dev/null +++ b/include/frontend/parser/printer/nodes/expressions/print_boolean.h @@ -0,0 +1,8 @@ +#ifndef PRINT_BOOLEAN_H +#define PRINT_BOOLEAN_H + +#include "frontend/ast/definitions.h" + +void print_boolean(const AstNode *node, int depth); + +#endif // PRINT_BOOLEAN_H \ No newline at end of file diff --git a/include/frontend/parser/printer/nodes/expressions/print_call.h b/include/frontend/parser/printer/nodes/expressions/print_call.h new file mode 100644 index 0000000..9fe3eee --- /dev/null +++ b/include/frontend/parser/printer/nodes/expressions/print_call.h @@ -0,0 +1,9 @@ +#ifndef PRINT_CALL_H +#define PRINT_CALL_H + +#include "frontend/ast/definitions.h" +#include "frontend/parser/printer/visited.h" + +void print_call(const AstNode *node, int depth, VisitedNodes *visited); + +#endif // PRINT_CALL_H \ No newline at end of file diff --git a/include/frontend/parser/printer/nodes/expressions/print_return.h b/include/frontend/parser/printer/nodes/expressions/print_return.h new file mode 100644 index 0000000..c70ea6b --- /dev/null +++ b/include/frontend/parser/printer/nodes/expressions/print_return.h @@ -0,0 +1,9 @@ +#ifndef PRINT_RETURN_H +#define PRINT_RETURN_H + +#include "frontend/ast/definitions.h" +#include "frontend/parser/printer/visited.h" + +void print_return(const AstNode *node, int depth, VisitedNodes *visited); + +#endif // PRINT_RETURN_H \ No newline at end of file From 19955e5d9c608e065a3f138b71f8d04f4b74c45e Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:23:11 -0300 Subject: [PATCH 17/21] feat(nodes): added boolean and return nodes --- include/frontend/ast/definitions.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/include/frontend/ast/definitions.h b/include/frontend/ast/definitions.h index 17d5588..f79a062 100644 --- a/include/frontend/ast/definitions.h +++ b/include/frontend/ast/definitions.h @@ -31,10 +31,20 @@ typedef enum { NODE_ARRAY_ACCESS, NODE_TERNARY, NODE_STRING, + NODE_RETURN, + NODE_BOOLEAN_LITERAL, } NodeType; typedef struct AstNode AstNode; +typedef struct { + char *value; +} BooleanLiteralNode; + +typedef struct { + AstNode *value; +} ReturnNode; + typedef struct { AstNode *condition; AstNode *consequent; @@ -49,6 +59,7 @@ typedef struct { typedef struct { AstNode *caller; AstNode **args; + size_t arg_count; } CallNode; typedef struct { From 285e6c6523097ec27376207887072e13dd7850f7 Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:23:56 -0300 Subject: [PATCH 18/21] feat(call): added call generator header --- .../backend/generator/expressions/generate_call.hpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 include/backend/generator/expressions/generate_call.hpp diff --git a/include/backend/generator/expressions/generate_call.hpp b/include/backend/generator/expressions/generate_call.hpp new file mode 100644 index 0000000..15fa89a --- /dev/null +++ b/include/backend/generator/expressions/generate_call.hpp @@ -0,0 +1,12 @@ +#ifndef GENERATE_CALL_H +#define GENERATE_CALL_H + +extern "C" { + #include "frontend/ast/definitions.h" +} +#include +#include + +llvm::Value *generate_call(CallNode *node, llvm::LLVMContext &Context, llvm::IRBuilder<> &Builder, llvm::Module &TheModule); + +#endif // GENERATE_CALL_H \ No newline at end of file From b03ef385146eb45cbded4ba634c1fdd2f5efd51a Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:24:28 -0300 Subject: [PATCH 19/21] feat(header): included map header --- .../generator/statements/generate_function_declaration_stmt.hpp | 1 + 1 file changed, 1 insertion(+) diff --git a/include/backend/generator/statements/generate_function_declaration_stmt.hpp b/include/backend/generator/statements/generate_function_declaration_stmt.hpp index c45e101..c5921fd 100644 --- a/include/backend/generator/statements/generate_function_declaration_stmt.hpp +++ b/include/backend/generator/statements/generate_function_declaration_stmt.hpp @@ -1,6 +1,7 @@ #ifndef GENERATE_FUNCTION_DECLARATION_STMT_H #define GENERATE_FUNCTION_DECLARATION_STMT_H +#include extern "C" { #include "frontend/ast/definitions.h" } From 952aa37e23cfe499480ef48ac0e1c3847b88f2fb Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:25:10 -0300 Subject: [PATCH 20/21] feat(generator): added generator to debug --- .vscode/c_cpp_properties.json | 15 +++++ .vscode/launch.json | 16 +++++ .vscode/settings.json | 106 ++++++++++++++++++++++++++++++++++ .vscode/tasks.json | 18 ++++++ 4 files changed, 155 insertions(+) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/settings.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..64d3f90 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,15 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [], + "cStandard": "c17", + "cppStandard": "c++17", + "intelliSenseMode": "linux-clang-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index 1c5f495..741204a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -64,6 +64,22 @@ "miDebuggerPath": "/usr/bin/gdb", "cwd": "${workspaceFolder}", "preLaunchTask": "Run Parser Test" + }, + { + "name": "Run Generator Test", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/src/backend/generator/generator_test", + "args": [ + "${workspaceFolder}/examples/a.glx" + ], + "stopAtEntry": false, + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "miDebuggerPath": "/usr/bin/gdb", + "cwd": "${workspaceFolder}", + "preLaunchTask": "Run Generator Test" } ] } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..4eb94ea --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,106 @@ +{ + "files.associations": { + "parse_multiplicative_expr.h": "c", + "parse_import_stmt.h": "c", + "parse_package_stmt.h": "c", + "parse_stmt.h": "c", + "parse_assignment_expr.h": "c", + "parse_additive_expr.h": "c", + "definitions.h": "c", + "stdio.h": "c", + "string.h": "c", + "parse_object_expr.h": "c", + "stdlib.h": "c", + "visited.h": "c", + "print_program.h": "c", + "print_assignment.h": "c", + "print_identifier.h": "c", + "print_numeric_literal.h": "c", + "print_object.h": "c", + "print_pre_decrement.h": "c", + "print_binary_expr.h": "c", + "print_ast.h": "c", + "print_package.h": "c", + "freetokens.h": "c", + "typeinfo": "c", + "text_encoding": "c", + "parse_logical_not_expr.h": "c", + "core.h": "c", + "parse_equality_expr.h": "c", + "parse_primary_expr.h": "c", + "print_indent.h": "c", + "print_import.h": "c", + "print_property.h": "c", + "print_pre_increment.h": "c", + "parse_call_expr.h": "c", + "parse_call_member_expr.h": "c", + "array": "cpp", + "atomic": "cpp", + "bit": "cpp", + "*.tcc": "cpp", + "bitset": "cpp", + "cctype": "cpp", + "charconv": "cpp", + "clocale": "cpp", + "cmath": "cpp", + "compare": "cpp", + "concepts": "cpp", + "cstdarg": "cpp", + "cstddef": "cpp", + "cstdint": "cpp", + "cstdio": "cpp", + "cstdlib": "cpp", + "cstring": "cpp", + "ctime": "cpp", + "cwchar": "cpp", + "cwctype": "cpp", + "deque": "cpp", + "set": "cpp", + "string": "cpp", + "unordered_map": "cpp", + "vector": "cpp", + "exception": "cpp", + "algorithm": "cpp", + "functional": "cpp", + "iterator": "cpp", + "memory": "cpp", + "memory_resource": "cpp", + "numeric": "cpp", + "optional": "cpp", + "random": "cpp", + "string_view": "cpp", + "system_error": "cpp", + "tuple": "cpp", + "type_traits": "cpp", + "utility": "cpp", + "format": "cpp", + "initializer_list": "cpp", + "iosfwd": "cpp", + "iostream": "cpp", + "istream": "cpp", + "limits": "cpp", + "new": "cpp", + "numbers": "cpp", + "ostream": "cpp", + "span": "cpp", + "stdexcept": "cpp", + "streambuf": "cpp", + "cinttypes": "cpp", + "variant": "cpp", + "list": "cpp", + "map": "cpp", + "parse_type.h": "c", + "print_for.h": "c", + "print_type.h": "c", + "parse_array_access_expr.h": "c", + "parse_ternary_expr.h": "c", + "chrono": "cpp", + "ratio": "cpp", + "iomanip": "cpp", + "sstream": "cpp", + "gettokentypename.h": "c", + "print_string.h": "c", + "print_call.h": "c" + }, + "C_Cpp.errorSquiggles": "disabled" +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json index c68562c..9fa146c 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -63,6 +63,24 @@ "cwd": "${workspaceFolder}" } }, + { + "label": "Run Generator Test", + "type": "shell", + "command": "./src/backend/generator/generator_test", + "args": [ + "./examples/a.glx" + ], + "group": { + "kind": "test", + "isDefault": true + }, + "problemMatcher": [ + "$gcc" + ], + "options": { + "cwd": "${workspaceFolder}" + } + }, { "type": "cppbuild", "label": "C/C++: gcc build active file", From 9a6b6c0e32e54ea72c897b759fde9a7d277f0088 Mon Sep 17 00:00:00 2001 From: wesuRage Date: Wed, 11 Dec 2024 15:26:22 -0300 Subject: [PATCH 21/21] feat(functions): first function test successful --- .vscode/c_cpp_properties.json | 15 ----- .vscode/settings.json | 106 ---------------------------------- examples/a.glx | 8 ++- 3 files changed, 7 insertions(+), 122 deletions(-) delete mode 100644 .vscode/c_cpp_properties.json delete mode 100644 .vscode/settings.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index 64d3f90..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "configurations": [ - { - "name": "Linux", - "includePath": [ - "${workspaceFolder}/**" - ], - "defines": [], - "cStandard": "c17", - "cppStandard": "c++17", - "intelliSenseMode": "linux-clang-x64" - } - ], - "version": 4 -} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 4eb94ea..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,106 +0,0 @@ -{ - "files.associations": { - "parse_multiplicative_expr.h": "c", - "parse_import_stmt.h": "c", - "parse_package_stmt.h": "c", - "parse_stmt.h": "c", - "parse_assignment_expr.h": "c", - "parse_additive_expr.h": "c", - "definitions.h": "c", - "stdio.h": "c", - "string.h": "c", - "parse_object_expr.h": "c", - "stdlib.h": "c", - "visited.h": "c", - "print_program.h": "c", - "print_assignment.h": "c", - "print_identifier.h": "c", - "print_numeric_literal.h": "c", - "print_object.h": "c", - "print_pre_decrement.h": "c", - "print_binary_expr.h": "c", - "print_ast.h": "c", - "print_package.h": "c", - "freetokens.h": "c", - "typeinfo": "c", - "text_encoding": "c", - "parse_logical_not_expr.h": "c", - "core.h": "c", - "parse_equality_expr.h": "c", - "parse_primary_expr.h": "c", - "print_indent.h": "c", - "print_import.h": "c", - "print_property.h": "c", - "print_pre_increment.h": "c", - "parse_call_expr.h": "c", - "parse_call_member_expr.h": "c", - "array": "cpp", - "atomic": "cpp", - "bit": "cpp", - "*.tcc": "cpp", - "bitset": "cpp", - "cctype": "cpp", - "charconv": "cpp", - "clocale": "cpp", - "cmath": "cpp", - "compare": "cpp", - "concepts": "cpp", - "cstdarg": "cpp", - "cstddef": "cpp", - "cstdint": "cpp", - "cstdio": "cpp", - "cstdlib": "cpp", - "cstring": "cpp", - "ctime": "cpp", - "cwchar": "cpp", - "cwctype": "cpp", - "deque": "cpp", - "set": "cpp", - "string": "cpp", - "unordered_map": "cpp", - "vector": "cpp", - "exception": "cpp", - "algorithm": "cpp", - "functional": "cpp", - "iterator": "cpp", - "memory": "cpp", - "memory_resource": "cpp", - "numeric": "cpp", - "optional": "cpp", - "random": "cpp", - "string_view": "cpp", - "system_error": "cpp", - "tuple": "cpp", - "type_traits": "cpp", - "utility": "cpp", - "format": "cpp", - "initializer_list": "cpp", - "iosfwd": "cpp", - "iostream": "cpp", - "istream": "cpp", - "limits": "cpp", - "new": "cpp", - "numbers": "cpp", - "ostream": "cpp", - "span": "cpp", - "stdexcept": "cpp", - "streambuf": "cpp", - "cinttypes": "cpp", - "variant": "cpp", - "list": "cpp", - "map": "cpp", - "parse_type.h": "c", - "print_for.h": "c", - "print_type.h": "c", - "parse_array_access_expr.h": "c", - "parse_ternary_expr.h": "c", - "chrono": "cpp", - "ratio": "cpp", - "iomanip": "cpp", - "sstream": "cpp", - "gettokentypename.h": "c", - "print_string.h": "c", - "print_call.h": "c" - }, - "C_Cpp.errorSquiggles": "disabled" -} \ No newline at end of file diff --git a/examples/a.glx b/examples/a.glx index 92c50ba..ca66200 100644 --- a/examples/a.glx +++ b/examples/a.glx @@ -1 +1,7 @@ -string oi := makonha == 3 ? "true" : "false"; \ No newline at end of file +def my_return( int i) -> int: + return i; +end; + +def main( ) -> int: + return my_return( 10) ; +end;