Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into cf-tweak
Browse files Browse the repository at this point in the history
  • Loading branch information
Akuli committed Jan 10, 2025
2 parents a7eb750 + 8f95d52 commit a9faf0f
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 33 deletions.
10 changes: 10 additions & 0 deletions bootstrap_compiler/typecheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,16 @@ static const Type *cast_array_members_to_a_common_type(const FunctionOrMethodTyp
Append(&compatible_with_all, *t);
}

if (compatible_with_all.len > 1) {
// Remove void* if exists, so that type of ["hello", NULL] becomes byte*[2]
for (int i = 0; i < compatible_with_all.len; i++) {
if (compatible_with_all.ptr[i] == voidPtrType) {
compatible_with_all.ptr[i] = compatible_with_all.ptr[--compatible_with_all.len];
break;
}
}
}

if (compatible_with_all.len != 1) {
List(char) namestr = {0};
for (const Type **t = distinct.ptr; t < End(distinct); t++) {
Expand Down
8 changes: 4 additions & 4 deletions compiler/build_cf_graph.jou
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def add_unary_op(
target: LocalVariable*,
) -> None:
ins = CfInstruction{location = location, kind = op, destvar = target}
operands = [arg, NULL as LocalVariable*]
operands = [arg, NULL]
ins.set_operands(operands)
add_instruction(st, ins)

Expand All @@ -129,7 +129,7 @@ def add_binary_op(
target: LocalVariable*,
) -> None:
ins = CfInstruction{location = location, kind = op, destvar = target}
operands = [lhs, rhs, NULL as LocalVariable*]
operands = [lhs, rhs, NULL]
ins.set_operands(operands)
add_instruction(st, ins)

Expand Down Expand Up @@ -331,7 +331,7 @@ def build_class_field_pointer(
assert sizeof(ins.fieldname) == sizeof(f->name)
strcpy(ins.fieldname, f->name)

operands = [instance, NULL as LocalVariable*]
operands = [instance, NULL]
ins.set_operands(operands)

add_instruction(st, ins)
Expand Down Expand Up @@ -917,7 +917,7 @@ def build_assert(st: State*, assert_location: Location, assertion: AstAssertion*
add_local_var(st, argtypes[0]),
add_local_var(st, argtypes[1]),
add_local_var(st, argtypes[2]),
NULL as LocalVariable*,
NULL,
]

add_constant(st, assert_location, Constant{kind = ConstantKind::String, str = assertion->condition_str}, args[0])
Expand Down
9 changes: 8 additions & 1 deletion compiler/typecheck.jou
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ global nth_result_buffer: byte[100]
def nth(n: int) -> byte*:
assert n >= 1

first_few = [NULL as byte*, "first", "second", "third", "fourth", "fifth", "sixth"]
first_few = [NULL, "first", "second", "third", "fourth", "fifth", "sixth"]
if n < sizeof(first_few) / sizeof(first_few[0]):
return first_few[n]

Expand Down Expand Up @@ -1067,6 +1067,13 @@ def cast_array_members_to_a_common_type(fom: FunctionOrMethodTypes*, error_locat
assert compatible_with_all != NULL
compatible_with_all[n_compatible_with_all++] = *t

if n_compatible_with_all > 1:
# Remove void* if exists, so that type of ["hello", NULL] becomes byte*[2]
for i = 0; i < n_compatible_with_all; i++:
if compatible_with_all[i] == voidPtrType:
compatible_with_all[i] = compatible_with_all[--n_compatible_with_all]
break

if n_compatible_with_all != 1:
size = 500L
for t = distinct; t < &distinct[ndistinct]; t++:
Expand Down
42 changes: 21 additions & 21 deletions examples/aoc2024/day21/part2.jou
Original file line number Diff line number Diff line change
Expand Up @@ -32,35 +32,35 @@ def init_tables() -> None:

# ^ A
# < v >
arrow_keypad_table['^']['^'] = ["A", NULL as byte*]
arrow_keypad_table['^']['A'] = [">A", NULL as byte*]
arrow_keypad_table['^']['<'] = ["v<A", NULL as byte*]
arrow_keypad_table['^']['v'] = ["vA", NULL as byte*]
arrow_keypad_table['^']['^'] = ["A", NULL]
arrow_keypad_table['^']['A'] = [">A", NULL]
arrow_keypad_table['^']['<'] = ["v<A", NULL]
arrow_keypad_table['^']['v'] = ["vA", NULL]
arrow_keypad_table['^']['>'] = ["v>A", ">vA"]

arrow_keypad_table['A']['^'] = ["<A", NULL as byte*]
arrow_keypad_table['A']['A'] = ["A", NULL as byte*]
arrow_keypad_table['A']['<'] = ["v<<A", NULL as byte*]
arrow_keypad_table['A']['^'] = ["<A", NULL]
arrow_keypad_table['A']['A'] = ["A", NULL]
arrow_keypad_table['A']['<'] = ["v<<A", NULL]
arrow_keypad_table['A']['v'] = ["v<A", "<vA"]
arrow_keypad_table['A']['>'] = ["vA", NULL as byte*]
arrow_keypad_table['A']['>'] = ["vA", NULL]

arrow_keypad_table['<']['^'] = [">^A", NULL as byte*]
arrow_keypad_table['<']['A'] = [">>^A", NULL as byte*]
arrow_keypad_table['<']['<'] = ["A", NULL as byte*]
arrow_keypad_table['<']['v'] = [">A", NULL as byte*]
arrow_keypad_table['<']['>'] = [">>A", NULL as byte*]
arrow_keypad_table['<']['^'] = [">^A", NULL]
arrow_keypad_table['<']['A'] = [">>^A", NULL]
arrow_keypad_table['<']['<'] = ["A", NULL]
arrow_keypad_table['<']['v'] = [">A", NULL]
arrow_keypad_table['<']['>'] = [">>A", NULL]

arrow_keypad_table['v']['^'] = ["^A", NULL as byte*]
arrow_keypad_table['v']['^'] = ["^A", NULL]
arrow_keypad_table['v']['A'] = [">^A", "^>A"]
arrow_keypad_table['v']['<'] = ["<A", NULL as byte*]
arrow_keypad_table['v']['v'] = ["A", NULL as byte*]
arrow_keypad_table['v']['>'] = [">A", NULL as byte*]
arrow_keypad_table['v']['<'] = ["<A", NULL]
arrow_keypad_table['v']['v'] = ["A", NULL]
arrow_keypad_table['v']['>'] = [">A", NULL]

arrow_keypad_table['>']['^'] = ["<^A", "^<A"]
arrow_keypad_table['>']['A'] = ["^A", NULL as byte*]
arrow_keypad_table['>']['<'] = ["<<A", NULL as byte*]
arrow_keypad_table['>']['v'] = ["<A", NULL as byte*]
arrow_keypad_table['>']['>'] = ["A", NULL as byte*]
arrow_keypad_table['>']['A'] = ["^A", NULL]
arrow_keypad_table['>']['<'] = ["<<A", NULL]
arrow_keypad_table['>']['v'] = ["<A", NULL]
arrow_keypad_table['>']['>'] = ["A", NULL]

# 7 8 9
# 4 5 6
Expand Down
9 changes: 9 additions & 0 deletions tests/should_succeed/array.jou
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,13 @@ def main() -> int:
increment(foo as int*) # same cast explicitly
printf("%d %d %d\n", foo[0], foo[1], foo[2]) # Output: 7 5 6

# corner case: byte* <--> void* can be converted both ways, use byte*
strings = ["hello", NULL, "world", NULL]
printf("%s %s\n", strings[0], strings[2]) # Output: hello world
# Output: strings[1] is NULL
# Output: strings[3] is NULL
for i = 0; i < 4; i++:
if strings[i] == NULL:
printf("strings[%d] is NULL\n", i)

return 0
8 changes: 1 addition & 7 deletions tests/wrong_type/array_mixed_types_ptr.jou
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
def foo() -> None:
# When we make an array of int* and void*:
# - we could cast int* to void*, and get an array of void*
# - we could cast void* to int*, and get an array of int*
#
# Because the compiler cannot be sure what you want, it refuses to
# guess and instead errors.
a = 1
x = [&a, &a as void*] # Error: array items have different types (int*, void*)
x = [&a, "hello"] # Error: array items have different types (int*, byte*)

0 comments on commit a9faf0f

Please sign in to comment.