-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.js
246 lines (223 loc) · 5.74 KB
/
parser.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
import fs from 'fs'
let llvm = 'llvm'
let lu = 'lua'
export default function parse(tokens, typex) {
if (tokens.length == 1) {
return [new expression('exp', 'exp', [tokens[0]])]
}
let i = 0
let final = []
while (i < tokens.length) {
// console.log(tokens[i])
if (tokens[i].type == 'WORD' && tokens[i].value == 'import') {
final.push(new importst('import', tokens[i + 1].value, [tokens[i + 1]]))
i += 2
}
else if (tokens[i].type == 'EXPI') {
// console.log('expi')
let j = i + 1
let r = []
while (true) {
if (tokens[j].type == 'EXPI') break;
r.push(tokens[j])
j++
}
i = j + 1
// console.log('eeee',r)
final.push(new expression('exp', 'exp', r))
} else
if (tokens[i].value == 'fun' && tokens[i].type !== 'STRING') {
let params = []
let j
if(typex == 'llvm'){ j = i+4 } else {j = i+3}
let b = false
while (true) {
if (tokens[j].type == 'C-PAREN') break;
if (tokens[j].type == 'COMMA') { j++; continue; }
if(typex == llvm) {
params.push({ ...tokens[j + 1], rtype: tokens[j] })
j += 2
continue
} else {
params.push(tokens[j])
j++
}
}
// console.log(params)
let body = []
b = false
j += 2
while (b == false) {
// console.log(tokens[j])
if (tokens[j].type == "C-BRACE") break;
body.push(tokens[j])
j++
}
let f;
if(typex == llvm) {
f = new func('fun', tokens[i + 2], { body: parse(body), params, rtype: tokens[i + 1] })
}else {
f = new func('fun', tokens[i + 1], { body: parse(body), params})
}
// console.log(f)
final.push(f)
i = j
} else if (tokens[i].type == 'WORD' && tokens[i + 1] && tokens[i + 1].type == 'O-PAREN') {
let data = []
let name = tokens[i]
let j = i + 2
while (true) {
if (tokens[j].type == 'C-PAREN') break;
// if (tokens[j].type == 'COMMA') { j++; continue; }
if(typex == llvm) {
data.push({ ...tokens[j + 1], rtype: tokens[j] })
j += 2
}else {
data.push({ ...tokens[j]})
j += 1
}
// data.push(tokens[j])
}
// console.log(data)
i = j + 1
let f = new funcCALL('funcCALL', name,(data))
// console.log(f)
final.push(f)
}
else if (tokens[i].value == 'let' && tokens[i].type !== 'STRING') {
let n = tokens[i + 1]
let v = []
let j = i + 3
while (true) {
if (tokens[j].type == 'SEMI') break
v.push(tokens[j])
j++
}
let varib = new dec('dec', n, parse(v))
final.push(varib)
i = (j)
}
else if (tokens[i + 1] && tokens[i + 1].type == 'DECLARATION') {
let n = tokens[i]
let v = []
let j = i + 2
while (true) {
if (tokens[j].type == 'SEMI') break
v.push(tokens[j])
j++
}
final.push(new redec('redec', n, parse(v)))
i = j
} else if (tokens[i].type == 'EOF') {
break
i++
} else if (tokens[i].type == "WORD" && tokens[i].value == 'LUA') {
// console.log('lua')
// console.log(tokens[i+2])
final.push(new lua('luaExp', 'luaExp', tokens[i + 2]))
i += 4
}
else if (tokens[i].type == "WORD" && tokens[i].value == 'IR') {
// console.log('lua')
// console.log(tokens[i+2])
final.push(new lua('IRExp', 'IRExp', tokens[i + 2]))
i += 4
}
// else if(tokens[i].type == 'WORD' && tokens[i].value == 'return') {
// final.push(new returnst('returnst', tokens[i+1], []))
// i+=3
// }
else {
// console.log('e')
let v = []
let j = 0
for (var d = i; d < tokens.length; d++) {
if (tokens[d].type == 'SEMI' || tokens[d].type == 'EXPI') break;
// console.log(tokens[d])
v.push(tokens[d])
j++
}
final.push(new expression('exp', 'exp', (v)))
i += j
}
i++
}
return final
}
class type {
constructor(type, value, data) {
this.type = type
this.value = value
this.data = data
}
classify() {
return 'nullset'
}
}
class funcCALL extends type {
constructor(type, value, data) {
super(type, value, data)
}
}
class importst extends type {
constructor(type, value, data) {
super(type, value, data)
}
}
class literal extends type {
constructor(type, value, data) {
super(type, value, data)
this.literal = data.literal
}
}
class func extends type {
constructor(type, value, data) {
super(type, value, data)
this.name = value
this.body = data.body
this.params = data.params
}
}
class returnst extends type {
constructor(type, value, data) {
super(type, value, data)
}
}
class dec extends type {
constructor(type, value, data) {
super(type, value, data)
this.type = type
this.data = data
this.value = value
}
}
class redec extends type {
constructor(type, value, data) {
super(type, value, data)
this.type = type
this.data = data
this.value = value
}
}
class expression extends type {
constructor(type, value, data) {
super(type, value, data)
this.type = type
this.value = value
this.data = data
}
}
class lua extends type {
constructor(type, value, data) {
super(type, value, data)
this.type = type
this.value = value
this.data = data
}
}
class command extends type {
}
class operation extends type {
}
class JSExp extends type {
}