67 lines
1.9 KiB
Plaintext
67 lines
1.9 KiB
Plaintext
evalAddSub = function()
|
|
result = evalMultDiv
|
|
while true
|
|
if not tokens then return result
|
|
op = tokens[0]
|
|
if op != "+" and op != "-" then return result
|
|
tokens.pull // (discard operator)
|
|
rhs = evalMultDiv
|
|
if result == null or rhs == null then return null
|
|
if op == "+" then result = result + rhs
|
|
if op == "-" then result = result - rhs
|
|
end while
|
|
end function
|
|
|
|
evalMultDiv = function()
|
|
result = evalAtom
|
|
while true
|
|
if not tokens then return result
|
|
op = tokens[0]
|
|
if op != "*" and op != "/" then return result
|
|
tokens.pull // (discard operator)
|
|
rhs = evalAtom
|
|
if result == null or rhs == null then return null
|
|
if op == "*" then result = result * rhs
|
|
if op == "/" then result = result / rhs
|
|
end while
|
|
end function
|
|
|
|
evalAtom = function()
|
|
if tokens[0] == "(" then
|
|
tokens.pull
|
|
result = evalAddSub
|
|
if not tokens or tokens.pull != ")" then
|
|
print "Unbalanced parantheses"
|
|
return null
|
|
end if
|
|
return result
|
|
end if
|
|
num = val(tokens.pull)
|
|
idx = availableDigits.indexOf(num)
|
|
if idx == null then
|
|
print str(num) + " is not available"
|
|
return null
|
|
else
|
|
availableDigits.remove idx
|
|
end if
|
|
return num
|
|
end function
|
|
|
|
choices = []
|
|
for i in range(1, 4)
|
|
choices.push ceil(rnd*9)
|
|
end for
|
|
result = null
|
|
while result != 24
|
|
availableDigits = choices[:] // (clones the list)
|
|
print "Using only the digits " + availableDigits + ","
|
|
tokens = input("enter an expression that comes to 24: ").replace(" ","").values
|
|
result = evalAddSub
|
|
if availableDigits then
|
|
print "You forgot to use: " + availableDigits
|
|
result = null
|
|
end if
|
|
if result != null then print "That equals " + result + "."
|
|
end while
|
|
print "Great job!"
|