From f6e5f70d030dd23e53c2c9bddd57869b27d0efd8 Mon Sep 17 00:00:00 2001 From: "Aidan K. Ewart" Date: Sun, 31 May 2020 10:20:33 +0100 Subject: [PATCH] buxfixes --- builtin.mjs | 6 +++--- eval.mjs | 25 ++++++++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/builtin.mjs b/builtin.mjs index e63ccbf..7802400 100644 --- a/builtin.mjs +++ b/builtin.mjs @@ -59,7 +59,7 @@ const eq = (_, args) => { } } -const arr = (_, args) => { +const tuple = (_, args) => { return [makeFn(args[0], (_, args) => {return [{type:"array", val:args}]})]; } @@ -90,7 +90,7 @@ addDefn("typeof", 1, type); addDefn("pair", 2, pair); addDefn("fst", ["pair"], fst); addDefn("snd", ["pair"], snd); -addDefn("arr", ["int"], arr); +addDefn("tuple", ["int"], tuple); addDefn("!!", ["int", "array"], index); addDefn("len", ["array"], len); addDefn("unsafeCoerce", 2, coerce); @@ -102,4 +102,4 @@ addRPNDefn("false", "(a b -> b)"); addRPNDefn("stop", "(-> \"stop)"); //addRPNDefn("id", "(a -> a)"); addRPNDefn("inv", "(x -> 1 x /)"); -addRPNDefn("fold", "(x acc fn -> '(-> acc) '(-> x acc fn 'fn fold) 'x \"stop ==)"); \ No newline at end of file +addRPNDefn("fold", "(x acc fn -> 'acc '(-> x acc fn 'fn fold) 'x \"stop ==)"); \ No newline at end of file diff --git a/eval.mjs b/eval.mjs index 455b696..7a7182a 100644 --- a/eval.mjs +++ b/eval.mjs @@ -73,7 +73,9 @@ const makeObj = (elem) => { } const cloneElem = (elem) => { - if (elem.type === "closure") { + if (Array.isArray(elem)) { + return elem.map(cloneElem); + } if (elem.type === "closure") { let argsClone = []; for (let i = 0; i < elem.args.length; i++) { argsClone.push(cloneElem(elem.args[i])); @@ -86,13 +88,11 @@ const cloneElem = (elem) => { const lookupScope = (name, scope) => { let n = scope[name]; - console.log(n); if (n) { return cloneElem(n); } n = builtinDefn[name]; if (n) { - console.log(name, n); return cloneElem(n); } else { throw 'var "' + name + '" not in scope' @@ -136,12 +136,23 @@ const applyMany = (outstack, stack) => { } } +const pushMany = (elems, stack) => { + for (let i = 0; i < elems.length; i++) { + pushS(elems[i], stack); + } +} + const pushS = (elem, stack) => { - if (elem.type === "ident") { - let id = lookupScope(elem.val, stack.scope); - stack.stack.push(id); + if (Array.isArray(elem)) { + pushMany(elem, stack); } else { - stack.stack.push(elem); + if (elem.type === "ident") { + let id = lookupScope(elem.val, stack.scope); + console.log(id); + pushS(id, stack); + } else { + stack.stack.push(elem); + } } }