-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathchurch_astify.js
722 lines (662 loc) · 22.5 KB
/
church_astify.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
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
var util = require('./util.js');
var church_builtins = require('./church_builtins.js');
var rename_map = require('./js_astify.js').rename_map;
var brackets_map = {"(": ")", "[": "]"};
var query_fns = ["rejection-query", "mh-query", "mh-query-scored", "enumeration-query", "conditional"];
var query_fns_to_num_params = {
"rejection-query": 0,
"enumeration-query": 0,
"conditional": 1,
"mh-query": 2,
"mh-query-scored": 2
}
var query_decls = ["define", "condition", "factor", "condition-equal", "condition-repeat-equal"];
var condition_fns = ["condition", "factor", "condition-equal", "condition-repeat-equal"];
function make_generic_node(head, children) {
return {"head": head, "children": children};
}
function deep_copy(obj) { return JSON.parse(JSON.stringify(obj)); }
function isquote(ast){return ast.children && ast.children[0].text && ast.children[0].text=="quote"}
// TODO: add all kinds of error-checking.
function church_astify(tokens) {
// astify changes the opening bracket tokens so the end site is the matching closing bracket
function astify(tokens) {
function helper(opening_bracket) {
// Tree nodes have keys [children, start, end]
var result = {children: [], start: opening_bracket ? opening_bracket.start : "1:1"};
while (tokens.length > 0) {
if (tokens[0].text == "(" || tokens[0].text == "[") {
var bracket = tokens[0];
storage.push(tokens.shift());
result.children.push(helper(bracket));
} else if (tokens[0].text == ")" || tokens[0].text == "]") {
if (!opening_bracket || tokens[0].text != brackets_map[opening_bracket.text]) {
throw util.make_church_error("SyntaxError", tokens[0].start, tokens[0].end, "Unexpected close parens");
} else {
result["end"] = tokens[0].start;
opening_bracket.end = tokens[0].start;
storage.push(tokens.shift());
return result;
}
} else {
var token = tokens.shift();
storage.push(token);
result.children.push(token);
}
}
if (!opening_bracket) {
return result;
} else {
throw util.make_church_error("SyntaxError", opening_bracket.start, opening_bracket.end, "Unclosed parens");
}
}
var storage = [];
var ast = helper();
for (var i = 0; i < storage.length; i++) {
tokens.push(storage[i]);
}
return ast;
}
function traverse(ast, fn, stopfn) {
if (!util.is_leaf(ast) && ast.children.length > 0 && (!stopfn || !stopfn(ast))) {
ast = fn(ast);
for (var i = 0; i < ast.children.length; i++) {
ast.children[i] = traverse(ast.children[i], fn, stopfn);
}
}
return ast;
}
function is_special_form(text) {
return ["define", "lambda", "case", "cond", "if", "let"].indexOf(text) != -1;
}
function assert_not_special_form(node) {
if (is_special_form(node.text)) {
throw util.make_church_error("SyntaxError", node.start, node.end, "Special form " + node.text + " cannot be used as an atom");
}
}
function validate_leaves(ast) {
for (var i = 1; i < ast.children.length; i++) {
assert_not_special_form(ast.children[i]);
}
return ast;
}
// NOTE: Many of the desugar functions don't add range information.
// For now, it seems unlikely they'll be needed.
function dsgr_define(ast) {
if (ast.children[0].text=="define") {
if (ast.children.length < 3) {
throw util.make_church_error("SyntaxError", ast.start, ast.end, "Invalid define");
}
// Function define sugar
if (!util.is_leaf(ast.children[1])) {
var lambda_args;
// Variadic sugar
if (ast.children[1].children.length == 3 && ast.children[1].children[1].text == ".") {
lambda_args = ast.children[1].children[2];
} else {
lambda_args = {children: ast.children[1].children.slice(1)};
}
var lambda = {
children: [
{text: "lambda"},
lambda_args
].concat(ast.children.slice(2))
};
return {
children: [ast.children[0], ast.children[1].children[0], lambda],
start: ast.start,
end: ast.end
};
}
}
return ast;
}
function dsgr_lambda(ast) {
if (ast.children[0].text=="lambda") {
if (ast.children.length < 3) {
throw util.make_church_error("SyntaxError", ast.start, ast.end, "lambda has no body");
}
}
return ast;
}
function dsgr_let(ast) {
var let_varieties = ["let", "let*","letrec"];
if (let_varieties.indexOf(ast.children[0].text)!=-1) {
if (ast.children.length < 3) {
throw util.make_church_error("SyntaxError", ast.start, ast.end, ast.children[0].text + " has no body");
}
var isNamedLet = (ast.children.length == 4);
var bindings = isNamedLet ? ast.children[2] : ast.children[1];
// named let
if (isNamedLet) {
// TODO: check that name is valid
}
var valid_bindings = true;
if (util.is_leaf(bindings)) {
valid_bindings = false;
} else {
for (var i = 0; i < bindings.children.length; i++) {
if (util.is_leaf(bindings.children[i]) || bindings.children[i].children.length != 2) {
valid_bindings = false;
break;
}
}
}
if (!valid_bindings) {
throw util.make_church_error_range("SyntaxError", bindings.start, bindings.end, ast.children[0].text + " has invalid bindings");
}
if (isNamedLet) {
// example input:
// (let
// loop ((arr '(3 -2 1 6 -5)))
// (if (null? arr)
// 0
// (+ 1 (loop (cdr arr)))))
// example output:
// ((lambda ()
// (define loop (lambda (arr) (if (null? arr) 0 (+ 1 (loop (cdr arr))))))
// (loop (quote (3 -2 1 6 -5)))))
// note that this is not iterative because we are doing this as a church desugaring
// rather than a js compilation
// i'm not sure that js compilation is feasible work, as the named let procedure might do randomness
// and the loop counter for addressing is probably not enough to identify different addresses
// (e.g., we could have the same loop counter for different procedure arguments, which would be bad)
var loopName = ast.children[1].text;
var argList = {children: bindings.children.map(function(x) {return x.children[0]})};
var initialArgValues = bindings.children.map(function(x) {return x.children[1]});
var body = ast.children[3];
return {
children: [
{
children: [
{text: "lambda"},
{children: []},
{children: [
{text: "define"},
{text: loopName},
{children: [
{text: "lambda"},
argList,
body
]}
]},
{children: [{text: loopName}].concat(initialArgValues)}
]
}
]
}
}
switch (ast.children[0].text) {
case "let":
return {
children: [
{
children: [
{text: "lambda"},
{children: bindings.children.map(function(x) {return x.children[0]})},
ast.children[2]
]
}
].concat(bindings.children.map(function(x) {return x.children[1]}))
};
case "let*":
var new_ast = {
children: [
{
children: [
{text: "lambda"},
{children: []},
ast.children[2]
]
}
]
}
for (var i = bindings.children.length-1; i >= 0; i--) {
// console.log(JSON.stringify(bindings.children[i].children[0],undefined,2))
new_ast = {
children: [
{
children: [
{text: "lambda"},
{children: [bindings.children[i].children[0]]},
new_ast
]
},
bindings.children[i].children[1]
]
}
}
return new_ast;
case "letrec":
// example input:
// (letrec ([is-even? (lambda (n) (if (= n 0) true (is-odd? (- n 1))))]
// [is-odd? (lambda (n) (if (= n 0) false (is-even? (- n 1))))])
// (is-odd? 131))
// example output:
// ((lambda ()
// (define is-even? (lambda (n) (if (= n 0) true (is-odd? (- n 1)))))
// (define is-odd? (lambda (n) (if (= n 0) false (is-even? (- n 1)))))
// (is-odd? 131)))
var children = bindings.children.map(function(child) {
return {
children: [{text: "define"}].concat(child.children)
}
})
var lambdaBody = children.concat(ast.children[2]);
// console.log(JSON.stringify(lambdaBody, null, 1));
var new_ast = {
children: [
{
children: [
{text: "lambda"},
{children: []}
].concat(lambdaBody)
}
]
};
return new_ast;
}
} else {
return ast;
}
}
function dsgr_quote(ast) {
var last = ast.children[ast.children.length-1];
if (last.text=="'") {
throw util.make_church_error("SyntaxError", last.start, last.end, "Invalid single quote");
}
for (var i = ast.children.length - 2; i >= 0; i--) {
if (ast.children[i].text == "'") {
ast.children.splice(i, 2, {
children: [{text: "quote", start: ast.children[i].start, end: ast.children[i].end}, ast.children[i+1]],
start: ast.children[i].start,
end: ast.children[i+1].end
});
}
}
return ast;
}
function dsgr_unquote(ast) {
for (var i = ast.children.length - 2; i >= 0; i--) {
if (ast.children[i].text == ",") {
ast.children.splice(i, 2, {
children: [{text: "unquote", start: ast.children[i].start, end: ast.children[i].end}, ast.children[i+1]],
start: ast.children[i].start,
end: ast.children[i+1].end
});
}
}
return ast;
}
//turn (... , @ foo ...) into (, (append ' (...) foo ' (...)))
//because this wraps each items have to also turn "... , foo ..." into "... (list foo) ..."
//note: do this before desugarring quote and unquote.
function dsgr_splunquote(ast) {
//todo:start and end
var ats = false
var children = [{text: "append"}]
for(var c = 0; c < ast.children.length; c++){
if(ast.children[c].text =="," && ast.children[c+1].text =="@") {
ats = true
children.push(ast.children[c+2])
c=c+2
} else if(ast.children[c].text ==","){
children.push({children:[{text:"list"}, ast.children[c+1]]})
c=c+1
} else {
children.push({text: "'"})
children.push({children: [ast.children[c]]})
}
}
if(ats){
return {children: [{text:"unquote"}, {children: children}]}
} else {
return ast
}
}
function dsgr_case(ast) {
function case_helper(key, clauses) {
if (clauses.length == 0) {
return undefined;
}
var clause = clauses[0];
if (util.is_leaf(clause) || clause.children.length != 2 ||
(util.is_leaf(clause.children[0]) && clause.children[0].text!="else")) {
throw util.make_church_error("SyntaxError", clause.start, clause.end, "Bad clause for case");
}
if (clause.children[0].text=="else") {
if (clauses.length > 1) {
throw util.make_church_error("SyntaxError", clause.start, clause.end, "Bad placement of else clause in case");
} else {
return clause.children[1];
}
} else {
for (var i = 0; i < clause.children[0]; i++) {
var datum = clause.children[0].children[i];
if (util.is_leaf(datum)) {
throw util.make_church_error("SyntaxError", datum.start, datum.end, " for case");
}
}
var next = case_helper(key, clauses.slice(1));
var new_ast = {
children: [
{text: "if"},
{
children: [
{text: "member"},
key,
{children: [{text: "list"}].concat(clause.children[0].children)}
// {children: [{text: "list"}].concat(clause.children[0])}
]
},
clause.children[1]
]
};
if (next) {
new_ast.children.push(next);
}
return new_ast;
}
}
if (ast.children[0].text=="case") {
if (ast.children.length < 3) {
throw util.make_church_error("SyntaxError", ast.start, ast.end, "case is missing clauses");
}
return case_helper(ast.children[1], ast.children.slice(2));
} else {
return ast;
}
}
function dsgr_cond(ast) {
function cond_helper(clauses) {
if (clauses.length == 0) {
return undefined;
}
var clause = clauses[0];
if (util.is_leaf(clause) || clause.children.length != 2) {
throw util.make_church_error("SyntaxError", clause.start, clause.end, "Bad clause for cond");
}
if (clause.children[0].text=="else") {
if (clauses.length > 1) {
throw util.make_church_error("SyntaxError", clause.start, clause.end, "Bad placement of else clause in cond");
} else {
return clause.children[1];
}
} else {
var next = cond_helper(
clauses.slice(1));
var new_ast = {
children: [
{text: "if"},
clause.children[0],
clause.children[1]
]
};
if (next) {
new_ast.children.push(next);
}
return new_ast;
}
}
if (ast.children[0].text=="cond") {
if (ast.children.length < 2) {
throw util.make_church_error("SyntaxError", ast.start, ast.end, "cond is missing clauses");
}
return cond_helper(ast.children.slice(1));
} else {
return ast;
}
}
function dsgr_eval(ast) {
if (ast.children[0].text == "eval") {
return {
children: [
{text: "eval"},
{
children: [
// churchToBareJs: for use in eval only
{text: "churchToBareJs"},
{
children: [
{text: "formatResult"},
ast.children[1]
]
}
]
}
]
}
}
return ast;
};
function dsgr_load(ast) {
if (ast.children[0].text == "load") {
if (ast.children[0].text == "load") {
return {
children: [
{text: "eval"},
{
children: [
// churchToBareJs: for use in eval only
{text: "load_url"},
ast.children[1]
]
}
]
}
}
}
return ast;
};
function dsgr_query(ast) {
// Makes the lambda that's passed to the query function
function query_helper(statements, args) {
// The final output of the query
var query_exp;
var query_exp_index;
for (var i = 0; i < statements.length; i++) {
// If query_exp is set, then any statement after that which is not a define, factor, or condition
// is implicitly a condition
if (util.is_leaf(statements[i]) || query_decls.indexOf(statements[i].children[0].text) == -1) {
if (query_exp) {
statements[i] = {
children: [{text: "condition"}, statements[i]],
start: condition.start,
end: condition.end
};
} else {
query_exp = statements[i];
query_exp_index = i;
}
}
}
args = args || {children: []};
return {
children: [
{text: "lambda"},
args
].concat(statements.slice(0, query_exp_index))
.concat(statements.slice(query_exp_index+1))
.concat(query_exp)
}
}
if (query_fns.indexOf(ast.children[0].text) != -1) {
var num_params = query_fns_to_num_params[ast.children[0].text];
if (ast.children.length < num_params + 3) {
throw util.make_church_error("SyntaxError", ast.start, ast.end, ast.children[0].text + " has the wrong number of arguments");
}
return {
children: [
ast.children[0],
query_helper(ast.children.slice(num_params+1))
].concat(ast.children.slice(1, num_params+1)),
start: ast.start,
end: ast.end
};
}
return ast;
}
function validate_if(ast) {
if (ast.children[0].text=="if") {
if (ast.children.length < 3 || ast.children.length > 4) {
throw util.make_church_error("SyntaxError", ast.start, ast.end, "if has the wrong number of arguments");
}
}
return ast;
}
function transform_equals_condition(ast) {
function is_equals_conditionable(ast) {
if (util.is_leaf(ast)) return false;
var fn = church_builtins.__annotations__[rename_map[ast.children[0].text]];
return fn && fn.erp && ast.children[fn.numArgs[fn.numArgs.length-1]] == undefined;
}
function transform_erp(erp, conditioned_value) {
erp.children.push(conditioned_value);
}
function try_transform(left, right) {
if (left == undefined) return false;
if (is_equals_conditionable(left)) {
transform_erp(left, right);
statements.splice(i, 1, left);
return true;
} else if (util.is_leaf(left) && define_table[left.text] && is_equals_conditionable(define_table[left.text].def)) {
var left_entry = define_table[left.text];
if (!util.is_identifier(right.text) || (
define_table[right.text] && left_entry.index > define_table[right.text].index)) {
transform_erp(left_entry.def, right);
statements.splice(i, 1);
return true;
}
}
return false;
}
var transformed;
if (query_fns.indexOf(ast.children[0].text) != -1) {
var define_table = {};
// Assumes preprocessing through dsgr_query
var statements = ast.children[1].children.slice(2);
var i = 0;
// Iterate through each lambda statement
for (var i = 0; i < statements.length; i++) {
if (!util.is_leaf(statements[i])) {
// If statement is a define and an ERP without an existing condition, put it in a table
if (statements[i].children[0].text == "define") {
define_table[statements[i].children[1].text] = {
index: i,
def: statements[i].children[2]
};
// If statement is a condition, check if it's an equality and attempt to transform
} else if (statements[i].children[0].text == "condition") {
var condition = statements[i].children[1];
if (!util.is_leaf(condition) && ["=", "equal?"].indexOf(condition.children[0].text) != -1 && condition.children.length == 3) {
var left = condition.children[1];
var right = condition.children[2];
if (!try_transform(left, right)) try_transform(right, left);
}
}
}
}
} else if (ast.children[0].text == "condition") {
function transform(left, right) {
if (is_equals_conditionable(left)) {
transform_erp(left, right);
return true;
}
}
var condition = ast.children[1];
if (!util.is_leaf(condition) && ["=", "equal?"].indexOf(condition.children[0].text) != -1 && condition.children.length == 3) {
var left = condition.children[1];
var right = condition.children[2];
if (!transform(left, right)) transform(right, left);
}
}
return ast;
}
function transform_repeat_equals_condition(ast) {
function try_transform(left, right) {
if (!util.is_leaf(left) && left.children[0].text == "repeat") {
ast.children[1].children[i+2] = {
children: [
{"text": "multi-equals-condition"},
left.children[2],
left.children[1],
right],
start: ast.children[1].children[i+2].children[0].start,
end: ast.children[1].children[i+2].children[0].end
};
return true;
}
return false;
}
if (query_fns.indexOf(ast.children[0].text) != -1) {
var statements = ast.children[1].children.slice(2);
for (var i = 0; i < statements.length; i++) {
if (!util.is_leaf(statements[i]) && statements[i].children[0].text == "condition") {
var condition = statements[i].children[1];
if (!util.is_leaf(condition) && condition.children[0].text == "equal?" && condition.children.length == 3) {
var left = condition.children[1];
var right = condition.children[2];
if (!try_transform(left, right)) try_transform(right, left);
}
}
}
}
return ast;
}
// Break out conditions with ands into multiple condition statements
function transform_and_condition(ast) {
if (["rejection-query", "enumeration-query", "mh-query"].indexOf(ast.children[0].text) != -1) {
var lambda = ast.children[1];
var stmts = lambda.children.splice(2);
for (var i = 0; i < stmts.length; i++) {
if (!util.is_leaf(stmts[i]) && stmts[i].children[0].text == "condition") {
var condition_stmt = stmts[i];
var condition = condition_stmt.children[1];
if (!util.is_leaf(condition) && condition.children[0].text == "and") {
for (var j=1;j<condition.children.length;j++) {
lambda.children.push({children: [condition_stmt.children[0], condition.children[j]]});
}
} else {
lambda.children.push(stmts[i]);
}
} else {
lambda.children.push(stmts[i]);
}
}
}
return ast;
}
// Order is important, particularly desugaring quotes before anything else.
var desugar_fns = [
validate_leaves, dsgr_define, dsgr_lambda, dsgr_let, dsgr_case, dsgr_cond, dsgr_eval, dsgr_load, dsgr_query, validate_if,
transform_and_condition, transform_equals_condition];
var ast = astify(tokens);
// Special top-level check
for (var i = 0; i < ast.children.length; i++) {
assert_not_special_form(ast.children[i]);
}
ast = traverse(ast, dsgr_splunquote);
ast = traverse(ast, dsgr_quote);
ast = traverse(ast, dsgr_unquote);
for (var i = 0; i < desugar_fns.length; i++) {
ast = traverse(ast, desugar_fns[i], isquote);
}
return ast;
}
function church_shallow_preconditions(ast) {
return traverse(ast, transform_equals_condition, isquote)
}
// Print a Church AST for debugging
function church_ast_to_string(ast) {
if (util.is_leaf(ast)) {
return ast.text;
} else {
return "(" + ast.children.map(function(x) {return church_ast_to_string(x)}).join(" ") + ")"
}
}
module.exports =
{
church_ast_to_string: church_ast_to_string,
church_astify: church_astify,
church_shallow_preconditions: church_shallow_preconditions
}