Rename AST structures to Tree and relocate freeing logic
This commit is contained in:
@@ -8,4 +8,14 @@
|
||||
#include "ast/declaration.h"
|
||||
#include "ast/module.h"
|
||||
|
||||
/**
|
||||
* Frees a module and all its children.
|
||||
*/
|
||||
void ast_free_module(ModuleTree* module);
|
||||
|
||||
/**
|
||||
* Frees a type expression.
|
||||
*/
|
||||
void ast_free_type(TypeTree* type);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -10,7 +10,7 @@ typedef struct {
|
||||
|
||||
/** @brief Whether the import is public or not. */
|
||||
bool is_public;
|
||||
} ImportDeclaration;
|
||||
} ImportTree;
|
||||
|
||||
/**
|
||||
* A declaration that aliases one type to another.
|
||||
@@ -20,8 +20,8 @@ typedef struct {
|
||||
const char* name;
|
||||
|
||||
/** @brief The value of the alias. */
|
||||
TypeExpression value;
|
||||
} AliasDeclaration;
|
||||
TypeTree value;
|
||||
} AliasTree;
|
||||
|
||||
/**
|
||||
* A declaration of a variable, which may be a constant or not, and may be static or not.
|
||||
@@ -31,10 +31,10 @@ typedef struct {
|
||||
char* name;
|
||||
|
||||
/** @brief The type of the variable. */
|
||||
TypeExpression type;
|
||||
TypeTree type;
|
||||
|
||||
/** @brief The optional initializer expression. */
|
||||
Expression* initializer;
|
||||
ExpressionTree* initializer;
|
||||
|
||||
/** @brief Whether the variable is public or not. */
|
||||
bool is_public;
|
||||
@@ -44,6 +44,6 @@ typedef struct {
|
||||
|
||||
/** @brief Whether the variable is a constant or not. */
|
||||
bool is_const;
|
||||
} VariableDeclaration;
|
||||
} VariableTree;
|
||||
|
||||
#endif
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "expression.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void ast_free_type(TypeTree* expr) {
|
||||
if (expr->tag == TYPE_TREE_ARRAY) {
|
||||
ast_free_type(expr->array.array);
|
||||
free(expr->array.array);
|
||||
}
|
||||
}
|
||||
+13
-13
@@ -4,37 +4,37 @@
|
||||
#include "../bool.h"
|
||||
|
||||
typedef enum {
|
||||
EXPRESSION_INTEGER,
|
||||
EXPRESSION_STRING,
|
||||
EXPRESSION_BOOLEAN
|
||||
} ExpressionTag;
|
||||
EXPRESSION_TREE_INTEGER,
|
||||
EXPRESSION_TREE_STRING,
|
||||
EXPRESSION_TREE_BOOLEAN
|
||||
} ExpressionTreeTag;
|
||||
|
||||
typedef struct {
|
||||
ExpressionTag tag;
|
||||
ExpressionTreeTag tag;
|
||||
union {
|
||||
int integer;
|
||||
const char* string;
|
||||
bool boolean;
|
||||
};
|
||||
} Expression;
|
||||
} ExpressionTree;
|
||||
|
||||
typedef enum {
|
||||
TYPE_EXPRESSION_BUILTIN,
|
||||
TYPE_EXPRESSION_ARRAY
|
||||
} TypeExpressionTag;
|
||||
TYPE_TREE_BUILTIN,
|
||||
TYPE_TREE_ARRAY
|
||||
} TypeTreeTag;
|
||||
|
||||
/**
|
||||
* An expression that evaluates to a type.
|
||||
*/
|
||||
typedef struct TypeExpression TypeExpression;
|
||||
struct TypeExpression {
|
||||
typedef struct TypeTree TypeTree;
|
||||
struct TypeTree {
|
||||
/** @brief defines which entry in the union is valid */
|
||||
TypeExpressionTag tag;
|
||||
TypeTreeTag tag;
|
||||
union {
|
||||
/** @brief Evaluates to an array of the given type. */
|
||||
struct {
|
||||
/** @brief A pointer to the type of the elements stored in the array. */
|
||||
TypeExpression* array;
|
||||
TypeTree* array;
|
||||
} array;
|
||||
/** @brief Evaluates to a builtin integer type.*/
|
||||
struct {
|
||||
|
||||
+1
-1
@@ -1,3 +1,3 @@
|
||||
# There are currently no .c files in the ast directory.
|
||||
# This file is provided for future consistency.
|
||||
AST_SRC :=
|
||||
AST_SRC := v0/ast/module.c v0/ast/expression.c
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
#include "module.h"
|
||||
#include "expression.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
void ast_free_type(TypeTree* type);
|
||||
|
||||
void ast_free_module(ModuleTree* module) {
|
||||
if (module == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (module->imports != NULL) {
|
||||
for(size_t i = 0; i < module->import_count; i++) {
|
||||
free(module->imports[i].module_name);
|
||||
}
|
||||
free(module->imports);
|
||||
}
|
||||
|
||||
if (module->aliases != NULL) {
|
||||
for(size_t i = 0; i < module->alias_count; i++) {
|
||||
free((void*)module->aliases[i].name);
|
||||
ast_free_type(&module->aliases[i].value);
|
||||
}
|
||||
free(module->aliases);
|
||||
}
|
||||
|
||||
if (module->variables != NULL) {
|
||||
for(size_t i = 0; i < module->variable_count; i++) {
|
||||
free(module->variables[i].name);
|
||||
ast_free_type(&module->variables[i].type);
|
||||
if (module->variables[i].initializer) {
|
||||
if (module->variables[i].initializer->tag == EXPRESSION_TREE_STRING) {
|
||||
free((void*)module->variables[i].initializer->string);
|
||||
}
|
||||
free(module->variables[i].initializer);
|
||||
}
|
||||
}
|
||||
free(module->variables);
|
||||
}
|
||||
|
||||
free(module->name);
|
||||
free(module);
|
||||
}
|
||||
+4
-4
@@ -13,22 +13,22 @@ typedef struct {
|
||||
char* name;
|
||||
|
||||
/** @brief The list of imports in the module. */
|
||||
ImportDeclaration* imports;
|
||||
ImportTree* imports;
|
||||
|
||||
/** @brief The number of imports in the module. */
|
||||
size_t import_count;
|
||||
|
||||
/** @brief The list of aliases in the module. */
|
||||
AliasDeclaration* aliases;
|
||||
AliasTree* aliases;
|
||||
|
||||
/** @brief The number of aliases in the module. */
|
||||
size_t alias_count;
|
||||
|
||||
/** @brief The list of variables in the module. */
|
||||
VariableDeclaration* variables;
|
||||
VariableTree* variables;
|
||||
|
||||
/** @brief The number of variables in the module. */
|
||||
size_t variable_count;
|
||||
} Module;
|
||||
} ModuleTree;
|
||||
|
||||
#endif
|
||||
|
||||
+1
-8
@@ -10,13 +10,6 @@
|
||||
* @param ts The TokenStream to read.
|
||||
* @returns The parsed module.
|
||||
*/
|
||||
Module* parser_parse(TokenStream* ts);
|
||||
|
||||
/**
|
||||
* Frees the parsed AST.
|
||||
*
|
||||
* @param module The AST return by parser_parse.
|
||||
*/
|
||||
void parser_free(Module* module);
|
||||
ModuleTree* parser_parse(TokenStream* ts);
|
||||
|
||||
#endif
|
||||
|
||||
+13
-13
@@ -2,12 +2,12 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
bool parse_import_declaration(Parser* p, Module* module, bool is_public) {
|
||||
bool parse_import_declaration(Parser* p, ModuleTree* module, bool is_public) {
|
||||
module->import_count++;
|
||||
module->imports = realloc(module->imports, sizeof(ImportDeclaration) * module->import_count);
|
||||
module->imports = realloc(module->imports, sizeof(ImportTree) * module->import_count);
|
||||
|
||||
ImportDeclaration* import = &module->imports[module->import_count - 1];
|
||||
memset(import, 0, sizeof(ImportDeclaration));
|
||||
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")) {
|
||||
@@ -23,13 +23,13 @@ bool parse_import_declaration(Parser* p, Module* module, bool is_public) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool parse_alias_declaration(Parser* p, Module* module, bool is_public) {
|
||||
bool parse_alias_declaration(Parser* p, ModuleTree* module, bool is_public) {
|
||||
(void)is_public;
|
||||
module->alias_count++;
|
||||
module->aliases = realloc(module->aliases, sizeof(AliasDeclaration) * module->alias_count);
|
||||
module->aliases = realloc(module->aliases, sizeof(AliasTree) * module->alias_count);
|
||||
|
||||
AliasDeclaration* alias = &module->aliases[module->alias_count - 1];
|
||||
memset(alias, 0, sizeof(AliasDeclaration));
|
||||
AliasTree* alias = &module->aliases[module->alias_count - 1];
|
||||
memset(alias, 0, sizeof(AliasTree));
|
||||
|
||||
if (!parser_require(p, TOKEN_IDENTIFIER, "expected alias identifier")) {
|
||||
return false;
|
||||
@@ -51,12 +51,12 @@ bool parse_alias_declaration(Parser* p, Module* module, bool is_public) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool parse_variable_declaration(Parser* p, Module* module, bool is_public, bool is_static, bool is_const) {
|
||||
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(VariableDeclaration) * module->variable_count);
|
||||
module->variables = realloc(module->variables, sizeof(VariableTree) * module->variable_count);
|
||||
|
||||
VariableDeclaration* var = &module->variables[module->variable_count - 1];
|
||||
memset(var, 0, sizeof(VariableDeclaration));
|
||||
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;
|
||||
@@ -73,7 +73,7 @@ bool parse_variable_declaration(Parser* p, Module* module, bool is_public, bool
|
||||
var->name = parser_to_text(p);
|
||||
|
||||
if (parser_accept(p, TOKEN_ASSIGN)) {
|
||||
var->initializer = malloc(sizeof(Expression));
|
||||
var->initializer = malloc(sizeof(ExpressionTree));
|
||||
if (!parse_expression(p, var->initializer)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
+19
-26
@@ -2,44 +2,44 @@
|
||||
#include "../log.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
bool parse_primitive_type_expression(Parser* p, TypeExpression* expr) {
|
||||
bool parse_primitive_type_expression(Parser* p, TypeTree* expr) {
|
||||
if (parser_accept(p, TOKEN_U8)) {
|
||||
expr->tag = TYPE_EXPRESSION_BUILTIN;
|
||||
expr->tag = TYPE_TREE_BUILTIN;
|
||||
expr->builtin.bitSize = 8;
|
||||
expr->builtin.isSigned = false;
|
||||
return true;
|
||||
} else if (parser_accept(p, TOKEN_U16)) {
|
||||
expr->tag = TYPE_EXPRESSION_BUILTIN;
|
||||
expr->tag = TYPE_TREE_BUILTIN;
|
||||
expr->builtin.bitSize = 16;
|
||||
expr->builtin.isSigned = false;
|
||||
return true;
|
||||
} else if (parser_accept(p, TOKEN_U32)) {
|
||||
expr->tag = TYPE_EXPRESSION_BUILTIN;
|
||||
expr->tag = TYPE_TREE_BUILTIN;
|
||||
expr->builtin.bitSize = 32;
|
||||
expr->builtin.isSigned = false;
|
||||
return true;
|
||||
} else if (parser_accept(p, TOKEN_U64)) {
|
||||
expr->tag = TYPE_EXPRESSION_BUILTIN;
|
||||
expr->tag = TYPE_TREE_BUILTIN;
|
||||
expr->builtin.bitSize = 64;
|
||||
expr->builtin.isSigned = false;
|
||||
return true;
|
||||
} else if (parser_accept(p, TOKEN_I8)) {
|
||||
expr->tag = TYPE_EXPRESSION_BUILTIN;
|
||||
expr->tag = TYPE_TREE_BUILTIN;
|
||||
expr->builtin.bitSize = 8;
|
||||
expr->builtin.isSigned = true;
|
||||
return true;
|
||||
} else if (parser_accept(p, TOKEN_I16)) {
|
||||
expr->tag = TYPE_EXPRESSION_BUILTIN;
|
||||
expr->tag = TYPE_TREE_BUILTIN;
|
||||
expr->builtin.bitSize = 16;
|
||||
expr->builtin.isSigned = true;
|
||||
return true;
|
||||
} else if (parser_accept(p, TOKEN_I32)) {
|
||||
expr->tag = TYPE_EXPRESSION_BUILTIN;
|
||||
expr->tag = TYPE_TREE_BUILTIN;
|
||||
expr->builtin.bitSize = 32;
|
||||
expr->builtin.isSigned = true;
|
||||
return true;
|
||||
} else if (parser_accept(p, TOKEN_I64)) {
|
||||
expr->tag = TYPE_EXPRESSION_BUILTIN;
|
||||
expr->tag = TYPE_TREE_BUILTIN;
|
||||
expr->builtin.bitSize = 64;
|
||||
expr->builtin.isSigned = true;
|
||||
return true;
|
||||
@@ -49,15 +49,15 @@ bool parse_primitive_type_expression(Parser* p, TypeExpression* expr) {
|
||||
}
|
||||
}
|
||||
|
||||
bool parse_array_type_expression(Parser* p, TypeExpression* expr) {
|
||||
TypeExpression elementType;
|
||||
bool parse_array_type_expression(Parser* p, TypeTree* expr) {
|
||||
TypeTree elementType;
|
||||
if (!parse_primitive_type_expression(p, &elementType)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (parser_accept(p, TOKEN_BRACKET_OPEN)) {
|
||||
expr->tag = TYPE_EXPRESSION_ARRAY;
|
||||
expr->array.array = malloc(sizeof(TypeExpression));
|
||||
expr->tag = TYPE_TREE_ARRAY;
|
||||
expr->array.array = malloc(sizeof(TypeTree));
|
||||
*expr->array.array = elementType;
|
||||
|
||||
if (!parser_expect(p, TOKEN_BRACKET_CLOSE, "expected ']' to end array type")) {
|
||||
@@ -70,36 +70,29 @@ bool parse_array_type_expression(Parser* p, TypeExpression* expr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
bool parse_type_expression(Parser* p, TypeExpression* expr) {
|
||||
bool parse_type_expression(Parser* p, TypeTree* expr) {
|
||||
return parse_array_type_expression(p, expr);
|
||||
}
|
||||
|
||||
bool parse_expression(Parser* p, Expression* expr) {
|
||||
bool parse_expression(Parser* p, ExpressionTree* expr) {
|
||||
if (parser_peek(p, TOKEN_INTEGER)) {
|
||||
expr->tag = EXPRESSION_INTEGER;
|
||||
expr->tag = EXPRESSION_TREE_INTEGER;
|
||||
expr->integer = atoi(p->token.text.data);
|
||||
parser_next_token(p);
|
||||
return true;
|
||||
} else if (parser_peek(p, TOKEN_STRING)) {
|
||||
expr->tag = EXPRESSION_STRING;
|
||||
expr->tag = EXPRESSION_TREE_STRING;
|
||||
expr->string = parser_to_text(p);
|
||||
return true;
|
||||
} else if (parser_accept(p, TOKEN_TRUE)) {
|
||||
expr->tag = EXPRESSION_BOOLEAN;
|
||||
expr->tag = EXPRESSION_TREE_BOOLEAN;
|
||||
expr->boolean = true;
|
||||
return true;
|
||||
} else if (parser_accept(p, TOKEN_FALSE)) {
|
||||
expr->tag = EXPRESSION_BOOLEAN;
|
||||
expr->tag = EXPRESSION_TREE_BOOLEAN;
|
||||
expr->boolean = false;
|
||||
return true;
|
||||
}
|
||||
log_on_line(&p->token.location, "expected expression");
|
||||
return false;
|
||||
}
|
||||
|
||||
void free_type_expression(TypeExpression* expr) {
|
||||
if (expr->tag == TYPE_EXPRESSION_ARRAY) {
|
||||
free_type_expression(expr->array.array);
|
||||
free(expr->array.array);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,18 +20,17 @@ char* parser_to_text(Parser* p);
|
||||
bool parser_accept_primitive(Parser* p);
|
||||
|
||||
// Base parsing (expressions, types)
|
||||
bool parse_primitive_type_expression(Parser* p, TypeExpression* expr);
|
||||
bool parse_array_type_expression(Parser* p, TypeExpression* expr);
|
||||
bool parse_type_expression(Parser* p, TypeExpression* expr);
|
||||
bool parse_expression(Parser* p, Expression* expr);
|
||||
void free_type_expression(TypeExpression* expr);
|
||||
bool parse_primitive_type_expression(Parser* p, TypeTree* expr);
|
||||
bool parse_array_type_expression(Parser* p, TypeTree* expr);
|
||||
bool parse_type_expression(Parser* p, TypeTree* expr);
|
||||
bool parse_expression(Parser* p, ExpressionTree* expr);
|
||||
|
||||
// Declaration parsing
|
||||
bool parse_import_declaration(Parser* p, Module* module, bool is_public);
|
||||
bool parse_alias_declaration(Parser* p, Module* module, bool is_public);
|
||||
bool parse_variable_declaration(Parser* p, Module* module, bool is_public, bool is_static, bool is_const);
|
||||
bool parse_import_declaration(Parser* p, ModuleTree* module, bool is_public);
|
||||
bool parse_alias_declaration(Parser* p, ModuleTree* module, bool is_public);
|
||||
bool parse_variable_declaration(Parser* p, ModuleTree* module, bool is_public, bool is_static, bool is_const);
|
||||
|
||||
// Module parsing
|
||||
bool parse_module_declaration(Parser* p, Module* module);
|
||||
bool parse_module_declaration(Parser* p, ModuleTree* module);
|
||||
|
||||
#endif
|
||||
|
||||
+5
-43
@@ -3,7 +3,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
bool parse_module_declaration(Parser* p, Module* module) {
|
||||
bool parse_module_declaration(Parser* p, ModuleTree* module) {
|
||||
if (!parser_expect(p, TOKEN_MODULE, "expected keyword 'module'")) {
|
||||
return false;
|
||||
}
|
||||
@@ -16,13 +16,13 @@ bool parse_module_declaration(Parser* p, Module* module) {
|
||||
return parser_expect(p, TOKEN_SEMICOLON, "expected ';' after module name");
|
||||
}
|
||||
|
||||
Module* parser_parse(TokenStream* ts) {
|
||||
ModuleTree* parser_parse(TokenStream* ts) {
|
||||
Parser* p = malloc(sizeof(Parser));
|
||||
p->ts = ts;
|
||||
parser_next_token(p);
|
||||
|
||||
Module* module = malloc(sizeof(Module));
|
||||
memset(module, 0, sizeof(Module));
|
||||
ModuleTree* module = malloc(sizeof(ModuleTree));
|
||||
memset(module, 0, sizeof(ModuleTree));
|
||||
if (!parse_module_declaration(p, module)) {
|
||||
goto fail;
|
||||
}
|
||||
@@ -82,44 +82,6 @@ Module* parser_parse(TokenStream* ts) {
|
||||
return module;
|
||||
fail:
|
||||
free(p);
|
||||
parser_free(module);
|
||||
ast_free_module(module);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void parser_free(Module* module) {
|
||||
if (module == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (module->imports != NULL) {
|
||||
for(size_t i = 0; i < module->import_count; i++) {
|
||||
free(module->imports[i].module_name);
|
||||
}
|
||||
free(module->imports);
|
||||
}
|
||||
|
||||
if (module->aliases != NULL) {
|
||||
for(size_t i = 0; i < module->alias_count; i++) {
|
||||
free((void*)module->aliases[i].name);
|
||||
free_type_expression(&module->aliases[i].value);
|
||||
}
|
||||
free(module->aliases);
|
||||
}
|
||||
|
||||
if (module->variables != NULL) {
|
||||
for(size_t i = 0; i < module->variable_count; i++) {
|
||||
free(module->variables[i].name);
|
||||
free_type_expression(&module->variables[i].type);
|
||||
if (module->variables[i].initializer) {
|
||||
if (module->variables[i].initializer->tag == EXPRESSION_STRING) {
|
||||
free((void*)module->variables[i].initializer->string);
|
||||
}
|
||||
free(module->variables[i].initializer);
|
||||
}
|
||||
}
|
||||
free(module->variables);
|
||||
}
|
||||
|
||||
free(module->name);
|
||||
free(module);
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ static void test_parser_bad_import_name(void) {
|
||||
}
|
||||
|
||||
static void test_parser_imports(void) {
|
||||
Module* m = test_get_ast();
|
||||
ModuleTree* m = test_get_ast();
|
||||
|
||||
assert_not_null(m, "expected module to be parsed");
|
||||
assert_str("my_module", m->name, "expected name 'my_module'");
|
||||
@@ -26,7 +26,7 @@ static void test_parser_imports(void) {
|
||||
}
|
||||
|
||||
static void test_parser_public_imports(void) {
|
||||
Module* m = test_get_ast();
|
||||
ModuleTree* m = test_get_ast();
|
||||
|
||||
assert_not_null(m, "expected module to be parsed");
|
||||
assert_str("my_module", m->name, "expected name 'my_module'");
|
||||
@@ -38,49 +38,49 @@ static void test_parser_public_imports(void) {
|
||||
}
|
||||
|
||||
static void test_parser_alias_simple(void) {
|
||||
Module* m = test_get_ast();
|
||||
ModuleTree* 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];
|
||||
AliasTree alias = m->aliases[0];
|
||||
assert_str("myalias", alias.name, "expected correct alias name");
|
||||
}
|
||||
|
||||
static void test_parser_variable_simple(void) {
|
||||
Module* m = test_get_ast();
|
||||
ModuleTree* m = test_get_ast();
|
||||
|
||||
assert_not_null(m, "expected module to be parsed");
|
||||
assert_int(1, (int)m->variable_count, "expected correct number of variables");
|
||||
VariableDeclaration var = m->variables[0];
|
||||
VariableTree var = m->variables[0];
|
||||
assert_str("my_var", var.name, "expected correct variable name");
|
||||
assert_false(var.is_const, "expected not const");
|
||||
assert_false(var.is_static, "expected not static");
|
||||
}
|
||||
|
||||
static void test_parser_variable_const(void) {
|
||||
Module* m = test_get_ast();
|
||||
ModuleTree* m = test_get_ast();
|
||||
|
||||
assert_not_null(m, "expected module to be parsed");
|
||||
assert_int(1, (int)m->variable_count, "expected correct number of variables");
|
||||
VariableDeclaration var = m->variables[0];
|
||||
VariableTree var = m->variables[0];
|
||||
assert_str("my_const", var.name, "expected correct variable name");
|
||||
assert_true(var.is_const, "expected const");
|
||||
assert_false(var.is_static, "expected not static");
|
||||
}
|
||||
|
||||
static void test_parser_variable_static(void) {
|
||||
Module* m = test_get_ast();
|
||||
ModuleTree* m = test_get_ast();
|
||||
|
||||
assert_not_null(m, "expected module to be parsed");
|
||||
assert_int(1, (int)m->variable_count, "expected correct number of variables");
|
||||
VariableDeclaration var = m->variables[0];
|
||||
VariableTree var = m->variables[0];
|
||||
assert_str("my_static", var.name, "expected correct variable name");
|
||||
assert_false(var.is_const, "expected not const");
|
||||
assert_true(var.is_static, "expected static");
|
||||
}
|
||||
|
||||
static void test_parser_multiple_vars(void) {
|
||||
Module* m = test_get_ast();
|
||||
ModuleTree* m = test_get_ast();
|
||||
|
||||
assert_not_null(m, "expected module to be parsed");
|
||||
assert_int(2, (int)m->variable_count, "expected correct number of variables");
|
||||
|
||||
+14
-14
@@ -4,49 +4,49 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
static void test_parser_alias_simple_type(void) {
|
||||
Module* m = test_get_ast();
|
||||
ModuleTree* 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_int(TYPE_EXPRESSION_BUILTIN, alias.value.tag, "expected correct alias tag");
|
||||
AliasTree alias = m->aliases[0];
|
||||
assert_int(TYPE_TREE_BUILTIN, alias.value.tag, "expected correct alias tag");
|
||||
assert_int(32, alias.value.builtin.bitSize, "expected bitSize 32");
|
||||
assert_true(alias.value.builtin.isSigned, "expected signed");
|
||||
}
|
||||
|
||||
static void test_parser_alias_array(void) {
|
||||
Module* m = test_get_ast();
|
||||
ModuleTree* 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_int(TYPE_EXPRESSION_ARRAY, alias.value.tag, "expected correct alias tag");
|
||||
TypeExpression* valueType = alias.value.array.array;
|
||||
AliasTree alias = m->aliases[0];
|
||||
assert_int(TYPE_TREE_ARRAY, alias.value.tag, "expected correct alias tag");
|
||||
TypeTree* 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(TYPE_TREE_BUILTIN, valueType->tag, "expected correct type tag");
|
||||
assert_int(32, valueType->builtin.bitSize, "expected bitSize 32");
|
||||
assert_true(valueType->builtin.isSigned, "expected signed");
|
||||
}
|
||||
|
||||
static void test_parser_variable_init(void) {
|
||||
Module* m = test_get_ast();
|
||||
ModuleTree* m = test_get_ast();
|
||||
|
||||
assert_not_null(m, "expected module to be parsed");
|
||||
assert_int(1, (int)m->variable_count, "expected 1 variable");
|
||||
VariableDeclaration* var = &m->variables[0];
|
||||
VariableTree* var = &m->variables[0];
|
||||
assert_str("x", var->name, "expected variable name 'x'");
|
||||
assert_not_null(var->initializer, "expected variable to have an initializer");
|
||||
assert_int(EXPRESSION_INTEGER, var->initializer->tag, "expected integer initializer");
|
||||
assert_int(EXPRESSION_TREE_INTEGER, var->initializer->tag, "expected integer initializer");
|
||||
assert_int(123, var->initializer->integer, "expected value 123");
|
||||
}
|
||||
|
||||
static void test_parser_variable_simple_type(void) {
|
||||
Module* m = test_get_ast();
|
||||
ModuleTree* m = test_get_ast();
|
||||
|
||||
assert_not_null(m, "expected module to be parsed");
|
||||
assert_int(1, (int)m->variable_count, "expected correct number of variables");
|
||||
VariableDeclaration var = m->variables[0];
|
||||
assert_int(TYPE_EXPRESSION_BUILTIN, var.type.tag, "expected correct type tag");
|
||||
VariableTree var = m->variables[0];
|
||||
assert_int(TYPE_TREE_BUILTIN, var.type.tag, "expected correct type tag");
|
||||
assert_int(32, var.type.builtin.bitSize, "expected bitSize 32");
|
||||
assert_true(var.type.builtin.isSigned, "expected signed");
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
static void test_parser_module_name(void) {
|
||||
Module* m = test_get_ast();
|
||||
ModuleTree* m = test_get_ast();
|
||||
|
||||
assert_not_null(m, "expected module to be parsed");
|
||||
assert_str("my_module", m->name, "expected name 'my_module'");
|
||||
|
||||
@@ -14,7 +14,7 @@ static char* s_logOutput = NULL;
|
||||
static const char* s_currentTestName = NULL;
|
||||
static char* s_testSource = NULL;
|
||||
|
||||
static Module* s_currentModule = NULL;
|
||||
static ModuleTree* s_currentModule = NULL;
|
||||
static TokenStream* s_currentTokenStream = NULL;
|
||||
|
||||
void fail(const char* msg) {
|
||||
@@ -90,7 +90,7 @@ TokenStream* test_get_tokenstream(void) {
|
||||
return s_currentTokenStream;
|
||||
}
|
||||
|
||||
Module* test_get_ast(void) {
|
||||
ModuleTree* test_get_ast(void) {
|
||||
if (s_currentModule == NULL) {
|
||||
s_currentModule = parser_parse(test_get_tokenstream());
|
||||
}
|
||||
@@ -277,7 +277,7 @@ int main(int argc, char** argv) {
|
||||
|
||||
// Free AST and TokenStream after each test
|
||||
if (s_currentModule) {
|
||||
parser_free(s_currentModule);
|
||||
ast_free_module(s_currentModule);
|
||||
s_currentModule = NULL;
|
||||
}
|
||||
if (s_currentTokenStream) {
|
||||
|
||||
Reference in New Issue
Block a user