-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcc.js
269 lines (198 loc) · 5.51 KB
/
cc.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
258
259
260
261
262
263
264
265
266
267
268
269
import {Lexer} from "./clex.js"
import {Parser} from './cpars.js'
import { CodeGenerator } from "./cgen.js"
import * as fs from 'fs'
try{
var data = fs.readFileSync(process.argv[2], 'ascii')
}
catch(err){
console.log('Errror reading file')
}
var l = new Lexer(data)
var t = l.next()
var all_toks = []
while (t){
all_toks.push(t)
t = l.next()
}
// -----------------------------------
var parser = new Parser(all_toks)
let res = parser.Parse()
// console.log('Parsing success')
// console.log(res.nodes.length)
// for (var c of res.nodes){
// pp(c)
// }
// // -----------------------------------
let cgen = new CodeGenerator(res)
let asm = cgen.Generate()
console.log(asm)
function pp(obj, idnt = 0){
switch(obj.__proto__.constructor.name){
case 'FunctionNode':
pf(obj, idnt)
break
case 'ReturnNode':
pr(obj, idnt)
break
case 'BinOpNode':
pe(obj, idnt)
break
case 'ConstantNode':
pc(obj, idnt)
break
case 'UnaryNode':
pu(obj, idnt)
break
case 'DeclNode':
pd(obj, idnt)
break
case 'VarNode':
pv(obj, idnt)
break
case 'AssignmentNode':
pa(obj, idnt)
break
case 'CondNode':
// console.log('FFF')
pcond(obj, idnt)
break
case 'CondExpNode':
// console.log('GGG')
pcondexp(obj, idnt)
break
case 'CompoundStatementNode':
console.log(" ".repeat(idnt) + 'COMPOUND STATEMENTS BEGIN')
for (let s of obj.statements)
pp(s, idnt + 1)
console.log(" ".repeat(idnt) + 'COMPOUND END')
break
case 'ForNode':
console.log(" ".repeat(idnt) + 'PRE')
pp(obj.pre_exp, idnt + 1)
console.log(" ".repeat(idnt) + 'TEST')
pp(obj.test_exp, idnt + 1)
console.log(" ".repeat(idnt) + 'POST')
pp(obj.post_exp, idnt + 1)
console.log(" ".repeat(idnt) + 'BOD')
pp(obj.body, idnt + 1)
break
case 'BreakNode':
console.log(" ".repeat(idnt) + 'BREAK')
break
case 'ContNode':
console.log(" ".repeat(idnt) + 'CONTINUE')
break
case 'FunCallNode':
console.log(" ".repeat(idnt) + "Call", obj.name)
console.log(" ".repeat(idnt) + 'Args begin')
for (let c of obj.args)
pp(c, idnt + 1)
console.log(" ".repeat(idnt) + 'Args end')
break
default:
console.log('Error? Can not Print AST? ')
process.exit(1)
}
}
function pcondexp(e, idnt = 0){
console.log(" ".repeat(idnt) + "CondExpNode")
pe(e.cond, idnt + 1)
if (e.if){
console.log(" ".repeat(idnt) + "If Part")
pe(e.if, idnt + 1)
console.log(" ".repeat(idnt) + "else part")
pp(e.else, idnt + 1)
}
}
function pcond(cond_n, idnt = 0){
// console.log("IF STATEMENT FOUND")
console.log(" ".repeat(idnt) + "IF Statement")
pe(cond_n.cond, idnt + 1)
pp(cond_n.if, idnt + 1)
if (cond_n.else){
pp(cond_n.else, idnt + 1)
}
console.log('PCOND DONE')
}
function pu(un, idnt = 0){
// print unary node
console.log(" ".repeat(idnt) + "Unary Node")
console.log(" ".repeat(idnt) + un.op)
pe(un.operand, idnt + 1)
return
}
function pe(en, idnt = 0){
// print expression node
// console.log("DEb", en)
if(en.__proto__.constructor.name == "ConstantNode"){
pc(en, idnt)
return
}
else if(en.__proto__.constructor.name == "UnaryNode"){
pu(en, idnt)
return
}
else if(en.__proto__.constructor.name == "VarNode"){
pv(en, idnt)
return
}
else if(en.__proto__.constructor.name == "AssignmentNode"){
pa(en, idnt)
return
}
else if(en.__proto__.constructor.name == "CondExpNode"){
pcondexp(en, idnt)
return
}
else if(en.__proto__.constructor.name == "FunCallNode"){
pp(en, idnt)
return
}
else if(en.op === undefined){
console.log(" ".repeat(idnt) + 'Empty statement?')
return
}
console.log(" ".repeat(idnt) + en.op)
pe(en.left, idnt + 1)
pe(en.right, idnt + 1)
}
function pr(sn, idnt = 0){
// print statement(return) node
console.log(" ".repeat(idnt) + "Return Node")
// console.log(sn.exp)
pe(sn.exp, idnt + 1)
}
function pf(fn, idnt = 0){
// print function node
console.log(" ".repeat(idnt) + "Function Name:", fn.name)
console.log(" ".repeat(idnt) + "Args: ", fn.args)
for(let s of fn.nodes){
pp(s, idnt + 1)
}
}
function pc(obj, idnt = 0){
// print constant node
console.log(" ".repeat(idnt) + obj.num)
}
function pa(obj, idnt = 0){
// print assignment node
console.log(" ".repeat(idnt) + obj.op)
pv(obj.left, idnt + 1)
pe(obj.right, idnt + 1)
}
function pv(obj, idnt = 0){
// print var node
// console.log('ABOVE')
console.log(" ".repeat(idnt) + obj.id)
console.log(" ".repeat(idnt), obj.type)
}
function pd(obj, idnt = 0){
// print decl node
// console.log(" ".repeat(idnt) + obj.left)
pv(obj.left, idnt)
if (! obj.right)
console.log(" ".repeat(idnt) + "undefined")
else
pe(obj.right, idnt)
}