146 lines
2.3 KiB
C
146 lines
2.3 KiB
C
#ifndef DATA_H_PARSER
|
|
#define DATA_H_PARSER
|
|
|
|
#include <ctype.h>
|
|
#include <stdbool.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
// 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 {
|
|
CONFIG_INT,
|
|
CONFIG_FLOAT,
|
|
CONFIG_STRING,
|
|
CONFIG_BOOL,
|
|
CONFIG_LIST,
|
|
CONFIG_UNKNOWN
|
|
} ConfigType;
|
|
|
|
typedef struct {
|
|
ConfigType type;
|
|
union {
|
|
int int_val;
|
|
double float_val;
|
|
char *string_val;
|
|
bool bool_val;
|
|
struct {
|
|
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;
|
|
|
|
typedef struct {
|
|
const char *input;
|
|
int pos;
|
|
int length;
|
|
} Lexer;
|
|
|
|
/**
|
|
* @struct KeyBinding
|
|
* @brief Make the link between a Key Sequence and a command to execute
|
|
*/
|
|
|
|
typedef struct {
|
|
char *key_sequence;
|
|
char *command;
|
|
} KeyBinding;
|
|
//@}
|
|
typedef struct {
|
|
KeyBinding *bindings;
|
|
int count;
|
|
int capacity;
|
|
} KeyBindingTable;
|
|
|
|
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);
|
|
|
|
#ifdef GLOBAL_ENV_
|
|
|
|
Env *global_env = NULL;
|
|
KeyBindingTable *active_keybindings = NULL;
|
|
|
|
#else
|
|
|
|
extern Env *global_env;
|
|
extern KeyBindingTable *active_keybindings;
|
|
|
|
#endif
|
|
#endif
|