-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.h
More file actions
259 lines (213 loc) · 4.26 KB
/
utils.h
File metadata and controls
259 lines (213 loc) · 4.26 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
* Only include header file once.
*
* Reference about header file:
* - https://www.gnu.org/software/c-intro-and-ref/manual/html_node/Once_002dOnly-Headers.html
* - https://stackoverflow.com/q/232785 (#pragma directive).
* - https://stackoverflow.com/a/1804719 (About include in header file).
*/
#ifndef SCLISP_UTILS
#define SCLISP_UTILS
#include <stddef.h>
enum ltype_e {
LVAL_ERR = -1,
LVAL_NUM = 1,
LVAL_SYM = 2,
LVAL_SEXPR = 3,
LVAL_QEXPR = 4,
LVAL_QEXPR_LEN = 5,
LVAL_FUNC = 6
};
/*
* We can use union to store different data type
* in the same memory location. We will use the
* largest data type size for the size of the
* union type.
*
* This means that we can only have
* _one value at a time_ in those memory
* location.
*/
union lcontent_u {
double num;
char *err;
char *sym;
};
struct lval;
struct lenv;
typedef struct lval *(*lbuiltin_td)(
struct lenv *,
struct lval *
);
/*
* Lisp value.
*
* The limit of double value is DBL_MAX from float.h.
*
* To reproduce the double overflow, we can do:
* DBL_MAX + 1E+292
*
* We use unsigned int for count because count expected
* not to have negative value.
*
* Reference:
* https://stackoverflow.com/a/37830627
*/
struct lval {
enum ltype_e type;
union lcontent_u content;
unsigned int count;
struct lval **cell;
lbuiltin_td builtin;
const char *builtin_name;
};
struct lenv {
unsigned int count;
char **syms;
struct lval **vals;
};
struct fbuiltin {
lbuiltin_td func;
const char *name;
};
struct lval *lval_eval(
struct lenv *env,
struct lval *value
);
struct lval *lval_eval_sexpr(
struct lenv *env,
struct lval *value
);
struct lval *lval_copy(struct lval *value);
struct lval *builtin_arith(
struct lval *value,
char op
);
struct lval *builtin_let(
struct lenv *env,
struct lval *value
);
struct lval *builtin_head(
struct lenv *env,
struct lval *value
);
struct lval *builtin_tail(
struct lenv *env,
struct lval *value
);
struct lval *builtin_list(
struct lenv *env,
struct lval *value
);
struct lval *builtin_eval(
struct lenv *env,
struct lval *value
);
struct lval *builtin_join(
struct lenv *env,
struct lval *value
);
struct lval *builtin_len(
struct lenv *env,
struct lval *value
);
struct lval *builtin_add(
struct lenv *env,
struct lval *value
);
struct lval *builtin_sub(
struct lenv *env,
struct lval *value
);
struct lval *builtin_mul(
struct lenv *env,
struct lval *value
);
struct lval *builtin_div(
struct lenv *env,
struct lval *value
);
struct lval *builtin_mod(
struct lenv *env,
struct lval *value
);
struct lval *builtin_pow(
struct lenv *env,
struct lval *value
);
struct lval *lval_join(struct lval *v1, struct lval *v2);
struct lval *lval_take(struct lval *value, int i);
/*
* We are using unsigned int for i because in this case,
* index should not be negative value.
*/
struct lval *lval_pop(
struct lval *value,
unsigned int i,
const char *filename,
unsigned int line_number
);
void lval_println(struct lval *value);
struct lval *lval_add(struct lval *v1, struct lval *v2);
void lval_del(struct lval *value);
struct lval *lval_err(
const char *mes,
const char *filename,
unsigned int line_number
);
struct lval *lval_builtin(
lbuiltin_td func,
const char *func_name
);
struct lval *lval_num(double num);
struct lval *lval_sym(char *sym);
struct lval *lval_sexpr(void);
struct lval *lval_qexpr(void);
struct lenv *lenv_init(void);
void lenv_del(struct lenv *env);
void lenv_add_builtin(
struct lenv *env,
const char *name,
lbuiltin_td func,
const char *func_name
);
void lenv_builtins_init(struct lenv *env);
struct lval *lenv_get(
struct lenv *env,
struct lval *value
);
void lenv_put(
struct lenv *env,
const char *sym,
struct lval *value
);
int stringcmp(const char *str1, const char *str2);
void stringcpy(
char *dest,
size_t dest_len,
const char *src,
size_t src_len,
const char *filename,
unsigned int line_number
);
void int_overflow_err(
const char *filename,
unsigned int line_number
);
void alloc_err(
const char *msg,
const char *filename,
unsigned int line_number
);
/*
* Similar to reallocarray() but using our internal error
* handling.
*/
void *alloc_util(
void *ptr,
size_t total_elements,
size_t size,
const char *filename,
unsigned int line_number
);
#endif /* sclisp utils included */