Skip to content

Commit

Permalink
feat: Add something
Browse files Browse the repository at this point in the history
  • Loading branch information
yardexx committed Nov 24, 2023
1 parent efd27c2 commit e9b9f35
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
7 changes: 6 additions & 1 deletion include/compiler/codegen/Instruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
#define HEADER \
fprintf(stdout, ".IFJcode23\n");

// TODO: Fix
void Instruction_pops_where(char * where, enum Frame frame);

void Instruction_defvar_where(char * where, enum Frame frame);

// --- UTILS ---

void Instruction_label_while_start(size_t id);
Expand Down Expand Up @@ -59,7 +64,7 @@ void Instruction_readInt(size_t id, enum Frame frame);

void Instruction_readFloat(size_t id, enum Frame frame);

void Instruction_write(size_t id, enum Frame frame);
void Instruction_write(char * id, enum Frame frame);

void Instruction_defvar(size_t id, enum Frame frame);

Expand Down
9 changes: 7 additions & 2 deletions src/compiler/codegen/Codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ void Codegen_generate(Codegen *codegen) {

void __Codegen_generate(Codegen *codegen) {
__Codegen_generatePreamble();
// TODO: Shortcut, fix it later
Instruction_defvar_where("WRITE_TMP", FRAME_GLOBAL);

__Codegen_generateGlobalVariablesDeclarations(codegen);
__Codegen_walkAST(codegen);
}
Expand Down Expand Up @@ -291,6 +294,8 @@ void __Codegen_evaluateFunctionDeclaration(Codegen *codegen, FunctionDeclaration

// Process body
__Codegen_evaluateBlock(codegen, functionDeclaration->body);

codegen->frame = FRAME_GLOBAL;
}


Expand Down Expand Up @@ -429,8 +434,8 @@ __Codegen_resolveBuiltInFunction(Codegen *codegen, FunctionCallASTNode *function
for (size_t i = 0; i < argumentList->arguments->size; ++i) {
ArgumentASTNode *argument = Array_get(argumentList->arguments, i);
__Codegen_evaluateStatement(codegen, (StatementASTNode *) argument->expression);
Instruction_pops(argument->label->id, codegen->frame);
Instruction_write(argument->label->id, codegen->frame);
Instruction_pops_where("WRITE_TMP", codegen->frame);
Instruction_write("WRITE_TMP", codegen->frame);
}
} break;
case FUNCTION_INT_TO_DOUBLE:
Expand Down
13 changes: 11 additions & 2 deletions src/compiler/codegen/Instruction.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,17 @@ void Instruction_pushs_bool(bool value) {
fprintf(stdout, "PUSHS bool@%s\n", value ? "true" : "false");
}

void Instruction_write(size_t id, enum Frame frame){
fprintf(stdout, "WRITE %s@$%lu\n", __Instruction_getFrame(frame), id);
void Instruction_write(char * id, enum Frame frame){
fprintf(stdout, "WRITE %s@$%s\n", __Instruction_getFrame(frame), id);
}

// TODO: This is very bad shortcut, should be fixed
void Instruction_pops_where(char * where, enum Frame frame) {
fprintf(stdout, "POPS %s@%s\n", __Instruction_getFrame(frame), where);
}

void Instruction_defvar_where(char * where, enum Frame frame) {
fprintf(stdout, "DEFVAR %s@%s\n", __Instruction_getFrame(frame), where);
}

void Instruction_pushs_int(long value) {
Expand Down

0 comments on commit e9b9f35

Please sign in to comment.