Skip to content

Commit

Permalink
Merge pull request #54 from Robotnik08/development
Browse files Browse the repository at this point in the history
into main
  • Loading branch information
Robotnik08 authored Nov 12, 2024
2 parents f03dcbc + 980e43c commit 874cf54
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 8 deletions.
10 changes: 9 additions & 1 deletion dosato_libraries/dosato.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 }
Expand Down
4 changes: 2 additions & 2 deletions include/standard_libraries/std_array.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion include/virtual-machine.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/standard_libraries/std_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
8 changes: 7 additions & 1 deletion src/virtual-machine.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 874cf54

Please sign in to comment.