From 74c71c3a1ba6ea601f1435d1462e3400b751f619 Mon Sep 17 00:00:00 2001 From: William Bell Date: Sun, 22 Jun 2025 19:00:03 +0100 Subject: [PATCH] start working on oop runtime --- null_test.ar | 1 + src/hashmap/hashmap.h | 26 --------------- src/main.c | 3 ++ src/memory.c | 4 +++ src/{ => runtime/internals}/hashmap/hashmap.c | 32 +++++++++---------- src/runtime/internals/hashmap/hashmap.h | 30 +++++++++++++++++ src/runtime/null/null.c | 5 --- src/runtime/objects/object.c | 9 ++++++ src/runtime/objects/object.h | 29 +++++++++++++++++ src/runtime/objects/type/type.c | 15 +++++++++ src/runtime/objects/type/type.h | 10 ++++++ src/runtime/runtime.c | 3 +- src/runtime/runtime.h | 4 --- src/translator/translator.c | 1 - test.ar | 3 ++ 15 files changed, 122 insertions(+), 53 deletions(-) create mode 100644 null_test.ar delete mode 100644 src/hashmap/hashmap.h rename src/{ => runtime/internals}/hashmap/hashmap.c (77%) create mode 100644 src/runtime/internals/hashmap/hashmap.h delete mode 100644 src/runtime/null/null.c create mode 100644 src/runtime/objects/object.c create mode 100644 src/runtime/objects/object.h create mode 100644 src/runtime/objects/type/type.c create mode 100644 src/runtime/objects/type/type.h diff --git a/null_test.ar b/null_test.ar new file mode 100644 index 0000000..ec747fa --- /dev/null +++ b/null_test.ar @@ -0,0 +1 @@ +null \ No newline at end of file diff --git a/src/hashmap/hashmap.h b/src/hashmap/hashmap.h deleted file mode 100644 index 2b1943f..0000000 --- a/src/hashmap/hashmap.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef HASHMAP_H -#define HASHMAP_H -#include - -struct node -{ - size_t key; - void *val; - struct node *next; -}; -struct hashmap -{ - size_t size; - size_t count; - struct node **list; -}; - -struct hashmap *createTable(int size); - -int hashCode(struct hashmap *t, int key); - -int remove(struct hashmap *t, int key); - -void insert(struct hashmap *t, int key, void* val); - -#endif // HASHMAP_H \ No newline at end of file diff --git a/src/main.c b/src/main.c index 82c95b7..1c1f888 100644 --- a/src/main.c +++ b/src/main.c @@ -4,6 +4,7 @@ #include "memory.h" #include "parser/parser.h" #include "translator/translator.h" +#include "runtime/runtime.h" #include #include @@ -73,6 +74,8 @@ int main(int argc, char *argv[]) { fclose(file); + runtime(translated); + free_translator(&translated); return 0; } diff --git a/src/memory.c b/src/memory.c index 3744178..8ddfe51 100644 --- a/src/memory.c +++ b/src/memory.c @@ -6,6 +6,9 @@ #include #include +// runtime +#include "runtime/objects/type/type.h" + void *checked_malloc(size_t size) { void *ptr = malloc(size); if (!ptr) { @@ -30,6 +33,7 @@ void gmp_gc_free(void *ptr, size_t size) { void ar_memory_init() { GC_INIT(); mp_set_memory_functions(GC_malloc, gmp_gc_realloc, gmp_gc_free); + init_type_obj(); } diff --git a/src/hashmap/hashmap.c b/src/runtime/internals/hashmap/hashmap.c similarity index 77% rename from src/hashmap/hashmap.c rename to src/runtime/internals/hashmap/hashmap.c index e64b7e2..f216bdc 100644 --- a/src/hashmap/hashmap.c +++ b/src/runtime/internals/hashmap/hashmap.c @@ -1,10 +1,11 @@ #include "hashmap.h" +#include #include #include -#include "../memory.h" +#include "../../../memory.h" -struct hashmap *createTable(int size) +struct hashmap *createHashmap(int size) { struct hashmap *t = (struct hashmap *)ar_alloc(sizeof(struct hashmap)); t->size = size; @@ -34,28 +35,26 @@ void resize_hashmap(struct hashmap *t) for (int i = 0; i < old_size; i++) { struct node *temp = old_list[i]; while (temp) { - insert(t, temp->key, temp->val); // Will increment count + hashmap_insert(t, temp->hash, temp->key, temp->val); // Will increment count temp = temp->next; } } } -int hashCode(struct hashmap *t, int key) +int hashCode(struct hashmap *t, uint64_t hash) { - if (key < 0) - return -(key % t->size); - return key % t->size; + return hash % t->size; } -int remove(struct hashmap *t, int key) +int hashmap_remove(struct hashmap *t, uint64_t hash) { - int pos = hashCode(t, key); + int pos = hashCode(t, hash); struct node *list = t->list[pos]; struct node *temp = list; struct node *prev = NULL; while (temp) { - if (temp->key == key) + if (temp->hash == hash) { if (prev) prev->next = temp->next; @@ -90,19 +89,19 @@ void resize(struct hashmap *t) for (int i = 0; i < old_size; i++) { struct node *temp = old_list[i]; while (temp) { - insert(t, temp->key, temp->val); // Will increment count + hashmap_insert(t, temp->hash, temp->key, temp->val); // Will increment count temp = temp->next; } } } -void insert(struct hashmap *t, int key, void* val) +void hashmap_insert(struct hashmap *t, uint64_t hash, ArgonObject* key, ArgonObject* val) { if ((t->count + 1) > t->size * 0.75) { resize(t); } - int pos = hashCode(t, key); + int pos = hashCode(t, hash); struct node *list = t->list[pos]; struct node *temp = list; @@ -119,6 +118,7 @@ void insert(struct hashmap *t, int key, void* val) // Insert new node struct node *newNode = (struct node *)ar_alloc(sizeof(struct node)); + newNode->hash = hash; newNode->key = key; newNode->val = val; newNode->next = list; @@ -126,14 +126,14 @@ void insert(struct hashmap *t, int key, void* val) t->count++; } -void *lookup(struct hashmap *t, int key) +void *lookup(struct hashmap *t, uint64_t hash) { - int pos = hashCode(t, key); + int pos = hashCode(t, hash); struct node *list = t->list[pos]; struct node *temp = list; while (temp) { - if (temp->key == key) + if (temp->hash == hash) { return temp->val; } diff --git a/src/runtime/internals/hashmap/hashmap.h b/src/runtime/internals/hashmap/hashmap.h new file mode 100644 index 0000000..da060eb --- /dev/null +++ b/src/runtime/internals/hashmap/hashmap.h @@ -0,0 +1,30 @@ +#ifndef HASHMAP_H +#define HASHMAP_H +#include +#include + +typedef struct ArgonObject ArgonObject; + +struct node +{ + uint64_t hash; + ArgonObject *key; + ArgonObject *val; + struct node *next; +}; +struct hashmap +{ + size_t size; + size_t count; + struct node **list; +}; + +struct hashmap *createHashmap(int size); + +int hashCode(struct hashmap *t, uint64_t hash); + +int hashmap_remove(struct hashmap *t, uint64_t hash); + +void hashmap_insert(struct hashmap *t, uint64_t hash, ArgonObject* key, ArgonObject* val); + +#endif // HASHMAP_H \ No newline at end of file diff --git a/src/runtime/null/null.c b/src/runtime/null/null.c deleted file mode 100644 index d87dd49..0000000 --- a/src/runtime/null/null.c +++ /dev/null @@ -1,5 +0,0 @@ -#include "../runtime.h" - -void run_null(Translated *translated, RuntimeState *state) { - -} \ No newline at end of file diff --git a/src/runtime/objects/object.c b/src/runtime/objects/object.c new file mode 100644 index 0000000..7b7a779 --- /dev/null +++ b/src/runtime/objects/object.c @@ -0,0 +1,9 @@ +#include "object.h" +#include "../../memory.h" + +ArgonObject* init_argon_object() { + ArgonObject *object = ar_alloc(sizeof(ArgonObject)); + object->type = TYPE_OBJECT; + object->cls = object; + object->fields = createHashmap(8); +} \ No newline at end of file diff --git a/src/runtime/objects/object.h b/src/runtime/objects/object.h new file mode 100644 index 0000000..9f7950b --- /dev/null +++ b/src/runtime/objects/object.h @@ -0,0 +1,29 @@ +#ifndef OBJECT_H +#define OBJECT_H +#include "../internals/hashmap/hashmap.h" +#include +#include + +typedef enum { + TYPE_NULL, + TYPE_BOOL, + TYPE_NUMBER, + TYPE_STRING, + TYPE_FUNCTION, + TYPE_NATIVE_FUNCTION, + TYPE_OBJECT, // generic user object +} ArgonType; + +struct ArgonObject { + ArgonType type; + struct ArgonObject *cls; // class pointer or type object + struct hashmap *fields; // dynamic fields/methods + union { + mpq_t as_number; + bool as_bool; + char *as_str; + void *native_fn; + // others as needed + } value; +}; +#endif // OBJECT_H \ No newline at end of file diff --git a/src/runtime/objects/type/type.c b/src/runtime/objects/type/type.c new file mode 100644 index 0000000..63e4393 --- /dev/null +++ b/src/runtime/objects/type/type.c @@ -0,0 +1,15 @@ +#include "../object.h" +#include "../../internals/hashmap/hashmap.h" +#include "../../../memory.h" +#include +#include "type.h" + +ArgonObject *ARGON_TYPE_OBJ = NULL; + +void init_type_obj() { + ARGON_TYPE_OBJ = ar_alloc(sizeof(ArgonObject)); + ARGON_TYPE_OBJ->type = TYPE_OBJECT; + ARGON_TYPE_OBJ->cls = ARGON_TYPE_OBJ; + ARGON_TYPE_OBJ->fields = createHashmap(8); + memset(&ARGON_TYPE_OBJ->value, 0, sizeof(ARGON_TYPE_OBJ->value)); +} \ No newline at end of file diff --git a/src/runtime/objects/type/type.h b/src/runtime/objects/type/type.h new file mode 100644 index 0000000..83f6783 --- /dev/null +++ b/src/runtime/objects/type/type.h @@ -0,0 +1,10 @@ +#ifndef TYPES_H +#define TYPES_H +#include "../object.h" + +extern ArgonObject *ARGON_TYPE_OBJ; + +void init_type_obj(); + + +#endif // TYPES_H \ No newline at end of file diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c index f5d0d26..295de2a 100644 --- a/src/runtime/runtime.c +++ b/src/runtime/runtime.c @@ -1,6 +1,7 @@ #include "runtime.h" #include #include +#include #include uint64_t pop_bytecode(Translated *translated, RuntimeState *state) { @@ -10,7 +11,7 @@ uint64_t pop_bytecode(Translated *translated, RuntimeState *state) { void run_instruction(Translated *translated, RuntimeState *state) { uint64_t opcode = pop_bytecode(translated, state); - switch (opcode) { case OP_LOAD_NULL: } + switch (opcode) { case OP_LOAD_NULL: pop_bytecode(translated, state);printf("null\n");} } void runtime(Translated translated) { diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h index 1960c06..afc47e5 100644 --- a/src/runtime/runtime.h +++ b/src/runtime/runtime.h @@ -8,10 +8,6 @@ typedef struct { } RuntimeState; -typedef struct { - -} ArObject; - void run_instruction(Translated *translated, RuntimeState *state); void runtime(Translated translated); diff --git a/src/translator/translator.c b/src/translator/translator.c index a824c72..a5bddc7 100644 --- a/src/translator/translator.c +++ b/src/translator/translator.c @@ -62,7 +62,6 @@ Translated init_translator() { void set_instruction_code(Translated *translator, size_t offset, uint64_t code) { - code = htole64(code); size_t *ptr = (translator->bytecode.data + offset); *ptr = code; } diff --git a/test.ar b/test.ar index 69966a8..4c92512 100644 --- a/test.ar +++ b/test.ar @@ -1,3 +1,6 @@ +null + + "h" "e" "ll"