aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2020-08-12 22:45:49 -0400
committerMarshall Lochbaum <mwlochbaum@gmail.com>2020-08-12 22:45:49 -0400
commit09365aca8c9247452b41f2a0c72dd5e889792672 (patch)
tree89110bc57617b76afaffb2b5cfea6b528a2d05ac
parent598c9ba8b3e1d8db61f58a2a35ac36c6730ea03b (diff)
Use a parent pointer for environments instead of an ancestor array
-rw-r--r--bqn.js7
1 files changed, 4 insertions, 3 deletions
diff --git a/bqn.js b/bqn.js
index 81ec1b16..28436f5d 100644
--- a/bqn.js
+++ b/bqn.js
@@ -25,7 +25,7 @@ let run = (B,O,S) => { // Bytecode, Objects, Sections/blocks
// Turn each block into an environment=>pushable value map
let D = S.map(([t,i,st,l]) => e => {
let v=Array(l).fill(null);
- let c = sv => vm(st,[sv.concat(v)].concat(e));
+ let c = sv => vm(st,[sv.concat(v),e]);
return [
n => n([]),
n => {let r=(f )=>n([r,f ]);r.m1=1;return r;},
@@ -35,6 +35,7 @@ let run = (B,O,S) => { // Bytecode, Objects, Sections/blocks
c
][i]);
});
+ let ge = (e,i) => i?ge(e[1],i-1):e[0];
// Execute
let vm = (p,e) => { // Program counter, Environment
let s=[]; // Stack
@@ -59,8 +60,8 @@ let run = (B,O,S) => { // Bytecode, Objects, Sections/blocks
case 13: {let[x,f,i]=s.splice(-3);s.push(set(0,i,call(f,x,get(i))));break;}
case 14:{s.pop();break;}
case 15:{s.push(D[num()](e));break;}
- case 21:{let v=e[num()][num()];assert(v!==null);s.push(v);break;}
- case 22:{s.push([e[num()],num()]);break;}
+ case 21:{let v=ge(e,num())[num()];assert(v!==null);s.push(v);break;}
+ case 22:{s.push([ge(e,num()),num()]);break;}
case 25:assert(s.length===1);return s[0];
}
}