-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval.js
187 lines (175 loc) · 6.16 KB
/
eval.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
const codeFrame = require("./util/code-frame");
const environment = require("./env");
const importModule = require("./util/import-module");
const operators = require("./operators");
const { arr, bool, func, obj } = require("./lib/types");
function evaluate(node, opts, env, expEnv) {
const eval = node => evaluate(node, opts, env);
const catchWithCf = (node, fn) => {
try {
return fn();
} catch (e) {
// NOTE: Overwriting existing code frame creates misleading output
if (opts.codeFrames)
e.cf = e.cf || codeFrame(opts.source, node.loc, node.value);
throw e;
}
};
const throwWithCf = (node, msg) => {
const error = new Error(msg);
opts.codeFrames &&
(error.cf = codeFrame(opts.source, node.loc, node.value));
throw error;
};
const getIdName = node => {
if (node.type !== "id")
throwWithCf(node, `Type error: Unexpected '${node.type}'`);
return node.value;
};
const evalOp = () => {
const fn = catchWithCf(node.callee, () =>
func(operators[node.callee.value])
);
const [right, left] = node.args;
if (!right)
return left => right => catchWithCf(node.callee, () => fn(right)(left));
const rApplied = catchWithCf(right, () => fn(eval(right)));
return left ? catchWithCf(right, () => rApplied(eval(left))) : rApplied;
};
const evalFunc = (node, args, scope) => {
if (args.length < node.args.length) {
return (...moreArgs) =>
evalFunc(node, [...args, ...moreArgs], scope.extend());
} else if (args.length > node.args.length) {
throwWithCf(args.slice(-1)[0], "Too many arguments");
}
args.forEach((arg, index) => scope.set(node.args[index].value, arg));
return evaluate(node.body, opts, scope);
};
const evalDestructuring = (left, right) => {
const names = catchWithCf(left, () => arr(eval(left)));
const values = catchWithCf(right, () => arr(eval(right)));
if (names.length > values.length + 1)
throwWithCf(right, `Input array too short`);
names.reduce((acc, name, i) => {
const [value, ...rest] = acc;
env.set(name, i < names.length - 1 ? value : acc);
return rest;
}, values);
};
const getMemberKeys = ({ object, property }) => {
if (object.type === "id") return [getIdName(object), getIdName(property)];
return [...getMemberKeys(object), getIdName(property)];
};
switch (node.type) {
case "unit":
return undefined;
case "number":
case "bool":
return node.value;
case "string":
return node.value.replace(/\\n/g, "\n");
case "array":
return node.elements.map(eval);
case "array-pattern":
return node.elements.map(getIdName);
case "id":
return catchWithCf(node, () => env.get(node.value));
case "object":
return Object.fromEntries(
node.properties.map(({ key, value }) => [getIdName(key), eval(value)])
);
case "member": {
const object = catchWithCf(node.object, () => eval(node.object));
const property = getIdName(node.property);
if (property in object) return object[property];
throwWithCf(node.property, `'${property}' is not defined.`);
}
case "property-accessor":
return object => {
const keys =
node.key.type === "id"
? [getIdName(node.key)]
: getMemberKeys(node.key);
return keys.reduce((acc, key) => {
if (key in acc) return acc[key];
throwWithCf(node.key, `Property '${key}' does not exist on object.`);
}, obj(object));
};
case "assign":
if (node.left.type === "array-pattern") {
return evalDestructuring(node.left, node.right);
}
return env.set(getIdName(node.left), eval(node.right));
case "call":
// NOTE: Set global cwd so that lib knows where the calling file lives
global.__asharp.cwd = opts.cwd;
if (node.callee.type === "op") return evalOp();
return catchWithCf(node.callee, () => {
const fn = func(eval(node.callee));
return node.args.reduce((acc, arg) => {
if (typeof acc !== "function") throwWithCf(arg, "Too many arguments");
return acc(eval(arg));
}, fn);
});
case "fun":
return (...args) => evalFunc(node, args, env.extend());
case "ternary":
case "if":
return catchWithCf(node.condition, () => bool(eval(node.condition)))
? eval(node.then)
: eval(node.else);
case "block":
const blockEnv = env.extend();
return node.body.reduce(
(_, node) => evaluate(node, opts, blockEnv),
null
);
case "export":
if (!expEnv) throwWithCf(node.left, "Cannot export in block scope");
env.set(getIdName(node.left), eval(node.right));
return expEnv.set(getIdName(node.left), eval(node.right));
case "import": {
const moduleExports = catchWithCf(node.source, () =>
importModule(eval(node.source), opts.cwd, tryEvaluate)
);
return node.ids.forEach(id => {
catchWithCf(id, () => {
const name = getIdName(id);
if (name in moduleExports) return env.set(name, moduleExports[name]);
else throwWithCf(id, `No export named '${name}'`);
});
});
}
case "import-all": {
const name = getIdName(node.id);
const moduleExports = catchWithCf(node.source, () =>
importModule(eval(node.source), opts.cwd, tryEvaluate)
);
return catchWithCf(node.id, () => env.set(name, moduleExports));
}
case "program":
const moduleEnv = environment();
const exportEnv = environment();
const body = node.body.reduce(
(_, node) => evaluate(node, opts, moduleEnv, exportEnv),
null
);
return Object.keys(exportEnv.vars).length > 0
? { __module: exportEnv.vars }
: body;
default:
throw Error(`Eval: Missing implementation for '${node.type}'`);
}
}
function tryEvaluate(ast, { codeFrames, source, cwd }) {
global.__asharp = global.__asharp || {};
try {
return evaluate(ast, { codeFrames, source, cwd });
} catch (e) {
if (e.cf) console.error(`${e.cf}\n\n${e.message}`);
else console.error(e.message);
process.exit(1);
}
}
module.exports = tryEvaluate;