Skip to content

Commit

Permalink
fix: final fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zffu committed Dec 23, 2024
1 parent fb81854 commit f99bc8a
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 28 deletions.
18 changes: 6 additions & 12 deletions benchmarks/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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");
Expand Down
26 changes: 19 additions & 7 deletions src/compiler/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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++;

Expand All @@ -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) {

Expand Down Expand Up @@ -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);
}
3 changes: 2 additions & 1 deletion src/compiler/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ struct IRNode {
char* type;

// Variable Properties
char* value;
void* value;
int valueSize;

// Function Properties
struct IRNode** variables;
Expand Down
10 changes: 8 additions & 2 deletions src/lexer/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions src/parser/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions src/parser/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ struct ASTNode {
struct ASTNode* next;

enum ASTNodeType type;

int valueSize;
char* value;
int endingIndex; // The index which the parsing ended

Expand Down
20 changes: 14 additions & 6 deletions src/parser/asts/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}

0 comments on commit f99bc8a

Please sign in to comment.