diff --git a/Lex_Yacc/al.lex b/Lex_Yacc/al.lex index d514520..84d56e5 100644 --- a/Lex_Yacc/al.lex +++ b/Lex_Yacc/al.lex @@ -41,7 +41,7 @@ yyerror (char const *s) "]" { return tCCROCH; } // Token crochet ouvrante "get" { return tGET; } // Token fonction get -"printf" { return tPRINTF; } // Token fonction print +"print" { return tPRINT; } // Token fonction print "stop" { return tSTOP; } // Token fonction stop "+" { return tADD; } // Token addition @@ -57,6 +57,8 @@ yyerror (char const *s) [0-9]+ { yylval.nombre = atoi(yytext); return tNB; } // Token nombre au format classique [0-9]+e[0-9]+ { yylval.nombre = -1; return tNB; } // Token nombre au format exponentiel +["][^"]*["] { strcpy(yylval.str, yytext); return tSTR; } + [a-zA-Z][a-zA-Z0-9_]* { strcpy(yylval.id, yytext); return tID; } // Chaine de caractère (identifiant variable, fonction..) diff --git a/Lex_Yacc/as.y b/Lex_Yacc/as.y index 4522914..5f49bb7 100644 --- a/Lex_Yacc/as.y +++ b/Lex_Yacc/as.y @@ -10,7 +10,8 @@ %union { int nombre; struct symbole_t symbole; - char id[30]; + char id[30]; + char str[300]; struct while_t my_while; } %{ @@ -45,7 +46,8 @@ int first_etoile = 1; %token tMUL tDIV tADD tSUB tEQ %token tNB tNBEXP %token tID -%token tPRINTF tGET tSTOP +%token tSTR +%token tPRINT tGET tSTOP %token tIF tELSE %token tWHILE %token tRETURN @@ -116,10 +118,84 @@ Get : tGET tOBRACE tCBRACE {int addr = push("0_TEMPORARY", 0, integer); }; // Print, une fonction particulière -Print : tPRINTF tOBRACE E tCBRACE {add_operation(PRI,$3,0,0); // On ajoute l'instruction PRI - pop(); // On supprime la variable temporaire +Print : tPRINT tOBRACE E tCBRACE {add_operation(PRI,$3,0,0); // On ajoute l'instruction PRI + pop(); // On supprime la variable temporaire }; +// Print, une fonction particulière +Print : tPRINT tOBRACE tSTR tCBRACE {int i = 0; + int decalage = 0; + char previous = 'a'; + while ($3[i] != '\0') { + if (previous == '\\' && $3[i] == 'n') { + $3[i - decalage - 1] = '\n'; + previous = 'a'; + decalage++; + } else { + $3[i-decalage] = $3[i]; + previous = $3[i]; + } + i++; + } + $3[i-decalage] = $3[i]; + i=0; + int termine = $3[i+1] == '"'; + int addr = push("0_TEMPORARY", 0, integer); + while (!termine) { + if ($3[i+1] != '"') { + add_operation(AFC, addr + i, $3[i+1], 0); + } else { + termine = 1; + } + i++; + if (!termine && $3[i+1] != '"') { + add_operation(AFC, addr + i, $3[i+1], 0); + } else { + termine = 1; + } + i++; + if (!termine && $3[i+1] != '"') { + add_operation(AFC, addr + i, $3[i+1], 0); + } else { + termine = 1; + } + i++; + if (!termine && $3[i+1] != '"') { + add_operation(AFC, addr + i, $3[i+1], 0); + } else { + termine = 1; + } + + i = i - 3; + termine = 0; + if ($3[i+1] != '"') { + add_operation(PRIC, addr + i, 0, 0); + } else { + termine = 1; + } + i++; + if (!termine && $3[i+1] != '"') { + add_operation(PRIC, addr + i, 0, 0); + } else { + termine = 1; + } + i++; + if (!termine && $3[i+1] != '"') { + add_operation(PRIC, addr + i, 0, 0); + } else { + termine = 1; + } + i++; + if (!termine && $3[i+1] != '"') { + add_operation(PRIC, addr + i, 0, 0); + } else { + termine = 1; + } + i++; + } + pop(); // On supprime la variable temporaire + }; + // Stop, une fonction particulière Stop : tSTOP tOBRACE tNB tCBRACE {add_operation(STOP,$3,0,0); // On ajoute juste l'instruction stop }; diff --git a/Makefile b/Makefile index dff707e..37bbfa9 100644 --- a/Makefile +++ b/Makefile @@ -31,7 +31,7 @@ clean_Lex_Yacc: ### COMPILATION ### ########################### build : clean build_Symboles build_Instructions build_Lex_Yacc build_Fonctions - gcc Lex_Yacc/as.tab.o Lex_Yacc/lex.yy.o Tables/Instructions/tab_instruc.o Tables/Symboles/table_symboles.o Tables/Fonctions/tab_fonctions.o -ll -o rondoudou_gcc + gcc Lex_Yacc/as.tab.o Lex_Yacc/lex.yy.o Tables/Instructions/tab_instruc.o Tables/Symboles/table_symboles.o Tables/Fonctions/tab_fonctions.o -ly -o rondoudou_gcc build_Symboles: clean_Symboles gcc -c Tables/Symboles/table_symboles.c -o Tables/Symboles/table_symboles.o diff --git a/Tables/Instructions/tab_instruc.c b/Tables/Instructions/tab_instruc.c index d3c5e38..7fe3010 100644 --- a/Tables/Instructions/tab_instruc.c +++ b/Tables/Instructions/tab_instruc.c @@ -75,6 +75,9 @@ char * get_asm_line_from_op(struct operation_t op){ case (PRI): sprintf(buffer,"PRI %d\n",op.arg1); break; + case (PRIC): + sprintf(buffer,"PRIC %d\n",op.arg1); + break; case (READ): sprintf(buffer,"READ %d %d\n",op.arg1, op.arg2); break; diff --git a/Tables/Instructions/tab_instruc.h b/Tables/Instructions/tab_instruc.h index 0d0953a..f5cdd53 100644 --- a/Tables/Instructions/tab_instruc.h +++ b/Tables/Instructions/tab_instruc.h @@ -8,7 +8,7 @@ // Liste de codes des instruction -enum opcode_t {ADD,MUL,SOU,DIV,COP,AFC,AFCA,JMP,JMF,INF,SUP,EQU,PRI,READ,WR,CALL,RET,GET,STOP}; +enum opcode_t {ADD,MUL,SOU,DIV,COP,AFC,AFCA,JMP,JMF,INF,SUP,EQU,PRI,PRIC,READ,WR,CALL,RET,GET,STOP}; //Ajoute une opération dans la table (à la fin) void add_operation(enum opcode_t opcode, int arg1, int arg2, int arg3);