add null object and add load const

This commit is contained in:
2025-06-24 03:49:05 +01:00
parent 498cd39c04
commit 908d627962
8 changed files with 94 additions and 24 deletions

View File

@@ -1 +1,2 @@
null null
"hello world"

View File

@@ -0,0 +1,10 @@
#include "../../internals/hashmap/hashmap.h"
#include "../object.h"
#include <string.h>
#include "null.h"
ArgonObject *ARGON_NULL = NULL;
void init_null() {
ARGON_NULL = init_argon_object();
}

View File

@@ -0,0 +1,10 @@
#ifndef NULL_H
#define NULL_H
#include "../object.h"
extern ArgonObject *ARGON_NULL;
void init_null();
#endif // NULL_H

View File

@@ -3,10 +3,17 @@
#include "../runtime.h" #include "../runtime.h"
#include <string.h> #include <string.h>
ArgonObject *BASE_OBJECT = NULL;
void init_base_field() {
add_field(BASE_OBJECT, "test", BASE_OBJECT);
}
ArgonObject* init_argon_object() { ArgonObject* init_argon_object() {
ArgonObject *object = ar_alloc(sizeof(ArgonObject)); ArgonObject *object = ar_alloc(sizeof(ArgonObject));
object->type = TYPE_OBJECT; object->type = TYPE_OBJECT;
object->typeObject = NULL; object->typeObject = NULL;
object->baseObject = BASE_OBJECT;
object->fields = createHashmap(); object->fields = createHashmap();
memset(&object->value, 0, sizeof(object->value)); memset(&object->value, 0, sizeof(object->value));
return object; return object;

View File

@@ -4,6 +4,8 @@
#include <gmp.h> #include <gmp.h>
#include <stdbool.h> #include <stdbool.h>
extern ArgonObject *BASE_OBJECT;
struct string_struct { struct string_struct {
char *data; char *data;
size_t length; size_t length;
@@ -21,6 +23,7 @@ typedef enum {
struct ArgonObject { struct ArgonObject {
ArgonType type; ArgonType type;
ArgonObject *baseObject;
ArgonObject *typeObject; ArgonObject *typeObject;
struct hashmap *fields; // dynamic fields/methods struct hashmap *fields; // dynamic fields/methods
union { union {
@@ -32,6 +35,7 @@ struct ArgonObject {
} value; } value;
}; };
typedef struct ArgonObject ArgonObject; typedef struct ArgonObject ArgonObject;
void init_base_field();
ArgonObject *init_argon_object(); ArgonObject *init_argon_object();

View File

@@ -1,15 +1,22 @@
#include "../object.h" #include "../object.h"
#include <stdint.h> #include <stdint.h>
#include <stdio.h>
ArgonObject *ARGON_STRING_TYPE = NULL; ArgonObject *ARGON_STRING_TYPE = NULL;
ArgonObject *ARGON_STRING_BASE = NULL;
void init_string_type() { void init_string_type() {
ARGON_STRING_TYPE = init_argon_object(); ARGON_STRING_TYPE = init_argon_object();
ARGON_STRING_BASE = init_argon_object();
} }
ArgonObject *init_string_object(char*data, size_t length) { ArgonObject *init_string_object(char*data, size_t length) {
printf("%s\n", data);
ArgonObject * object = init_argon_object(); ArgonObject * object = init_argon_object();
object->typeObject = ARGON_STRING_TYPE; object->typeObject = ARGON_STRING_TYPE;
object->baseObject = ARGON_STRING_BASE;
object->value.as_str.data = data; object->value.as_str.data = data;
object->value.as_str.length = length; object->value.as_str.length = length;
return object; return object;

View File

@@ -1,17 +1,25 @@
#include "runtime.h" #include "runtime.h"
#include "../translator/translator.h"
#include "internals/siphash/siphash.h"
#include "objects/null/null.h"
#include "objects/object.h"
#include "objects/string/string.h"
#include "objects/type/type.h"
#include <fcntl.h>
#include <stddef.h> #include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include "internals/siphash/siphash.h"
#include "objects/type/type.h"
#include "objects/string/string.h"
void init_types() { void init_types() {
BASE_OBJECT = init_argon_object();
init_type(); init_type();
init_null();
init_string_type(); init_string_type();
init_base_field();
} }
uint64_t pop_bytecode(Translated *translated, RuntimeState *state) { uint64_t pop_bytecode(Translated *translated, RuntimeState *state) {
@@ -19,40 +27,62 @@ uint64_t pop_bytecode(Translated *translated, RuntimeState *state) {
return *instruction; return *instruction;
} }
void load_const(Translated *translated, RuntimeState *state) {
uint64_t to_register = pop_bytecode(translated, state);
types type = pop_bytecode(translated, state);
size_t length = pop_bytecode(translated, state);
uint64_t offset = pop_bytecode(translated, state);
void*data = ar_alloc(length);
memcpy(data, arena_get(&translated->constants,offset), length);
ArgonObject *object = ARGON_NULL;
switch (type) {
case TYPE_OP_STRING:
object = init_string_object(data, length);
break;
}
state->registers[to_register] = object;
}
void run_instruction(Translated *translated, RuntimeState *state) { void run_instruction(Translated *translated, RuntimeState *state) {
uint64_t opcode = pop_bytecode(translated, state); OperationType opcode = pop_bytecode(translated, state);
switch (opcode) { case OP_LOAD_NULL: pop_bytecode(translated, state);printf("null\n");} switch (opcode) {
case OP_LOAD_NULL:
state->registers[pop_bytecode(translated, state)] = ARGON_NULL;
break;
case OP_LOAD_CONST:
load_const(translated,state);
break;
}
} }
void runtime(Translated translated) { void runtime(Translated translated) {
RuntimeState state = { RuntimeState state = {
checked_malloc(translated.registerCount * sizeof(size_t)), 0}; checked_malloc(translated.registerCount * sizeof(ArgonObject *)), 0};
while (state.head < translated.bytecode.size) while (state.head < translated.bytecode.size)
run_instruction(&translated, &state); run_instruction(&translated, &state);
free(state.registers); free(state.registers);
} }
static uint8_t siphash_key[16]; static uint8_t siphash_key[16];
void generate_siphash_key() { void generate_siphash_key() {
int fd = open("/dev/urandom", O_RDONLY); int fd = open("/dev/urandom", O_RDONLY);
if (fd < 0 || read(fd, siphash_key, 16) != 16) { if (fd < 0 || read(fd, siphash_key, 16) != 16) {
// Fallback or abort // Fallback or abort
} }
close(fd); close(fd);
} }
uint64_t siphash64_bytes(const void *data, size_t len) { uint64_t siphash64_bytes(const void *data, size_t len) {
uint8_t out[8]; uint8_t out[8];
if (siphash(data, len, siphash_key, out, sizeof(out)) != 0) if (siphash(data, len, siphash_key, out, sizeof(out)) != 0)
return 0; return 0;
uint64_t hash = 0; uint64_t hash = 0;
for (int i = 0; i < 8; ++i) for (int i = 0; i < 8; ++i)
hash |= ((uint64_t)out[i]) << (8 * i); hash |= ((uint64_t)out[i]) << (8 * i);
return hash; return hash;
} }

View File

@@ -1,9 +1,10 @@
#ifndef RUNTIME_H #ifndef RUNTIME_H
#define RUNTIME_H #define RUNTIME_H
#include "../translator/translator.h" #include "../translator/translator.h"
#include "internals/hashmap/hashmap.h"
typedef struct { typedef struct {
uint64_t *registers; ArgonObject **registers;
size_t head; size_t head;
} RuntimeState; } RuntimeState;