1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
|
/**
* @file function.c
* @brief Function implementation for Baba Yaga
* @author eli_oat
* @version 0.0.1
* @date 2025
*
* This file implements the function system for the Baba Yaga language.
* Functions support closures, partial application, and first-class behavior.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "baba_yaga.h"
/* Forward declarations */
extern Scope* scope_create(Scope* parent);
extern void scope_destroy(Scope* scope);
extern bool scope_define(Scope* scope, const char* name, Value value, bool is_constant);
extern Value interpreter_evaluate_expression(void* node, Scope* scope);
/* ============================================================================
* Function Structure Definitions
* ============================================================================ */
/**
* @brief Function parameter
*/
typedef struct {
char* name; /**< Parameter name */
bool is_optional; /**< Whether parameter is optional */
} FunctionParam;
typedef enum {
FUNC_NATIVE, /**< Native C function */
FUNC_USER /**< User-defined function */
} FunctionType;
/**
* @brief Function body (placeholder for AST node)
*/
typedef struct {
void* ast_node; /**< AST node representing function body */
char* source; /**< Source code for debugging */
} FunctionBody;
/**
* @brief Function value structure
*/
typedef struct {
char* name; /**< Function name (can be NULL for anonymous) */
FunctionType type; /**< Function type */
FunctionParam* params; /**< Array of parameters */
int param_count; /**< Number of parameters */
int required_params; /**< Number of required parameters */
union {
Value (*native_func)(Value*, int); /**< Native function pointer */
FunctionBody user_body; /**< User function body */
} body;
void* closure_scope; /**< Closure scope (placeholder) */
int ref_count; /**< Reference count for memory management */
} FunctionValue;
/* ============================================================================
* Function Creation and Management
* ============================================================================ */
/* TODO: Implement parameter management functions */
/**
* @brief Destroy a function body
*
* @param body Function body to destroy
*/
static void function_body_destroy(FunctionBody* body) {
if (body != NULL && body->source != NULL) {
free(body->source);
body->source = NULL;
}
/* Note: ast_node cleanup will be handled by AST system */
}
/* ============================================================================
* Public Function API
* ============================================================================ */
Value baba_yaga_value_function(const char* name, Value (*body)(Value*, int),
int param_count, int required_param_count) {
Value value;
value.type = VAL_FUNCTION;
FunctionValue* func_value = malloc(sizeof(FunctionValue));
if (func_value == NULL) {
value.type = VAL_NIL;
return value;
}
func_value->name = name != NULL ? strdup(name) : NULL;
func_value->type = FUNC_NATIVE;
func_value->param_count = param_count;
func_value->required_params = required_param_count;
func_value->ref_count = 1;
func_value->closure_scope = NULL; /* TODO: Implement closure scope */
/* Allocate parameter array */
if (param_count > 0) {
func_value->params = calloc(param_count, sizeof(FunctionParam));
if (func_value->params == NULL) {
free(func_value->name);
free(func_value);
value.type = VAL_NIL;
return value;
}
/* Initialize parameters with placeholder names */
for (int i = 0; i < param_count; i++) {
char param_name[16];
snprintf(param_name, sizeof(param_name), "param_%d", i + 1);
func_value->params[i].name = strdup(param_name);
func_value->params[i].is_optional = (i >= required_param_count);
}
} else {
func_value->params = NULL;
}
/* Set native function pointer */
func_value->body.native_func = body;
value.data.function = func_value;
return value;
}
Value baba_yaga_function_call(const Value* func, const Value* args,
int arg_count, Scope* scope) {
if (func == NULL || func->type != VAL_FUNCTION || args == NULL) {
return baba_yaga_value_nil();
}
FunctionValue* func_value = (FunctionValue*)func->data.function;
/* Check if we have enough arguments */
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();
}
/* 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);
}
break;
case FUNC_USER:
/* Execute user-defined function */
if (func_value->body.user_body.ast_node != NULL) {
/* Create new scope for function execution */
Scope* func_scope = scope_create(scope); /* Pass current scope as parent for closures */
if (func_scope == NULL) {
DEBUG_ERROR("Failed to create function scope");
return baba_yaga_value_nil();
}
/* Bind parameters to arguments */
for (int i = 0; i < arg_count && i < func_value->param_count; i++) {
const char* param_name = func_value->params[i].name;
if (param_name != NULL) {
scope_define(func_scope, param_name, args[i], false);
}
}
/* Execute function body */
Value result = interpreter_evaluate_expression(
func_value->body.user_body.ast_node,
func_scope
);
/* Clean up function scope */
scope_destroy(func_scope);
return result;
}
break;
}
return baba_yaga_value_nil();
}
/* ============================================================================
* Internal Function Management
* ============================================================================ */
/**
* @brief Increment reference count for a function
*
* @param func Function value
*/
void function_increment_ref(Value* func) {
if (func != NULL && func->type == VAL_FUNCTION) {
FunctionValue* func_value = (FunctionValue*)func->data.function;
func_value->ref_count++;
}
}
/**
* @brief Decrement reference count for a function
*
* @param func Function value
*/
void function_decrement_ref(Value* func) {
if (func != NULL && func->type == VAL_FUNCTION) {
FunctionValue* func_value = (FunctionValue*)func->data.function;
func_value->ref_count--;
if (func_value->ref_count <= 0) {
/* Clean up function */
free(func_value->name);
/* Clean up parameters */
if (func_value->params != NULL) {
for (int i = 0; i < func_value->param_count; i++) {
free(func_value->params[i].name);
}
free(func_value->params);
}
/* Clean up function body */
if (func_value->type == FUNC_USER) {
function_body_destroy(&func_value->body.user_body);
}
/* TODO: Clean up closure scope */
free(func_value);
}
}
}
/* ============================================================================
* Function Utility Functions
* ============================================================================ */
/**
* @brief Get function name
*
* @param func Function value
* @return Function name, or NULL if anonymous
*/
const char* function_get_name(const Value* func) {
if (func == NULL || func->type != VAL_FUNCTION) {
return NULL;
}
FunctionValue* func_value = (FunctionValue*)func->data.function;
return func_value->name;
}
/**
* @brief Get function parameter count
*
* @param func Function value
* @return Number of parameters
*/
int function_get_param_count(const Value* func) {
if (func == NULL || func->type != VAL_FUNCTION) {
return 0;
}
FunctionValue* func_value = (FunctionValue*)func->data.function;
return func_value->param_count;
}
/**
* @brief Get function required parameter count
*
* @param func Function value
* @return Number of required parameters
*/
int function_get_required_param_count(const Value* func) {
if (func == NULL || func->type != VAL_FUNCTION) {
return 0;
}
FunctionValue* func_value = (FunctionValue*)func->data.function;
return func_value->required_params;
}
|