-
Notifications
You must be signed in to change notification settings - Fork 0
/
codegen.js
257 lines (191 loc) · 6.43 KB
/
codegen.js
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
var parser = require('./grammar').parser;
if (process.argv.length < 3) {
console.log('Usage: node ' + process.argv[1] + ' FILENAME');
process.exit(1);
}
// Read the file and print its contents.
var fs = require('fs'),
filename = process.argv[2];
fs.readFile(filename, 'utf8', function(err, program) {
if (err) throw err;
console.log('\n************************ CODIGO EM CAFEZINHO\n'+program+'\n\n\n\n');
/*
programa-node
id-node
vector-node
declfuncvar-node
declprog-node
intconst-node
declvar-node
notype-id-node
notype-vector-node
declfunc-node
listaparametros-node
nosize-vector-node
listaparametroscont-node
listadeclvar-node
int-node
car-node
listacomando-nodew
comando-vazio-node
comando-expr-node
comando-retorne-node
comando-leia-node
comando-escreva-node
comando-novalinha-node
comando-seentao-node
comando-seentaosenao
comando-enquanto-node
assignexpr-node
assignexpr-vector-node
or-node
and-node
equals-node
dif-node
'less-node'
'greater-node'
'greater-equals-node'
'less-equals-node'
'plus-node'
'minus-node'
'multiplication-node'
'division-node'
'modulus-node'
'negation-node'
'no-node'
'access-vector-node'
'function-node'
access-vector-node'
notype-id-node'
'string-node'
'listexpr-node'
*/
function main() {
var ast = parser.parse(program);
console.log('#################### ARVORE SINTATICA ABSTRATA\n\n');
console.log(ast);
console.log('\n\n\n')
// console.log(ast)
var source = codegen(ast);
console.log('##################### CODIGO EM JS\n\n'+source+'\n\n');
}
function codegen(ast) {
if (ast)
switch (ast[0]) {
case 'programa-node':
return codegen(ast[2]) + '\nmain =function()' + codegen(ast[3]) + '\nmain(); '
case 'id-node':
return "var " + ast[1].name;
case 'vector-node':
return "var " + ast[1].name + "= new Array(" + codegen(ast[1].size) + ")";
case 'declfuncvar-node':
return codegen(ast[3]) + codegen(ast[4]) + '\n' + codegen(ast[5]);
case 'declprog-node':
return codegen(ast[2]) + '\n';
case 'intconst-node':
return ast[1].val;
case 'declvar-node':
return ' , ' + codegen(ast[2]) + codegen(ast[3]);
case 'notype-id-node':
return ast[1].name;
case 'notype-vector-node':
return ast[1].name + "= new Array(" + codegen(ast[1].size) + ")";
case 'declfunc-node':
return '=function(' + codegen(ast[2]) + ')' + codegen(ast[3]);
case 'listaparametros-node':
return codegen(ast[2]);
case 'listaparametroscont-node':
return codegen(ast[2]) + ' , ' + codegen(ast[3]);
case 'nosize-vector-node':
return ast[1].name;
case 'bloco-listacomando-node':
return '{\n' + codegen(ast[2]) + '\n' + codegen(ast[3]) + '\n}\n';
case 'bloco-node':
return '{\n' + codegen(ast[2]) + '\n}\n';
case 'listadeclvar-node':
return codegen(ast[2]) + ' ' + codegen(ast[3]) + ';\n' + codegen(ast[4]);
case 'comando-node':
return codegen(ast[2]) + ';\n';
case 'listacomando-node':
return codegen(ast[2]) + ' ' + codegen(ast[3]);
case 'intconst-node':
return ast[1].val;
case 'carconst-node':
return ast[1].val;
case 'comando-vazio-node':
return ';';
case 'comando-expr-node':
return codegen(ast[2]) + ';\n';
case 'comando-retorne-node':
return 'return ' + codegen(ast[2]);
case 'comando-leia-node':
return codegen(ast[2]) + '=parseInt(prompt());\n';
case 'comando-escreva-node':
return 'console.log(' + codegen(ast[2]) + ');\n';
case 'comando-novalinha-node':
return ' ';
case 'comando-seentao-node':
return 'if(' + codegen(ast[2]) + '){\n' + codegen(ast[3]) + '}\n';
case 'comando-seentaosenao':
return 'if(' + codegen(ast[2]) + '){\n' + codegen(ast[3]) + '}\nelse{ ' + codegen(ast[4]) + '}\n';
case 'comando-enquanto-node':
return 'while(' + codegen(ast[2]) + ')' + codegen(ast[3]) + '\n';
case 'assignexpr-node':
return codegen(ast[2]) + ' = ' + codegen(ast[3]);
case 'assignexpr-vector-node':
return codegen(ast[2]) + '[' + codegen(ast[3]) + '] = ' + codegen(ast[4]);
case 'or-node':
return codegen(ast[2]) + ' || ' + codegen(ast[3]);
case 'and-node':
return codegen(ast[2]) + ' && ' + codegen(ast[3]);
case 'equals-node':
return codegen(ast[2]) + ' == ' + codegen(ast[3]);
case 'dif-node':
return codegen(ast[2]) + ' != ' + codegen(ast[3]);
case 'less-node':
return codegen(ast[2]) + ' < ' + codegen(ast[3]);
case 'greater-node':
return codegen(ast[2]) + ' > ' + codegen(ast[3]);
case 'greater-equals-node':
return codegen(ast[2]) + ' >= ' + codegen(ast[3]);
case 'less-equals-node':
return codegen(ast[2]) + ' <= ' + codegen(ast[3]);
case 'plus-node':
return codegen(ast[2]) + ' + ' + codegen(ast[3]);
case 'minus-node':
return codegen(ast[2]) + ' - ' + codegen(ast[3]);
case 'multiplication-node':
return codegen(ast[2]) + ' * ' + codegen(ast[3]);
case 'division-node':
return codegen(ast[2]) + ' / ' + codegen(ast[3]);
case 'modulus-node':
return codegen(ast[2]) + ' % ' + codegen(ast[3]);
case 'negation-node':
return '-' + codegen(ast[2]);
case 'no-node':
return '!' + codegen(ast[2]);
case 'access-vector-node':
return ast[1].name + '[' + codegen(ast[1].position) + ']';
case 'function-node':
return ast[1].name + '()';
case 'function-params-node':
return ast[1].name + '(' + codegen(ast[2]) + ')';
case 'string-node':
return ast[1].text;
case 'listexpr-node':
return codegen(ast[2]) + ' , ' + codegen(ast[3]);
case 'null':
return '';
case 'comando-expr-vector-node':
return codegen(ast[2]);
default:
return 'Unknown statement/expression: ' + ast[0];
}
}
function codegenList(list) {
return list.reduce(function(prev, curr) {
return prev + codegen(curr);
}, '');
}
main();
});