2021-05-11 14:38:23 +08:00
|
|
|
|
2021-06-02 17:33:29 +08:00
|
|
|
## If statements.
|
2021-05-11 14:38:23 +08:00
|
|
|
variable = null ## Will be changed by the control flow.
|
|
|
|
unreachable = func assert(false, 'Unreachable') end
|
|
|
|
|
2021-05-22 21:27:40 +08:00
|
|
|
if true then variable = 42 else unreachable() end
|
2021-05-11 14:38:23 +08:00
|
|
|
assert(variable == 42, 'If statement failed.')
|
|
|
|
|
|
|
|
if false then unreachable()
|
2021-06-16 16:14:35 +08:00
|
|
|
elsif true then variable = null
|
2021-05-11 14:38:23 +08:00
|
|
|
else unreachable() end
|
2021-06-18 12:42:57 +08:00
|
|
|
assert(variable == null)
|
2021-05-11 14:38:23 +08:00
|
|
|
|
|
|
|
if false then unreachable()
|
2021-06-16 16:14:35 +08:00
|
|
|
elsif false then unreachable()
|
|
|
|
elsif false then unreachable()
|
2021-05-11 14:38:23 +08:00
|
|
|
else variable = "changed" end
|
2021-06-18 12:42:57 +08:00
|
|
|
assert(variable == "changed")
|
2021-05-11 14:38:23 +08:00
|
|
|
|
|
|
|
if false then unreachable()
|
2021-06-16 16:14:35 +08:00
|
|
|
elsif true
|
2021-05-11 14:38:23 +08:00
|
|
|
if false
|
|
|
|
unreachable()
|
2021-06-16 16:14:35 +08:00
|
|
|
elsif true
|
2021-05-11 14:38:23 +08:00
|
|
|
variable = 123
|
|
|
|
else
|
|
|
|
unreachable()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert(variable == 123, 'Nested if statement failed.')
|
|
|
|
|
2021-06-02 17:33:29 +08:00
|
|
|
## While statements
|
|
|
|
|
|
|
|
while true
|
|
|
|
variable = 1212
|
|
|
|
if true then break end
|
|
|
|
end
|
|
|
|
assert(variable == 1212)
|
|
|
|
|
|
|
|
while true
|
|
|
|
variable = 22
|
2021-06-16 16:14:35 +08:00
|
|
|
if true then elsif false then end
|
2021-06-02 17:33:29 +08:00
|
|
|
if false
|
2021-06-16 16:14:35 +08:00
|
|
|
elsif true
|
2021-06-02 17:33:29 +08:00
|
|
|
variable += 2300
|
|
|
|
end
|
|
|
|
variable += 1
|
|
|
|
break
|
|
|
|
end
|
|
|
|
assert(variable == 2323)
|
2021-06-12 14:56:09 +08:00
|
|
|
|
|
|
|
map = { 'a':10, 'b':12, 'c':32 }; sum = 0
|
|
|
|
for k in map
|
|
|
|
sum += map[k]
|
|
|
|
end
|
|
|
|
assert(sum == 54)
|
|
|
|
|
|
|
|
|
2021-06-16 02:54:30 +08:00
|
|
|
# If we got here, that means all test were passed.
|
|
|
|
print('All TESTS PASSED')
|