-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlval.h
More file actions
153 lines (129 loc) · 3.21 KB
/
lval.h
File metadata and controls
153 lines (129 loc) · 3.21 KB
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
#ifndef LVAL_H
#define LVAL_H
#include "mpc.h"
struct LVal;
struct LEnv;
typedef struct LVal LVal;
typedef struct LEnv LEnv;
/* LVal Types */
enum {
LVAL_NUM,
LVAL_ERR,
LVAL_SYM,
LVAL_STR,
LVAL_SEXPR,
LVAL_QEXPR,
LVAL_FUN
};
/**
* @brief A function pointer for LBuiltins
* @note LBuiltins are lispy's native functions
* @param LEnv *: A LEnv to hold environment variables
* @param LVal *: A list of LVals to operate on
* @retval Evaluation as a LVal
*/
typedef LVal *(*LBuiltin)(LEnv *, LVal *);
/**
* @brief Store number or error in an abstract type
* @note LVal are lispy native values
*/
struct LVal {
/* Type */
int type;
/* Value */
long num;
char *err;
char *sym;
char *str;
/* Functions */
LBuiltin lbuiltin;
LEnv *lenv;
LVal *lformals;
LVal *lbody;
/* Child Expressions */
struct LVal **children;
int child_count;
};
/**
* @brief A struct to store variables with their (l)values
* @note LEnv maybe local to function or the parent environment
*/
struct LEnv {
/* Points to the parent environment */
LEnv *parent;
/* List of symbols in the environment */
char **syms;
/* And their corresponding LVals */
LVal **lvals;
int child_count;
};
/**
* @brief Print the value of a LVal, adding a new line at the end
* @param val: An LVal
* @retval None
*/
void lval_println(LVal *lval);
/**
* @brief Read ast as a LVal
* @param *node: The ast to be converted
* @retval A LVal
*/
LVal *lval_read_ast(mpc_ast_t *node);
/**
* @brief Delete a LVal
* @param *lval: The LVal which need to be freed along with its contents
* @retval None
*/
void lval_del(LVal *lval);
/**
* @brief Evaluate an LVal
* @note Fetches symbols from LEnv, Handles SEXPR, or just returns LVal
* @param *lenv: LEnv from which the symbols must be fetched
* @param *lval: A LVal of any type
* @retval A LVal computed depending on type (@see @note)
*/
LVal *lval_eval(LEnv *lenv, LVal *lval);
/**
* @brief Create a new LEnv
* @retval A LEnv with fields initialized to NULL/0
*/
LEnv *lenv_new(void);
/**
* @brief Delete a LEnv
* @param *lenv: The LEnv to be deleted
* @retval None
*/
void lenv_del(LEnv *lenv);
/**
* @brief Add default builtins to LEnv
* @param *lenv: The LEnv where the builtins are to be added
* @retval None
*/
void lenv_init_builtins(LEnv *lenv);
/**
* @brief Add a LVal to another LVal
* @param *parent: The parent LVal, the child is added to this LVal
* @param *child: The child LVal which needs to be added
* @retval Returns the parent LVal
*/
LVal *lval_add(LVal *parent, LVal *child);
/**
* @brief Create a empty S-Expression
* @note Wrapper for lval_wrap_expr
* @retval A LVal with type LVAL_SEXPR
*/
LVal *lval_wrap_sexpr(void);
/**
* @brief Wrap a string as a LVal
* @param *str: The string to be wrapped
* @retval A LVal of type LVAL_STR
*/
LVal *lval_wrap_str(char *str);
/**
* @brief Load files containing valid lispy expression
* @param *lenv: The environment where the expressions are loaded
* @param *lval: The LVal containing the filename
* @retval A LVal of result or LVAL_ERR on error
*/
LVal *builtin_load(LEnv *lenv, LVal *lval);
#endif /* lval.h */