diff --git a/eval.mjs b/eval.mjs index 901e463..455b696 100644 --- a/eval.mjs +++ b/eval.mjs @@ -86,11 +86,13 @@ 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' @@ -107,20 +109,24 @@ const giveArg = (closure, arg, scope) => { } const apply = (elem, stack) => { - if (elem.type === "closure") { - if (elem.func.nargs === 0) { - applyMany(elem.func.defn(stack.scope, []), stack); - } else if (stack.stack.length > 0) { - let out = giveArg(elem, stack.stack.pop(), stack.scope); - applyMany(out, stack); + if (Array.isArray(elem)) { + applyMany(elem, stack); + } else { + if (elem.type === "closure") { + if (elem.func.nargs === 0) { + apply(elem.func.defn(stack.scope, []), stack); + } else if (stack.stack.length > 0) { + let out = giveArg(elem, stack.stack.pop(), stack.scope); + apply(out, stack); + } else { + stack.stack.push(elem); + } + } else if (elem.type === "ident") { + let id = lookupScope(elem.val, stack.scope); + apply(id, stack); } else { stack.stack.push(elem); } - } else if (elem.type === "ident") { - let id = lookupScope(elem.val, stack.scope); - apply(id, stack); - } else { - stack.stack.push(elem); } } @@ -140,7 +146,7 @@ const pushS = (elem, stack) => { } const defn = (elem, name, stack) => { - stack.scope[name] = makeObj(elem); + stack.scope[name] = execRPN(stack.scope, elem).stack; } const doStep = (ins, stack) => { diff --git a/index.html b/index.html index 0fad777..6e4a9f8 100644 --- a/index.html +++ b/index.html @@ -1,13 +1,12 @@ +
@ me (aidanprattewart@protonmail.com) if you have any errors, with console output please.
-
-Example Programs:
-
-(swap; a b -> a b) 1 2 swap
N.B. args are in reverse order
-
-1 + 2 (v partial -> 'v partial)
Evaluate a partial
-