test functions

This commit is contained in:
Arthur Barraux
2025-09-18 13:52:46 +02:00
parent 5818a8ecfc
commit ee2a34a3b1
21 changed files with 1252 additions and 1434 deletions
+120 -126
View File
@@ -1,151 +1,145 @@
#ifndef DATA_H_PARSER
#define DATA_H_PARSER
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Token types for lexical analysis
// Forward declarations
typedef struct Value Value;
typedef struct Env Env;
// Value types
typedef enum {
VAL_NIL,
VAL_NUMBER,
VAL_STRING,
VAL_SYMBOL,
VAL_LIST,
VAL_FUNCTION,
VAL_KEYMAP
} ValueType;
typedef enum {
TOKEN_COMMA, // ,
TOKEN_PERCENT, // %
TOKEN_LPAREN, // (
TOKEN_RPAREN, // )
TOKEN_SYMBOL, // identifiers/function names
TOKEN_STRING, // "quoted strings"
TOKEN_NUMBER, // integers and floats
TOKEN_BOOLEAN, // true false
TOKEN_NEWLINE, // \n (statement separator)
TOKEN_EOF,
TOKEN_ERROR
} token_type_t;
CONFIG_INT,
CONFIG_FLOAT,
CONFIG_STRING,
CONFIG_BOOL,
CONFIG_LIST,
CONFIG_UNKNOWN
} ConfigType;
typedef struct {
token_type_t type;
char *value;
int line;
int column;
} token_t;
// AST node types
typedef enum {
NODE_SYMBOL,
NODE_STRING,
NODE_NUMBER,
NODE_BOOLEAN,
NODE_FUNCTION_REF, // %function-name
NODE_FUNCTION_CALL, // ,function-name()
NODE_LIST
} node_type_t;
typedef struct node {
node_type_t type;
ConfigType type;
union {
char *symbol;
char *string;
double number;
bool boolean;
char *function_ref;
int int_val;
double float_val;
char *string_val;
bool bool_val;
struct {
char *function_name;
struct node **args;
int arg_count;
} call;
struct {
struct node **children;
int child_count;
} list;
} data;
} node_t;
char **items;
int count;
} list_val;
} value;
} ConfigValue;
/**
* @typedef Functions
*
*/
typedef Value *(*Function)(char **params, int param_count, Value *body,
Env *closure);
// Value structure
struct Value {
ValueType type;
union {
double number;
char *string;
char *symbol;
struct {
Value *car;
Value *cdr;
} list;
Function *function;
struct {
char **keys;
Value **values;
int count;
int capacity;
} keymap;
} data;
};
// Environment for variable bindings
struct Env {
char **names;
Value **values;
int count;
int capacity;
Env *parent;
};
typedef enum {
TOK_LPAREN,
TOK_RPAREN,
TOK_QUOTE,
TOK_SYMBOL,
TOK_NUMBER,
TOK_STRING,
TOK_EOF
} TokenType;
typedef struct {
TokenType type;
char *value;
} Token;
// Lexer state
typedef struct {
const char *input;
int pos;
int line;
int column;
char current_char;
} lexer_t;
int length;
} Lexer;
// Configuration storage
typedef struct key_mapping {
char *key_combo;
char *function_name;
struct key_mapping *next;
} key_mapping_t;
/**
* @struct KeyBinding
* @brief Make the link between a Key Sequence and a command to execute
*/
typedef struct config_var {
char *name;
node_t *value;
struct config_var *next;
} config_var_t;
typedef struct {
char *key_sequence;
char *command;
} KeyBinding;
//@}
typedef struct {
KeyBinding *bindings;
int count;
int capacity;
} KeyBindingTable;
typedef struct config {
key_mapping_t *key_mappings;
config_var_t *variables;
} config_t;
Value *make_value(ValueType type);
Value *make_nil(void);
Value *make_number(double n);
Value *make_string(const char *s);
Value *make_symbol(const char *s);
Value *make_list(Value *car, Value *cdr);
Value *make_builtin(BuiltinFunc func);
Env *make_env(Env *parent);
void env_set(Env *env, const char *name, Value *value);
Value *env_get(Env *env, const char *name);
// Execution context for function calls
typedef struct exec_context {
struct {
char **names;
node_t **values;
int count;
} local_vars;
} exec_context_t;
#ifdef GLOBAL_ENV_
typedef void (*command_func_t)(void);
Env *global_env = NULL;
KeyBindingTable *active_keybindings = NULL;
typedef enum { FUNC_BUILTIN, FUNC_USER_DEFINED, FUNC_REGISTRY } function_type_t;
typedef struct unified_function {
char *name;
function_type_t type;
union {
// For built-in functions
struct {
int (*handler)(node_t **args, int arg_count);
bool eval_args; // Whether to evaluate arguments before calling
} builtin;
// For user-defined functions
struct {
char **parameters;
int param_count;
node_t *body;
} user_defined;
// For registry functions
struct {
command_func_t func;
} registry;
} data;
struct unified_function *next;
} unified_function_t;
void advance_char(lexer_t *lexer);
void skip_whitespace(lexer_t *lexer);
void skip_comment(lexer_t *lexer);
void add_arg_to_call(node_t *call, node_t *args);
void add_to_list(node_t *node, node_t *element);
#endif
#ifdef GLOBAL_CONFIG
unified_function_t *unified_functions = NULL;
int registry_count;
config_var_t variable_registry[256];
int var_registry_count;
config_t config;
#else
extern unified_function_t *unified_functions;
extern int registry_count;
extern config_var_t variable_registry[256];
extern int var_registry_count;
extern Env *global_env;
extern KeyBindingTable *active_keybindings;
extern config_t config;
#endif
#endif