46 lines
832 B
Plaintext
46 lines
832 B
Plaintext
# Milestone 6: compound assignment (`+=`, `-=`, `*=`, `/=`) and the binary
|
|
# arithmetic operators `-`, `*`, `/` with multiplicative precedence.
|
|
|
|
check_float :: func() i32 {
|
|
x f64 = 10.0
|
|
x /= 4.0 # 2.5
|
|
x *= 2.0 # 5.0
|
|
x -= 1.0 # 4.0
|
|
x += 0.5 # 4.5
|
|
if x > 4.0 {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
check_unsigned :: func() i32 {
|
|
n u32 = 100
|
|
n /= 7 # 14 (truncating integer division)
|
|
n -= 4 # 10
|
|
if n == 10 {
|
|
return 1
|
|
}
|
|
return 0
|
|
}
|
|
|
|
main :: func() i32 {
|
|
total i32 = 0
|
|
total += 10 # 10
|
|
total -= 3 # 7
|
|
total *= 4 # 28
|
|
total /= 2 # 14
|
|
|
|
# binary operators honour precedence: 14 + (2 * 3) - 4 == 16
|
|
total = total + 2 * 3 - 4
|
|
|
|
# compound assignment as a while-loop update
|
|
i i32 = 0
|
|
while i < 5 : i += 1 {
|
|
total += 1 # +5 => 21
|
|
}
|
|
|
|
total += check_float() # +1 => 22
|
|
total += check_unsigned() # +1 => 23
|
|
return total
|
|
}
|