implement print

This commit is contained in:
Arnaud Vergnet 2021-04-06 16:33:49 +02:00
parent 74e97fe963
commit ec138f82b3
5 changed files with 17 additions and 4 deletions

2
as.y
View file

@ -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 ;
//

View file

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

2
test.c
View file

@ -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;
}

View file

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

View file

@ -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