Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,37 @@
tests:=T("3 4 2 * 1 5 - 2 3 ^ ^ / +","1 2 + 3 4 + ^ 5 6 + ^");
var opa=D(
"^",T(4, True),
"*",T(3, False), "/",T(3, False),
"+",T(2, False), "-",T(2, False) );
const nPrec = 9;
foreach t in (tests) { parseRPN(t) }
fcn parseRPN(e){
println("\npostfix:", e);
stack:=L();
foreach tok in (e.split()){
println("token: ", tok);
opPrec,rAssoc:=opa.find(tok,T(Void,Void));
if(opPrec){
rhsPrec,rhsExpr := stack.pop();
lhsPrec,lhsExpr := stack.pop();
if(lhsPrec < opPrec or (lhsPrec == opPrec and rAssoc))
lhsExpr = "(" + lhsExpr + ")";
lhsExpr += " " + tok + " ";
if(rhsPrec < opPrec or (rhsPrec == opPrec and not rAssoc)){
lhsExpr += "(" + rhsExpr + ")"
} else
lhsExpr += rhsExpr;
lhsPrec = opPrec;
stack.append(T(lhsPrec,lhsExpr));
} else
stack.append(T(nPrec, tok));
foreach f in (stack){
println(0'| %d "%s"|.fmt(f.xplode()))
}
}
println("infix:", stack[0][1])
}