start working on runtime

This commit is contained in:
2025-06-20 02:50:05 +01:00
parent e5e4f22481
commit bddfb59886
7 changed files with 55 additions and 10 deletions

View File

@@ -1,13 +1,14 @@
#include "hashmap.h"
#include <gc/gc.h>
#include <stdlib.h>
#include "../memory.h"
struct table *createTable(int size)
{
struct table *t = (struct table *)checked_malloc(sizeof(struct table));
struct table *t = (struct table *)ar_alloc(sizeof(struct table));
t->size = size;
t->list = (struct node **)checked_malloc(sizeof(struct node *) * size);
t->list = (struct node **)ar_alloc(sizeof(struct node *) * size);
int i;
for (i = 0; i < size; i++)
t->list[i] = NULL;
@@ -49,7 +50,7 @@ void insert(struct table *t, int key, void* val)
{
int pos = hashCode(t, key);
struct node *list = t->list[pos];
struct node *newNode = (struct node *)checked_malloc(sizeof(struct node));
struct node *newNode = (struct node *)ar_alloc(sizeof(struct node));
struct node *temp = list;
while (temp)
{

View File

@@ -1,15 +1,17 @@
#ifndef HASHMAP_H
#define HASHMAP_H
#include <stdlib.h>
struct node
{
int key;
size_t key;
void *val;
struct node *next;
};
struct table
{
int size;
size_t size;
size_t count;
struct node **list;
};

23
src/runtime/runtime.c Normal file
View File

@@ -0,0 +1,23 @@
#include "runtime.h"
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
uint64_t pop_bytecode(Translated *translated, RuntimeState *state) {
uint64_t *instruction = darray_get(&translated->bytecode, state->head++);
return *instruction;
}
void run_instruction(Translated *translated, RuntimeState *state) {
uint64_t opcode = pop_bytecode(translated, state);
switch (opcode) { case OP_LOAD_NULL: }
}
void runtime(Translated translated) {
RuntimeState state = {
checked_malloc(translated.registerCount * sizeof(size_t)), 0};
while (state.head < translated.bytecode.size)
run_instruction(&translated, &state);
free(state.registers);
}

19
src/runtime/runtime.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef RUNTIME_H
#define RUNTIME_H
#include "../translator/translator.h"
typedef struct {
uint64_t *registers;
size_t head;
} RuntimeState;
typedef struct {
} ArObject;
void run_instruction(Translated *translated, RuntimeState *state);
void runtime(Translated translated);
#endif // RUNTIME_H

View File

@@ -4,11 +4,11 @@
#include <stdio.h>
#include <string.h>
size_t translate_parsed_string(Translated *translated, ParsedString parsedString, size_t to_register) {
size_t translate_parsed_string(Translated *translated, ParsedString parsedString) {
size_t string_pos = arena_push(&translated->constants, parsedString.string, parsedString.length);
set_registers(translated, to_register+1);
set_registers(translated, 1);
size_t start = push_instruction_code(translated, OP_LOAD_CONST);
push_instruction_code(translated, to_register);
push_instruction_code(translated, 0);
push_instruction_code(translated, TYPE_OP_STRING);
push_instruction_code(translated,parsedString.length);
push_instruction_code(translated, string_pos);

View File

@@ -3,6 +3,6 @@
#include "../translator.h"
#include "../../parser/string/string.h"
size_t translate_parsed_string(Translated *translated, ParsedString parsedString, size_t to_register);
size_t translate_parsed_string(Translated *translated, ParsedString parsedString);
#endif

View File

@@ -82,7 +82,7 @@ void set_registers(Translated *translator, size_t count) {
size_t translate_parsed(Translated *translated, ParsedValue *parsedValue) {
switch (parsedValue->type) {
case AST_STRING:
return translate_parsed_string(translated, *((ParsedString*)parsedValue->data), 0);
return translate_parsed_string(translated, *((ParsedString*)parsedValue->data));
case AST_DECLARATION:
return translate_parsed_declaration(translated, *((DArray*)parsedValue->data));
case AST_NUMBER: