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}]})];
}
@ -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 ==)");
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) => {
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);
}
}
}