diff options
Diffstat (limited to 'api')
-rw-r--r-- | api/bool.c | 6 | ||||
-rw-r--r-- | api/bool.h | 5 | ||||
-rw-r--r-- | api/int.c | 11 | ||||
-rw-r--r-- | api/int.h | 4 | ||||
-rw-r--r-- | api/nil.c | 8 | ||||
-rw-r--r-- | api/nil.h | 3 | ||||
-rw-r--r-- | api/ref.c | 17 | ||||
-rw-r--r-- | api/ref.h | 4 | ||||
-rw-r--r-- | api/str.c | 8 | ||||
-rw-r--r-- | api/str.h | 5 | ||||
-rw-r--r-- | api/util.c | 15 | ||||
-rw-r--r-- | api/util.h | 8 | ||||
-rw-r--r-- | api/vm.c | 74 | ||||
-rw-r--r-- | api/vm.h | 14 |
14 files changed, 112 insertions, 70 deletions
@@ -25,7 +25,7 @@ bool uwubool_get(UwUVMValue vm_value) return true; } -static void *uwubool_copy(void *data) +static void *uwubool_clone(void *data) { bool *copy = malloc(sizeof(*copy)); *copy = *(bool *) data; @@ -38,7 +38,7 @@ static char *uwubool_print(void *data) } UwUVMType uwubool_type = { - .copy = &uwubool_copy, - .delete = &free, + .clone = &uwubool_clone, + .delet = &free, .print = &uwubool_print, }; @@ -5,8 +5,7 @@ #include "vm.h" extern UwUVMType uwubool_type; - -UwUVMValue uwubool_create(bool value); -bool uwubool_get(UwUVMValue vm_value); +UwUVMValue uwubool_create(bool value); +bool uwubool_get(UwUVMValue vm_value); #endif @@ -13,7 +13,12 @@ UwUVMValue uwuint_create(int value) return vm_value; } -void *uwuint_copy(void *data) +int uwuint_get(UwUVMValue vm_value) +{ + return *(int *) vm_value.data; +} + +void *uwuint_clone(void *data) { int *copy = malloc(sizeof(*copy)); *copy = *(int *) data; @@ -26,7 +31,7 @@ char *uwuint_print(void *data) } UwUVMType uwuint_type = { - .copy = &uwuint_copy, - .delete = &free, + .clone = &uwuint_clone, + .delet = &free, .print = &uwuint_print, }; @@ -4,7 +4,7 @@ #include "vm.h" extern UwUVMType uwuint_type; - -UwUVMValue uwuint_create(int value); +UwUVMValue uwuint_create(int value); +int uwuint_get(UwUVMValue vm_value); #endif @@ -9,12 +9,12 @@ UwUVMValue uwunil_create() }; } -static void *uwunil_copy(void *data) +static void *uwunil_clone(void *data) { return data; } -static void uwunil_delete(void *data) +static void uwunil_delet(void *data) { (void) data; } @@ -26,7 +26,7 @@ static char *uwunil_print(void *data) } UwUVMType uwunil_type = { - .copy = &uwunil_copy, - .delete = &uwunil_delete, + .clone = &uwunil_clone, + .delet = &uwunil_delet, .print = &uwunil_print, }; @@ -4,7 +4,6 @@ #include "vm.h" extern UwUVMType uwunil_type; - -UwUVMValue uwunil_create(); +UwUVMValue uwunil_create(); #endif @@ -1,20 +1,25 @@ #include "../src/util.h" #include "ref.h" -UwUVMValue uwuref_create(UwUVMFunction *function) +UwUVMValue uwuref_create(UwUVMFunction *value) { return (UwUVMValue) { .type = &uwuref_type, - .data = function, + .data = value, }; } -static void *uwuref_copy(void *data) +UwUVMFunction *uwuref_get(UwUVMValue value) +{ + return value.data; +} + +static void *uwuref_clone(void *data) { return data; } -static void uwuref_delete(void *data) +static void uwuref_delet(void *data) { (void) data; } @@ -25,7 +30,7 @@ static char *uwuref_print(void *data) } UwUVMType uwuref_type = { - .copy = &uwuref_copy, - .delete = &uwuref_delete, + .clone = &uwuref_clone, + .delet = &uwuref_delet, .print = &uwuref_print, }; @@ -4,7 +4,7 @@ #include "vm.h" extern UwUVMType uwuref_type; - -UwUVMValue uwuref_create(UwUVMFunction *function); +UwUVMValue uwuref_create(UwUVMFunction *value); +UwUVMFunction *uwuref_get(UwUVMValue vm_value); #endif @@ -12,10 +12,10 @@ UwUVMValue uwustr_create(const char *value) char *uwustr_get(UwUVMValue vm_value) { - vm_value.type->print(vm_value.data); + return uwuvm_print_value(vm_value); } -static void *uwustr_copy(void *data) +static void *uwustr_clone(void *data) { return strdup(data); } @@ -26,7 +26,7 @@ static char *uwustr_print(void *data) } UwUVMType uwustr_type = { - .copy = &uwustr_copy, - .delete = &free, + .clone = &uwustr_clone, + .delet = &free, .print = &uwustr_print, }; @@ -4,8 +4,7 @@ #include "vm.h" extern UwUVMType uwustr_type; - -UwUVMValue uwustr_create(const char *value); -char *uwustr_get(UwUVMValue vm_value); +UwUVMValue uwustr_create(const char *value); +char *uwustr_get(UwUVMValue vm_value); #endif diff --git a/api/util.c b/api/util.c new file mode 100644 index 0000000..c3e8a38 --- /dev/null +++ b/api/util.c @@ -0,0 +1,15 @@ +#include "../src/err.h" +#include "util.h" +#include "bool.h" + +UwUVMValue uwuutil_is_type(const char *fnname, UwUVMArgs *args, UwUVMType *type) +{ + if (args->num < 1) + error("error: %s requires at least one argument\n", fnname); + + for (size_t i = 0; i < args->num; i++) + if (uwuvm_get_arg(args, i).type != type) + return uwubool_create(false); + + return uwubool_create(true); +} diff --git a/api/util.h b/api/util.h new file mode 100644 index 0000000..259f53a --- /dev/null +++ b/api/util.h @@ -0,0 +1,8 @@ +#ifndef _API_UTIL_H_ +#define _API_UTIL_H_ + +#include "vm.h" + +UwUVMValue uwuutil_is_type(const char *fnname, UwUVMArgs *args, UwUVMType *type); + +#endif @@ -6,33 +6,22 @@ #include "ref.h" #include "int.h" -void uwuvm_free_value(UwUVMValue value) +UwUVMValue uwuvm_clone_value(UwUVMValue value) { - value.type->delete(value.data); + return (UwUVMValue) { + .type = value.type, + .data = value.type->clone(value.data), + }; } -void uwuvm_free_args(UwUVMArgs *args) +void uwuvm_delet_value(UwUVMValue value) { - if (args->evaluated) { - for (size_t i = 0; i < args->num; i++) { - UwUVMValue *value = args->evaluated[i]; - - if (value) { - uwuvm_free_value(*value); - free(value); - } - } - - free(args->evaluated); - } + value.type->delet(value.data); } -UwUVMValue uwuvm_copy_value(UwUVMValue value) +char *uwuvm_print_value(UwUVMValue value) { - return (UwUVMValue) { - .type = value.type, - .data = value.type->copy(value.data), - }; + return value.type->print(value.data); } UwUVMValue uwuvm_get_arg(UwUVMArgs *args, size_t i) @@ -58,30 +47,53 @@ UwUVMValue uwuvm_evaluate_expression(UwUVMExpression *expression, UwUVMArgs *arg if ((size_t) expression->value.int_value >= args->num) error("error: not enough arguments (accessed argument $%d, but only %lu arguments were passed)\n", expression->value.int_value, args->num); - return uwuvm_copy_value(uwuvm_get_arg(args, expression->value.int_value)); + return uwuvm_clone_value(uwuvm_get_arg(args, expression->value.int_value)); case EX_FNNAME: return uwuref_create(expression->value.ref_value); case EX_FNCALL: - return uwuvm_run_function(expression->value.cll_value.function, (UwUVMArgs) { - .num = expression->value.cll_value.num_args, - .evaluated = expression->value.cll_value.num_args == 0 ? NULL : calloc(expression->value.cll_value.num_args, sizeof(UwUVMValue *)), - .unevaluated = expression->value.cll_value.args, - .super = args, - }); + return uwuvm_call_function( + expression->value.cll_value.function, + expression->value.cll_value.num_args, + expression->value.cll_value.args, + args + ); default: return (UwUVMValue) {}; } } -UwUVMValue uwuvm_run_function(UwUVMFunction *function, UwUVMArgs args) +UwUVMValue uwuvm_call_function(UwUVMFunction *function, size_t num_args, UwUVMExpression *unevaluated_args, UwUVMArgs *super_args) { - UwUVMValue value = function->type == MODULE_PLAIN + UwUVMValue *evaluated_args[num_args]; + + for (size_t i = 0; i < num_args; i++) + evaluated_args[i] = NULL; + + UwUVMArgs args = { + .num = num_args, + .evaluated = evaluated_args, + .unevaluated = unevaluated_args, + .super = super_args, + }; + + UwUVMValue return_value = function->type == MODULE_PLAIN ? uwuvm_evaluate_expression(function->value.plain, &args) : function->value.native(&args); - uwuvm_free_args(&args); - return value; + if (num_args > 0) { + for (size_t i = 0; i < num_args; i++) { + UwUVMValue *value = evaluated_args[i]; + + if (value) { + uwuvm_delet_value(*value); + free(value); + } + } + + } + + return return_value; } @@ -13,9 +13,9 @@ typedef enum typedef struct { - void *(*copy )(void *data); - void (*delete)(void *data); - char *(*print )(void *data); + void *(*clone)(void *data); + void (*delet)(void *data); + char *(*print)(void *data); } UwUVMType; typedef struct @@ -71,11 +71,11 @@ typedef struct size_t num_libraries; } UwUVMProgram; -void uwuvm_free_value(UwUVMValue value); -void uwuvm_free_args(UwUVMArgs *args); -UwUVMValue uwuvm_copy_value(UwUVMValue value); +UwUVMValue uwuvm_clone_value(UwUVMValue value); +void uwuvm_delet_value(UwUVMValue value); +char *uwuvm_print_value(UwUVMValue value); UwUVMValue uwuvm_get_arg(UwUVMArgs *args, size_t i); UwUVMValue uwuvm_evaluate_expression(UwUVMExpression *expression, UwUVMArgs *args); -UwUVMValue uwuvm_run_function(UwUVMFunction *function, UwUVMArgs args); +UwUVMValue uwuvm_call_function(UwUVMFunction *function, size_t num_args, UwUVMExpression *unevaluated_args, UwUVMArgs *super_args); #endif |