diff options
Diffstat (limited to 'js/scripting-lang/baba-yaga-c/src/function.c')
-rw-r--r-- | js/scripting-lang/baba-yaga-c/src/function.c | 49 |
1 files changed, 39 insertions, 10 deletions
diff --git a/js/scripting-lang/baba-yaga-c/src/function.c b/js/scripting-lang/baba-yaga-c/src/function.c index 57910cc..bb5bedf 100644 --- a/js/scripting-lang/baba-yaga-c/src/function.c +++ b/js/scripting-lang/baba-yaga-c/src/function.c @@ -46,6 +46,8 @@ typedef struct { char* source; /**< Source code for debugging */ } FunctionBody; + + /** * @brief Function value structure */ @@ -56,7 +58,7 @@ typedef struct { int param_count; /**< Number of parameters */ int required_params; /**< Number of required parameters */ union { - Value (*native_func)(Value*, int); /**< Native function pointer */ + Value (*native_func)(Value*, int, Scope*); /**< Native function pointer */ FunctionBody user_body; /**< User function body */ } body; void* closure_scope; /**< Closure scope (placeholder) */ @@ -86,7 +88,9 @@ static void function_body_destroy(FunctionBody* body) { * Public Function API * ============================================================================ */ -Value baba_yaga_value_function(const char* name, Value (*body)(Value*, int), + + +Value baba_yaga_value_function(const char* name, Value (*body)(Value*, int, Scope*), int param_count, int required_param_count) { Value value; value.type = VAL_FUNCTION; @@ -140,18 +144,41 @@ Value baba_yaga_function_call(const Value* func, const Value* args, FunctionValue* func_value = (FunctionValue*)func->data.function; - /* Check if we have enough arguments */ + + + /* Check if we have enough arguments for partial application */ if (arg_count < func_value->required_params) { - /* TODO: Implement partial application */ - /* For now, return a new function with fewer required parameters */ - return baba_yaga_value_nil(); + /* Implement partial application */ + /* Create a new function with bound arguments */ + Value partial_func = baba_yaga_value_function("partial", stdlib_partial_apply, + func_value->required_params - arg_count, + func_value->required_params - arg_count); + + /* Store the original function and bound arguments in the scope */ + char temp_name[64]; + snprintf(temp_name, sizeof(temp_name), "_partial_func_%p", (void*)func); + scope_define(scope, temp_name, *func, true); + + /* Store bound arguments */ + for (int i = 0; i < arg_count; i++) { + char arg_name[64]; + snprintf(arg_name, sizeof(arg_name), "_partial_arg_%d_%p", i, (void*)func); + scope_define(scope, arg_name, args[i], true); + } + + /* Store the number of bound arguments */ + char count_name[64]; + snprintf(count_name, sizeof(count_name), "_partial_count_%p", (void*)func); + scope_define(scope, count_name, baba_yaga_value_number(arg_count), true); + + return partial_func; } /* Execute function based on type */ switch (func_value->type) { case FUNC_NATIVE: if (func_value->body.native_func != NULL) { - return func_value->body.native_func((Value*)args, arg_count); + return func_value->body.native_func((Value*)args, arg_count, scope); } break; @@ -187,6 +214,8 @@ Value baba_yaga_function_call(const Value* func, const Value* args, return result; } break; + + } return baba_yaga_value_nil(); @@ -231,9 +260,9 @@ void function_decrement_ref(Value* func) { } /* Clean up function body */ - if (func_value->type == FUNC_USER) { - function_body_destroy(&func_value->body.user_body); - } + if (func_value->type == FUNC_USER) { + function_body_destroy(&func_value->body.user_body); + } /* TODO: Clean up closure scope */ |