-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfunction.js
462 lines (400 loc) · 11.6 KB
/
function.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
// # Copyright (c) 2014-2021 Feudal Code Limitada - MIT license #
"use strict"
import { eatExpression } from "./expression.js"
import { err, unexpected } from "./helper.js"
import { registerAnonymousFunction, registerInnerFunction, registerLocalVariable, registerParameter, markUsed, endGlobalFunction } from "./register.js"
import { see, eat, eatValue, eatKind, eatName, eatConstOrLet, seeEndOfBlockOrEndOfLine, seeEndOfBlockOrTrueEndOfLine } from "./scan.js"
var rat
var blocks = [ ]
var lastClosedBlock = "" // keeps obsolet value when function ends!!
var currentBranch = ""
var closedBranches = [ ] // MUST BE RESET at start of new function
export function init(r) {
rat = r
}
export function eatGlobalFunction() {
// name already registered in parser.js
closedBranches = [ ] // erasing old values
eatFunctionCore()
endGlobalFunction()
}
export function eatAnonymousFunction() {
registerAnonymousFunction() // no token needed
eatFunctionCore()
}
function eatInnerFunction() {
registerInnerFunction(eatName())
eatFunctionCore()
}
export function getCurrentBranch() {
return currentBranch
}
///////////////////////////////////////////////////////////////////////////////
function eatFunctionCore() { // recursive code!
openBranch()
eatFunctionParams()
eatFunctionBody()
}
function eatFunctionParams() {
eatValue("(")
if (see().value == ")") { eat(); return }
while (true) {
let token = eatName()
registerParameter(token)
token = eat()
if (token.value == ")") { return }
if (token.value != ",") { unexpected(token) }
}
}
// branches ///////////////////////////////////////////////////////////////////
function openBranch() {
currentBranch = valueForNewBranch()
}
function valueForNewBranch() {
if (currentBranch == "") { return "1" }
//
let n = 0
while (true) {
n += 1
const candidate = currentBranch + "." + n
if (closedBranches.indexOf(candidate) == -1) { return candidate }
}
}
function closeBranch() {
closedBranches.push(currentBranch)
//
const parts = currentBranch.split(".")
parts.pop()
currentBranch = parts.join(".")
}
// body main loop /////////////////////////////////////////////////////////////
function eatFunctionBody() {
eatOpenBlock("body")
//
while (true) {
const token = see()
const kind = token.kind
const value = token.value
if (value == "}") {
eatCloseBlock()
// check must be on "body" closed
// because this function is recursive!!!
if (lastClosedBlock == "body") { return }
}
else if (value == "if") {
eatIf()
}
else if (value == "else") {
eatElse()
}
else if (value == "while") {
eatWhile()
}
else if (value == "break") {
eatBreak()
}
else if (value == "continue") {
eatContinue()
}
else if (value == "for") {
eatFor()
}
else if (value == "return") {
eatReturn()
}
else if (value == "const") {
eatConstOrLetStatement(true)
}
else if (value == "let") {
eatConstOrLetStatement(false)
}
else if (value == "this") {
eatCallOrAssign()
}
else if (value == "delete") {
eatDelete()
}
else if (value == "throw") {
eatThrow()
}
else if (value == "try") {
eatTry()
}
else if (value == "catch") {
// using forceEatCatch instead of eatCatch
misplacedCatch()
}
else if (value == "finally") {
eatFinally()
}
else if (kind == "name") {
eatCallOrAssign()
}
else if (kind == ";") {
eat()
}
else if (kind == "end-of-line") {
eat()
}
else if (value == "function") {
eat()
eatInnerFunction()
}
else {
unexpected(token)
}
}
}
function eatOpenBlock(val) {
blocks.push(val)
eatValue("{")
}
function eatCloseBlock() {
closeBranch()
eat()
lastClosedBlock = blocks.pop()
if (lastClosedBlock == "try") { forceEatCatch() }
}
///////////////////////////////////////////////////////////////////////////////
function eatReturn() {
eat()
const kind = see().kind
if (kind == "}" || kind == ";" || kind == "end-of-line") { return }
eatExpression()
seeEndOfBlockOrTrueEndOfLine()
}
///////////////////////////////////////////////////////////////////////////////
function eatDelete() {
eat()
eatExpression()
seeEndOfBlockOrEndOfLine()
}
///////////////////////////////////////////////////////////////////////////////
function eatThrow() {
eat()
eatValue("new")
eatValue("Error")
eatValue("(")
eatExpression()
eatValue(")")
seeEndOfBlockOrEndOfLine()
}
///////////////////////////////////////////////////////////////////////////////
function eatConstOrLetStatement(isConstant) {
eat() // const, let or comma
let token = eatName()
registerLocalVariable(token, isConstant)
token = see()
if (token.value == ",") { eatConstOrLetStatement(isConstant) }
if (token.value == "=") {
eat()
eatExpression()
}
seeEndOfBlockOrEndOfLine()
}
///////////////////////////////////////////////////////////////////////////////
function eatIf() {
eat()
eatValue("(")
eatExpression()
eatValue(")")
eatOpenBlock("if")
openBranch()
}
function eatElse() {
const token = eat()
if (lastClosedBlock != "if") { err(token, "'else' does not match 'if'") }
if (see().value == "if") { eatIf(); return }
eatOpenBlock("else")
openBranch()
}
///////////////////////////////////////////////////////////////////////////////
function eatTry() {
const token = eat()
const tcf = blockTryCatchOrFinally()
if (tcf != "") { err(token, "'try' inside block '" + tcf + "'") }
eatOpenBlock("try")
openBranch()
}
function forceEatCatch() {
if (see().kind == "end-of-line") { eat() }
eatValue("catch")
eatValue("(")
const token = eatName()
registerLocalVariable(token)
eatValue(")")
eatOpenBlock("catch")
openBranch()
}
function misplacedCatch() {
const token = eat()
const tcf = blockTryCatchOrFinally()
if (tcf != "") { err(token, "'catch' inside block '" + tcf + "'") }
if (lastClosedBlock != "catch") { err(token, "'catch' not after block 'try'") }
}
function eatFinally() {
const token = eat()
const tcf = blockTryCatchOrFinally()
if (tcf != "") { err(token, "'finally' inside block '" + tcf + "'") }
if (lastClosedBlock != "catch") { err(token, "'finally' not after block 'catch'") }
eatOpenBlock("finally")
openBranch()
}
///////////////////////////////////////////////////////////////////////////////
function eatWhile() {
eat()
eatValue("(")
eatExpression()
eatValue(")")
eatOpenBlock("while")
openBranch()
}
function eatBreak() {
const token = eat()
if (! isInLoop()) { err(token, "'break' not inside loop") }
seeEndOfBlockOrTrueEndOfLine()
}
function eatContinue() {
const token = eat()
if (! isInLoop()) { err(token, "'continue' not inside loop") }
seeEndOfBlockOrTrueEndOfLine()
}
///////////////////////////////////////////////////////////////////////////////
function eatFor() {
openBranch()
eat()
eatValue("(")
const token = eatConstOrLet() // const or let
//
const token2 = eat() // [ or name
if (token2.value == "[") { eatForKeyValue(token); return }
if (token2.kind != "name") { unexpected(token2) }
//
const token3 = see() // symbol
if (token3.value == "=") { eatForCounter(token, token2); return }
if (token3.value == "of") { eatForOf(token, token2); return }
if (token3.value == "in") { eatForOf(token, token2); return } // 'for in' == 'for of'
unexpected(token3)
}
function eatForOf(token, token2) {
if (token.value != "const") { err(token, "this kind of loop wants 'const'") }
//
registerLocalVariable(token2, true)
eat() // of or in
eatExpression()
eatValue(")")
eatOpenBlock("for")
}
function eatForKeyValue(token) {
if (token.value != "const") { err(token, "this kind of loop wants 'const'") }
//
const token2 = eatName()
registerLocalVariable(token2, true)
//
eatValue(",")
//
const token3 = eatName()
registerLocalVariable(token3)
//
eatValue("]")
eatValue("of")
eatExpression()
eatValue(")")
eatOpenBlock("for")
}
function eatForCounter(token, token2) {
// accepts only the complete traditional style //
if (token.value != "let") { err(token, "this kind of loop wants 'let'") }
//
registerLocalVariable(token2)
markUsed(token2)
const name = token2.value
eat() // =
eatExpression()
eatValue(";")
//
eatValue(name)
eatKind("<")
eatExpression()
eatValue(";")
//
eatValue(name)
const token3 = eat()
if (token3.kind == "=") {
eatExpression()
}
else if (token3.kind != "++") {
unexpected(token3)
}
eatValue(")")
eatOpenBlock("for")
}
///////////////////////////////////////////////////////////////////////////////
function eatCallOrAssign() {
const token = eat() // name
markUsed(token)
const assigns = eatCallOrAssign2()
if (assigns) { token.wasAssigned = true }
}
function eatCallOrAssign2() {
const token = eat()
if (token.value == ".") {
eatName()
eatCallOrAssign2()
return false
}
if (token.value == "[") {
eatExpression()
if (see().kind == "end-of-line") { eat() }
eatValue("]")
eatCallOrAssign2()
return false
}
if (token.value == "(") {
eatArguments()
eatValue(")")
const kind = see().kind
if (kind == "}") { return }
if (kind == ";") { return }
if (kind == "end-of-line") { return }
eatCallOrAssign2()
return false
}
if (token.kind == "=") {
eatExpression()
seeEndOfBlockOrEndOfLine()
return true
}
unexpected(token)
}
///////////////////////////////////////////////////////////////////////////////
function isInLoop() {
if (blocks.indexOf("while") != -1) { return true }
if (blocks.indexOf("for") != -1) { return true }
return false
}
function blockTryCatchOrFinally() {
if (blocks.indexOf("try") != -1) { return "try" }
if (blocks.indexOf("catch") != -1) { return "catch" }
if (blocks.indexOf("finally") != -1) { return "finally" }
return ""
}
///////////////////////////////////////////////////////////////////////////////
function eatArguments() {
if (see().value == ")") { return }
eatArgument()
}
function eatArgument() {
eatExpression()
const kind = see().kind
if (kind == ")") { return }
if (kind == "end-of-line") {
eat()
if (see().kind == ")") { return }
unexpected(eat())
}
const token = eat()
if (token.value != ",") { unexpected(token) }
eatArgument()
}
//