From 33fe8f12dd1dc1b8da3446bebd03697850e495c4 Mon Sep 17 00:00:00 2001 From: SimonFJ20 Date: Fri, 5 Apr 2024 19:16:58 +0200 Subject: [PATCH] hawd >~< --- checker.c | 77 +++++++++++++++++++++++++++++++++++++++++- parser.h => compiler.h | 37 +++++++++++++++++--- main.c | 2 +- parser.c | 2 +- vm.c | 2 +- 5 files changed, 112 insertions(+), 8 deletions(-) rename parser.h => compiler.h (87%) diff --git a/checker.c b/checker.c index c1dfa96..531d2b8 100644 --- a/checker.c +++ b/checker.c @@ -1 +1,76 @@ -#include "parser.h" +#include "compiler.h" +#include + +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) { } +} diff --git a/parser.h b/compiler.h similarity index 87% rename from parser.h rename to compiler.h index 813db4a..5cc0a01 100644 --- a/parser.h +++ b/compiler.h @@ -1,5 +1,5 @@ -#ifndef PARSER_H -#define PARSER_H +#ifndef COMPILER_H +#define COMPILER_H #include #include @@ -249,11 +249,40 @@ ASTNode* parser_parse_block(Parser* parser); ASTNode* parser_parse_if(Parser* parser); ASTNode* parser_parse_loop(Parser* parser); +typedef enum { + SymbolType_Local, + SymbolType_Global, + SymbolType_Builtin, +} SymbolType; + typedef struct { - int _a; + SymbolType type; +} Symbol; + +size_t common_string_hash_djb2(const unsigned char* value, size_t length); + +typedef struct { + size_t key_hash; + Symbol value; +} StringSymbolMapEntry; + +typedef struct { + StringSymbolMapEntry* data; + size_t length; + size_t capacity; +} StringSymbolMap; + +int string_symbol_map_construct(StringSymbolMap* map); +void string_symbol_map_destroy(StringSymbolMap* map); +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); + +typedef struct { + bool failed; } Checker; void checker_construct(Checker* checker); -void checker_(Checker* checker); +bool checker_failed(const Checker* checker); +void checker_check_statements(Checker* checker, ASTNode* node); #endif diff --git a/main.c b/main.c index 61ce2e2..71fa9c4 100644 --- a/main.c +++ b/main.c @@ -1,4 +1,4 @@ -#include "parser.h" +#include "compiler.h" #include #include #include diff --git a/parser.c b/parser.c index be9a96e..0c84c3c 100644 --- a/parser.c +++ b/parser.c @@ -1,4 +1,4 @@ -#include "parser.h" +#include "compiler.h" #include #include #include diff --git a/vm.c b/vm.c index d3561cc..d7c5194 100644 --- a/vm.c +++ b/vm.c @@ -1,4 +1,4 @@ -#include "vm.h" +#include "compiler.h" #include void vm_construct(