Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix arrays of strings and NULLs #580

Merged
merged 4 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_cfg.jou
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,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 @@ -128,7 +128,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 @@ -330,7 +330,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 @@ -916,7 +916,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 @@ -888,7 +888,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 @@ -1075,6 +1075,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*)
Loading