88 lines
2.5 KiB
C
88 lines
2.5 KiB
C
#include "internal.h"
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
bool parse_import_declaration(Parser* p, ModuleTree* module, bool is_public) {
|
|
module->import_count++;
|
|
module->imports = realloc(module->imports, sizeof(ImportTree) * module->import_count);
|
|
|
|
ImportTree* import = &module->imports[module->import_count - 1];
|
|
memset(import, 0, sizeof(ImportTree));
|
|
import->is_public = is_public;
|
|
|
|
if (!parser_require(p, TOKEN_IDENTIFIER, "expected module identifier")) {
|
|
return false;
|
|
}
|
|
|
|
import->module_name = parser_to_text(p);
|
|
|
|
if (!parser_expect(p, TOKEN_SEMICOLON, "expected ';' after import")) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool parse_alias_declaration(Parser* p, ModuleTree* module, bool is_public) {
|
|
(void)is_public;
|
|
module->alias_count++;
|
|
module->aliases = realloc(module->aliases, sizeof(AliasTree) * module->alias_count);
|
|
|
|
AliasTree* alias = &module->aliases[module->alias_count - 1];
|
|
memset(alias, 0, sizeof(AliasTree));
|
|
|
|
if (!parser_require(p, TOKEN_IDENTIFIER, "expected alias identifier")) {
|
|
return false;
|
|
}
|
|
alias->name = parser_to_text(p);
|
|
|
|
if (!parser_expect(p, TOKEN_ASSIGN, "expected '=' after alias name")) {
|
|
return false;
|
|
}
|
|
|
|
if (!parse_type_expression(p, &alias->value)) {
|
|
return false;
|
|
}
|
|
|
|
if (!parser_expect(p, TOKEN_SEMICOLON, "expected ';' after alias declaration")) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
bool parse_variable_declaration(Parser* p, ModuleTree* module, bool is_public, bool is_static, bool is_const) {
|
|
module->variable_count++;
|
|
module->variables = realloc(module->variables, sizeof(VariableTree) * module->variable_count);
|
|
|
|
VariableTree* var = &module->variables[module->variable_count - 1];
|
|
memset(var, 0, sizeof(VariableTree));
|
|
var->is_public = is_public;
|
|
var->is_static = is_static;
|
|
var->is_const = is_const;
|
|
|
|
if (parser_accept_primitive(p)) {
|
|
if (!parse_type_expression(p, &var->type)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (!parser_require(p, TOKEN_IDENTIFIER, "expected variable identifier")) {
|
|
return false;
|
|
}
|
|
var->name = parser_to_text(p);
|
|
|
|
if (parser_accept(p, TOKEN_ASSIGN)) {
|
|
var->initializer = malloc(sizeof(ExpressionTree));
|
|
if (!parse_expression(p, var->initializer)) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (!parser_expect(p, TOKEN_SEMICOLON, "expected ';' after variable declaration")) {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|