mirror of
https://github.com/zekexiao/pocketlang.git
synced 2025-02-06 04:37:47 +08:00
61 lines
1.1 KiB
Plaintext
61 lines
1.1 KiB
Plaintext
|
|
## If statements.
|
|
variable = null ## Will be changed by the control flow.
|
|
unreachable = func assert(false, 'Unreachable') end
|
|
|
|
if true then variable = 42 else unreachable() end
|
|
assert(variable == 42, 'If statement failed.')
|
|
|
|
if false then unreachable()
|
|
elsif true then variable = null
|
|
else unreachable() end
|
|
assert(variable == null)
|
|
|
|
if false then unreachable()
|
|
elsif false then unreachable()
|
|
elsif false then unreachable()
|
|
else variable = "changed" end
|
|
assert(variable == "changed")
|
|
|
|
if false then unreachable()
|
|
elsif true
|
|
if false
|
|
unreachable()
|
|
elsif true
|
|
variable = 123
|
|
else
|
|
unreachable()
|
|
end
|
|
end
|
|
assert(variable == 123, 'Nested if statement failed.')
|
|
|
|
## While statements
|
|
|
|
while true
|
|
variable = 1212
|
|
if true then break end
|
|
end
|
|
assert(variable == 1212)
|
|
|
|
while true
|
|
variable = 22
|
|
if true then elsif false then end
|
|
if false
|
|
elsif true
|
|
variable += 2300
|
|
end
|
|
variable += 1
|
|
break
|
|
end
|
|
assert(variable == 2323)
|
|
|
|
map = { 'a':10, 'b':12, 'c':32 }; sum = 0
|
|
for k in map
|
|
sum += map[k]
|
|
end
|
|
assert(sum == 54)
|
|
|
|
|
|
# If we got here, that means all test were passed.
|
|
print('All TESTS PASSED')
|