152 lines
2.9 KiB
C
152 lines
2.9 KiB
C
#ifndef DATA_H_PARSER
|
|
#define DATA_H_PARSER
|
|
|
|
#include <stdbool.h>
|
|
|
|
// Token types for lexical analysis
|
|
|
|
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;
|
|
|
|
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;
|
|
union {
|
|
char *symbol;
|
|
char *string;
|
|
double number;
|
|
bool boolean;
|
|
char *function_ref;
|
|
struct {
|
|
char *function_name;
|
|
struct node **args;
|
|
int arg_count;
|
|
} call;
|
|
struct {
|
|
struct node **children;
|
|
int child_count;
|
|
} list;
|
|
} data;
|
|
} node_t;
|
|
|
|
// Lexer state
|
|
typedef struct {
|
|
const char *input;
|
|
int pos;
|
|
int line;
|
|
int column;
|
|
char current_char;
|
|
} lexer_t;
|
|
|
|
// Configuration storage
|
|
typedef struct key_mapping {
|
|
char *key_combo;
|
|
char *function_name;
|
|
struct key_mapping *next;
|
|
} key_mapping_t;
|
|
|
|
typedef struct config_var {
|
|
char *name;
|
|
node_t *value;
|
|
struct config_var *next;
|
|
} config_var_t;
|
|
|
|
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);
|
|
|
|
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
|