Skip to content

Commit f3b7d6f

Browse files
committed
Added support for global struct declarations.
1 parent 5495495 commit f3b7d6f

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

source/compiler/test/main.s

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@
66
// - numerical literals are always interpreted as i32
77

88
// TODO:
9+
// - global struct declarations
910
// - DIAGNOSTICS:
1011
// - better messages
1112
// - more info related to numerical errors (hex etc)
1213
// - add namespaces to messages, whenever applicable (ie. x::y::test)
1314
// - BUGS:
1415
// - structs as function parameters (rework the < op)
1516

16-
i32 main() {
17-
struct key {
18-
i32* value;
19-
};
17+
struct key {
18+
i32* value;
19+
};
2020

21+
i32 main() {
2122
struct user {
2223
key k;
2324
};
@@ -29,7 +30,6 @@ i32 main() {
2930
my_user.k.value[1] = 321;
3031
3132
printf("key: %d %d\n", my_user.k.value[0], my_user.k.value[1]);
32-
printf("%d\n", sizeof(user));
3333

3434
ret 0;
3535
}

source/parser/parser.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ namespace sigma {
4444
// parse nested namespaces
4545
TRY(result, parse_namespace_declaration());
4646
}
47+
else if(m_tokens.get_current_token() == token_type::STRUCT) {
48+
// parse global struct declarations
49+
TRY(result, parse_struct_declaration());
50+
EXPECT_CURRENT_TOKEN(token_type::SEMICOLON);
51+
m_tokens.next();
52+
}
4753
else if (peek_is_function_definition()) {
4854
// parse globally declared functions
4955
TRY(result, parse_function_declaration());

tests/structs/global_declaration.s

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
struct key {
2+
i32* value;
3+
};
4+
5+
i32 main() {
6+
struct user {
7+
key k;
8+
};
9+
10+
user my_user;
11+
12+
my_user.k.value = cast<i32*>(malloc(sizeof(i32) * 2));
13+
my_user.k.value[0] = 123;
14+
my_user.k.value[1] = 321;
15+
16+
printf("key: %d %d\n", my_user.k.value[0], my_user.k.value[1]);
17+
18+
ret 0;
19+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
key: 123 321

0 commit comments

Comments
 (0)