From f99bc8a0e15ea08a127d3f45d40cd8497273601a Mon Sep 17 00:00:00 2001 From: Zffu Date: Mon, 23 Dec 2024 22:50:50 +0100 Subject: [PATCH] fix: final fixes --- benchmarks/main.c | 18 ++++++------------ src/compiler/compiler.c | 26 +++++++++++++++++++------- src/compiler/ir.h | 3 ++- src/lexer/lexer.c | 10 ++++++++-- src/parser/ast.c | 1 + src/parser/ast.h | 2 ++ src/parser/asts/functions.c | 20 ++++++++++++++------ 7 files changed, 52 insertions(+), 28 deletions(-) diff --git a/benchmarks/main.c b/benchmarks/main.c index 1d49f55..3985c50 100644 --- a/benchmarks/main.c +++ b/benchmarks/main.c @@ -123,7 +123,7 @@ void main(int argc, char* argv[]) { endTimer(i, 0); startTimer(); - struct LexerResult result = runLexer(buff); + struct LexerResult result = runLexer(buff, size); free(buff); @@ -136,20 +136,14 @@ void main(int argc, char* argv[]) { endTimer(i, 2); startTimer(); - struct Context ctx = parseContext(node); - char* compiled = compileV2(ctx); + fptr = fopen("output.txt", "w"); - endTimer(i, 3); - - startTimer(); - - fptr = fopen("output.txt", "w"); - fprintf(fptr, compiled); - fclose(fptr); + IR_CTX* ctx = makeContext(node); + compile(ctx, fptr); - endTimer(i, 4); + fclose(fptr); - free(compiled); + endTimer(i, 3); } printf("========= Benchmarking Results =========\n"); diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 5992f9f..73cb241 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -14,6 +14,8 @@ #include "../utils/hash.h" #include "../utils/hashmap.h" +#include "./pe/pe.h" + /** * Parses the AST tree into a context. * @param tree the AST tree. @@ -76,11 +78,13 @@ IR_CTX* makeContext(AST_NODE* tree) { node->variables[node->variableIndex] = var; node->variableIndex++; - hashPut(node->variableMap, hashstr(tree->right->value), var); + hashPut(node->variableMap, hashstr(tree->left->left->right->value), var); tree->left->left = tree->left->left->next; } + node->tree = tree->right; + ctx->nodes[ctx->nodeIndex] = node; ctx->nodeIndex++; @@ -98,6 +102,7 @@ IR_CTX* makeContext(AST_NODE* tree) { node = createIRNode(IR_ASM_FUNCTION, tree->left->right->value); node->value = tree->value; + node->valueSize = tree->valueSize; while(tree->left->left->next != NULL) { @@ -151,26 +156,33 @@ void compile(IR_CTX* ctx, FILE* out) { return; } - while(node->tree->next != NULL) { + while(node->tree != NULL) { if(node->tree->type == AST_FUNCTION_INVOKE) { + int hash = hashstr(node->tree->value); - AST_NODE* wa = hashGet(ctx->nodeMap, hash); + IR_NODE* wa = hashGet(ctx->nodeMap, hash); if(wa == NULL) { printf("Error: The %s function doesn't exist!\n", node->tree->value); return; } - if(wa->type == IR_ASM_FUNCTION) { - printf("%d", wa->value); + if(wa->nodeType == IR_ASM_FUNCTION) { + unsigned char b; + unsigned char* ptr = (unsigned char*) wa->value; + + for(int ii = 0; ii < wa->valueSize; ++ii) { + buff[i] = ptr[ii]; + ++i; + } } } node->tree = node->tree->next; } - - + //todo: change format based on user + compilePE(out, buff, i); } diff --git a/src/compiler/ir.h b/src/compiler/ir.h index bec18d7..9271653 100644 --- a/src/compiler/ir.h +++ b/src/compiler/ir.h @@ -31,7 +31,8 @@ struct IRNode { char* type; // Variable Properties - char* value; + void* value; + int valueSize; // Function Properties struct IRNode** variables; diff --git a/src/lexer/lexer.c b/src/lexer/lexer.c index 2d21ccb..c61f19a 100644 --- a/src/lexer/lexer.c +++ b/src/lexer/lexer.c @@ -45,13 +45,19 @@ struct LexerResult runLexer(char* string, int size) { else if (isdigit(c)) { int numLen = 0; - while(isdigit(c) || c == 'x') { + int foundX = 0; + while(isdigit(c) || c == 'x' || (foundX == 1 && c != '\0' && c != ' ' && c != '\n')) { buff[numLen] = c; numLen++; - c = *string++; + if(c == 'x') foundX = 1; + + ++i; + c = string[i]; } + buff[numLen] = '\0'; + pushToken(&result, NUMBER); result.tokens[result.size - 1].value = buff; diff --git a/src/parser/ast.c b/src/parser/ast.c index 21dc281..4400d84 100644 --- a/src/parser/ast.c +++ b/src/parser/ast.c @@ -13,6 +13,7 @@ AST_NODE* createASTNode(enum ASTNodeType type) { AST_NODE* node = malloc(sizeof(AST_NODE)); + node->valueSize = 0; node->type = type; node->left = NULL; node->right = NULL; diff --git a/src/parser/ast.h b/src/parser/ast.h index 9c9c916..d666f0b 100644 --- a/src/parser/ast.h +++ b/src/parser/ast.h @@ -45,6 +45,8 @@ struct ASTNode { struct ASTNode* next; enum ASTNodeType type; + + int valueSize; char* value; int endingIndex; // The index which the parsing ended diff --git a/src/parser/asts/functions.c b/src/parser/asts/functions.c index 12aec00..330b3f6 100644 --- a/src/parser/asts/functions.c +++ b/src/parser/asts/functions.c @@ -180,15 +180,20 @@ AST_NODE* parseASMFunctionDeclaration(struct LexerResult result, int index) { break; } - if(t.type != STRING) { + if(t.type != NUMBER) { return NULL; } buff[buffIndex] = strtol(t.value, NULL, 16); + buffIndex++; } node->endingIndex = index; - node->value = (char*) buff; + + buff = realloc(buff, sizeof(uint8_t) * buffIndex); + + node->valueSize = buffIndex; + node->value = buff; return node; } @@ -198,11 +203,14 @@ AST_NODE* parseFunctionInvoke(struct LexerResult result, int index) { node->value = result.tokens[index].value; - AST_NODE* args = parseArguments(result, index + 1); + AST_NODE* args = parseArguments(result, index + 2); - if(args != NULL) node->right = args; - - node->endingIndex = args->endingIndex; + node->endingIndex = index; + + if(args != NULL) { + node->right = args; + node->endingIndex = args->endingIndex; + } return node; }