mirror of
https://github.com/zekexiao/pocketlang.git
synced 2025-03-04 13:15:55 +08:00

The primary purpose of this change is to disambiguate between `else if` and `else \n if`. Even though it's a breaking change, since it's at the very early state we're allowed to make such breaking syntax change. lang.write function moved to io and io.stdin, io.stdout, io.stderr added for future streamed io operations io.File read, write, open, close, getline, seek, tell implemented str * int multiplication implemented
76 lines
1.3 KiB
Plaintext
76 lines
1.3 KiB
Plaintext
|
|
## If statements.
|
|
variable = null ## Will be changed by the control flow.
|
|
unreachable = fn assert(false, 'Unreachable') end
|
|
|
|
if true then variable = 42 else unreachable() end
|
|
assert(variable == 42, 'If statement failed.')
|
|
|
|
if false then unreachable()
|
|
elif true then variable = null
|
|
else unreachable() end
|
|
assert(variable == null)
|
|
|
|
if false then unreachable()
|
|
elif false then unreachable()
|
|
elif false then unreachable()
|
|
else variable = "changed" end
|
|
assert(variable == "changed")
|
|
|
|
if false then unreachable()
|
|
elif true
|
|
if false
|
|
unreachable()
|
|
elif 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 elif false then end
|
|
if false
|
|
elif 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)
|
|
|
|
val = 2
|
|
if val == 1 then val = null
|
|
elif val == 2 then val = 'foo'
|
|
else val = null
|
|
end
|
|
assert(val == 'foo')
|
|
|
|
val = 2
|
|
if val == 1 then val = null
|
|
else
|
|
if val == 2 then val = 'bar'
|
|
end ##< Need an extra end since 'elif' != 'else \n if'.
|
|
end
|
|
assert(val == 'bar')
|
|
|
|
|
|
# If we got here, that means all test were passed.
|
|
print('All TESTS PASSED')
|