main()
{
SimpleLoops()
Ifs(123)
LoopsWithExits()
}
SimpleLoops()
{
first_array = [9, 8, 7]
test1 := []
for v in first_array {
test1.Push(v)
}
assert(test1 == first_array)
for v in first_array index i {
assert(v == 9-i)
}
test2 := []
for i to 3 {
test2.Push(i)
}
assert(test2 == [0, 1, 2])
test3 := []
for i to 10 inc 3 {
test3.Push(i)
}
assert(test3 == [0, 3, 6, 9])
sum := 0
for i mut to 10
{
i .= 9
sum += i;
}
assert(sum == 9)
}
Ifs(int param_val)
{
result := 0
if param_val != 0 { result .= 1 }
if 1 { result |= 2 }
if 0 { result |= 4 }
assert(result == 3)
if 0 {
}
else if 1
result |= 8
else
return
assert(if(param_val == 123, 987, 456) == 987)
result2 := 0
test1 := 3; if 1 || ChangeValue(0, test1 mut) { result2 |= 0x01 } else { result2 |= 0x02 }
assert( result2 == 0x01); assert(test1 == 3)
test2 := 3; if 0 || ChangeValue(0, test2 mut) { result2 |= 0x04 } else { result2 |= 0x08 }
assert( result2 == 0x09); assert(test2 == 0)
test3 := 3; if 1 && ChangeValue(1, test3 mut) { result2 |= 0x10 } else { result2 |= 0x20 }
assert( result2 == 0x19); assert(test3 == 1)
test4 := 3; if 0 && ChangeValue(0, test4 mut) { result2 |= 0x40 } else { result2 |= 0x80 }
assert( result2 == 0x99); assert(test4 == 3)
}
ChangeValue(bool b, int v mut) bool { v .= 0 + b; return b }
LoopsWithExits()
{
result := 0
for i to 5 { }
_OnBreak { result |= 1 }
_OnComplete { result |= 2 }
assert(result == 2)
index := 0
for v in [1, 2, 3] index i { if v == 3 break }
_OnBreak { result |= 4; index .= i }
_OnComplete { result |= 8 }
assert(index == 2)
assert(result == 6)
whileCounter := 2
while whileCounter-- > 0 { }
_OnBreak { result |= 0x10 }
_OnComplete { result |= 0x20 }
assert(result == 0x26)
for v1 in [3, 4, 8] {
for v2 in [5, 9, 10] {
if v1 * v2 == 20 {
break
}
} _OnBreak {
break
}
}
_OnBreak { result |= 0x1000 }
assert(result == 0x1026)
for v in [] {
break
}
_OnEmpty { result |= 0x20000 }
_OnBreak { result |= 0x40000 }
_OnComplete { result |= 0x80000 }
assert(result == 0xA1026)
}