-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.js
82 lines (69 loc) · 1.9 KB
/
generate.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
var fs = require("fs");
var output = "";
function write(line){
output += line + ";\n";
}
function requires(){
write('var sf = require("./stockflow")');
write('var sfstd = require("./stockflowstd")');
}
function generate(ast){
//requires();
ast.map(function(l){
write(line(l));
});
fs.writeFileSync("output/output.js", output);
//eval(output);
}
function line(l){
if(l.type === "mapping"){
return "var "+ l.id + " = " + expression(l.expression);
} else {
return expression(l);
}
}
function expression(e){
if(e.type){
if (e.type==="id"){
return e.val;
}
if(e.type ==="array"){
return "sf.flow(["+e.val.map(function(a){
return expression(a);
})+"])";
}
if (e.type==="expression"){
var args = e.args.map(function(a){
return expression(a);
});
return e.mod+"(["+args+"])";
}
if (e.type==="stdexpression"){
var args = e.args.map(function(a){
return expression(a);
});
return "sfstd."+e.mod+"(["+args+"])";
}
if(e.type==="flow"){
return "sf.flow(" + e.val + ")";
}
if(e.type==="mapper"){
return "function($){\n"+e.args.map(function(e,c){
return "var "+e+"=$["+c+"];";
}).join("")+e.expression.map(function(a,c){
if(c===e.expression.length-1){
return "return "+line(a)+";\n";
}
return line(a)+";\n";
}).join("")+"}";
}
if(e.type==="when"){
return "sf.when("+expression(e.expression)+","+expression(e.then)+","+expression(e.else)+")";
}
if(e.type==="unfold"){
return "sf.unfold("+e.array+",function("+e.id+"){return "+expression(e.expression)+"})";
}
}
return e;
}
module.exports = generate;