Skip to content

Commit

Permalink
Fixed minor bugs related to array indexing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Goubermouche committed Feb 10, 2024
1 parent f3b7d6f commit f9f4af4
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
11 changes: 6 additions & 5 deletions source/compiler/compiler/type_system/semantic_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ namespace sigma {
}

switch (type.get_kind()) {
case type::VOID: return VOID_TYPE;
case type::VOID: return VOID_TYPE;
case type::BOOL:
case type::I8:
case type::U8: return I8_TYPE;
case type::U8: return I8_TYPE;
case type::I16:
case type::U16: return I16_TYPE;
case type::U16: return I16_TYPE;
case type::CHAR:
case type::I32:
case type::U32: return I32_TYPE;
case type::U32: return I32_TYPE;
case type::I64:
case type::U64: return I64_TYPE;
case type::U64: return I64_TYPE;
case type::STRUCT: return PTR_TYPE;
default: NOT_IMPLEMENTED();
}

Expand Down
28 changes: 16 additions & 12 deletions source/compiler/test/main.s
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@
// - numerical literals are always interpreted as i32

// TODO:
// - global struct declarations
// - DIAGNOSTICS:
// - better messages
// - more info related to numerical errors (hex etc)
// - add namespaces to messages, whenever applicable (ie. x::y::test)
// - BUGS:
// - structs as function parameters (rework the < op)
// - STRUCT parameters are buggy af (TODO: proper > operator for types)
// - return types are unchecked

struct key {
i32* value;
struct user {
i32* k;
i32 x;
};

i32 main() {
struct user {
key k;
};
void print(user usr) {
printf("key: %d %d %d\n", usr.k[0], usr.k[1], usr.x);
}

i32 main() {
user my_user;

my_user.k.value = cast<i32*>(malloc(sizeof(i32) * 2));
my_user.k.value[0] = 123;
my_user.k.value[1] = 321;
my_user.k = cast<i32*>(malloc(8));
my_user.k[0] = 123;
my_user.k[1] = 321;

my_user.x = 111;

printf("key: %d %d\n", my_user.k.value[0], my_user.k.value[1]);
printf("key: %d %d %d\n", my_user.k[0], my_user.k[1], my_user.x);
print(my_user);

ret 0;
}
3 changes: 2 additions & 1 deletion source/ir_translator/ir_translator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ namespace sigma {
case ast::node_type::ALIGNOF: return translate_alignof(ast_node);
case ast::node_type::SIZEOF: return translate_sizeof(ast_node);
case ast::node_type::CAST: return translate_cast(ast_node);
default: PANIC("irgen for node '{}' is not implemented", ast_node->type.to_string());
}

return nullptr;
Expand Down Expand Up @@ -381,7 +382,7 @@ namespace sigma {
handle<ir::node> base = translate_node(access_node->children[0]);

const type base_type = access_node->get<ast::type_expression>().type;
const u16 alignment = base_type.get_alignment();
const u16 alignment = base_type.dereference(1).get_alignment(); // get the alignment of the type we're accessing

// chained accesses
if (
Expand Down
8 changes: 4 additions & 4 deletions source/type_checker/type_checker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ namespace sigma {

// type check inner statements
for(const ast_node& statement : declaration->children) {
TRY(type_check_node(statement, declaration, function.signature.return_type));
TRY(type_check_node(statement, declaration));
}

TRY(m_context.semantics.verify_control_flow(declaration));
Expand Down Expand Up @@ -449,9 +449,9 @@ namespace sigma {
}

auto type_checker::implicit_type_cast(type original_type, type target_type, ast_node parent, ast_node target) const -> type_check_result {
if(original_type.is_pure_void() || target_type.is_pure_void()) {
return error::emit(error::code::INVALID_VOID, target->location);
}
// if(original_type.is_pure_void() || target_type.is_pure_void()) {
// return error::emit(error::code::INVALID_VOID, target->location);
// }

if(target_type.is_unknown()) {
return original_type;
Expand Down

0 comments on commit f9f4af4

Please sign in to comment.