1
0
mirror of https://github.com/Baidicoot/rpncalc-v4 synced 2024-12-04 23:39:56 +00:00
This commit is contained in:
Aidan K. Ewart 2020-05-31 10:20:33 +01:00
parent 908af0dd72
commit f6e5f70d03
2 changed files with 21 additions and 10 deletions

View File

@ -59,7 +59,7 @@ const eq = (_, args) => {
} }
} }
const arr = (_, args) => { const tuple = (_, args) => {
return [makeFn(args[0], (_, args) => {return [{type:"array", val:args}]})]; return [makeFn(args[0], (_, args) => {return [{type:"array", val:args}]})];
} }
@ -90,7 +90,7 @@ addDefn("typeof", 1, type);
addDefn("pair", 2, pair); addDefn("pair", 2, pair);
addDefn("fst", ["pair"], fst); addDefn("fst", ["pair"], fst);
addDefn("snd", ["pair"], snd); addDefn("snd", ["pair"], snd);
addDefn("arr", ["int"], arr); addDefn("tuple", ["int"], tuple);
addDefn("!!", ["int", "array"], index); addDefn("!!", ["int", "array"], index);
addDefn("len", ["array"], len); addDefn("len", ["array"], len);
addDefn("unsafeCoerce", 2, coerce); addDefn("unsafeCoerce", 2, coerce);
@ -102,4 +102,4 @@ addRPNDefn("false", "(a b -> b)");
addRPNDefn("stop", "(-> \"stop)"); addRPNDefn("stop", "(-> \"stop)");
//addRPNDefn("id", "(a -> a)"); //addRPNDefn("id", "(a -> a)");
addRPNDefn("inv", "(x -> 1 x /)"); addRPNDefn("inv", "(x -> 1 x /)");
addRPNDefn("fold", "(x acc fn -> '(-> acc) '(-> x acc fn 'fn fold) 'x \"stop ==)"); addRPNDefn("fold", "(x acc fn -> 'acc '(-> x acc fn 'fn fold) 'x \"stop ==)");

View File

@ -73,7 +73,9 @@ const makeObj = (elem) => {
} }
const cloneElem = (elem) => { const cloneElem = (elem) => {
if (elem.type === "closure") { if (Array.isArray(elem)) {
return elem.map(cloneElem);
} if (elem.type === "closure") {
let argsClone = []; let argsClone = [];
for (let i = 0; i < elem.args.length; i++) { for (let i = 0; i < elem.args.length; i++) {
argsClone.push(cloneElem(elem.args[i])); argsClone.push(cloneElem(elem.args[i]));
@ -86,13 +88,11 @@ const cloneElem = (elem) => {
const lookupScope = (name, scope) => { const lookupScope = (name, scope) => {
let n = scope[name]; let n = scope[name];
console.log(n);
if (n) { if (n) {
return cloneElem(n); return cloneElem(n);
} }
n = builtinDefn[name]; n = builtinDefn[name];
if (n) { if (n) {
console.log(name, n);
return cloneElem(n); return cloneElem(n);
} else { } else {
throw 'var "' + name + '" not in scope' 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) => { const pushS = (elem, stack) => {
if (elem.type === "ident") { if (Array.isArray(elem)) {
let id = lookupScope(elem.val, stack.scope); pushMany(elem, stack);
stack.stack.push(id);
} else { } 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);
}
} }
} }