start working on runtime oop

This commit is contained in:
William Bell
2025-08-02 20:17:24 +01:00
parent 8ea69cce67
commit a7d7ded803
19 changed files with 142 additions and 71 deletions

View File

@@ -7,11 +7,11 @@
#ifndef AROBJECT_H
#define AROBJECT_H
#include <gmp.h>
#include "runtime/internals/dynamic_array_armem/darray_armem.h"
#include "runtime/internals/hashmap/hashmap.h"
#include <gmp.h>
typedef struct ArgonObject ArgonObject; // forward declaration
typedef struct ArgonObject ArgonObject; // forward declaration
typedef enum ArgonType {
TYPE_NULL,
@@ -40,13 +40,12 @@ struct argon_function_struct {
char **parameters;
};
// full definition of ArgonObject (no typedef again!)
struct ArgonObject {
ArgonType type;
char *name;
ArgonObject *self;
ArgonObject *baseObject;
struct hashmap_GC *fields;
struct hashmap_GC *dict;
union {
mpq_t as_number;
bool as_bool;

View File

@@ -8,17 +8,14 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../memory.h"
void darray_init(DArray *arr, size_t element_size) {
arr->element_size = element_size;
arr->size = 0;
arr->capacity = CHUNK_SIZE;
arr->data = malloc(CHUNK_SIZE * element_size);
arr->capacity = CHUNK_SIZE / element_size;
arr->data = checked_malloc(CHUNK_SIZE); // fixed byte allocation
arr->resizable = true;
if (!arr->data) {
fprintf(stderr, "darray_init: allocation failed\n");
exit(EXIT_FAILURE);
}
}
void darray_resize(DArray *arr, size_t new_size) {
@@ -26,9 +23,17 @@ void darray_resize(DArray *arr, size_t new_size) {
fprintf(stderr, "darray_resize: unresizable darray\n");
exit(EXIT_FAILURE);
}
size_t new_capacity = ((new_size + CHUNK_SIZE) / CHUNK_SIZE) * CHUNK_SIZE;
// Determine number of full chunks needed to store new_size elements
size_t required_bytes = new_size * arr->element_size;
size_t new_capacity_bytes = ((required_bytes + CHUNK_SIZE - 1) / CHUNK_SIZE) * CHUNK_SIZE;
size_t new_capacity = new_capacity_bytes / arr->element_size;
if (!new_capacity) {
return;
}
if (new_capacity != arr->capacity) {
void *new_data = realloc(arr->data, new_capacity * arr->element_size);
void *new_data = realloc(arr->data, new_capacity_bytes);
if (!new_data) {
fprintf(stderr, "darray_resize: reallocation failed\n");
exit(EXIT_FAILURE);

View File

@@ -10,7 +10,7 @@
#include <stdbool.h>
#include <stddef.h> // for size_t
#define CHUNK_SIZE 1048576
#define CHUNK_SIZE 4096
typedef struct {
void *data;

View File

@@ -140,7 +140,7 @@ void output_err(ArErr err) {
fprintf(stderr, " ");
}
fprintf(stderr, "|\n");
for (size_t i = 0;i<len;i++) {
for (ssize_t i = 0;i<len;i++) {
if (buffer[i] == '\n') {
buffer[i] = '\0';
break;

View File

@@ -38,6 +38,8 @@ void ar_memory_init() {
void *ar_alloc(size_t size) { return GC_MALLOC(size); }
void *ar_realloc(void * old,size_t size) { return GC_REALLOC(old, size); }
void ar_finalizer(void *obj, GC_finalization_proc fn, void *client_data,
GC_finalization_proc *old_fn, void **old_client_data) {
return GC_register_finalizer_no_order(obj, fn, client_data, old_fn, old_client_data);

View File

@@ -15,6 +15,7 @@
void ar_finalizer(void *obj, GC_finalization_proc fn, void *client_data,
GC_finalization_proc *old_fn, void **old_client_data);
void *ar_alloc(size_t size);
void *ar_realloc(void * old,size_t size);
void *ar_alloc_atomic(size_t size);
char *ar_strdup(const char *str);

0
src/runtime/call/call.c Normal file
View File

0
src/runtime/call/call.h Normal file
View File

View File

@@ -19,6 +19,7 @@ ArErr runtime_declaration(Translated *translated, RuntimeState *state,
SourceLocation *source_location = darray_get(&translated->source_locations, source_location_index);
return create_err(source_location->line, source_location->column, source_location->length, state->path, "Runtime Error", "Identifier '%.*s' has already been declared in the current scope", length, arena_get(&translated->constants, offset));
}
hashmap_insert_GC(stack->scope, hash, arena_get(&translated->constants, offset), state->registers[from_register], 0);
ArgonObject * key = init_string_object(arena_get(&translated->constants, offset), length);
hashmap_insert_GC(stack->scope, hash, key, state->registers[from_register], 0);
return no_err;
}

View File

@@ -7,6 +7,7 @@
#ifndef runtime_declaration_H
#define runtime_declaration_H
#include "../runtime.h"
#include "../objects/string/string.h"
ArErr runtime_declaration(Translated *translated, RuntimeState *state,
struct Stack *stack);

View File

@@ -12,11 +12,17 @@
#include <string.h>
void darray_armem_init(darray_armem *arr, size_t element_size) {
if (element_size > CHUNK_SIZE) {
fprintf(stderr, "darray_armem_init: element size larger than chunk size\n");
exit(EXIT_FAILURE);
}
arr->element_size = element_size;
arr->size = 0;
arr->capacity = CHUNK_SIZE;
arr->data = ar_alloc(CHUNK_SIZE * element_size);
arr->capacity = CHUNK_SIZE / element_size;
arr->data = ar_alloc(CHUNK_SIZE); // fixed-size byte allocation
arr->resizable = true;
if (!arr->data) {
fprintf(stderr, "darray_armem_init: allocation failed\n");
exit(EXIT_FAILURE);
@@ -28,15 +34,23 @@ void darray_armem_resize(darray_armem *arr, size_t new_size) {
fprintf(stderr, "darray_armem_resize: unresizable darray_armem\n");
exit(EXIT_FAILURE);
}
size_t new_capacity = ((new_size + CHUNK_SIZE) / CHUNK_SIZE) * CHUNK_SIZE;
size_t required_bytes = new_size * arr->element_size;
size_t new_capacity_bytes =
((required_bytes + CHUNK_SIZE - 1) / CHUNK_SIZE) * CHUNK_SIZE;
size_t new_capacity = new_capacity_bytes / arr->element_size;
if (!new_capacity) {
return;
}
if (new_capacity != arr->capacity) {
void *new_data = ar_alloc(new_capacity * arr->element_size);
memccpy(new_data,arr->data, arr->element_size, arr->capacity);
if (!new_data) {
printf("%zu\n", new_capacity_bytes);
arr->data = ar_realloc(arr->data, new_capacity_bytes);
if (!arr->data) {
fprintf(stderr, "darray_armem_resize: reallocation failed\n");
exit(EXIT_FAILURE);
}
arr->data = new_data;
arr->capacity = new_capacity;
}
@@ -67,11 +81,11 @@ void darray_armem_pop(darray_armem *arr, void (*free_data)(void *)) {
return;
if (free_data) {
void *target = (char *)arr->data + (arr->size-1) * arr->element_size;
void *target = (char *)arr->data + (arr->size - 1) * arr->element_size;
free_data(target);
}
darray_armem_resize(arr, arr->size-1);
darray_armem_resize(arr, arr->size - 1);
}
void *darray_armem_get(darray_armem *arr, size_t index) {

View File

@@ -10,7 +10,7 @@
#include <stdbool.h>
#include <stddef.h> // for size_t
#define CHUNK_SIZE 1048576
#define CHUNK_SIZE 4096
typedef struct {
void *data;

View File

@@ -29,8 +29,9 @@ ArgonObject *init_argon_class(char *name) {
ArgonObject *object = ar_alloc(sizeof(ArgonObject));
object->name = name;
object->type = TYPE_OBJECT;
object->self = NULL;
object->baseObject = ARGON_TYPE;
object->self = object;
object->classObject = ;
object->baseObject = BASE_CLASS;
object->fields = createHashmap_GC();
memset(&object->value, 0, sizeof(object->value));
return object;

View File

@@ -23,14 +23,6 @@
#include <string.h>
#include <unistd.h>
uint64_t bytes_to_uint64(const uint8_t bytes[8]) {
uint64_t value = 0;
for (int i = 0; i < 8; i++) {
value |= ((uint64_t)bytes[i]) << (i * 8);
}
return value;
}
void init_types() {
BASE_CLASS = init_argon_class("BASE_CLASS");
@@ -40,6 +32,13 @@ void init_types() {
init_string_type();
init_base_field();
BASE_CLASS->baseObject = ARGON_NULL;
}
int compare_by_order(const void *a, const void *b) {
const struct node_GC *itemA = (const struct node_GC *)a;
const struct node_GC *itemB = (const struct node_GC *)b;
return itemA->order - itemB->order;
}
uint8_t pop_byte(Translated *translated, RuntimeState *state) {
@@ -47,11 +46,11 @@ uint8_t pop_byte(Translated *translated, RuntimeState *state) {
}
uint64_t pop_bytecode(Translated *translated, RuntimeState *state) {
uint8_t bytes[8];
for (size_t i = 0; i < sizeof(bytes); i++) {
bytes[i] = *((uint8_t *)darray_get(&translated->bytecode, state->head++));
uint64_t value = 0;
for (int i = 0; i < 8; i++) {
value |= ((uint64_t)pop_byte(translated, state)) << (i * 8);
}
return bytes_to_uint64(bytes);
return value;
}
void load_const(Translated *translated, RuntimeState *state) {
@@ -147,7 +146,28 @@ ArErr run_instruction(Translated *translated, RuntimeState *state,
case OP_NEW_SCOPE:
*stack = create_scope(*stack);
break;
case OP_POP_SCOPE:
case OP_POP_SCOPE:;
// struct node_GC *array =
// checked_malloc(sizeof(struct node_GC) * (*stack)->scope->count);
// size_t j = 0;
// for (size_t i = 0; i < (*stack)->scope->size; i++) {
// struct node_GC *temp = (*stack)->scope->list[i];
// while (temp) {
// array[j++] = *temp;
// temp = temp->next;
// }
// }
// qsort(array, (*stack)->scope->count, sizeof(struct node_GC),
// compare_by_order);
// for (size_t i = 0; i < (*stack)->scope->count; i++) {
// struct node_GC temp = array[i];
// printf("%.*s = %.*s\n",
// (int)((ArgonObject *)temp.key)->value.as_str.length,
// ((ArgonObject *)temp.key)->value.as_str.data,
// (int)((ArgonObject *)temp.val)->value.as_str.length,
// ((ArgonObject *)temp.val)->value.as_str.data);
// }
// free(array);
*stack = (*stack)->prev;
break;
default:
@@ -160,7 +180,7 @@ ArErr run_instruction(Translated *translated, RuntimeState *state,
RuntimeState init_runtime_state(Translated translated, char *path) {
return (RuntimeState){
checked_malloc(translated.registerCount * sizeof(ArgonObject *)), 0, path,
ARGON_NULL};
ARGON_NULL, NULL, NULL};
}
Stack *create_scope(Stack *prev) {
@@ -172,11 +192,21 @@ Stack *create_scope(Stack *prev) {
ArErr runtime(Translated translated, RuntimeState state, Stack *stack) {
state.head = 0;
while (state.head < translated.bytecode.size) {
ArErr err = run_instruction(&translated, &state, &stack);
if (err.exists) {
return err;
StackFrame *currentStackFrame = checked_malloc(sizeof(StackFrame));
*currentStackFrame = (StackFrame){translated, state, stack, NULL};
state.currentStackFramePointer = &currentStackFrame;
ArErr err = no_err;
while (currentStackFrame) {
while (currentStackFrame->state.head <
currentStackFrame->translated.bytecode.size &&
!err.exists) {
err =
run_instruction(&currentStackFrame->translated,
&currentStackFrame->state, &currentStackFrame->stack);
}
StackFrame *tempStackFrame = currentStackFrame;
currentStackFrame = currentStackFrame->previousStackFrame;
free(tempStackFrame);
}
return no_err;
return err;
}

View File

@@ -6,20 +6,35 @@
#ifndef RUNTIME_H
#define RUNTIME_H
#include "../returnTypes.h"
#include "../translator/translator.h"
#include "internals/hashmap/hashmap.h"
#include "../returnTypes.h"
typedef struct {
typedef struct StackFrame StackFrame;
typedef struct RuntimeState RuntimeState;
typedef ArErr (*error_result)(ArErr, Translated *translated,
RuntimeState *state, struct Stack **stack);
typedef struct RuntimeState {
ArgonObject **registers;
size_t head;
char*path;
ArgonObject * return_value;
char *path;
ArgonObject *return_value;
StackFrame **currentStackFramePointer;
error_result result;
} RuntimeState;
typedef struct StackFrame {
Translated translated;
RuntimeState state;
Stack *stack;
StackFrame *previousStackFrame;
} StackFrame;
void init_types();
extern struct hashmap * runtime_hash_table;
extern struct hashmap *runtime_hash_table;
uint64_t runtime_hash(const void *data, size_t len, uint64_t prehash);