From a9d0ba0318dfdcbad7a8604a539ee75ae1b43dfc Mon Sep 17 00:00:00 2001 From: William Bell Date: Thu, 26 Jun 2025 05:11:34 +0100 Subject: [PATCH] add function object --- null_test.ar | 2 +- src/runtime/objects/function/functions.c | 0 src/runtime/objects/function/functions.h | 6 --- src/runtime/objects/functions/functions.c | 46 +++++++++++++++++++++++ src/runtime/objects/functions/functions.h | 9 +++++ src/runtime/objects/object.c | 3 +- src/runtime/objects/object.h | 10 +++++ src/runtime/objects/string/string.c | 1 + src/runtime/objects/type/type.c | 3 +- src/runtime/runtime.c | 5 +++ src/runtime/runtime.h | 2 + src/translator/function/function.c | 4 +- 12 files changed, 80 insertions(+), 11 deletions(-) delete mode 100644 src/runtime/objects/function/functions.c delete mode 100644 src/runtime/objects/function/functions.h create mode 100644 src/runtime/objects/functions/functions.c create mode 100644 src/runtime/objects/functions/functions.h diff --git a/null_test.ar b/null_test.ar index 051364a..c156d2e 100644 --- a/null_test.ar +++ b/null_test.ar @@ -1 +1 @@ -let x(hello,lol,world, WORLD,HELLOOOO,LOLLLLLL, WORLD)="HELLOOOO WORLD" \ No newline at end of file +let x(hello,lol,world, WORLD,HELLOOOO,LOLLLLLL, WORLD)=let f(x)="bruh" \ No newline at end of file diff --git a/src/runtime/objects/function/functions.c b/src/runtime/objects/function/functions.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/runtime/objects/function/functions.h b/src/runtime/objects/function/functions.h deleted file mode 100644 index fe94518..0000000 --- a/src/runtime/objects/function/functions.h +++ /dev/null @@ -1,6 +0,0 @@ -#ifndef FUNCTION_H -#define FUNCTION_H -#include "../object.h" - - -#endif // FUNCTION_H \ No newline at end of file diff --git a/src/runtime/objects/functions/functions.c b/src/runtime/objects/functions/functions.c new file mode 100644 index 0000000..86c0ed9 --- /dev/null +++ b/src/runtime/objects/functions/functions.c @@ -0,0 +1,46 @@ +#include "../../runtime.h" +#include "../object.h" +#include +#include +#include +#include + +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; +} \ No newline at end of file diff --git a/src/runtime/objects/functions/functions.h b/src/runtime/objects/functions/functions.h new file mode 100644 index 0000000..244f748 --- /dev/null +++ b/src/runtime/objects/functions/functions.h @@ -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 \ No newline at end of file diff --git a/src/runtime/objects/object.c b/src/runtime/objects/object.c index 115b15e..273a86e 100644 --- a/src/runtime/objects/object.c +++ b/src/runtime/objects/object.c @@ -3,6 +3,7 @@ #include "../runtime.h" #include #include +#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; diff --git a/src/runtime/objects/object.h b/src/runtime/objects/object.h index 05b18dc..2766e79 100644 --- a/src/runtime/objects/object.h +++ b/src/runtime/objects/object.h @@ -1,8 +1,10 @@ #ifndef OBJECT_H #define OBJECT_H #include "../internals/hashmap/hashmap.h" +#include "../../dynamic_array/darray.h" #include #include +#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; }; diff --git a/src/runtime/objects/string/string.c b/src/runtime/objects/string/string.c index 98f438b..96e3694 100644 --- a/src/runtime/objects/string/string.c +++ b/src/runtime/objects/string/string.c @@ -2,6 +2,7 @@ #include #include #include +#include "string.h" ArgonObject *ARGON_STRING_TYPE = NULL; diff --git a/src/runtime/objects/type/type.c b/src/runtime/objects/type/type.c index b32a735..3a66158 100644 --- a/src/runtime/objects/type/type.c +++ b/src/runtime/objects/type/type.c @@ -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; } \ No newline at end of file diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c index 32a4413..a28c961 100644 --- a/src/runtime/runtime.c +++ b/src/runtime/runtime.c @@ -5,6 +5,7 @@ #include "objects/object.h" #include "objects/string/string.h" #include "objects/type/type.h" +#include "objects/functions/functions.h" #include #include #include @@ -16,6 +17,7 @@ void init_types() { BASE_CLASS = init_argon_class("BASE_CLASS"); init_type(); + init_function_type(); init_null(); init_string_type(); @@ -53,6 +55,9 @@ void run_instruction(Translated *translated, RuntimeState *state, struct Stack s case OP_LOAD_CONST: load_const(translated,state); break; + case OP_LOAD_FUNCTION: + load_argon_function(translated, state, stack); + break; } } diff --git a/src/runtime/runtime.h b/src/runtime/runtime.h index fe53bae..000cefc 100644 --- a/src/runtime/runtime.h +++ b/src/runtime/runtime.h @@ -15,6 +15,8 @@ typedef struct Stack { void init_types(); +uint64_t pop_bytecode(Translated *translated, RuntimeState *state); + void run_instruction(Translated *translated, RuntimeState *state, struct Stack stack); void runtime(Translated translated); diff --git a/src/translator/function/function.c b/src/translator/function/function.c index a2357b0..e9cd8a2 100644 --- a/src/translator/function/function.c +++ b/src/translator/function/function.c @@ -12,7 +12,7 @@ size_t translate_parsed_function(Translated *translated, translate_parsed(translated, parsedFunction->body); size_t function_bytecode_offset = arena_push(&translated->constants, translated->bytecode.data, - translated->bytecode.size); + translated->bytecode.size*translated->bytecode.element_size); size_t function_bytecode_length = translated->bytecode.size; translated->bytecode = main_bytecode; darray_free(&temp_bytecode, NULL); @@ -30,6 +30,6 @@ size_t translate_parsed_function(Translated *translated, push_instruction_code(translated, strlen(*parameter_name)); } push_instruction_code(translated, function_bytecode_offset); - push_instruction_code(translated, function_bytecode_length); + push_instruction_code(translated, function_bytecode_length*translated->bytecode.element_size); return start; } \ No newline at end of file