-
Notifications
You must be signed in to change notification settings - Fork 47
/
expression.ts
333 lines (305 loc) · 10.8 KB
/
expression.ts
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import * as estree from 'estree'
import State from '../state'
import compile from '.'
import { OP } from '../share/const'
import { compileFunc, compileClass, compilePattern } from './helper'
export function ThisExpression(node: estree.ThisExpression, state: State) {
state.opCodes.push({ op: OP.LOADV, val: state.symbols.get('this').pointer, this: true })
}
export function ArrayExpression(node: estree.ArrayExpression, state: State) {
const spread: number[] = []
for (let i = 0; i < node.elements.length; i++) {
const item = node.elements[i]
if (item.type === 'SpreadElement') {
compile(item.argument, state)
spread.push(i)
} else {
compile(item, state)
}
}
spread.push(node.elements.length)
state.opCodes.push({ op: OP.ARR, val: spread })
}
export function ObjectExpression(node: estree.ObjectExpression, state: State) {
const propKinds: string[] = []
for (let i = 0; i < node.properties.length; i++) {
const property = node.properties[i]
if (property.type as any === 'SpreadElement') {
state.opCodes.push({ op: OP.LOADK, val: null }) // placeholder for spread element
compile((property as any).argument, state)
propKinds.push('sprd')
} else {
// key
const propKey = property.key
if (property.computed) {
compile(propKey, state)
} else {
state.opCodes.push({ op: OP.LOADK, val: (propKey as estree.Identifier).name })
}
// value
compile(property.value, state)
propKinds.push(property.kind)
}
}
state.opCodes.push({ op: OP.OBJ, val: propKinds })
}
export function FunctionExpression(node: estree.FunctionExpression, state: State) {
if (node.id && node.id.name) {
state.symbols.pushScope()
// allocate a placeholder for a tmp function to use inside the function scope
const pointer = state.symbols.set(node.id.name, 'const').pointer
state.opCodes.push({ op: OP.LOADK })
state.opCodes.push({ op: OP.ALLOC, val: pointer })
// compile the function
compileFunc(node, state)
// store the function into the placeholder above
state.opCodes.push({ op: OP.COPY })
state.opCodes.push({ op: OP.STORE, val: pointer })
state.symbols.popScope()
} else {
compileFunc(node, state)
}
}
export function UnaryExpression(node: estree.UnaryExpression, state: State) {
if (node.operator === 'typeof' && node.argument.type === 'Identifier') {
try {
state.opCodes.push({ op: OP.LOADV, val: state.symbols.get(node.argument.name).pointer })
} catch (err) {
state.opCodes.push({ op: OP.LOADK })
}
} else {
compile(node.argument, state)
}
state.opCodes.push({ op: OP.UNOP, val: node.operator })
}
export function UpdateExpression(node: estree.UpdateExpression, state: State) {
if (node.argument.type === 'Identifier') {
// load value
const pointer = state.symbols.get(node.argument.name).pointer
state.opCodes.push({ op: OP.LOADV, val: pointer })
// update value
if (!node.prefix) {
state.opCodes.push({ op: OP.COPY })
}
state.opCodes.push({ op: OP.LOADK, val: 1 })
state.opCodes.push({ op: OP.BIOP, val: node.operator[0] })
if (node.prefix) {
state.opCodes.push({ op: OP.COPY })
}
// store value
state.opCodes.push({ op: OP.STORE, val: pointer })
} else if (node.argument.type === 'MemberExpression') {
// load value
compile(node.argument, state)
// update value
if (!node.prefix) {
state.opCodes.push({ op: OP.COPY })
}
state.opCodes.push({ op: OP.LOADK, val: 1 })
state.opCodes.push({ op: OP.BIOP, val: node.operator[0] })
if (node.prefix) {
state.opCodes.push({ op: OP.COPY })
}
// store value
compile(node.argument.object, state)
const property = node.argument.property
if (node.argument.computed) {
compile(property, state)
} else {
state.opCodes.push({ op: OP.LOADK, val: (property as estree.Identifier).name })
}
state.opCodes.push({ op: OP.MSET })
}
}
export function BinaryExpression(node: estree.BinaryExpression, state: State) {
compile(node.left, state)
compile(node.right, state)
state.opCodes.push({ op: OP.BIOP, val: node.operator })
}
export function AssignmentExpression(node: estree.AssignmentExpression, state: State) {
compile(node.right, state)
state.opCodes.push({ op: OP.COPY })
const left = node.left
if (left.type === 'Identifier') {
const symbol = state.symbols.get(left.name)
if (symbol.type === 'const') {
throw new TypeError('Assignment to constant variable')
}
const binaryOp = node.operator.substring(0, node.operator.length - 1)
if (binaryOp) {
state.opCodes.push({ op: OP.LOADV, val: symbol.pointer })
state.opCodes.push({ op: OP.BIOP, val: binaryOp })
}
state.opCodes.push({ op: OP.STORE, val: symbol.pointer })
} else if (left.type === 'MemberExpression') {
compile(left.object, state)
if (left.object.type === 'Super') {
state.opCodes.push({ op: OP.LOADV, val: state.symbols.get('this').pointer })
}
const property = left.property
if (left.computed) {
compile(property, state)
} else {
state.opCodes.push({ op: OP.LOADK, val: (property as estree.Identifier).name })
}
state.opCodes.push({ op: OP.MSET, val: left.object.type === 'Super' })
} else {
compilePattern(left, state)
}
}
export function LogicalExpression(node: estree.LogicalExpression, state: State) {
compile(node.left, state)
compile(node.right, state)
state.opCodes.push({ op: OP.BIOP, val: node.operator })
}
export function MemberExpression(node: estree.MemberExpression, state: State) {
compile(node.object, state)
if (node.object.type === 'Super') {
state.opCodes.push({ op: OP.LOADV, val: state.symbols.get('this').pointer })
}
const property = node.property
if (node.computed) {
compile(property, state)
} else {
state.opCodes.push({ op: OP.LOADK, val: (property as estree.Identifier).name })
}
state.opCodes.push({ op: OP.MGET, val: node.object.type === 'Super' })
}
export function ConditionalExpression(node: estree.ConditionalExpression, state: State) {
compile(node.test, state)
const ifnotCode = { op: OP.IFNOT, val: -1 }
state.opCodes.push(ifnotCode)
// if true
compile(node.consequent, state)
const trueEndCode = { op: OP.JMP, val: -1 }
state.opCodes.push(trueEndCode)
ifnotCode.val = state.opCodes.length
// else
compile(node.alternate, state)
trueEndCode.val = state.opCodes.length
}
export function CallExpression(node: estree.CallExpression, state: State) {
const spread: number[] = []
for (let i = 0; i < node.arguments.length; i++) {
const arg = node.arguments[i]
if (arg.type === 'SpreadElement') {
compile(arg.argument, state)
spread.push(i)
} else {
compile(arg, state)
}
}
spread.push(node.arguments.length)
const callee = node.callee
if (callee.type === 'MemberExpression') {
if (callee.object.type === 'Super') {
state.opCodes.push({ op: OP.LOADV, val: state.symbols.get('this').pointer })
compile(callee.object, state)
} else {
compile(callee.object, state)
state.opCodes.push({ op: OP.COPY })
}
const property = callee.property
if (callee.computed) {
compile(property, state)
} else {
state.opCodes.push({ op: OP.LOADK, val: (property as estree.Identifier).name })
}
state.opCodes.push({ op: OP.MGET })
} else {
state.opCodes.push({ op: OP.LOADV, val: state.symbols.get('this').pointer })
compile(callee, state)
if (callee.type === 'Super') {
state.opCodes.push({ op: OP.LOADK, val: 'constructor' })
state.opCodes.push({ op: OP.MGET })
}
}
const catchPcStack = state.catchPcStack
state.opCodes.push({
op: OP.CALL,
val: spread,
super: callee.type === 'Super',
catch: catchPcStack[catchPcStack.length - 1]
})
}
export function NewExpression(node: estree.NewExpression, state: State) {
const spread: number[] = []
for (let i = 0; i < node.arguments.length; i++) {
const arg = node.arguments[i]
if (arg.type === 'SpreadElement') {
compile(arg.argument, state)
spread.push(i)
} else {
compile(arg, state)
}
}
spread.push(node.arguments.length)
compile(node.callee, state)
const catchPcStack = state.catchPcStack
state.opCodes.push({
op: OP.NEW,
val: spread,
catch: catchPcStack[catchPcStack.length - 1]
})
}
export function MetaProperty(node: estree.MetaProperty, state: State) {
state.opCodes.push({ op: OP.LOADV, val: state.symbols.get('this').pointer })
state.opCodes.push({ op: OP.LOADK, val: '__proto__' })
state.opCodes.push({ op: OP.MGET })
state.opCodes.push({ op: OP.LOADK, val: 'constructor' })
state.opCodes.push({ op: OP.MGET })
}
export function SequenceExpression(node: estree.SequenceExpression, state: State) {
for (let i = 0; i < node.expressions.length; i++) {
compile(node.expressions[i], state)
}
}
export function ArrowFunctionExpression(node: estree.ArrowFunctionExpression, state: State) {
compileFunc(node, state)
}
export function TemplateLiteral(node: estree.TemplateLiteral, state: State) {
const quasis = node.quasis
const expressions = node.expressions
state.opCodes.push({ op: OP.LOADK, val: quasis.shift().value.raw })
let expr: estree.Expression
while (expr = expressions.shift()) {
compile(expr, state)
state.opCodes.push({ op: OP.BIOP, val: '+' })
state.opCodes.push({ op: OP.LOADK, val: quasis.shift().value.raw })
state.opCodes.push({ op: OP.BIOP, val: '+' })
}
}
export function TaggedTemplateExpression(node: estree.TaggedTemplateExpression, state: State) {
}
export function ClassExpression(node: estree.ClassExpression, state: State) {
if (node.id && node.id.name) {
state.symbols.pushScope()
// allocate a placeholder for a tmp class to use inside the constructor function scope
const pointer = state.symbols.set(node.id.name, 'const').pointer
state.opCodes.push({ op: OP.LOADK })
state.opCodes.push({ op: OP.ALLOC, val: pointer })
// compile the class
compileClass(node, state)
// store the class into the placeholder above
state.opCodes.push({ op: OP.COPY })
state.opCodes.push({ op: OP.STORE, val: pointer })
state.symbols.popScope()
} else {
compileClass(node, state)
}
}
export function Super(node: estree.Super, state: State) {
state.opCodes.push({ op: OP.LOADV, val: state.symbols.get('this').pointer })
state.opCodes.push({ op: OP.LOADK, val: '__proto__' })
state.opCodes.push({ op: OP.MGET })
state.opCodes.push({ op: OP.LOADK, val: '__proto__' })
state.opCodes.push({ op: OP.MGET })
}
export function YieldExpression(node: estree.YieldExpression, state: State) {
compile(node.argument, state)
state.opCodes.push({ op: OP.YIELD, val: node.delegate })
}
export function AwaitExpression(node: estree.AwaitExpression, state: State) {
compile(node.argument, state)
state.opCodes.push({ op: OP.AWAIT })
}