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