add function object

This commit is contained in:
2025-06-26 05:11:34 +01:00
parent a275a0a0ad
commit a9d0ba0318
12 changed files with 80 additions and 11 deletions

View File

@@ -1,6 +0,0 @@
#ifndef FUNCTION_H
#define FUNCTION_H
#include "../object.h"
#endif // FUNCTION_H

View File

@@ -0,0 +1,46 @@
#include "../../runtime.h"
#include "../object.h"
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
ArgonObject *ARGON_FUNCTION_TYPE = NULL;
void init_function_type() {
ARGON_FUNCTION_TYPE = init_argon_class("Function");
}
void load_argon_function(Translated *translated, RuntimeState *state,
struct Stack stack) {
ArgonObject *object = init_child_argon_object(ARGON_FUNCTION_TYPE);
object->type = TYPE_FUNCTION;
uint64_t offset = pop_bytecode(translated, state);
uint64_t length = pop_bytecode(translated, state);
object->name = ar_alloc_atomic(length + 1);
memcpy(object->name, arena_get(&translated->constants, offset), length);
object->name[length] = '\0';
object->value.argon_fn.number_of_parameters = pop_bytecode(translated, state);
object->value.argon_fn.parameters =
ar_alloc(object->value.argon_fn.number_of_parameters * sizeof(char *));
printf("%s(", object->name);
for (size_t i = 0; i < object->value.argon_fn.number_of_parameters; i++) {
if(i!=0)printf(", ");
offset = pop_bytecode(translated, state);
length = pop_bytecode(translated, state);
object->value.argon_fn.parameters[i] = ar_alloc_atomic(length + 1);
memcpy(object->value.argon_fn.parameters[i], arena_get(&translated->constants, offset), length);
object->value.argon_fn.parameters[i][length] = '\0';
printf("%s", object->value.argon_fn.parameters[i]);
}
printf(") = ");
offset = pop_bytecode(translated, state);
length = pop_bytecode(translated, state);
darray_init(&object->value.argon_fn.bytecode, sizeof(uint64_t));
darray_resize(&object->value.argon_fn.bytecode, length/object->value.argon_fn.bytecode.element_size);
memcpy(object->value.argon_fn.bytecode.data, arena_get(&translated->constants, offset), length);
object->value.argon_fn.stack = stack;
fwrite(object->value.argon_fn.bytecode.data, object->value.argon_fn.bytecode.element_size, object->value.argon_fn.bytecode.size, stdout);
printf("\n");
state->registers[0]=object;
}

View File

@@ -0,0 +1,9 @@
#ifndef FUNCTION_H
#define FUNCTION_H
#include "../object.h"
void init_function_type();
ArgonObject *load_argon_function(Translated *translated, RuntimeState *state, struct Stack stack);
#endif // FUNCTION_H

View File

@@ -3,6 +3,7 @@
#include "../runtime.h"
#include <stdbool.h>
#include <string.h>
#include "type/type.h"
ArgonObject *BASE_CLASS = NULL;
@@ -24,7 +25,7 @@ ArgonObject* init_argon_class(char*name) {
object->name = name;
object->type = TYPE_OBJECT;
object->self = NULL;
object->baseObject = BASE_CLASS;
object->baseObject = ARGON_TYPE;
object->fields = createHashmap();
memset(&object->value, 0, sizeof(object->value));
return object;

View File

@@ -1,8 +1,10 @@
#ifndef OBJECT_H
#define OBJECT_H
#include "../internals/hashmap/hashmap.h"
#include "../../dynamic_array/darray.h"
#include <gmp.h>
#include <stdbool.h>
#include "../runtime.h"
extern ArgonObject *BASE_CLASS;
@@ -10,6 +12,12 @@ struct string_struct {
char *data;
size_t length;
};
struct argon_function_struct {
DArray bytecode;
struct Stack stack;
size_t number_of_parameters;
char** parameters;
};
typedef enum {
TYPE_NULL,
@@ -17,6 +25,7 @@ typedef enum {
TYPE_NUMBER,
TYPE_STRING,
TYPE_FUNCTION,
TYPE_NATIVE_FUNCTION,
TYPE_OBJECT, // generic user object
} ArgonType;
@@ -31,6 +40,7 @@ struct ArgonObject {
bool as_bool;
struct string_struct as_str;
void *native_fn;
struct argon_function_struct argon_fn;
// others as needed
} value;
};

View File

@@ -2,6 +2,7 @@
#include <stdint.h>
#include <stdio.h>
#include <stdio.h>
#include "string.h"
ArgonObject *ARGON_STRING_TYPE = NULL;

View File

@@ -6,5 +6,6 @@
ArgonObject *ARGON_TYPE = NULL;
void init_type() {
ARGON_TYPE = init_argon_class("function");
ARGON_TYPE = init_argon_class("type");
ARGON_TYPE->baseObject = BASE_CLASS;
}