From d4c03ea6ddd72967431d2e8ecaf7236ac1e4a9e5 Mon Sep 17 00:00:00 2001 From: Robotnik08 <89689979+Robotnik08@users.noreply.github.com> Date: Tue, 12 Nov 2024 20:22:29 +0100 Subject: [PATCH] dosato api fix --- dosato_libraries/dosato.h | 10 +++++++++- include/standard_libraries/std_array.h | 4 ++-- include/virtual-machine.h | 2 +- src/standard_libraries/std_array.c | 6 +++--- src/virtual-machine.c | 8 +++++++- 5 files changed, 22 insertions(+), 8 deletions(-) diff --git a/dosato_libraries/dosato.h b/dosato_libraries/dosato.h index 7fd91c8..91916b9 100644 --- a/dosato_libraries/dosato.h +++ b/dosato_libraries/dosato.h @@ -231,7 +231,7 @@ typedef struct { extern void printValue(Value value, bool extensive); /** - * @brief Casts a value to a specific type, if possible. The value will be destroyed and replaced with the new value, so make sure you have a copy of the value if you need it. + * @brief Casts a value to a specific type, if possible. * @param value The value to cast. * @param type The type to cast to, must be a value type, if you want to cast to a string, use the CAST_TO_STRING macro. * @return An error code, 0 if successful. if you get an error code, make sure to handle it accordingly. @@ -336,6 +336,14 @@ extern void removeFromKey(ValueObject* object, char* key); */ extern DosatoObject* buildDosatoObject(void* body, DataType type, bool sweep, void* vm); +/** + * @brief Calls the function with the given arguments. + * @param function The function to call, must be of type 'TYPE_FUNCTION', returns an exception if not. + * @param args The arguments to pass to the function. + * @param debug If true, prints debug information. + */ +extern Value callExternalFunction(Value function, ValueArray args, bool debug); + #define BUILD_STRING(value) (Value){ TYPE_STRING, .as.objectValue = buildDosatoObject(value, TYPE_STRING, false, main_vm), .defined = false, .is_variable_type = false, .is_constant = false } #define BUILD_ARRAY(value) (Value){ TYPE_ARRAY, .as.objectValue = buildDosatoObject(value, TYPE_ARRAY, false, main_vm), .defined = false, .is_variable_type = false, .is_constant = false } #define BUILD_OBJECT(value) (Value){ TYPE_OBJECT, .as.objectValue = buildDosatoObject(value, TYPE_OBJECT, false, main_vm), .defined = false, .is_variable_type = false, .is_constant = false } diff --git a/include/standard_libraries/std_array.h b/include/standard_libraries/std_array.h index b6024ec..b99e11b 100644 --- a/include/standard_libraries/std_array.h +++ b/include/standard_libraries/std_array.h @@ -3,8 +3,8 @@ #include "common_std.h" -Value dosato_quick_sort (ValueArray* array, int left, int right, Function* function); -Value dosato_partition (ValueArray* array, int left, int right, Function* function); +Value dosato_quick_sort (ValueArray* array, int left, int right, Value function); +Value dosato_partition (ValueArray* array, int left, int right, Value function); Value array_sort(ValueArray args, bool debug); Value array_push(ValueArray args, bool debug); diff --git a/include/virtual-machine.h b/include/virtual-machine.h index 042d3ec..e9a6656 100644 --- a/include/virtual-machine.h +++ b/include/virtual-machine.h @@ -76,7 +76,7 @@ void destroy_FunctionList(FunctionList* list); void init_Function(Function* func); -Value callExternalFunction(Function* function, ValueArray args, bool debug); +Value callExternalFunction(Value function, ValueArray args, bool debug); DosatoObject* buildDosatoObject(void* body, DataType type, bool sweep, void* vm); void markObjects (VirtualMachine* vm); diff --git a/src/standard_libraries/std_array.c b/src/standard_libraries/std_array.c index 3127412..0570eaf 100644 --- a/src/standard_libraries/std_array.c +++ b/src/standard_libraries/std_array.c @@ -37,7 +37,7 @@ int compareValues (const void* a, const void* b) { } } -Value dosato_quick_sort (ValueArray* array, int left, int right, Function* function) { +Value dosato_quick_sort (ValueArray* array, int left, int right, Value function) { if (left < right) { Value pivot_value = dosato_partition(array, left, right, function); if (pivot_value.type == TYPE_EXCEPTION || pivot_value.type == TYPE_HLT) { @@ -56,7 +56,7 @@ Value dosato_quick_sort (ValueArray* array, int left, int right, Function* funct return UNDEFINED_VALUE; } -Value dosato_partition (ValueArray* array, int left, int right, Function* function) { +Value dosato_partition (ValueArray* array, int left, int right, Value function) { Value pivot = array->values[right]; int i = left - 1; @@ -119,7 +119,7 @@ Value array_sort(ValueArray args, bool debug) { if (arg2.type != TYPE_FUNCTION) { return BUILD_EXCEPTION(E_NOT_A_FUNCTION); } - Value res = dosato_quick_sort(AS_ARRAY(arg), 0, AS_ARRAY(arg)->count - 1, AS_FUNCTION(arg2)); + Value res = dosato_quick_sort(AS_ARRAY(arg), 0, AS_ARRAY(arg)->count - 1, arg2); if (res.type == TYPE_EXCEPTION || res.type == TYPE_HLT) { return res; diff --git a/src/virtual-machine.c b/src/virtual-machine.c index 296cc92..8fd0841 100644 --- a/src/virtual-machine.c +++ b/src/virtual-machine.c @@ -1977,7 +1977,13 @@ int runVirtualMachine (VirtualMachine* vm, int debug, bool is_main) { #undef PRINT_ERROR -Value callExternalFunction(Function* function, ValueArray args, bool debug) { +Value callExternalFunction(Value func, ValueArray args, bool debug) { + if (func.type != TYPE_FUNCTION) { + return BUILD_EXCEPTION(E_NOT_A_FUNCTION); + } + + Function* function = AS_FUNCTION(func); + if (function->is_compiled) { Value return_val = ((DosatoFunction)function->func_ptr)(args, debug); return return_val;