Ajout chaines de caractère au compilateur

This commit is contained in:
Faure Paul 2021-07-13 17:17:21 +02:00
parent 5eaffa4453
commit 012f46f03c
5 changed files with 88 additions and 7 deletions

View file

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

View file

@ -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<nombre> tNB tNBEXP
%token<id> tID
%token tPRINTF tGET tSTOP
%token<str> tSTR
%token tPRINT tGET tSTOP
%token<nombre> tIF tELSE
%token<my_while> 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
};

View file

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

View file

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

View file

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