diff --git a/builtin.mjs b/builtin.mjs index 3004340..df8cfc7 100644 --- a/builtin.mjs +++ b/builtin.mjs @@ -39,7 +39,7 @@ const root = (_, args) => { } const type = (_, args) => { - return [{type:"string", val:args[0].type}]; + return [{type:"type", val:args[0].type}]; } const pair = (_, args) => { @@ -84,11 +84,12 @@ addDefn("snd", ["pair"], snd); addDefn("arr", ["int"], arr); addDefn("!!", ["int", "array"], index); addDefn("len", ["array"], len); -addRPNDefn("unit", "(-> 0 arr)"); -addRPNDefn("mono", "(-> 1 arr)"); +//addRPNDefn("unit", "(-> 0 arr)"); +//addRPNDefn("mono", "(-> 1 arr)"); +//addRPNDefn("unwrap", "(-> 0 !!)"); addRPNDefn("true", "(a b -> a)"); addRPNDefn("false", "(a b -> b)"); addRPNDefn("stop", "(-> \"stop)"); -addRPNDefn("id", "(a -> a)"); +//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 diff --git a/eval.mjs b/eval.mjs index 4a26678..901e463 100644 --- a/eval.mjs +++ b/eval.mjs @@ -93,7 +93,7 @@ const lookupScope = (name, scope) => { if (n) { return cloneElem(n); } else { - throw "var " + n + " not in scope" + throw 'var "' + name + '" not in scope' } } @@ -154,11 +154,24 @@ const doStep = (ins, stack) => { } } +const showIns = (curr) => { + if (curr.type === "ident") { + return curr.val; + } else if (curr.type === "push") { + return "'" + showIns(curr.elem) + } +} + export const execRPN = (scope, i) => { let ins = JSON.parse(JSON.stringify(i)); let stack = {scope:scope, stack:[]}; while (ins.length > 0) { - doStep(ins, stack); + let curr = ins[0]; + try { + doStep(ins, stack); + } catch (error) { + throw error + ' while executing "' + showIns(curr) + '"' + } } return stack; } \ No newline at end of file diff --git a/parse.mjs b/parse.mjs index 9029456..efa0c0c 100644 --- a/parse.mjs +++ b/parse.mjs @@ -144,7 +144,7 @@ const parseName = (stream) => { return {parsed:id.parsed.val, stream:syn.stream}; } -const parseString = (stream) => { +const parseType = (stream) => { let syn = attempt(parseSyntax("\""))(stream); if (syn.parsed === null) { @@ -154,7 +154,7 @@ const parseString = (stream) => { if (id.parsed === null) { return {parsed:null, stream:id.stream}; } - return {parsed:{type:"string", val:id.parsed.val}, stream:id.stream}; + return {parsed:{type:"type", val:id.parsed.val}, stream:id.stream}; //return {parsed:null, stream:stream}; } @@ -193,7 +193,7 @@ const parseLambda = (stream) => { } /* takes in stream, outputs parsed item or null */ -const parseExpr = or(parseString, or(parseIdent, or(parseInteger, or(parsePush, attempt(parens(parseLambda)))))); +const parseExpr = or(parseType, or(parseIdent, or(parseInteger, or(parsePush, attempt(parens(parseLambda)))))); /* takes in stream, outputs parsed items */ export const parseExprs = many(parseExpr); \ No newline at end of file