# All the button labels, row by row
var rows = [["7", "8", "9", "+"],
["4", "5", "6", "-"],
- ["1", "2", "3", "*"],
- ["0", ".", "C", "/"],
+ ["1", "2", "3", "×"],
+ ["0", ".", "C", "÷"],
["="]]
for row in rows do
else if op.is_numeric then
var n = op.to_i
context.push_digit n
- else
+ else if op != null then
buttons["."].enabled = true
- context.push_op op.chars.first
+ context.push_op op
end
display.text = context.display_text
var result: nullable Numeric = null
# Last operation pushed with `push_op`, to be executed on the next push
- var last_op: nullable Char = null
+ var last_op: nullable Text = null
# Value currently being entered
var current: nullable FlatBuffer = null
var buf = new FlatBuffer
- if result != null and (current == null or last_op != '=') then
- if last_op == '=' then buf.append "= "
+ if result != null and (current == null or last_op != "=") then
+ if last_op == "=" then buf.append "= "
buf.append result.to_s
buf.add ' '
end
- if last_op != null and last_op != '=' then
- buf.add last_op
+ if last_op != null and last_op != "=" then
+ buf.append last_op
buf.add ' '
end
end
# Push operation `op`, will usually execute the last operation
- fun push_op(op: Char)
+ fun push_op(op: Text)
do
apply_last_op_if_any
- if op == 'C' then
+ if op == "C" then
self.result = null
last_op = null
else
current.add digit.to_s.chars.first
self.current = current
- if last_op == '=' then
+ if last_op == "=" then
self.result = null
last_op = null
end
if op == null then
result = current.to_n
else if result != null then
- if op == '+' then
+ if op == "+" then
result = result.add(current.to_n)
- else if op == '-' then
+ else if op == "-" then
result = result.sub(current.to_n)
- else if op == '/' then
+ else if op == "/" or op == "÷" then
result = result.div(current.to_n)
- else if op == '*' then
+ else if op == "*" or op == "×" then
result = result.mul(current.to_n)
end
end
var context = new CalculatorContext
context.push_digit( 1 )
context.push_digit( 2 )
-context.push_op( '+' )
+context.push_op( "+" )
context.push_digit( 3 )
-context.push_op( '*' )
+context.push_op( "*" )
context.push_digit( 2 )
-context.push_op( '=' )
+context.push_op( "=" )
var r = context.result
assert r == 30 else print r or else "-"
context.push_digit( 4 )
context.switch_to_decimals
context.push_digit( 1 )
-context.push_op( '*' )
+context.push_op( "*" )
context.push_digit( 3 )
-context.push_op( '=' )
+context.push_op( "=" )
r = context.result
assert r == 42.3 else print r or else "-"
-context.push_op( '+' )
+context.push_op( "+" )
context.push_digit( 1 )
context.push_digit( 1 )
-context.push_op( '=' )
+context.push_op( "=" )
r = context.result
assert r == 53.3 else print r or else "-"
context.push_digit( 2 )
context.switch_to_decimals
context.push_digit( 3 )
-context.push_op( '/' )
+context.push_op( "/" )
context.push_digit( 3 )
-context.push_op( '=' )
+context.push_op( "=" )
r = context.result
assert r == 14.1 else print r or else "-"
context.push_digit( 1 )
context.push_digit( 2 )
context.push_digit( 3 )
-context.push_op( '+' )
+context.push_op( "+" )
context.push_digit( 1 )
-context.push_op( '=' )
+context.push_op( "=" )
r = context.result
assert r == 51.123 else print r or else "-"
-#test 'C' button
+#test "C" button
context = new CalculatorContext
context.push_digit( 1 )
context.push_digit( 0 )
-context.push_op( '+' )
+context.push_op( "+" )
context.push_digit( 1 )
context.push_digit( 0 )
-context.push_op( '=' )
-context.push_op( 'C' )
+context.push_op( "=" )
+context.push_op( "C" )
r = context.result
assert r == null else print r