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 c06611b commit fb81854
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 16 deletions.
14 changes: 11 additions & 3 deletions src/cli/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

#include "../lexer/lexer.h"

#include "../parser/ast.h"
#include "../parser/parser.h"

#include "../compiler/compiler.h"
#include "../compiler/pe/pe.h"
#include "../compiler/ir.h"

#include "../utils/logging.c"

// Version
#define VERSION "0.1.0"

uint8_t code_section[6] = {
0xB8, 0x4C, 0x00, 0x00, 0x00, 0xC3
};

void showCommandEntry(char* commandName, char* description, int argumentCount, char* argumentNames[], char* argumentDescriptions[]) {
printf("\n > %s\n\n %s%sDescription%s: %s\n", commandName, STYLE_BOLD, STYLE_UNDERLINE, RESET, description);
Expand Down Expand Up @@ -108,11 +113,11 @@ int main(int argc, char* argv[]) {

char* buff = malloc(size + 1); // Allocates one more byte for the \0 char.

fread(buff, 1, size, fptr);
size = fread(buff, 1, size, fptr);
buff[size] = '\0';
fclose(fptr);

struct LexerResult result = runLexer(buff);
struct LexerResult result = runLexer(buff, size);
struct ASTNode* root = parseNodes(result, 0, AST_ROOT);

IR_CTX* ctx = makeContext(root);
Expand All @@ -122,7 +127,10 @@ int main(int argc, char* argv[]) {
return -1;
}

compile(ctx, outputFile);
fptr = fopen(outputFile, "w");
compile(ctx, fptr);

//compilePE(fptr, code_section, sizeof(code_section));

break;
case 'v':
Expand Down
54 changes: 47 additions & 7 deletions src/compiler/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* The compiler of Quickfall.
*/

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>

Expand All @@ -26,9 +27,7 @@ IR_CTX* makeContext(AST_NODE* tree) {
ctx->nodeIndex = 0;
ctx->nodeMap = createHashmap(512,200);

while(tree->next != NULL) {
tree = tree->next;

while(tree != NULL) {
switch(tree->type) {
case AST_VARIABLE_DECLARATION:

Expand Down Expand Up @@ -59,10 +58,10 @@ IR_CTX* makeContext(AST_NODE* tree) {

case AST_FUNCTION_DECLARATION:

hash = hashstr(tree->left->value);
hash = hashstr(tree->left->right->value);

if(hashGet(ctx->nodeMap, hash) != NULL) {
printf("Function %s was already declared!\n", tree->left->value);
printf("Function %s was already declared!\n", tree->left->right->value);
return NULL;
}

Expand All @@ -86,9 +85,9 @@ IR_CTX* makeContext(AST_NODE* tree) {
ctx->nodeIndex++;

hashPut(ctx->nodeMap, hash, node);
break;

case AST_ASM_FUNCTION_DECLARATION:

hash = hashstr(tree->left->right->value);

if(hashGet(ctx->nodeMap, hash) != NULL) {
Expand Down Expand Up @@ -121,6 +120,8 @@ IR_CTX* makeContext(AST_NODE* tree) {
break;

}

tree = tree->next;
}

return ctx;
Expand All @@ -131,6 +132,45 @@ IR_CTX* makeContext(AST_NODE* tree) {
* @param ctx the IR context.
* @param char the output file name.
*/
void compile(IR_CTX* ctx, char* outputFileName) {
void compile(IR_CTX* ctx, FILE* out) {

uint8_t* buff = malloc(sizeof(uint8_t) * 512);
int i = 0;

int h = hashstr("main");

if(hashGet(ctx->nodeMap, h) == NULL) {
printf("Error: the main function wasn't defined!\n");
return;
}

IR_NODE* node = hashGet(ctx->nodeMap, h);

if(node->nodeType != IR_FUNCTION) {
printf("Error: main must be a function!\n");
return;
}

while(node->tree->next != NULL) {

if(node->tree->type == AST_FUNCTION_INVOKE) {
int hash = hashstr(node->tree->value);

AST_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);
}
}

node->tree = node->tree->next;
}



}
4 changes: 2 additions & 2 deletions src/compiler/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ IR_CTX* makeContext(AST_NODE* tree);
/**
* Compiles the Context tree to an executable named the provided name.
* @param ctx the IR context.
* @param char the output file name.
* @param char the output file.
*/
void compile(IR_CTX* ctx, char* outputFileName);
void compile(IR_CTX* ctx, FILE* outputFileName);

#endif
3 changes: 3 additions & 0 deletions src/compiler/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ struct IRContext {
int nodeIndex;

struct Hashmap* nodeMap;

IR_NODE* mainFunc;

};

typedef struct IRContext IR_CTX;
Expand Down
4 changes: 3 additions & 1 deletion src/parser/ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ enum ASTNodeType {
AST_FUNCTION_HEADER,

AST_FUNCTION_INVOKE,


AST_FUNCTION_ROOT,

AST_MATH_OPERATOR,
AST_MATH_OPERATION,
AST_MATH_OP_HEADER,
Expand Down
10 changes: 8 additions & 2 deletions src/parser/asts/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ AST_NODE* parseParameters(struct LexerResult result, int index) {
case PAREN_CLOSE:
root->endingIndex = index;
return root;
case PAREN_OPEN:
continue;
default:
printf("Type: %d", t.type);
return NULL;

}
Expand Down Expand Up @@ -109,6 +112,7 @@ AST_NODE* parseFunctionDeclaration(struct LexerResult result, int index) {
node->left = createASTNode(AST_FUNCTION_HEADER);

if(result.tokens[index].type != KEYWORD) {
printf("jd");
return NULL;
}

Expand Down Expand Up @@ -136,7 +140,9 @@ AST_NODE* parseFunctionDeclaration(struct LexerResult result, int index) {

node->left->left = params;

node->right = parseNodes(result, params->endingIndex, AST_ROOT);
node->right = parseNodes(result, params->endingIndex, AST_FUNCTION_ROOT);

node->endingIndex = node->right->endingIndex;

return node;
}
Expand Down Expand Up @@ -199,4 +205,4 @@ AST_NODE* parseFunctionInvoke(struct LexerResult result, int index) {
node->endingIndex = args->endingIndex;

return node;
}
}
8 changes: 7 additions & 1 deletion src/parser/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@ AST_NODE* parseNodes(struct LexerResult result, int index, enum ASTNodeType type
AST_NODE* node = NULL;

switch(t.type) {
case BRACKETS_CLOSE:
if(type == AST_FUNCTION_ROOT) {
root->endingIndex = index;
return root;
}
break;
case FUNCTION:
node = parseFunctionDeclaration(result, index);
node = parseFunctionDeclaration(result, index + 1);
if(node != NULL) {
current->next = node;
current = node;
Expand Down
1 change: 1 addition & 0 deletions src/utils/hashmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* A simple hashmap implementation.
*/

#include <stdio.h>
#include <stdlib.h>

#include "./hashmap.h"
Expand Down

0 comments on commit fb81854

Please sign in to comment.