This commit is contained in:
Arthur Barraux
2025-06-12 17:00:19 +02:00
parent 04464ec2e7
commit 5818a8ecfc
14 changed files with 977 additions and 232 deletions
+16 -22
View File
@@ -5,30 +5,24 @@
#include "parser.h"
#include <stdio.h>
config_t *config_create(void);
void free_node(node_t *node);
void config_destroy(config_t *config);
int handle_map_key(config_t *config, node_t **args, int arg_count);
int handle_define(config_t *config, node_t **args, int arg_count);
int execute_function_call(config_t *config, node_t *call);
int config_parse_string(config_t *config, const char *input);
int config_parse_file(config_t *config, const char *filename);
void config_create(void);
void init_builtin_functions(void);
const char *config_get_key_mapping(config_t *config, const char *key_combo);
node_t *find_variable(config_t *config, const char *name);
int handle_map_key(node_t **args, int arg_count);
int handle_define(node_t **args, int arg_count);
int handle_function(node_t **args, int arg_count);
const char *config_get_string(config_t *config, const char *path,
const char *default_value);
int config_get_int(config_t *config, const char *path, int default_value);
double config_get_double(config_t *config, const char *path,
double default_value);
bool config_get_bool(config_t *config, const char *path, bool default_value);
void config_print_all(config_t *config);
int execute_function_call(node_t *call);
int config_parse_string(const char *input);
int config_parse_file(const char *filename);
static struct {
const char *name;
int (*handler)(config_t *config, node_t **args, int arg_count);
} builtin_functions[] = {
{"map-key", handle_map_key}, {"define", handle_define}, {NULL, NULL}};
const char *config_get_key_mapping(const char *key_combo);
node_t *find_variable(const char *name);
const char *config_get_string(const char *path, const char *default_value);
int config_get_int(const char *path, int default_value);
double config_get_double(const char *path, double default_value);
bool config_get_bool(const char *path, bool default_value);
void config_print_all(void);
#endif
+72 -21
View File
@@ -1,12 +1,7 @@
#ifndef DATA_H_PARSER
#define DATA_H_PARSER
#include "define.h"
#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Token types for lexical analysis
@@ -26,7 +21,7 @@ typedef enum {
typedef struct {
token_type_t type;
char value[MAX_TOKEN_LENGTH];
char *value;
int line;
int column;
} token_t;
@@ -37,23 +32,28 @@ typedef enum {
NODE_STRING,
NODE_NUMBER,
NODE_BOOLEAN,
NODE_FUNCTION_REF, // %function-name
NODE_FUNCTION_CALL // ,function-name()
NODE_FUNCTION_REF, // %function-name
NODE_FUNCTION_CALL, // ,function-name()
NODE_LIST
} node_type_t;
typedef struct node {
node_type_t type;
union {
char symbol[MAX_SYMBOL_LENGTH];
char string[MAX_STRING_LENGTH];
char *symbol;
char *string;
double number;
bool boolean;
char function_ref[MAX_SYMBOL_LENGTH];
char *function_ref;
struct {
char function_name[MAX_SYMBOL_LENGTH];
struct node *args[MAX_ARGS];
char *function_name;
struct node **args;
int arg_count;
} call;
struct {
struct node **children;
int child_count;
} list;
} data;
} node_t;
@@ -79,22 +79,73 @@ typedef struct config_var {
struct config_var *next;
} config_var_t;
typedef struct {
typedef struct config {
key_mapping_t *key_mappings;
config_var_t *variables;
} config_t;
// Execution context for function calls
typedef struct exec_context {
struct {
char **names;
node_t **values;
int count;
} local_vars;
} exec_context_t;
typedef void (*command_func_t)(void);
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);
node_t *create_node(node_type_t type);
node_t *create_symbol_node(const char *symbol);
node_t *create_string_node(const char *string);
node_t *create_number_node(double number);
node_t *create_boolean_node(bool value);
node_t *create_function_ref_node(const char *function_name);
node_t *create_function_call_node(const char *function_name);
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 config_t config;
#endif
+24
View File
@@ -0,0 +1,24 @@
#ifndef FUNCTION_H_
#define FUNCTION_H_
#include "data.h"
int register_builtin_function(const char *name, int (*handler)(node_t **, int),
bool eval_args);
int register_user_function(const char *name, char **parameters, int param_count,
node_t *body);
int register_registry_function(const char *name, command_func_t func);
int register_function(const char *name, command_func_t func);
unified_function_t *find_unified_function(const char *name);
node_t *execute_unified_function(exec_context_t *ctx, const char *name,
node_t **args, int arg_count);
command_func_t find_function(const char *name);
int execute_command(const char *name);
node_t *execute_function_call_in_context(exec_context_t *ctx, node_t *call);
#endif
+2
View File
@@ -2,6 +2,8 @@
#define LEXER_H_
#include "data.h"
#include <ctype.h>
#include <string.h>
token_t next_token(lexer_t *lexer);
+20
View File
@@ -0,0 +1,20 @@
#ifndef NODE_T_
#define NODE_T_
#include "data.h"
node_t *create_node(node_type_t type);
node_t *create_symbol_node(const char *symbol);
node_t *create_string_node(const char *string);
node_t *create_number_node(double number);
node_t *create_boolean_node(bool value);
node_t *create_list_node(void);
node_t *create_function_ref_node(const char *function_name);
node_t *create_function_call_node(const char *function_name);
void free_node(node_t *node);
node_t *copy_node(node_t *src);
node_t *evaluate_node(exec_context_t *ctx, node_t *node);
#endif
+1 -1
View File
@@ -2,10 +2,10 @@
#define PARSER_H_
#include "data.h"
#include "lexer.h"
node_t *parse_atom(lexer_t *lexer, token_t *token);
node_t *parse_function_call(lexer_t *lexer, const char *function_name);
node_t *parse_statement(lexer_t *lexer);
node_t *parse_expression(lexer_t *lexer, token_t *token);
#endif