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

@@ -0,0 +1,143 @@
#include "hashmap.h"
#include <stdint.h>
#include <gc/gc.h>
#include <stdlib.h>
#include "../../../memory.h"
struct hashmap *createHashmap(int size)
{
struct hashmap *t = (struct hashmap *)ar_alloc(sizeof(struct hashmap));
t->size = size;
t->list = (struct node **)ar_alloc(sizeof(struct node *) * size);
int i;
for (i = 0; i < size; i++)
t->list[i] = NULL;
return t;
}
void resize_hashmap(struct hashmap *t)
{
int old_size = t->size;
int new_size = old_size * 2;
struct node **old_list = t->list;
// Create new list
t->list = (struct node **)ar_alloc(sizeof(struct node *) * new_size);
for (int i = 0; i < new_size; i++)
t->list[i] = NULL;
t->size = new_size;
t->count = 0;
// Rehash old entries into new list
for (int i = 0; i < old_size; i++) {
struct node *temp = old_list[i];
while (temp) {
hashmap_insert(t, temp->hash, temp->key, temp->val); // Will increment count
temp = temp->next;
}
}
}
int hashCode(struct hashmap *t, uint64_t hash)
{
return hash % t->size;
}
int hashmap_remove(struct hashmap *t, uint64_t hash)
{
int pos = hashCode(t, hash);
struct node *list = t->list[pos];
struct node *temp = list;
struct node *prev = NULL;
while (temp)
{
if (temp->hash == hash)
{
if (prev)
prev->next = temp->next;
else
t->list[pos] = temp->next;
free(temp);
return 1;
}
prev = temp;
temp = temp->next;
}
return 0;
}
void resize(struct hashmap *t)
{
int old_size = t->size;
int new_size = old_size * 2;
struct node **old_list = t->list;
// Create new list
t->list = (struct node **)ar_alloc(sizeof(struct node *) * new_size);
for (int i = 0; i < new_size; i++)
t->list[i] = NULL;
t->size = new_size;
t->count = 0;
// Rehash old entries into new list
for (int i = 0; i < old_size; i++) {
struct node *temp = old_list[i];
while (temp) {
hashmap_insert(t, temp->hash, temp->key, temp->val); // Will increment count
temp = temp->next;
}
}
}
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, hash);
struct node *list = t->list[pos];
struct node *temp = list;
// Check if key exists → overwrite
while (temp)
{
if (temp->key == key)
{
temp->val = val;
return;
}
temp = temp->next;
}
// 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;
t->list[pos] = newNode;
t->count++;
}
void *lookup(struct hashmap *t, uint64_t hash)
{
int pos = hashCode(t, hash);
struct node *list = t->list[pos];
struct node *temp = list;
while (temp)
{
if (temp->hash == hash)
{
return temp->val;
}
temp = temp->next;
}
return NULL;
}

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