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-30 23:49:46 +01:00
parent 1c6bee1947
commit 83e6cb247a
3 changed files with 23 additions and 9 deletions

View File

@ -39,7 +39,7 @@ const root = (_, args) => {
} }
const type = (_, args) => { const type = (_, args) => {
return [{type:"string", val:args[0].type}]; return [{type:"type", val:args[0].type}];
} }
const pair = (_, args) => { const pair = (_, args) => {
@ -84,11 +84,12 @@ addDefn("snd", ["pair"], snd);
addDefn("arr", ["int"], arr); addDefn("arr", ["int"], arr);
addDefn("!!", ["int", "array"], index); addDefn("!!", ["int", "array"], index);
addDefn("len", ["array"], len); addDefn("len", ["array"], len);
addRPNDefn("unit", "(-> 0 arr)"); //addRPNDefn("unit", "(-> 0 arr)");
addRPNDefn("mono", "(-> 1 arr)"); //addRPNDefn("mono", "(-> 1 arr)");
//addRPNDefn("unwrap", "(-> 0 !!)");
addRPNDefn("true", "(a b -> a)"); addRPNDefn("true", "(a b -> a)");
addRPNDefn("false", "(a b -> b)"); 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

@ -93,7 +93,7 @@ const lookupScope = (name, scope) => {
if (n) { if (n) {
return cloneElem(n); return cloneElem(n);
} else { } 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) => { export const execRPN = (scope, i) => {
let ins = JSON.parse(JSON.stringify(i)); let ins = JSON.parse(JSON.stringify(i));
let stack = {scope:scope, stack:[]}; let stack = {scope:scope, stack:[]};
while (ins.length > 0) { 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; return stack;
} }

View File

@ -144,7 +144,7 @@ const parseName = (stream) => {
return {parsed:id.parsed.val, stream:syn.stream}; return {parsed:id.parsed.val, stream:syn.stream};
} }
const parseString = (stream) => { const parseType = (stream) => {
let syn = attempt(parseSyntax("\""))(stream); let syn = attempt(parseSyntax("\""))(stream);
if (syn.parsed === null) { if (syn.parsed === null) {
@ -154,7 +154,7 @@ const parseString = (stream) => {
if (id.parsed === null) { if (id.parsed === null) {
return {parsed:null, stream:id.stream}; 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}; //return {parsed:null, stream:stream};
} }
@ -193,7 +193,7 @@ const parseLambda = (stream) => {
} }
/* takes in stream, outputs parsed item or null */ /* 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 */ /* takes in stream, outputs parsed items */
export const parseExprs = many(parseExpr); export const parseExprs = many(parseExpr);