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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
|
/**
* @file baba_yaga.h
* @brief Main public API header for Baba Yaga interpreter
* @author eli_oat
* @version 0.0.1
* @date 2025
*
* This header provides the public API for the Baba Yaga scripting language
* implementation in C. It includes all necessary types, functions, and
* constants for interacting with the language interpreter.
*/
#ifndef BABA_YAGA_H
#define BABA_YAGA_H
#include <stdbool.h>
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Baba Yaga interpreter instance
*
* This opaque structure represents a Baba Yaga interpreter instance.
* All interpreter operations are performed through this handle.
*/
typedef struct Interpreter Interpreter;
/* Forward declarations for internal types */
typedef struct Scope Scope;
typedef struct ASTNode ASTNode;
/**
* @brief Baba Yaga value types
*/
typedef enum {
VAL_NUMBER, /**< Numeric value (double) */
VAL_STRING, /**< String value (char*) */
VAL_BOOLEAN, /**< Boolean value (bool) */
VAL_TABLE, /**< Table value (opaque) */
VAL_FUNCTION, /**< Function value (opaque) */
VAL_NIL /**< Nil/null value */
} ValueType;
/**
* @brief Baba Yaga value structure
*
* Represents a value in the Baba Yaga language. The actual data
* is stored in the union based on the type field.
*/
typedef struct {
ValueType type; /**< Type of the value */
union {
double number; /**< Numeric value */
char* string; /**< String value */
bool boolean; /**< Boolean value */
void* table; /**< Table value (opaque) */
void* function; /**< Function value (opaque) */
} data;
} Value;
/**
* @brief Baba Yaga execution result
*/
typedef enum {
EXEC_SUCCESS, /**< Execution completed successfully */
EXEC_ERROR, /**< Execution failed with error */
EXEC_SYNTAX_ERROR, /**< Syntax error in source code */
EXEC_RUNTIME_ERROR /**< Runtime error during execution */
} ExecResult;
/**
* @brief Baba Yaga error information
*/
typedef struct {
char* message; /**< Error message */
int line; /**< Line number where error occurred */
int column; /**< Column number where error occurred */
char* source_file; /**< Source file where error occurred */
} BabaYagaError;
/* ============================================================================
* Core API Functions
* ============================================================================ */
/**
* @brief Create a new Baba Yaga interpreter instance
*
* @return New interpreter instance, or NULL on failure
*
* @note The returned interpreter must be freed with baba_yaga_destroy()
*/
Interpreter* baba_yaga_create(void);
/**
* @brief Destroy a Baba Yaga interpreter instance
*
* @param interp Interpreter instance to destroy
*
* @note This function frees all memory associated with the interpreter
*/
void baba_yaga_destroy(Interpreter* interp);
/**
* @brief Execute Baba Yaga source code
*
* @param interp Interpreter instance
* @param source Source code to execute
* @param source_len Length of source code (0 for null-terminated)
* @param result Output parameter for execution result
* @return Value result of execution
*
* @note The returned value must be freed with baba_yaga_value_destroy()
*/
Value baba_yaga_execute(Interpreter* interp, const char* source,
size_t source_len, ExecResult* result);
/**
* @brief Execute Baba Yaga source code from file
*
* @param interp Interpreter instance
* @param filename Path to source file
* @param result Output parameter for execution result
* @return Value result of execution
*
* @note The returned value must be freed with baba_yaga_value_destroy()
*/
Value baba_yaga_execute_file(Interpreter* interp, const char* filename,
ExecResult* result);
/* ============================================================================
* Value Management Functions
* ============================================================================ */
/**
* @brief Create a number value
*
* @param number Numeric value
* @return New number value
*/
Value baba_yaga_value_number(double number);
/**
* @brief Create a string value
*
* @param string String value (will be copied)
* @return New string value
*
* @note The string is copied internally
*/
Value baba_yaga_value_string(const char* string);
/**
* @brief Create a boolean value
*
* @param boolean Boolean value
* @return New boolean value
*/
Value baba_yaga_value_boolean(bool boolean);
/**
* @brief Create a nil value
*
* @return New nil value
*/
Value baba_yaga_value_nil(void);
/**
* @brief Destroy a Baba Yaga value
*
* @param value Value to destroy
*
* @note This function frees all memory associated with the value
*/
void baba_yaga_value_destroy(Value* value);
/**
* @brief Copy a Baba Yaga value
*
* @param value Value to copy
* @return New copy of the value
*
* @note The returned value must be freed with baba_yaga_value_destroy()
*/
Value baba_yaga_value_copy(const Value* value);
/* ============================================================================
* Table Management Functions
* ============================================================================ */
/**
* @brief Create a new empty table
*
* @return New table value
*/
Value baba_yaga_value_table(void);
/**
* @brief Get a value from a table by key
*
* @param table Table value
* @param key Key to look up (string)
* @return Value at key, or nil if not found
*/
Value baba_yaga_table_get(const Value* table, const char* key);
/**
* @brief Set a value in a table by key
*
* @param table Table value to modify
* @param key Key to set (string)
* @param value Value to set
* @return New table with the updated value
*
* @note Tables are immutable, so this returns a new table
*/
Value baba_yaga_table_set(const Value* table, const char* key, const Value* value);
/**
* @brief Get a value from a table by numeric index
*
* @param table Table value
* @param index Numeric index (1-based)
* @return Value at index, or nil if not found
*/
Value baba_yaga_table_get_index(const Value* table, int index);
/**
* @brief Set a value in a table by numeric index
*
* @param table Table value to modify
* @param index Numeric index (1-based)
* @param value Value to set
* @return New table with the updated value
*
* @note Tables are immutable, so this returns a new table
*/
Value baba_yaga_table_set_index(const Value* table, int index, const Value* value);
/**
* @brief Get the size of a table
*
* @param table Table value
* @return Number of elements in the table
*/
size_t baba_yaga_table_size(const Value* table);
/**
* @brief Check if a table contains a key
*
* @param table Table value
* @param key Key to check
* @return true if key exists, false otherwise
*/
bool baba_yaga_table_has_key(const Value* table, const char* key);
/* ============================================================================
* Function Management Functions
* ============================================================================ */
/**
* @brief Create a new function value
*
* @param name Function name (can be NULL for anonymous)
* @param param_count Number of parameters
* @param required_param_count Number of required parameters
* @param body Function body (function pointer)
* @return New function value
*/
Value baba_yaga_value_function(const char* name, Value (*body)(Value*, int),
int param_count, int required_param_count);
/**
* @brief Call a function with arguments
*
* @param func Function value to call
* @param args Array of argument values
* @param arg_count Number of arguments
* @param scope Current scope for function execution
* @return Result of function call
*/
Value baba_yaga_function_call(const Value* func, const Value* args,
int arg_count, Scope* scope);
/* ============================================================================
* Internal Table Management Functions
* ============================================================================ */
/**
* @brief Increment reference count for a table
*
* @param table Table value
*/
void table_increment_ref(Value* table);
/**
* @brief Decrement reference count for a table
*
* @param table Table value
*/
void table_decrement_ref(Value* table);
/* ============================================================================
* Internal Function Management Functions
* ============================================================================ */
/**
* @brief Increment reference count for a function
*
* @param func Function value
*/
void function_increment_ref(Value* func);
/**
* @brief Decrement reference count for a function
*
* @param func Function value
*/
void function_decrement_ref(Value* func);
/* ============================================================================
* 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);
/**
* @brief Get function parameter count
*
* @param func Function value
* @return Number of parameters
*/
int function_get_param_count(const Value* func);
/**
* @brief Get function required parameter count
*
* @param func Function value
* @return Number of required parameters
*/
int function_get_required_param_count(const Value* func);
/* ============================================================================
* Lexer Functions
* ============================================================================ */
/**
* @brief Tokenize source code
*
* @param source Source code to tokenize
* @param source_len Length of source code
* @param tokens Output array for tokens
* @param max_tokens Maximum number of tokens to read
* @return Number of tokens read, or -1 on error
*/
int baba_yaga_tokenize(const char* source, size_t source_len,
void** tokens, size_t max_tokens);
/**
* @brief Free tokens
*
* @param tokens Array of tokens
* @param count Number of tokens
*/
void baba_yaga_free_tokens(void** tokens, size_t count);
/* ============================================================================
* Parser Functions
* ============================================================================ */
/**
* @brief Parse source code into AST
*
* @param tokens Array of tokens
* @param token_count Number of tokens
* @return Root AST node, or NULL on error
*/
/* ============================================================================
* AST Node Types
* ============================================================================ */
typedef enum {
NODE_LITERAL,
NODE_IDENTIFIER,
NODE_BINARY_OP,
NODE_UNARY_OP,
NODE_FUNCTION_CALL,
NODE_FUNCTION_DEF,
NODE_VARIABLE_DECL,
NODE_WHEN_EXPR,
NODE_WHEN_PATTERN,
NODE_TABLE,
NODE_TABLE_ACCESS,
NODE_IO_OPERATION,
NODE_SEQUENCE
} NodeType;
void* baba_yaga_parse(void** tokens, size_t token_count);
/**
* @brief Destroy AST
*
* @param node Root AST node
*/
void baba_yaga_destroy_ast(void* node);
/* ============================================================================
* AST Accessor Functions
* ============================================================================ */
NodeType baba_yaga_ast_get_type(void* node);
Value baba_yaga_ast_get_literal(void* node);
const char* baba_yaga_ast_get_identifier(void* node);
void* baba_yaga_ast_get_function_call_func(void* node);
int baba_yaga_ast_get_function_call_arg_count(void* node);
void* baba_yaga_ast_get_function_call_arg(void* node, int index);
void* baba_yaga_ast_get_binary_op_left(void* node);
void* baba_yaga_ast_get_binary_op_right(void* node);
const char* baba_yaga_ast_get_binary_op_operator(void* node);
void* baba_yaga_ast_get_unary_op_operand(void* node);
const char* baba_yaga_ast_get_unary_op_operator(void* node);
const char* baba_yaga_ast_get_function_def_name(void* node);
int baba_yaga_ast_get_function_def_param_count(void* node);
void* baba_yaga_ast_get_function_def_param(void* node, int index);
void* baba_yaga_ast_get_function_def_body(void* node);
const char* baba_yaga_ast_get_variable_decl_name(void* node);
void* baba_yaga_ast_get_variable_decl_value(void* node);
/* Sequence node accessors */
int baba_yaga_ast_get_sequence_statement_count(void* node);
void* baba_yaga_ast_get_sequence_statement(void* node, int index);
/* When expression accessors */
void* baba_yaga_ast_get_when_expr_test(void* node);
int baba_yaga_ast_get_when_expr_pattern_count(void* node);
void* baba_yaga_ast_get_when_expr_pattern(void* node, int index);
void* baba_yaga_ast_get_when_pattern_test(void* node);
void* baba_yaga_ast_get_when_pattern_result(void* node);
/**
* @brief Print AST for debugging
*
* @param node Root AST node
* @param indent Initial indentation level
*/
void baba_yaga_print_ast(void* node, int indent);
/* ============================================================================
* Debug and Logging Functions
* ============================================================================ */
/**
* @brief Debug levels
*/
typedef enum {
DEBUG_NONE = 0,
DEBUG_ERROR = 1,
DEBUG_WARN = 2,
DEBUG_INFO = 3,
DEBUG_DEBUG = 4,
DEBUG_TRACE = 5
} DebugLevel;
/**
* @brief Set debug level
*
* @param level Debug level to set
*/
void baba_yaga_set_debug_level(DebugLevel level);
/**
* @brief Get current debug level
*
* @return Current debug level
*/
DebugLevel baba_yaga_get_debug_level(void);
/**
* @brief Debug logging function
*
* @param level Debug level for this message
* @param file Source file name
* @param line Line number
* @param func Function name
* @param format Format string
* @param ... Variable arguments
*/
void baba_yaga_debug_log(DebugLevel level, const char* file, int line,
const char* func, const char* format, ...);
/* Debug macros */
#define DEBUG_ERROR(fmt, ...) \
baba_yaga_debug_log(DEBUG_ERROR, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__)
#define DEBUG_WARN(fmt, ...) \
baba_yaga_debug_log(DEBUG_WARN, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__)
#define DEBUG_INFO(fmt, ...) \
baba_yaga_debug_log(DEBUG_INFO, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__)
#define DEBUG_DEBUG(fmt, ...) \
baba_yaga_debug_log(DEBUG_DEBUG, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__)
#define DEBUG_TRACE(fmt, ...) \
baba_yaga_debug_log(DEBUG_TRACE, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__)
/* ============================================================================
* Error Handling Functions
* ============================================================================ */
/**
* @brief Get the last error from an interpreter
*
* @param interp Interpreter instance
* @return Error information, or NULL if no error
*
* @note The returned error must be freed with baba_yaga_error_destroy()
*/
BabaYagaError* baba_yaga_get_error(const Interpreter* interp);
/**
* @brief Destroy error information
*
* @param error Error to destroy
*
* @note This function frees all memory associated with the error
*/
void baba_yaga_error_destroy(BabaYagaError* error);
/* ============================================================================
* Standard Library Functions
* ============================================================================ */
/* Core combinator */
Value stdlib_apply(Value* args, int argc);
/* Arithmetic functions */
Value stdlib_add(Value* args, int argc);
Value stdlib_subtract(Value* args, int argc);
Value stdlib_multiply(Value* args, int argc);
Value stdlib_divide(Value* args, int argc);
Value stdlib_modulo(Value* args, int argc);
Value stdlib_pow(Value* args, int argc);
Value stdlib_negate(Value* args, int argc);
/* Comparison functions */
Value stdlib_equals(Value* args, int argc);
Value stdlib_not_equals(Value* args, int argc);
Value stdlib_less(Value* args, int argc);
Value stdlib_less_equal(Value* args, int argc);
Value stdlib_greater(Value* args, int argc);
Value stdlib_greater_equal(Value* args, int argc);
/* Logical functions */
Value stdlib_and(Value* args, int argc);
Value stdlib_or(Value* args, int argc);
Value stdlib_xor(Value* args, int argc);
Value stdlib_not(Value* args, int argc);
/* Function composition */
Value stdlib_compose(Value* args, int argc);
/* IO functions */
Value stdlib_out(Value* args, int argc);
Value stdlib_in(Value* args, int argc);
Value stdlib_assert(Value* args, int argc);
/* Higher-order functions */
Value stdlib_map(Value* args, int argc);
Value stdlib_filter(Value* args, int argc);
Value stdlib_reduce(Value* args, int argc);
/* ============================================================================
* Scope Management Functions
* ============================================================================ */
/* Scope creation and destruction */
Scope* scope_create(Scope* parent);
void scope_destroy(Scope* scope);
/* Variable operations */
Value scope_get(Scope* scope, const char* name);
bool scope_set(Scope* scope, const char* name, Value value);
bool scope_define(Scope* scope, const char* name, Value value, bool is_constant);
bool scope_has(Scope* scope, const char* name);
/* Scope utilities */
int scope_get_names(Scope* scope, char** names, int max_names);
void scope_print(Scope* scope, int indent);
/* ============================================================================
* Utility Functions
* ============================================================================ */
/**
* @brief Get the type of a value
*
* @param value Value to check
* @return Type of the value
*/
ValueType baba_yaga_value_get_type(const Value* value);
/**
* @brief Check if a value is truthy
*
* @param value Value to check
* @return true if value is truthy, false otherwise
*/
bool baba_yaga_value_is_truthy(const Value* value);
/**
* @brief Convert a value to string representation
*
* @param value Value to convert
* @return String representation (must be freed by caller)
*
* @note The returned string must be freed with free()
*/
char* baba_yaga_value_to_string(const Value* value);
/* ============================================================================
* Version Information
* ============================================================================ */
/**
* @brief Get the Baba Yaga C implementation version
*
* @return Version string (do not free)
*/
const char* baba_yaga_get_version(void);
#ifdef __cplusplus
}
#endif
#endif /* BABA_YAGA_H */
|