-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcclib.lix
768 lines (677 loc) · 16.5 KB
/
cclib.lix
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
#!/usr/local/bin/node ./lix.js
join := [arr]->{ arr .join "" }
map := [arr cb]->{
ret := []
arr foreach [i index]->{
ret .push (i cb index)
}
ret
}
counterGenerator := []->{
counter := 0
[]->{
ret := counter
counter = counter + 1
ret
}
}
getCount := counterGenerator call
getUniqVarName := []->{ '_' + (getCount call) }
generateFuncArgs := [args env ctx]->{
args map [i]->{
varname := i.0 transformVarName
env.(varname) = true
varname
} | .join ', '
}
ctx0 := [x]->{x}
# withSeqLayout := [statements ctx]->{
# funcName := getUniqVarName call
# suffix := [v]->{
# v + '()'
# }
# body := statements map [stmtClosure index]->{
# stmt := stmtClosure call suffix
# if (index == (statements.length - 1)) {
# stmt = 'return ' + stmt
# }
# stmt
# }
# }
generateSeq := [expr env ctx def]->{
funcName := getUniqVarName call
getCount := counterGenerator call
chainDefer := [
'function () {\n'
'thisDefer(defer_stack, true)(function (' ') {'
seqFuncParamsName.defer ' && ' seqFuncParamsName.defer '();'
'}, 0);\n'
seqFuncParamsName.defer ' && ' seqFuncParamsName.defer '();\n'
'}\n' ] join
ctx := [current]->{
[v loop]->{
next := current
if (loop == undefined) {
next = next + 1
}
[ v '(function (' seqFuncParamsName.ret ') {\n'
'return ' funcName '(' seqFuncParamsName.cb ', '
next ', true, ' seqFuncParamsName.ret ', ' chainDefer ');\n'
'}, 0, false, undefined, ' chainDefer ')'
# '}, 0)'
] join
}
}
body := expr.0 map [item index]->{
current := (getCount call)
stmt := item generate env (current ctx) def
stmt := ['case ' current ':\n' stmt] join
} | .join ";\n"
[
'function ' funcName '('
seqFuncParamsName.cb ', ' seqFuncParamsName.step ', '
seqFuncParamsName.cont ', ' seqFuncParamsName.ret ', '
seqFuncParamsName.defer ') {\n'
'try {\n'
'switch (' seqFuncParamsName.step ') {\n'
body
'\n'
'default:\n'
'}\n'
'thisDefer(defer_stack, false)(function () {\n'
seqFuncParamsName.cb '(' seqFuncParamsName.ret ');\n'
'}, 0);\n'
'if (' seqFuncParamsName.cont ') {\n'
seqFuncParamsName.cb '(' seqFuncParamsName.ret ');\n'
'} else {\n'
'return ' seqFuncParamsName.ret ';\n'
'}\n'
'} catch (e) {\n'
'if (e == ' builtinNames.ccException ') {\n'
'throw e;\n'
'} else {\n'
'Lraise(e)(function (x) {return x;}, 0);\n'
'}\n'
'}\n'
'}\n'
] | join | def.appendExpr
funcName
}
generateOr := [expr env ctx def]->{
['(' (expr.0 generate env ctx def) " || " (expr.2 generate env ctx def) ')'] | join
}
generateAnd := [expr env ctx def]->{
['(' (expr.0 generate env ctx def) " && " (expr.2 generate env ctx def) ')'] | join
}
generateIf := [expr env ctx def]->{
expr.0 map [i]->{
if (i.1 == 'else') {
['{\n' (i.0 generate env ctx def | ctx) '\n}'] join
} (i.1 == 'if') {
(i generateIf env ctx def)
} else {
[ 'if (' (i.0 generate env ctx def) ') {\n'
(i.1 generate env ctx def | ctx)
'\n}'
] join
}
} | .join ' else '
}
operateFuncName := {
.("+") '__add__'
.("-") '__sub__'
.("*") '__mul__'
.("/") '__div__'
.("%") '__mod__'
.(">=") '__ge__'
.(">") '__gt__'
.("==") '__eq__'
.("!=") '__ne__'
.("<") '__lt__'
.("<=") '__le__'
}
generateAtomic := [expr env ctx def]->{
ret := expr.0
if (expr.2 == '{var}') {
if (operateFuncName.(expr.0)) {
ret = operateFuncName.(expr.0) transformVarName
} (expr.3 == undefined) {
ret = expr.0 transformVarName
}
}
ret
}
generatePropertyName := [expr env ctx def]->{
if (expr.1 == '{atomic}') {
expr generateAtomic env ctx def
} (expr.1 == '{index}') {
expr.0 generateAtomic env ctx def
}
}
generateProperty := [expr env ctx def]->{
propertyName := expr.0 generatePropertyName env ctx0 def
propertyValue := expr.2 generate env ctx def
[propertyName ": " propertyValue] | join
}
generateObjectLiteral := [expr env ctx def]->{
objectBody := expr.0 map [i]->{
(i generateProperty env ctx def)
} | .join ",\n"
["{\n" objectBody "\n}"] | join
}
transformVarName := [name]->{
if (name == 'require') {
name = '_require(require)'
} (name == 'defer') {
name = '_defer(defer_stack)'
} ((name != 'exports') | and (name != 'undefined') |
and (name != 'null') | and (name != 'true') | and (name != 'false')) {
name = 'L' + name
}
name
}
getVarName := [expr]->{
if (expr.1 == '{atomic}') {
expr generateAtomic {} ctx0
} (expr.1 == '{.}') {
expr.0 getVarName
}
}
generateField := [expr env ctx def]->{
if (expr.1 == '{atomic}') {
'.' + (expr generateAtomic env ctx0 def)
} (expr.1 == '{index}') {
['[' (expr.0 generate env ctx def) ']'] join
}
}
generateFieldAccess := [expr env ctx def]->{
if (expr.1 == '{atomic}') {
expr generateAtomic env ctx0 def
} (expr.1 == '{.}') {
obj := (expr.0 generateFieldAccess env ctx def)
field := (expr.2 generateField env ctx def)
obj + field
}
}
generateArray := [expr env ctx def]->{
arrayBody := expr.0 map [i]->{
(i generate env ctx def)
} | .join ', '
['[' arrayBody ']'] | join
}
generateMethod := [expr env ctx]->{
localVarName := getUniqVarName call
localValueName := getUniqVarName call
methodName := (expr.0 generateField env ctx0)
method := localVarName + methodName
tmpVar := (getUniqVarName call)
["(function (" localVarName ", " localValueName ") {\n"
"if (typeof " method " === 'function') {\n"
"arguments = Array.prototype.slice.call(arguments, 0, arguments.length);\n"
"return " method ".apply(" localVarName ", arguments);\n"
"} else if (" method " !== undefined) {\n"
"if (" localValueName " === undefined) {\n"
"return function (_ret) {\n"
"return " method ';\n'
"}\n"
"}\n"
"return function (_ret) {\n"
method " = " localValueName ";\n"
"}\n"
"}"
"})"] join
}
generateWhile := [expr env ctx def]->{
['while (' (expr.0 generate env ctx def) ') {\n'
(expr.2 generate env ctx def | ctx true)
"\n}" ] join
}
generateBreak := [expr env ctx]->{
'break'
}
generateEmpty := [expr env ctx def]->{
""
}
generateDef := [expr env ctx def]->{
varname := expr.0 getVarName
varname def.defineVar
ret := [varname ' = ' (expr.2 generate env ctx def)] | join
env.(varname) = true
ret
}
generateAssign := [expr env ctx def]->{
varname := expr.0 getVarName
if (env.(varname) == undefined | and (varname != seqFuncParamsName.ret)) {
varname console.log
"this var is undefined" console.log
}
varname := (expr.0 generate env ctx0 def)
value := (expr.2 generate env ctx def)
# [seqFuncParamsName.ret " = " value] | join | def.appendExpr
[varname " = " value] | join
}
generateFunc := [expr env ctx def]->{
funcEnv := env env_new
args := expr.0 generateFuncArgs funcEnv ctx0
tmpVar := (getUniqVarName call)
def := GenerateDefNew call
body := expr.2 generate funcEnv ctx0 def
['(function (' args ') {\n'
'var ' ('arguments' transformVarName) ' = arguments, defer_stack = [];\n'
(def.defineVar call) ";\n"
(def.appendExpr call) ";\n"
'return ' body ";\n"
'})'] .join ''
}
generateExpr := [expr env ctx def]->{
func := (expr.1 generate env ctx0 def)
args := (expr.0 generate env ctx0 def)
expr foreach [item index]->{
if (index > 1) {
args = [args ', ' (item generate env ctx0 def)] join
}
}
[func '(' args ')'] | join | ctx
}
GenerateDefNew := []->{
vars := {}
funcs := []
{
.defineVar [v]->{
if (v) {
vars.(v) = v
} else {
vecs := vars map [i]->{i}
if (vecs.length > 0) {
'var ' + (vecs .join ', ')
} else {
''
}
}
}
.appendExpr [v]->{
if (v) {
funcs .push v
} else {
funcs .join ';\n'
}
}
}
}
generateStart := [expr env ctx def]->{
def := GenerateDefNew call
body := expr.0 generate env ctx def
body := [
'module.exports = ' body '\n'
] join
[
'var defer_stack = [];\n'
'function ' seqFuncParamsName.chainDefer '() {\n'
'thisDefer(defer_stack)(function(){return;}, 0);\n'
'}\n'
(def.defineVar call) ";\n"
(def.appendExpr call) ";\n"
body
] join
}
generateMono := [expr env ctx def]->{
expr.0 generate env ctx def
}
generatePathItem := [expr env ctx def]->{
['"' expr.0 '"'] | join
}
generatePathArgItem := [expr env ctx def]->{
['("' expr.2 '" + ' (expr.0 generate env ctx def) ')'] | join
}
generatePath := [expr env ctx def]->{
path := expr.0 map [i]->{
i generate env ctx def
} | .join ','
['[' path '].join("/")'] | join
}
generateModule := [expr env ctx def]->{
name := [(['"' expr.0 '"'] .join '') '{atomic}']
moduleExpr := [name ['module' '{atomic}' '{var}']]
}
generateOpTable := {
.('{start}') generateStart
.('{seq}') generateSeq
.('{mono}') generateMono
.('{atomic}') generateAtomic
.('{.}') generateFieldAccess
.('{module}') generateModule
.('{func}') generateFunc
.('{array}') generateArray
.('{object}') generateObjectLiteral
.('{method}') generateMethod
.('{empty}') generateEmpty
.('{path}') generatePath
.('{path_item}') generatePathItem
.('{path_arg_item}') generatePathArgItem
.(':=') generateDef
.('=') generateAssign
.('if') generateIf
.('and') generateAnd
.('or') generateOr
.('while') generateWhile
.('break') generateBreak
}
generateOp := [hint]->{
if (generateOpTable.(hint)) {
generateOpTable.(hint)
} else {
generateExpr
}
}
generate := [expr env ctx def]->{
expr (expr.1 generateOp) env ctx def
}
env0 := {
.exports null
.__builtin__ "require('/lib/lix/__builtin__.js');"
}
env_new := [env]->{
env = env or env0
Env := []->{}
Env.prototype = env
Env _instance_
}
flattenExpr := [expr define]->{
expr map [item]->{
item flatten define
} | define
}
flattenBreak := [expr define]->{
expr
}
flattenWhile := [expr define]->{
whileDefine := statementFlattenGenerator call
expr.0 = expr.0 flatten whileDefine
expr.2 = expr.2 flatten define0
expr.(2).(0) = expr.(2).(0) .concat (whileDefine call)
whileDefine call | foreach [i]->{
i define true
}
expr define true
['undefined' '{atomic}']
}
flattenOr := [expr define]->{
ret := expr.0 flatten define
if (ret.2 != 'var') {
ret := ret define
}
getAndOpSeq := []->{
AndDefine := statementFlattenGenerator call
seqRet := expr.2 flatten AndDefine
seq := (AndDefine call)
seq .push [ret '=' seqRet]
[seq '{seq}']
}
[[[ret [[] '{empty}']] [(getAndOpSeq call) 'else']] 'if'] define true
ret
}
flattenAnd := [expr define]->{
ret := expr.0 flatten define
if (ret.2 != 'var') {
ret := ret define
}
getAndOpSeq := []->{
AndDefine := statementFlattenGenerator call
seqRet := expr.2 flatten AndDefine
seq := (AndDefine call)
seq .push [ret '=' seqRet]
[seq '{seq}']
}
[[[ret (getAndOpSeq call)]] 'if'] define true
ret
}
_flattenIf := [expr define tmpVar ctx]->{
expr.0 foreach [i]->{
if (i.1 == 'else') {
elseSeq := i.0 flatten define0 ctx
[[[tmpVar elseSeq]] 'if'] define true
} (i.1 == 'if') {
i _flattenIf define tmpVar ctx | define
} else {
ifSeq := i.1 flatten define0 ctx
ifSeq.0 .unshift [tmpVar '=' ['false' '{atomic}']]
cond := i.0 flatten define
cond := [tmpVar 'and' cond]
[[[cond ifSeq]] 'if'] define true
}
}
}
flattenIf := [expr define]->{
tmpVar := ['true' '{atomic}'] define
retVar := ['null' '{atomic}'] define
ctx := [i]->{
[retVar '=' i]
}
expr _flattenIf define tmpVar ctx
retVar
}
flattenAssign := [expr define]->{
expr.2 = expr.2 flatten define
expr
}
flattenBasicOp := [expr builtinOpName define]->{
expr.1 = [builtinOpName '{atomic}' '{var}']
if (expr.length > 3) {
partialExpr := [expr.2 ['__' '{atomic}' '{var}']]
i := 3
while (i < expr.length) {
partialExpr .push expr.(i)
i = i + 1
}
expr = [expr.0 expr.1 partialExpr]
}
expr flattenExpr define
}
flattenBind := [expr define]->{
expr flattenBasicOp '__bind__' define
}
flattenCompose := [expr define]->{
expr flattenBasicOp '__compose__' define
}
flattenReverseCompose := [expr define]->{
expr flattenBasicOp '__rcompose__' define
}
flattenReverseCPSCompose := [expr define]->{
expr flattenBasicOp '__rvcompose__' define
}
flattenCPSCompose := [expr define]->{
expr flattenBasicOp '__vcompose__' define
}
flattenDef := flattenAssign
flattenPathArgItem := [expr define]->{
expr.0 = expr.0 flatten define
expr
}
flattenPathItem := [expr define]->{
expr
}
flattenPath := [expr define]->{
expr.0 = expr.0 map [i]->{
i flatten define
}
expr
}
flattenEmpty := [expr define]->{
expr
}
flattenField := [expr define]->{
if (expr.1 == '{index}') {
expr.0 = expr.0 flatten define
}
expr
}
flattenMethod := [expr define]->{
expr.0 = expr.0 flattenField define
expr
}
flattenProperty := [expr define]->{
expr.2 = expr.2 flatten define
expr
}
flattenObjectLiteral := [expr define]->{
expr.0 = expr.0 map [i]->{
i flattenProperty define
}
expr
}
flattenArray := [expr define]->{
expr.0 = expr.0 map [i]->{
i flatten define
}
expr
}
flattenFunc := [expr define]->{
expr.2 = expr.2 flatten define
expr
}
flattenFieldAccess := [expr define]->{
if (expr.1 == '{.}') {
expr.0 = expr.0 flattenFieldAccess define
expr.2 = expr.2 flattenField define
} else {
expr = expr flatten define
}
expr
}
flattenAtomic := [expr define]->{
expr
}
flattenMono := [expr define]->{
expr.0 flatten define
}
statementFlattenGenerator := []->{
data := []
[statement tmpVarName]->{
if (statement == undefined) {
data
} (tmpVarName == true) {
data .push statement
} (tmpVarName == undefined) {
varName := getUniqVarName call
lixVar := [varName '{atomic}' '{var}' 'tmp']
tmpRet := [seqFuncParamsName.ret '{atomic}' '{var}' 'aux']
data .push [tmpRet ':=' statement]
data .push [lixVar ':=' tmpRet]
lixVar
}
}
}
flattenSeq := [expr define seqCtx]->{
ret := []
ctx := [stmt index]->{
ret := stmt
if (seqCtx and ((index parseInt) == (expr.(0).length - 1))) {
ret := stmt seqCtx
}
ret
}
expr.0 foreach [item index]->{
exprDefine := statementFlattenGenerator call
itemRet := item flatten exprDefine
ret = ret .concat (exprDefine call)
stmtRet := itemRet ctx index
isTmpVar := [expr]->{
(expr.(1) == '{atomic}') and (expr.(3) != undefined)
}
isRealVar := [expr]->{
(expr.(1) == '{atomic}') and (expr.(3) == undefined)
}
isRealExpr := [expr]->{
(expr isRealVar) or ((expr.(1) != 'if') and (expr.1 != 'while'))
}
if ((stmtRet.(1) != '{empty}') and (stmtRet isTmpVar | not)) {
if (stmtRet isRealExpr) {
stmtRet = [[seqFuncParamsName.ret '{atomic}' '{var}' 'aux'] '=' stmtRet]
}
ret .push stmtRet
}
}
expr.0 = ret
expr
}
flattenStart := [expr define]->{
ret := expr.0 flatten define
[ret '{start}']
}
flattenModule := [expr define]->{
if (expr.2 == '{var}') {
name := [(['"' expr.0 '"'] .join '') '{atomic}']
} else if (expr.2 == '{index}') {
name := expr.0 flatten define
}
jsName := [name ['__add__' '{atomic}' '{var}'] ['"-lix"' '{atomic}']] define
moduleExpr := [jsName ['require' '{atomic}' '{var}']]
moduleExpr define
}
flattenDefer := [expr define]->{
['undefined' '{atomic}']
}
flattenOpTable := {
.('{start}') flattenStart
.('{seq}') flattenSeq
.('{mono}') flattenMono
.('{atomic}') flattenAtomic
.('{.}') flattenFieldAccess
.('{module}') flattenModule
.('{func}') flattenFunc
.('{array}') flattenArray
.('{object}') flattenObjectLiteral
.('{method}') flattenMethod
.('{empty}') flattenEmpty
.('{path}') flattenPath
.('{path_item}') flattenPathItem
.('{path_arg_item}') flattenPathArgItem
.(':=') flattenDef
.('=') flattenAssign
.('>>=') flattenBind
.('>>>') flattenCPSCompose
.('<<<') flattenReverseCPSCompose
.('>>') flattenCompose
.('<<') flattenReverseCompose
.('if') flattenIf
.('and') flattenAnd
.('or') flattenOr
.('while') flattenWhile
.('break') flattenBreak
.('defer') flattenDefer
}
flattenOp := [hint]->{
if (flattenOpTable.(hint)) {
flattenOpTable.(hint)
} else {
flattenExpr
}
}
flatten := [expr define ctx]->{
expr (expr.1 flattenOp) define ctx
}
define0 := [i]->{i}
def0 := {
.defineVar [i]->{}
.appendExpr [i]->{}
}
seqFuncParamsName := {
.cb '_cb'
.step '_step'
.cont '_cont'
.ret '_ret'
.defer 'cb_defer'
.thisDefer 'this_defer'
.chainDefer 'chain_defer'
}
builtinNames := {
.ccException '_lixCCException'
}
compile := [expr]->{
libs := env0 | map [i]->{i} | .join "\n"
flattenedExpr := expr | flatten [i]->{} [i]->{i}
code := flattenedExpr generateStart (env_new call) ctx0 def0
[libs "\n" code] join
}
exports.compile = compile
compile