-
Notifications
You must be signed in to change notification settings - Fork 0
/
semantic.c
executable file
·125 lines (104 loc) · 3.2 KB
/
semantic.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <ctype.h>
#include <stdio.h>
#include <string.h>
#include "headers/semantic.h"
#include "headers/fAuxs.h"
extern char token_buffer[];
extern FILE * out_ensambler;
/* ------------------- RUTINAS SEMANTICAS ------------------- */
REG_EXPRESION process_literal(void)
{
/* Convierte cadena que representa numero a numero entero y construye un registro semantico */
REG_EXPRESION reg;
reg.clase = INTLITERAL;
strcpy(reg.name, token_buffer);
sscanf(token_buffer, "%d", ®.value);
return reg;
}
REG_EXPRESION process_id(void)
{
/* Declare ID y construye el correspondiente registro semantico */
REG_EXPRESION reg;
check_id(token_buffer);
reg.clase = ID;
strcpy(reg.name, token_buffer);
return reg;
}
char * process_op(void)
{
/* Declare OP y construye el correspondiente registro semantico */
return token_buffer;
}
void read_id(REG_EXPRESION in)
{
/* Genera la instruccion para leer */
generate("Read", in.name, "Integer", "");
}
void write_expr(REG_EXPRESION out)
{
/* Genera la instruccion para escribir */
generate("Write", extract(&out), "Integer", "");
}
REG_EXPRESION gen_infix(REG_EXPRESION e1, char *op, REG_EXPRESION e2)
{
/* Genera la instruccion para una operacion infija y construye un registro semantico con el result */
REG_EXPRESION reg,e_rec;
static unsigned int numTemp = 1;
char cadTemp[MAXIDLEN] ="Temp_";
char cadNum[MAXIDLEN];
char cadOp[MAXIDLEN];
char arr[5];
if ( op[0] == '-' ) strcpy(cadOp, "Sub");
if ( op[0] == '+' ) strcpy(cadOp, "Add");
sprintf(cadNum, "%d", numTemp);
numTemp++;
strcat(cadTemp, cadNum);
if ( e1.clase == ID) check_id( extract(&e1));
if ( e2.clase == ID) check_id( extract(&e2));
//CONSTANT FOLDING SI SON DOS ENTEROS SEGUIDOS LOS CALCULA Y CONSTRUYE UNA NUEVA EXPRESION
if (e1.clase == INTLITERAL && e2.clase == INTLITERAL){
e_rec.clase = INTLITERAL;
strcpy(e_rec.name, cadTemp);
if ( op[0] == '-'){
e_rec.value = e1.value - e2.value;
strcpy(arr, "sub");
}else{
e_rec.value = e1.value + e2.value;
strcpy(arr, "add");
}
sprintf(cadTemp, "%d", e_rec.value);
strcpy(e_rec.name, cadTemp);
return e_rec;
}else{
check_id(cadTemp);
generate(cadOp, e1.name, e2.name, cadTemp);
strcpy(reg.name, cadTemp);
return reg;
}
}
REG_EXPRESION conditional_expressions(REG_EXPRESION e1, REG_EXPRESION e2, REG_EXPRESION e3){
REG_EXPRESION e_rec;
static unsigned int numTempN = 1;
static unsigned int numTempK = 1;
char cadTemp[MAXIDLEN] ="Temp_C&";
char cadNumN[MAXIDLEN];
char cadNumK[MAXIDLEN];
sprintf(cadNumN, "%d", numTempN);
numTempN++;
strcat(cadTemp, cadNumN);
strcpy(e1.name, cadTemp);
sprintf(cadTemp, "%d", e1.value);
printf("IF_%s %s\n",cadTemp,cadTemp);
printf("THEN_\n%s\n",e2.name);
printf("ELSE_\n%s\n",e3.name);
printf("END_IF\n");
/* if(e1.value == 0){
e_rec.value = e3.value;
}else{
e_rec.value = e2.value;
}
sprintf(cadTemp, "%d", e_rec.value);
strcpy(e_rec.name, cadTemp);
return e_rec;*/
return e_rec;
}