compound assignment
This commit is contained in:
@@ -23,8 +23,8 @@ main :: func() void {
|
||||
_ = native.configured_value(9)
|
||||
_ = native.IMPORTED_SHADOW_OBJECT
|
||||
_ = native.IMPORTED_SHADOW_FUNCTION
|
||||
native.imported_global = native.IMPORTED_MAGIC
|
||||
native.imported_record_global.value = native.IMPORTED_MAGIC
|
||||
native.imported_global += native.IMPORTED_MAGIC
|
||||
native.imported_record_global.value += 2
|
||||
color native.Imported_Color :: native.IMPORTED_COLOR
|
||||
_ = native.imported_check_state(
|
||||
color,
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
# 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
|
||||
}
|
||||
Reference in New Issue
Block a user