Rename AST structures to Tree and relocate freeing logic
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user