-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.js
144 lines (135 loc) · 4.46 KB
/
generator.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
// TODO: File system operations currently have no context to use for path resolution.
let count = 0;
const uniqueName = prefix => {
count++;
return `${prefix}${String(count)}`;
};
const emptyCompare = (thing1, thing2, operator) => {
if (thing1 === "[]") return `${thing2}.length ${operator} 0`;
if (thing2 === "[]") return `${thing1}.length ${operator} 0`;
};
const operators = {
"+": (l, r) => `${l} + ${r}`,
"-": (l, r) => `${l} - ${r}`,
"/": (l, r) => `${l} / ${r}`,
"%": (l, r) => `${l} / ${r}`,
"*": (l, r) => `${l} * ${r}`,
"**": (l, r) => `Math.pow(${l}, ${r})`,
"::": (l, r) => `[${l}, ...${r}]`,
"@": (l, r) => `[...${l}, ...${r}]`,
"<=": (l, r) => `${l} <= ${r}`,
">=": (l, r) => `${l} >= ${r}`,
"<": (l, r) => `${l} < ${r}`,
">": (l, r) => `${l} > ${r}`,
"==": (l, r) => emptyCompare(r, l, "===") || `${l} === ${r}`,
"!=": (l, r) => emptyCompare(r, l, "!==") || `${l} !== ${r}`,
"||": (l, r) => `${l} || ${r}`,
"&&": (l, r) => `${l} && ${r}`,
"|>": (l, r) => `${r}(${l})`,
"<|": (l, r) => `${l}(${r})`,
">>": (l, r) => {
const args = uniqueName("args");
return `(...${args}) => ${r}(${l}(...${args}))`;
},
"<<": (l, r) => {
const args = uniqueName("args");
return `(...${args}) => ${l}(${r}(...${args}))`;
}
};
module.exports = function gen(node) {
const generateOp = () => {
const [right, left] = node.args.map(gen);
const op = gen(node.callee);
return left
? operators[op](left, right)
: right
? `(x => ${operators[op]("x", right)})`
: `((x, y) => ${operators[op]("x", "y")})`;
};
const generateFunc = node => {
const body = genBlock(node.body);
const args = node.args.map(gen).join(" => ");
return `${args} => ${body}`;
};
const genBlock = body => {
if (body.type === "block") {
const [last, ...restRev] = body.body.reverse().map(gen);
return `{\n${[...restRev.reverse(), `return ${last}`].join(";\n")};\n}`;
}
return gen(body);
};
const genDestructuring = (left, right) => {
const [lastName, ...names] = gen(left).reverse();
const values = gen(right);
return `const [${names.reverse().join(", ")}, ...${lastName}] = ${values}`;
};
const genImportSource = node => {
const { value } = node;
const isLib = !value.startsWith(".") && !value.startsWith("/");
return isLib ? `./asharp-lib/${value}` : value.replace(".asharp", ".js");
};
switch (node.type) {
case "unit":
return "()";
case "number":
case "bool":
case "op":
return node.value;
case "string":
return `"${node.value}"`;
case "id":
return node.value.replace(/-/g, "_");
case "array":
return `[${node.elements.map(gen).join(", ")}]`;
case "array-pattern":
return node.elements.map(gen);
case "object":
const properties = node.properties
.map(({ key, value }) => `${gen(key)}: ${gen(value)}`)
.join(",\n");
return `{\n${properties}\n}`;
case "member":
return `${gen(node.object)}.${gen(node.property)}`;
case "property-accessor":
return `(obj => obj[${gen(node.key)}])`;
case "assign":
if (node.left.type === "array-pattern") {
return genDestructuring(node.left, node.right);
}
return `const ${gen(node.left)} = ${gen(node.right)}`;
case "call":
if (node.callee.type === "op") return generateOp();
const args = node.args.map(gen).map(a => `(${a})`);
return `${gen(node.callee)}${args.join("")}`;
case "fun":
return generateFunc(node);
case "ternary":
case "if":
return `${gen(node.condition)} ? ${gen(node.then)} : ${gen(node.else)}`;
case "block":
const content = genBlock(node);
return `(() => ${content})()`;
case "export": {
const name = gen(node.left);
return `const ${name} = ${gen(node.right)};\nexports.${name} = ${name}`;
}
case "import": {
return `const { ${node.ids
.map(gen)
.join(", ")} } = require('${genImportSource(node.source)}')`;
}
case "import-all": {
return `const ${gen(node.id)} = require('${genImportSource(
node.source
)}')`;
}
case "program":
const code = node.body.map(gen).join(";\n") + ";\n";
const dependencies = node.body
.filter(({ type }) => type === "import" || type === "import-all")
.map(({ source }) => source.value);
return { code, dependencies };
default:
throw Error(`Eval: Missing implementation for '${node.type}'`);
}
};