semos/checker.c

77 lines
1.8 KiB
C
Raw Normal View History

2024-04-05 18:16:58 +01:00
#include "compiler.h"
#include <stdlib.h>
size_t common_string_hash_djb2(const unsigned char* value, size_t length)
{
size_t hash = 5381;
for (size_t i = 0; i < length && value[i] != '\0'; ++i)
hash = ((hash << 5) + hash) + value[i];
return hash;
}
int string_symbol_map_construct(StringSymbolMap* map)
{
const size_t start_capacity = 4;
*map = (StringSymbolMap) {
.data = malloc(sizeof(StringSymbolMap) * start_capacity),
.length = 0,
.capacity = start_capacity,
};
if (map->data == NULL) {
return -1;
}
return 0;
}
void string_symbol_map_destroy(StringSymbolMap* map)
{
if (map->data != NULL) {
free(map->data);
}
}
Symbol* string_symbol_map_get(const StringSymbolMap* map, size_t key_hash)
{
for (size_t i = 0; i < map->length; ++i) {
if (map->data[i].key_hash == key_hash) {
return &map->data[i].value;
}
}
return NULL;
}
int string_symbol_map_set(StringSymbolMap* map, size_t key_hash, Symbol value)
{
Symbol* existing = string_symbol_map_get(map, key_hash);
if (existing != NULL) {
*existing = value;
return 0;
}
if (map->length + 1 >= map->capacity) {
map->capacity *= 2;
StringSymbolMapEntry* data
= realloc(map->data, sizeof(StringSymbolMapEntry) * map->capacity);
if (data == NULL) {
return -1;
}
map->data = data;
}
map->data[map->length] = (StringSymbolMapEntry) { key_hash, value };
map->length += 1;
return 0;
}
void checker_construct(Checker* checker)
{
*checker = (Checker) {
.failed = false,
};
}
bool checker_failed(const Checker* checker) { return checker->failed; }
void checker_check_statements(Checker* checker, ASTNode* node)
{
for (size_t i = 0; i < node->statements.length; ++i) { }
}