Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
|
|
@ -0,0 +1,58 @@
|
|||
procedure main()
|
||||
every rpn := ![
|
||||
"3 4 2 * 1 5 - 2 3 ^ ^ / +", # reqd
|
||||
"1 2 + 3 4 + 5 6 + ^ ^",
|
||||
"1 2 + 3 4 + 5 6 + ^ ^",
|
||||
"1 2 + 3 4 + ^ 5 6 + ^" # reqd
|
||||
] do {
|
||||
printf("\nRPN = %i\n",rpn)
|
||||
printf("infix = %i\n",RPN2Infix(rpn,show))
|
||||
show := 1 # turn off inner working display
|
||||
}
|
||||
end
|
||||
|
||||
link printf
|
||||
|
||||
procedure RPN2Infix(expr,show) #: RPN to Infix conversion
|
||||
static oi
|
||||
initial {
|
||||
oi := table([9,"'"]) # precedence & associativity
|
||||
every oi[!"+-"] := [2,"l"] # 2L
|
||||
every oi[!"*/"] := [3,"l"] # 3L
|
||||
oi["^"] := [4,"r"] # 4R
|
||||
}
|
||||
|
||||
show := if /show then printf else 1 # to show inner workings or not
|
||||
stack := []
|
||||
expr ? until pos(0) do { # Reformat as a tree
|
||||
tab(many(' ')) # consume previous seperator
|
||||
token := tab(upto(' ')|0) # get token
|
||||
if token := numeric(token) then { # ... numeric
|
||||
push(stack,oi[token]|||[token])
|
||||
show("pushed numeric %i : %s\n",token,list2string(stack))
|
||||
}
|
||||
else { # ... operator
|
||||
every b|a := pop(stack) # pop & reverse operands
|
||||
pr := oi[token,1]
|
||||
as := oi[token,2]
|
||||
if a[1] < pr | (a[1] = pr & oi[token,2] == "r") then
|
||||
a[3] := sprintf("( %s )",a[3])
|
||||
if b[1] < pr | (b[1] == pr & oi[token,2] == "l") then
|
||||
b[3] := sprintf("( %s )",b[3])
|
||||
s := sprintf("%s %s %s",a[3],token,b[3])
|
||||
push(stack,[pr,as,s]) # stack [prec, assoc, expr]
|
||||
show("applied operator %s : %s\n",token,list2string(stack))
|
||||
}
|
||||
}
|
||||
|
||||
if *stack ~= 1 then stop("Parser failure")
|
||||
return stack[1,3]
|
||||
end
|
||||
|
||||
procedure list2string(L) #: format list as a string
|
||||
s := "[ "
|
||||
every x := !L do
|
||||
s ||:= ( if type(x) == "list" then list2string(x)
|
||||
else x) || " "
|
||||
return s || "]"
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue