Skip to content

Commit eac3333

Browse files
authored
Add - (minus) operator for arrays (#1376)
1 parent d71dadd commit eac3333

5 files changed

Lines changed: 50 additions & 29 deletions

File tree

compiler/ast_to_builder.jou

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,13 @@ class AstToBuilder:
280280
return self.builder.dereference(out_ptr)
281281

282282
def build_binop(self, op: AstExpressionKind, lhs: BuilderValue, rhs: BuilderValue) -> BuilderValue:
283+
if (
284+
(op == AstExpressionKind.Add or op == AstExpressionKind.Sub)
285+
and lhs.type == rhs.type
286+
and lhs.type.kind == TypeKind.Array
287+
):
288+
return self.build_array_binop(op, lhs, rhs)
289+
283290
match op:
284291
case AstExpressionKind.Eq:
285292
return self.builder.eq(lhs, rhs)
@@ -294,13 +301,8 @@ class AstToBuilder:
294301
case AstExpressionKind.Ge:
295302
return self.builder.not_(self.builder.lt(lhs, rhs))
296303
case AstExpressionKind.Add:
297-
assert lhs.type == rhs.type
298-
match lhs.type.kind:
299-
case TypeKind.Array:
300-
return self.build_array_binop(op, lhs, rhs)
301-
case _:
302-
# TODO: this should probably be split up to separate int vs floating adds
303-
return self.builder.add(lhs, rhs)
304+
# TODO: These should probably be split up to separate e.g. int vs floating adds.
305+
return self.builder.add(lhs, rhs)
304306
case AstExpressionKind.Sub:
305307
return self.builder.sub(lhs, rhs)
306308
case AstExpressionKind.Mul:

compiler/typecheck/step3_function_and_method_bodies.jou

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ def check_binop(
500500
or (got_enums and not equals_compare)
501501
or (got_pointers and not some_kind_of_compare)
502502
or (need_integers and not got_integers)
503-
or (got_number_arrays and op != AstExpressionKind.Add)
503+
or (got_number_arrays and op != AstExpressionKind.Add and op != AstExpressionKind.Sub)
504504
):
505505
snprintf(msg, sizeof(msg), "wrong types: cannot %s %s and %s", do_what, ltype.name, rtype.name)
506506
fail(location, msg)

tests/should_succeed/array_add.jou

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import "stdlib/io.jou"
2+
3+
def test_add() -> None:
4+
array = [1, 2] + [3, 4]
5+
printf("%d %d\n", array[0], array[1]) # Output: 4 6
6+
7+
array += [1, 1]
8+
printf("%d %d\n", array[0], array[1]) # Output: 5 7
9+
10+
with_offset = array + [0.5, 0.5]
11+
printf("%f %f\n", with_offset[0], with_offset[1]) # Output: 5.500000 7.500000
12+
13+
with_float_offset = array + [0.5 as float, 0.5 as float]
14+
printf("%f %f\n", with_float_offset[0], with_float_offset[1]) # Output: 5.500000 7.500000
15+
16+
# Output: Sizes: 16 and 8
17+
printf("Sizes: %d and %d\n", sizeof(with_offset), sizeof(with_float_offset))
18+
19+
20+
def test_sub() -> None:
21+
array = [1, 2] - [3, 5]
22+
printf("%d %d\n", array[0], array[1]) # Output: -2 -3
23+
24+
array -= [1, 1]
25+
printf("%d %d\n", array[0], array[1]) # Output: -3 -4
26+
27+
with_offset = array - [0.5, 0.5]
28+
printf("%f %f\n", with_offset[0], with_offset[1]) # Output: -3.500000 -4.500000
29+
30+
with_float_offset = array - [0.5 as float, 0.5 as float]
31+
printf("%f %f\n", with_float_offset[0], with_float_offset[1]) # Output: -3.500000 -4.500000
32+
33+
# Output: Sizes: 16 and 8
34+
printf("Sizes: %d and %d\n", sizeof(with_offset), sizeof(with_float_offset))
35+
36+
37+
def main() -> int:
38+
test_add()
39+
test_sub()
40+
return 0

tests/wrong_type/array_sub.jou

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)