Refactor AST and Parser into modular subdirectories
- Split ast.h into granular headers in v0/ast/ - Split parser.c into modular implementation files in v0/parser/ - Move and rename parser tests to v0/parser/test_*.c - Update build system (include.mk) with modular sub-makefiles - Maintain v0/ast.h and v0/parser.h as umbrella headers
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
#include "internal.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
bool parse_import_declaration(Parser* p, Module* module, bool is_public) {
|
||||
module->import_count++;
|
||||
module->imports = realloc(module->imports, sizeof(ImportDeclaration) * module->import_count);
|
||||
|
||||
ImportDeclaration* import = &module->imports[module->import_count - 1];
|
||||
memset(import, 0, sizeof(ImportDeclaration));
|
||||
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, Module* module, bool is_public) {
|
||||
(void)is_public;
|
||||
module->alias_count++;
|
||||
module->aliases = realloc(module->aliases, sizeof(AliasDeclaration) * module->alias_count);
|
||||
|
||||
AliasDeclaration* alias = &module->aliases[module->alias_count - 1];
|
||||
memset(alias, 0, sizeof(AliasDeclaration));
|
||||
|
||||
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, Module* module, bool is_public, bool is_static, bool is_const) {
|
||||
module->variable_count++;
|
||||
module->variables = realloc(module->variables, sizeof(VariableDeclaration) * module->variable_count);
|
||||
|
||||
VariableDeclaration* var = &module->variables[module->variable_count - 1];
|
||||
memset(var, 0, sizeof(VariableDeclaration));
|
||||
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(Expression));
|
||||
if (!parse_expression(p, var->initializer)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (!parser_expect(p, TOKEN_SEMICOLON, "expected ';' after variable declaration")) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
Reference in New Issue
Block a user