hawd >~<
This commit is contained in:
parent
afb271be68
commit
33fe8f12dd
77
checker.c
77
checker.c
@ -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) { }
|
||||
}
|
||||
|
@ -1,5 +1,5 @@
|
||||
#ifndef PARSER_H
|
||||
#define PARSER_H
|
||||
#ifndef COMPILER_H
|
||||
#define COMPILER_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
@ -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
|
2
main.c
2
main.c
@ -1,4 +1,4 @@
|
||||
#include "parser.h"
|
||||
#include "compiler.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
2
parser.c
2
parser.c
@ -1,4 +1,4 @@
|
||||
#include "parser.h"
|
||||
#include "compiler.h"
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
Loading…
Reference in New Issue
Block a user