Commit | Line | Data |
---|---|---|
732f2170 JM |
1 | export function new_env(outer={}, binds=[], exprs=[]) { |
2 | var e = Object.setPrototypeOf({}, outer); | |
3 | // Bind symbols in binds to values in exprs | |
4 | for (var i=0; i<binds.length; i++) { | |
5 | if (binds[i] == "&") { | |
6 | e[binds[i+1]] = exprs.slice(i); // variable length arguments | |
7 | break; | |
8 | } else { | |
9 | e[binds[i]] = exprs[i]; | |
10 | } | |
11 | } | |
12 | Object.defineProperties(e, { | |
13 | "get": { | |
14 | value: function(sym) { | |
15 | if (sym.name in this) { | |
16 | return this[sym.name] | |
17 | } else { | |
18 | throw Error("'" + sym + "' not found") | |
19 | } | |
20 | } | |
21 | }, | |
22 | "set": { | |
23 | value: function(sym, val) { | |
24 | return this[sym.name] = val; | |
25 | } | |
26 | } | |
27 | }); | |
28 | return e; | |
29 | } |