From ec138f82b361f57b662dd600f0b067e1766a607d Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Tue, 6 Apr 2021 16:33:49 +0200 Subject: [PATCH] implement print --- as.y | 2 +- asm_instructions.c | 8 +++++++- test.c | 2 +- yacc_util.c | 7 ++++++- yacc_util.h | 2 ++ 5 files changed, 17 insertions(+), 4 deletions(-) diff --git a/as.y b/as.y index 89a3678..42dc931 100644 --- a/as.y +++ b/as.y @@ -109,7 +109,7 @@ VariableIdentifier: tID { add_symbol(&symbol_table, current_type, $1); } // // Cond : tNOT Cond | Cond tAND Cond | Cond tOR Cond | E tEQ2 E | E tINF E | E tINFEQ E | E tSUP E | E tSUPEQ E | E tNOTEQ E | tNOT tPO Cond tPF { printf("Cond\n"); } ; -Printf : tPRINTF tPO tID tPF tPV { printf("Printf\n"); } ; +Printf : tPRINTF tPO E tPF tPV { write_print(&instruction_table, $3); } ; // If : tIF tPO Cond tPF Body ; // diff --git a/asm_instructions.c b/asm_instructions.c index c9b5af5..8a44798 100644 --- a/asm_instructions.c +++ b/asm_instructions.c @@ -41,5 +41,11 @@ void write_instruction_table(InstructionTable *table, FILE *file) { } void write_instruction(InstructionItem *item, FILE *file) { - fprintf(file, "%s %d %d %d\n", instructions_labels[item->instruction], item->arg1, item->arg2, item->arg3); + if (item->instruction == PRI || item->instruction == JMP) { + fprintf(file, "%s %d\n", instructions_labels[item->instruction], item->arg1); + } else if (item->instruction == COP || item->instruction == AFC || item->instruction == JMF) { + fprintf(file, "%s %d %d\n", instructions_labels[item->instruction], item->arg1, item->arg2); + } else { + fprintf(file, "%s %d %d %d\n", instructions_labels[item->instruction], item->arg1, item->arg2, item->arg3); + } } \ No newline at end of file diff --git a/test.c b/test.c index 78697ad..c647b12 100644 --- a/test.c +++ b/test.c @@ -1,7 +1,7 @@ int main(){ int x, y, z; const PL5_op, b, c; - printf(x); x = 2; + printf(x); y = 3 + 3 + x; } diff --git a/yacc_util.c b/yacc_util.c index 12d4572..b7d3cbb 100644 --- a/yacc_util.c +++ b/yacc_util.c @@ -3,7 +3,7 @@ void write_affectation(SymbolTable* symbol_table, InstructionTable *instruction_table, char* symbol_dest, SymbolItem* src) { mark_symbol_initialized(symbol_table, symbol_dest); SymbolItem *dest = get_symbol_item(symbol_table, symbol_dest); - add_instruction(instruction_table, COP, src->address, dest->address, 0); + add_instruction(instruction_table, COP, dest->address, src->address, 0); } const SymbolItem* write_op(SymbolTable* symbol_table, InstructionTable *instruction_table, Instruction instruction, SymbolItem *op1, SymbolItem *op2) { @@ -28,3 +28,8 @@ const SymbolItem* init_temp_symbol(SymbolTable* symbol_table, InstructionTable * return dest; } +void write_print(InstructionTable *instruction_table, SymbolItem *src) { + add_instruction(instruction_table, PRI, src->address, 0, 0); +} + + diff --git a/yacc_util.h b/yacc_util.h index 0fd1058..2c5277a 100644 --- a/yacc_util.h +++ b/yacc_util.h @@ -13,4 +13,6 @@ const SymbolItem* write_negation(SymbolTable* symbol_table, InstructionTable *in const SymbolItem* init_temp_symbol(SymbolTable* symbol_table, InstructionTable *instruction_table, int constant); +void write_print(InstructionTable *instruction_table, SymbolItem *src); + #endif //COMPILATOR_2000_YACC_UTIL_H