This commit is contained in:
SimonFJ20 2024-04-05 19:16:58 +02:00
parent afb271be68
commit 33fe8f12dd
5 changed files with 112 additions and 8 deletions

View File

@ -1 +1,76 @@
#include "parser.h" #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) { }
}

View File

@ -1,5 +1,5 @@
#ifndef PARSER_H #ifndef COMPILER_H
#define PARSER_H #define COMPILER_H
#include <stdbool.h> #include <stdbool.h>
#include <stddef.h> #include <stddef.h>
@ -249,11 +249,40 @@ ASTNode* parser_parse_block(Parser* parser);
ASTNode* parser_parse_if(Parser* parser); ASTNode* parser_parse_if(Parser* parser);
ASTNode* parser_parse_loop(Parser* parser); ASTNode* parser_parse_loop(Parser* parser);
typedef enum {
SymbolType_Local,
SymbolType_Global,
SymbolType_Builtin,
} SymbolType;
typedef struct { 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; } Checker;
void checker_construct(Checker* 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 #endif

2
main.c
View File

@ -1,4 +1,4 @@
#include "parser.h" #include "compiler.h"
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>

View File

@ -1,4 +1,4 @@
#include "parser.h" #include "compiler.h"
#include <stddef.h> #include <stddef.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>

2
vm.c
View File

@ -1,4 +1,4 @@
#include "vm.h" #include "compiler.h"
#include <stdlib.h> #include <stdlib.h>
void vm_construct( void vm_construct(