Use proper public keyword

This commit is contained in:
2026-04-29 11:43:14 +02:00
parent e09bd72441
commit f90cad2b96
6 changed files with 54 additions and 5 deletions
+29 -4
View File
@@ -52,7 +52,7 @@ Module* parser_parse(TokenStream* ts) {
t = tokenstream_next(ts); t = tokenstream_next(ts);
bool is_public = false; bool is_public = false;
if (t.token == TOKEN_IDENTIFIER && strncmp(t.text.data, "public", t.text.length) == 0) { if (t.token == TOKEN_PUBLIC) {
is_public = true; is_public = true;
t = tokenstream_next(ts); t = tokenstream_next(ts);
} }
@@ -106,17 +106,34 @@ Module* parser_parse(TokenStream* ts) {
} }
t = tokenstream_next(ts); t = tokenstream_next(ts);
if (t.token != TOKEN_IDENTIFIER || strncmp(t.text.data, "int32", t.text.length) != 0) {
TypeExpression type;
if (t.token == TOKEN_IDENTIFIER && strncmp(t.text.data, "int32", t.text.length) == 0) {
type = (TypeExpression){ .tag = TYPE_EXPRESSION_BUILTIN, .builtin = { .bitSize = 32, .isSigned = true } };
t = tokenstream_next(ts);
if (t.token == TOKEN_BRACKET_OPEN) {
t = tokenstream_next(ts);
if (t.token != TOKEN_BRACKET_CLOSE) {
log_on_line(&t.location, t.location.column_end, "expected ']'");
parser_free(module);
return NULL;
}
TypeExpression* inner = malloc(sizeof(TypeExpression));
*inner = type;
type = (TypeExpression){ .tag = TYPE_EXPRESSION_ARRAY, .array = { .array = inner } };
t = tokenstream_next(ts);
}
} else {
log_on_line(&t.location, t.location.column_end, "expected type"); log_on_line(&t.location, t.location.column_end, "expected type");
parser_free(module); parser_free(module);
return NULL; return NULL;
} }
alias.value = (TypeExpression){ .tag = TYPE_EXPRESSION_BUILTIN, .builtin = { .bitSize = 32, .isSigned = true } };
alias.value = type;
module->aliases[module->alias_count] = alias; module->aliases[module->alias_count] = alias;
module->alias_count++; module->alias_count++;
t = tokenstream_next(ts);
if (t.token != TOKEN_SEMICOLON) { if (t.token != TOKEN_SEMICOLON) {
log_on_line(&t.location, t.location.column_end, "expected ';'"); log_on_line(&t.location, t.location.column_end, "expected ';'");
parser_free(module); parser_free(module);
@@ -128,6 +145,13 @@ Module* parser_parse(TokenStream* ts) {
return module; return module;
} }
void free_type_expression(TypeExpression* expr) {
if (expr->tag == TYPE_EXPRESSION_ARRAY) {
free_type_expression(expr->array.array);
free(expr->array.array);
}
}
void parser_free(Module* module) { void parser_free(Module* module) {
if (module == NULL) return; if (module == NULL) return;
if (module->imports != NULL) { if (module->imports != NULL) {
@@ -139,6 +163,7 @@ void parser_free(Module* module) {
if (module->aliases != NULL) { if (module->aliases != NULL) {
for(size_t i = 0; i < module->alias_count; i++) { for(size_t i = 0; i < module->alias_count; i++) {
free((void*)module->aliases[i].name); free((void*)module->aliases[i].name);
free_type_expression(&module->aliases[i].value);
} }
free(module->aliases); free(module->aliases);
} }
+3
View File
@@ -195,6 +195,7 @@ static TestCase s_tests[] = {
{"parser_imports", test_parser_imports}, {"parser_imports", test_parser_imports},
{"parser_public_imports", test_parser_public_imports}, {"parser_public_imports", test_parser_public_imports},
{"parser_alias_simple", test_parser_alias_simple}, {"parser_alias_simple", test_parser_alias_simple},
{"parser_alias_array", test_parser_alias_array},
{"log_error", test_log_error}, {"log_error", test_log_error},
{"log_on_line", test_log_on_line}, {"log_on_line", test_log_on_line},
{"log_on_line_variadic", test_log_on_line_variadic}, {"log_on_line_variadic", test_log_on_line_variadic},
@@ -222,6 +223,7 @@ int main(int argc, char** argv) {
s_currentTestName = s_tests[i].name; s_currentTestName = s_tests[i].name;
log_set_output(log_append); log_set_output(log_append);
printf("%s...", s_tests[i].name); printf("%s...", s_tests[i].name);
fflush(stdout);
s_failMsg = NULL; s_failMsg = NULL;
if (setjmp(s_testJmp) == 0) { if (setjmp(s_testJmp) == 0) {
@@ -237,6 +239,7 @@ int main(int argc, char** argv) {
printf(" [FAIL]: %s\n", s_failMsg ? s_failMsg : ""); printf(" [FAIL]: %s\n", s_failMsg ? s_failMsg : "");
failedTests[failedCount++] = s_tests[i].name; failedTests[failedCount++] = s_tests[i].name;
} }
fflush(stdout);
} }
if (s_testSource) free(s_testSource); if (s_testSource) free(s_testSource);
+17
View File
@@ -86,6 +86,7 @@ static void test_parser_public_imports(void) {
static void test_parser_alias_simple(void) { static void test_parser_alias_simple(void) {
Module* m = test_get_ast(); Module* m = test_get_ast();
assert_not_null(m, "expected module to be parsed");
assert_int(1, (int)m->alias_count, "expected correct number of aliases"); assert_int(1, (int)m->alias_count, "expected correct number of aliases");
AliasDeclaration alias = m->aliases[0]; AliasDeclaration alias = m->aliases[0];
assert_str("myalias", alias.name, "expected correct alias name"); assert_str("myalias", alias.name, "expected correct alias name");
@@ -93,3 +94,19 @@ static void test_parser_alias_simple(void) {
assert_int(32, alias.value.builtin.bitSize, "expected bitSize 32"); assert_int(32, alias.value.builtin.bitSize, "expected bitSize 32");
assert_true(alias.value.builtin.isSigned, "expected signed"); assert_true(alias.value.builtin.isSigned, "expected signed");
} }
static void test_parser_alias_array(void) {
Module* m = test_get_ast();
assert_not_null(m, "expected module to be parsed");
assert_int(1, (int)m->alias_count, "expected correct number of aliases");
AliasDeclaration alias = m->aliases[0];
assert_str("myalias", alias.name, "expected correct alias name");
assert_int(TYPE_EXPRESSION_ARRAY, alias.value.tag, "expected correct alias tag");
TypeExpression* valueType = alias.value.array.array;
assert_not_null(valueType, "expected pointer to array type");
assert_int(TYPE_EXPRESSION_BUILTIN, valueType->tag, "expected correct type tag");
assert_int(32, valueType->builtin.bitSize, "expected bitSize 32");
assert_true(valueType->builtin.isSigned, "expected signed");
}
+3
View File
@@ -0,0 +1,3 @@
module mymodule;
alias myalias = int32[];
+1 -1
View File
@@ -26,11 +26,11 @@ typedef struct {
const char* keyword; const char* keyword;
TokenType token; TokenType token;
} KeywordMap; } KeywordMap;
static const KeywordMap keywords[] = { static const KeywordMap keywords[] = {
{"module", TOKEN_MODULE}, {"module", TOKEN_MODULE},
{"import", TOKEN_IMPORT}, {"import", TOKEN_IMPORT},
{"alias", TOKEN_ALIAS}, {"alias", TOKEN_ALIAS},
{"public", TOKEN_PUBLIC},
{"void", TOKEN_VOID}, {"void", TOKEN_VOID},
}; };
+1
View File
@@ -15,6 +15,7 @@ typedef enum {
TOKEN_IMPORT, TOKEN_IMPORT,
TOKEN_SEMICOLON, TOKEN_SEMICOLON,
TOKEN_ALIAS, TOKEN_ALIAS,
TOKEN_PUBLIC,
/* Symbols */ /* Symbols */
TOKEN_PARENT_OPEN, TOKEN_PARENT_OPEN,