start working on oop runtime

This commit is contained in:
2025-06-22 19:00:03 +01:00
parent fcffdc9000
commit 74c71c3a1b
15 changed files with 122 additions and 53 deletions

View File

@@ -1,26 +0,0 @@
#ifndef HASHMAP_H
#define HASHMAP_H
#include <stdlib.h>
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

View File

@@ -4,6 +4,7 @@
#include "memory.h"
#include "parser/parser.h"
#include "translator/translator.h"
#include "runtime/runtime.h"
#include <endian.h>
#include <string.h>
@@ -73,6 +74,8 @@ int main(int argc, char *argv[]) {
fclose(file);
runtime(translated);
free_translator(&translated);
return 0;
}

View File

@@ -6,6 +6,9 @@
#include <string.h>
#include <stdio.h>
// 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();
}

View File

@@ -1,10 +1,11 @@
#include "hashmap.h"
#include <stdint.h>
#include <gc/gc.h>
#include <stdlib.h>
#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;
}

View File

@@ -0,0 +1,30 @@
#ifndef HASHMAP_H
#define HASHMAP_H
#include <stdint.h>
#include <stdlib.h>
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

View File

@@ -1,5 +0,0 @@
#include "../runtime.h"
void run_null(Translated *translated, RuntimeState *state) {
}

View File

@@ -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);
}

View File

@@ -0,0 +1,29 @@
#ifndef OBJECT_H
#define OBJECT_H
#include "../internals/hashmap/hashmap.h"
#include <gmp.h>
#include <stdbool.h>
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

View File

@@ -0,0 +1,15 @@
#include "../object.h"
#include "../../internals/hashmap/hashmap.h"
#include "../../../memory.h"
#include <string.h>
#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));
}

View File

@@ -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

View File

@@ -1,6 +1,7 @@
#include "runtime.h"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
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) {

View File

@@ -8,10 +8,6 @@ typedef struct {
} RuntimeState;
typedef struct {
} ArObject;
void run_instruction(Translated *translated, RuntimeState *state);
void runtime(Translated translated);

View File

@@ -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;
}