add symbols
This commit is contained in:
parent
33fe8f12dd
commit
2c2c32b28f
95
checker.c
95
checker.c
@ -61,16 +61,109 @@ int string_symbol_map_set(StringSymbolMap* map, size_t key_hash, Symbol value)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int statements_symbols_vec_construct(StatementsSymbolsVec* vec)
|
||||||
|
{
|
||||||
|
const size_t start_capacity = 4;
|
||||||
|
StatementsSymbols* data = malloc(start_capacity * sizeof(StatementsSymbols));
|
||||||
|
if (data == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
*vec = (StatementsSymbolsVec) {
|
||||||
|
.data = data,
|
||||||
|
.length = 0,
|
||||||
|
.capacity = start_capacity,
|
||||||
|
};
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void statements_symbols_vec_destroy(StatementsSymbolsVec* vec)
|
||||||
|
{
|
||||||
|
if (vec->data != NULL) {
|
||||||
|
free(vec->data);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int statements_symbols_vec_push(StatementsSymbolsVec* vec, StatementsSymbols pair)
|
||||||
|
{
|
||||||
|
if (vec->length + 1 > vec->capacity) {
|
||||||
|
StatementsSymbols* new_data = realloc(vec->data, vec->capacity * 2 * sizeof(StatementsSymbols));
|
||||||
|
if (new_data == NULL) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
vec->capacity *= 2;
|
||||||
|
vec->data = new_data;
|
||||||
|
}
|
||||||
|
vec->data[vec->length] = pair;
|
||||||
|
vec->length += 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void symbol_table_construct(SymbolTable* table, SymbolTable* parent)
|
||||||
|
{
|
||||||
|
*table = (SymbolTable) {
|
||||||
|
.parent = parent,
|
||||||
|
.map = { 0 },
|
||||||
|
};
|
||||||
|
string_symbol_map_construct(&table->map);
|
||||||
|
}
|
||||||
|
|
||||||
|
void symbol_table_destroy(SymbolTable* table)
|
||||||
|
{
|
||||||
|
string_symbol_map_destroy(&table->map);
|
||||||
|
}
|
||||||
|
|
||||||
void checker_construct(Checker* checker)
|
void checker_construct(Checker* checker)
|
||||||
{
|
{
|
||||||
*checker = (Checker) {
|
*checker = (Checker) {
|
||||||
.failed = false,
|
.failed = false,
|
||||||
|
.statements_symbols = { 0 },
|
||||||
};
|
};
|
||||||
|
statements_symbols_vec_construct(&checker->statements_symbols);
|
||||||
|
}
|
||||||
|
|
||||||
|
void checker_destroy(Checker* checker)
|
||||||
|
{
|
||||||
|
(void)checker;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool checker_failed(const Checker* checker) { return checker->failed; }
|
bool checker_failed(const Checker* checker) { return checker->failed; }
|
||||||
|
|
||||||
|
CheckerResult checker_result(Checker* checker)
|
||||||
|
{
|
||||||
|
return (CheckerResult) {
|
||||||
|
.head_table = checker->head_table,
|
||||||
|
.statements_symbols = checker->statements_symbols,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
void checker_check_statements(Checker* checker, ASTNode* node)
|
void checker_check_statements(Checker* checker, ASTNode* node)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < node->statements.length; ++i) { }
|
SymbolTable table;
|
||||||
|
symbol_table_construct(&table, &checker->head_table);
|
||||||
|
for (size_t i = 0; i < node->statements.length; ++i) {
|
||||||
|
checker_check_statement(checker, &table, node->statements.data[i]);
|
||||||
|
}
|
||||||
|
statements_symbols_vec_push(&checker->statements_symbols, (StatementsSymbols) {
|
||||||
|
.table = table,
|
||||||
|
.statements = node,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void checker_check_statement(Checker* checker, SymbolTable* table, ASTNode* node)
|
||||||
|
{
|
||||||
|
switch (node->node_type) {
|
||||||
|
case ASTNodeType_Block:
|
||||||
|
checker_check_statements(checker, node);
|
||||||
|
case ASTNodeType_If:
|
||||||
|
case ASTNodeType_Loop:
|
||||||
|
case ASTNodeType_Assign:
|
||||||
|
case ASTNodeType_Let:
|
||||||
|
case ASTNodeType_Break:
|
||||||
|
case ASTNodeType_Fn:
|
||||||
|
case ASTNodeType_Return:
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
39
compiler.h
39
compiler.h
@ -192,10 +192,10 @@ struct ASTNode {
|
|||||||
ASTNodeType node_type;
|
ASTNodeType node_type;
|
||||||
Pos pos;
|
Pos pos;
|
||||||
union {
|
union {
|
||||||
char* id_value;
|
char* id_value;
|
||||||
int int_value;
|
int int_value;
|
||||||
ASTNode* group_value;
|
ASTNode* group_value;
|
||||||
ASTNodeVec statements;
|
ASTNodeVec statements;
|
||||||
ASTIfNode if_node;
|
ASTIfNode if_node;
|
||||||
ASTLoopNode loop_node;
|
ASTLoopNode loop_node;
|
||||||
ASTCallNode call_node;
|
ASTCallNode call_node;
|
||||||
@ -277,12 +277,47 @@ void string_symbol_map_destroy(StringSymbolMap* map);
|
|||||||
Symbol* string_symbol_map_get(const StringSymbolMap* map, size_t key_hash);
|
Symbol* string_symbol_map_get(const StringSymbolMap* map, size_t key_hash);
|
||||||
int string_symbol_map_set(StringSymbolMap* map, size_t key_hash, Symbol value);
|
int string_symbol_map_set(StringSymbolMap* map, size_t key_hash, Symbol value);
|
||||||
|
|
||||||
|
typedef struct SymbolTable SymbolTable;
|
||||||
|
|
||||||
|
struct SymbolTable {
|
||||||
|
SymbolTable* parent;
|
||||||
|
StringSymbolMap map;
|
||||||
|
};
|
||||||
|
|
||||||
|
void symbol_table_construct(SymbolTable* table, SymbolTable* parent);
|
||||||
|
void symbol_table_destroy(SymbolTable* table);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
ASTNode* statements;
|
||||||
|
SymbolTable table;
|
||||||
|
} StatementsSymbols;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
StatementsSymbols* data;
|
||||||
|
size_t length;
|
||||||
|
size_t capacity;
|
||||||
|
} StatementsSymbolsVec;
|
||||||
|
|
||||||
|
int statements_symbols_vec_construct(StatementsSymbolsVec* vec);
|
||||||
|
void statements_symbols_vec_destroy(StatementsSymbolsVec* vec);
|
||||||
|
int statements_symbols_vec_push(StatementsSymbolsVec* vec, StatementsSymbols pair);
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
bool failed;
|
bool failed;
|
||||||
|
SymbolTable head_table;
|
||||||
|
StatementsSymbolsVec statements_symbols;
|
||||||
} Checker;
|
} Checker;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
SymbolTable head_table;
|
||||||
|
StatementsSymbolsVec statements_symbols;
|
||||||
|
} CheckerResult;
|
||||||
|
|
||||||
void checker_construct(Checker* checker);
|
void checker_construct(Checker* checker);
|
||||||
|
void checker_destroy(Checker* checker);
|
||||||
bool checker_failed(const Checker* checker);
|
bool checker_failed(const Checker* checker);
|
||||||
|
CheckerResult checker_result(Checker* checker);
|
||||||
void checker_check_statements(Checker* checker, ASTNode* node);
|
void checker_check_statements(Checker* checker, ASTNode* node);
|
||||||
|
void checker_check_statement(Checker* checker, SymbolTable* table, ASTNode* node);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user