-
Notifications
You must be signed in to change notification settings - Fork 1
/
parsing.h
125 lines (109 loc) · 3.54 KB
/
parsing.h
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
#ifndef SHERLANG_H
#define SHERLANG_H
#include<stdbool.h>
struct lval;
struct lenv;
typedef struct lval lval;
typedef struct lenv lenv;
typedef lval*(*lbuiltin)(lenv*, lval*);
// Enum for LISP value types
enum { LVAL_ERR, LVAL_NUM, LVAL_BOOL, LVAL_SYM, LVAL_STR, LVAL_FUN, LVAL_SEXPR, LVAL_QEXPR };
struct lval {
// Basic
int type;
double num;
char* err;
char* sym;
char* str;
// Functions
lbuiltin builtin;
lenv* env;
lval* formals; // function args
lval* body; // function body
// maintain count and pointer to list of sval* to represent s expressions/q expressions (this is the fundamental cons cell in LISP)
int count;
struct lval** cell;
};
// maintains mapping of variable names and LISP Values
struct lenv{
lenv* par; // parent environment to allow functions to access global environment (which contain other builtins)
int count;
char** syms;
lval** vals;
};
// Function declarations
// LISP val functions
lval* lval_num(double x);
lval* lval_err(char* fmt, ...);
lval* lval_sym(char* s);
lval* lval_sexpr(void);
lval* lval_qexpr(void);
lval* lval_fun(lbuiltin func);
lval* lval_lambda(lval* formals, lval* body);
lval* lval_bool(void);
lval* lval_str(char* s);
void lval_del(lval* v);
lval* lval_add(lval* v, lval* x);
lval* lval_pop(lval* v, int i);
lval* lval_take(lval* v, int i);
lval* lval_copy(lval* v);
int lval_eq(lval* x, lval* y);
lval* lval_read_str(char* s, int* i);
lval* lval_read_sym(char* s, int* i);
lval* lval_read_expr(char* s, int* i, char end);
lval* lval_read(char* s, int* i);
lval* lval_eval_sexpr(lenv* e, lval* v);
lval* lval_eval(lenv* e, lval* v);
void lval_expr_print(lval* v, char open, char close);
void lval_print_str(lval* v);
void lval_print(lval* v);
void lval_println(lval* v);
lval* lval_call(lenv* e, lval* f, lval* a);
// env functions
lenv* lenv_new(void);
void lenv_del(lenv* v);
lval* lenv_get(lenv* e, lval* k);
void lenv_put(lenv* e, lval* k, lval* v);
void lenv_def(lenv* e, lval* k, lval* v);
lenv* lenv_copy(lenv* e);
char* ltype_name(int t);
// builtin functions
lval* builtin_op(lenv* e, lval* a, char* op);
lval* builtin_add(lenv* e, lval* a);
lval* builtin_sub(lenv* e, lval* a);
lval* builtin_mul(lenv* e, lval* a);
lval* builtin_div(lenv* e, lval* a);
lval* builtin_mod(lenv* e, lval* a);
lval* builtin_ord(lenv* e, lval* a, char* op);
lval* builtin_gt(lenv* e, lval* a);
lval* builtin_lt(lenv* e, lval* a);
lval* builtin_ge(lenv* e, lval* a);
lval* builtin_le(lenv* e, lval* a);
lval* builtin_or(lenv* e, lval* a);
lval* builtin_and(lenv* e, lval* a);
lval* builtin_not(lenv* e, lval* a);
lval* builtin_cmp(lenv* e, lval* a, char* op);
lval* builtin_head(lenv* e, lval* a);
lval* builtin_tail(lenv* e, lval* a);
lval* builtin_list(lenv* e, lval* a);
lval* builtin_eval(lenv* e, lval* a);
lval* builtin_join(lenv* e, lval* a);
lval* lval_join(lval* x, lval* y);
lval* builtin_len(lenv* e, lval* a);
lval* builtin_cons(lenv* e, lval* a);
lval* builtin_var(lenv* e, lval* a, char* func);
lval* builtin_def(lenv* e, lval* a);
lval* builtin_put(lenv* e, lval* a);
lval* builtin_lambda(lenv* e, lval* a);
lval* builtin_if(lenv* e, lval* a);
lval* builtin_load(lenv* e, lval* a);
lval* builtin_print(lenv* e, lval* a);
lval* builtin_error(lenv* e, lval* a);
void lenv_add_builtin(lenv* e, char* name, lbuiltin func);
void lenv_add_builtins(lenv* e);
// parsing functions
lval* lval_read_expr(char* s, int* i, char end);
char lval_str_unescape(char x);
char* lval_str_escape(char x);
void lval_print_str(lval* v);
#endif