From bddfb59886f52a34785e85b735b3828f5dd5ef2e Mon Sep 17 00:00:00 2001 From: William Bell Date: Fri, 20 Jun 2025 02:50:05 +0100 Subject: [PATCH] start working on runtime --- src/hashmap/hashmap.c | 7 ++++--- src/hashmap/hashmap.h | 6 ++++-- src/runtime/runtime.c | 23 +++++++++++++++++++++++ src/runtime/runtime.h | 19 +++++++++++++++++++ src/translator/string/string.c | 6 +++--- src/translator/string/string.h | 2 +- src/translator/translator.c | 2 +- 7 files changed, 55 insertions(+), 10 deletions(-) create mode 100644 src/runtime/runtime.c create mode 100644 src/runtime/runtime.h diff --git a/src/hashmap/hashmap.c b/src/hashmap/hashmap.c index 787172d..4635899 100644 --- a/src/hashmap/hashmap.c +++ b/src/hashmap/hashmap.c @@ -1,13 +1,14 @@ #include "hashmap.h" +#include #include #include "../memory.h" struct table *createTable(int size) { - struct table *t = (struct table *)checked_malloc(sizeof(struct table)); + struct table *t = (struct table *)ar_alloc(sizeof(struct table)); t->size = size; - t->list = (struct node **)checked_malloc(sizeof(struct node *) * size); + t->list = (struct node **)ar_alloc(sizeof(struct node *) * size); int i; for (i = 0; i < size; i++) t->list[i] = NULL; @@ -49,7 +50,7 @@ void insert(struct table *t, int key, void* val) { int pos = hashCode(t, key); struct node *list = t->list[pos]; - struct node *newNode = (struct node *)checked_malloc(sizeof(struct node)); + struct node *newNode = (struct node *)ar_alloc(sizeof(struct node)); struct node *temp = list; while (temp) { diff --git a/src/hashmap/hashmap.h b/src/hashmap/hashmap.h index 5e2315f..2f8c101 100644 --- a/src/hashmap/hashmap.h +++ b/src/hashmap/hashmap.h @@ -1,15 +1,17 @@ #ifndef HASHMAP_H #define HASHMAP_H +#include struct node { - int key; + size_t key; void *val; struct node *next; }; struct table { - int size; + size_t size; + size_t count; struct node **list; }; diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c new file mode 100644 index 0000000..f5d0d26 --- /dev/null +++ b/src/runtime/runtime.c @@ -0,0 +1,23 @@ +#include "runtime.h" +#include +#include +#include + +uint64_t pop_bytecode(Translated *translated, RuntimeState *state) { + uint64_t *instruction = darray_get(&translated->bytecode, state->head++); + return *instruction; +} + +void run_instruction(Translated *translated, RuntimeState *state) { + uint64_t opcode = pop_bytecode(translated, state); + switch (opcode) { case OP_LOAD_NULL: } +} + +void runtime(Translated translated) { + RuntimeState state = { + checked_malloc(translated.registerCount * sizeof(size_t)), 0}; + + while (state.head < translated.bytecode.size) + run_instruction(&translated, &state); + free(state.registers); +} \ No newline at end of file diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h new file mode 100644 index 0000000..0c7b6dd --- /dev/null +++ b/src/runtime/runtime.h @@ -0,0 +1,19 @@ +#ifndef RUNTIME_H +#define RUNTIME_H +#include "../translator/translator.h" + +typedef struct { + uint64_t *registers; + size_t head; +} RuntimeState; + + +typedef struct { + +} ArObject; + +void run_instruction(Translated *translated, RuntimeState *state); + +void runtime(Translated translated); + +#endif // RUNTIME_H \ No newline at end of file diff --git a/src/translator/string/string.c b/src/translator/string/string.c index 14c6687..91aff1b 100644 --- a/src/translator/string/string.c +++ b/src/translator/string/string.c @@ -4,11 +4,11 @@ #include #include -size_t translate_parsed_string(Translated *translated, ParsedString parsedString, size_t to_register) { +size_t translate_parsed_string(Translated *translated, ParsedString parsedString) { size_t string_pos = arena_push(&translated->constants, parsedString.string, parsedString.length); - set_registers(translated, to_register+1); + set_registers(translated, 1); size_t start = push_instruction_code(translated, OP_LOAD_CONST); - push_instruction_code(translated, to_register); + push_instruction_code(translated, 0); push_instruction_code(translated, TYPE_OP_STRING); push_instruction_code(translated,parsedString.length); push_instruction_code(translated, string_pos); diff --git a/src/translator/string/string.h b/src/translator/string/string.h index 4b03039..d56fd9e 100644 --- a/src/translator/string/string.h +++ b/src/translator/string/string.h @@ -3,6 +3,6 @@ #include "../translator.h" #include "../../parser/string/string.h" -size_t translate_parsed_string(Translated *translated, ParsedString parsedString, size_t to_register); +size_t translate_parsed_string(Translated *translated, ParsedString parsedString); #endif \ No newline at end of file diff --git a/src/translator/translator.c b/src/translator/translator.c index dafd10e..a824c72 100644 --- a/src/translator/translator.c +++ b/src/translator/translator.c @@ -82,7 +82,7 @@ void set_registers(Translated *translator, size_t count) { size_t translate_parsed(Translated *translated, ParsedValue *parsedValue) { switch (parsedValue->type) { case AST_STRING: - return translate_parsed_string(translated, *((ParsedString*)parsedValue->data), 0); + return translate_parsed_string(translated, *((ParsedString*)parsedValue->data)); case AST_DECLARATION: return translate_parsed_declaration(translated, *((DArray*)parsedValue->data)); case AST_NUMBER: