-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprimitives.h
178 lines (123 loc) · 4.19 KB
/
primitives.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
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
/*
PRIMITIVES
Primitive functions operate on actual C objects (like numbers and
lists). primitives.c creates a list of the functions and list of
the names of these functions and then passes them to env.c, where
they are zipped together to create the frame of the base_env.
TODO:
* primitive names should be moved to keywords.h
*/
#ifndef PRIMITIVES_GUARD
#define PRIMITIVES_GUARD
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include "flags.h"
#include "keywords.h"
#include "objects.h"
#include "print.h"
#include "read.h"
Obj applyPrimitive(Obj func, Obj arglist);
/* primitive arithmetic functions */
#define arith_len 6
twoArgFunc add_;
#define addprim TWOFUNC(add_, ADD_NAME)
twoArgFunc sub_;
#define subprim TWOFUNC(sub_, "-")
twoArgFunc mul_;
#define mulprim TWOFUNC(mul_, MUL_NAME)
twoArgFunc div_;
#define divprim TWOFUNC(div_, "/")
oneArgFunc addone_;
#define addoneprim ONEFUNC(addone_, "add1")
oneArgFunc subone_;
#define suboneprim ONEFUNC(subone_, "sub1")
/* primitive boolean functions */
#define bool_len 7
oneArgFunc not_;
#define notprim ONEFUNC(not_, "not")
twoArgFunc eq_;
#define eqprim TWOFUNC(eq_, "=")
twoArgFunc lt_;
#define ltprim TWOFUNC(lt_, "<")
twoArgFunc gt_;
#define gtprim TWOFUNC(gt_, ">")
oneArgFunc iszero_;
#define iszeroprim ONEFUNC(iszero_, "zero?")
oneArgFunc isone_;
#define isoneprim ONEFUNC(isone_, "one?")
twoArgFunc geneq_;
#define geneqprim TWOFUNC(geneq_, "eq?")
/* primitive type-check functions */
#define type_len 5
oneArgFunc null_;
#define nullprim ONEFUNC(null_, "null?")
oneArgFunc isnumber_;
#define isnumberprim ONEFUNC(isnumber_, "number?")
oneArgFunc islist_;
#define islistprim ONEFUNC(islist_, "list?")
oneArgFunc isbool_;
#define isboolprim ONEFUNC(isbool_, "boolean?")
oneArgFunc issymbol_;
#define issymbolprim ONEFUNC(issymbol_, "symbol?")
/* primitive list functions */
#define list_len 11
twoArgFunc cons_;
#define consprim TWOFUNC(cons_, "cons")
oneArgFunc car_;
#define carprim ONEFUNC(car_, "car")
oneArgFunc cdr_;
#define cdrprim ONEFUNC(cdr_, "cdr")
twoArgFunc setcar_;
#define setcarprim TWOFUNC(setcar_, "set-car!")
twoArgFunc setcdr_;
#define setcdrprim TWOFUNC(setcdr_, "set-cdr!")
oneArgFunc cadr_;
#define cadrprim ONEFUNC(cadr_, "cadr")
oneArgFunc cddr_;
#define cddrprim ONEFUNC(cddr_, "cddr")
oneArgFunc cdadr_;
#define cdadrprim ONEFUNC(cdadr_, "cdadr")
oneArgFunc caddr_;
#define caddrprim ONEFUNC(caddr_, "caddr")
oneArgFunc cdddr_;
#define cdddrprim ONEFUNC(cdddr_, "cdddr")
oneArgFunc cadddr_;
#define cadddrprim ONEFUNC(cadddr_, "cadddr")
/* I/O */
#define io_len 4
nilArgFunc read_;
#define readprim NILFUNC(read_, "read")
oneArgFunc display_;
#define displayprim ONEFUNC(display_, "display")
nilArgFunc newline_;
#define newlineprim NILFUNC(newline_, "newline")
nilArgFunc error_;
#define errorprim NILFUNC(error_, "error")
/* primitive application */
#define prim_app_len 3
twoArgFunc apply_nil_;
#define applynilprim TWOFUNC(apply_nil_, "applyNilFunc")
twoArgFunc apply_one_;
#define applyoneprim TWOFUNC(apply_one_, "applyOneFunc")
twoArgFunc apply_two_;
#define applytwoprim TWOFUNC(apply_two_, "applyTwoFunc")
// make sure to keep these updated!!!
#define LIST_OF_PRIMITIVES \
{ \
addprim, subprim, mulprim, divprim, addoneprim, suboneprim, notprim, \
eqprim, ltprim, gtprim, iszeroprim, isoneprim, geneqprim, \
nullprim, isnumberprim, islistprim, isboolprim, issymbolprim, \
carprim, cdrprim, consprim, setcarprim, setcdrprim, cadrprim, \
cddrprim, cdadrprim, caddrprim, cdddrprim, cadddrprim, readprim, \
displayprim, errorprim, newlineprim, applynilprim, applyoneprim, \
applytwoprim, \
}
#define PRIM_LEN \
(arith_len + bool_len + type_len + list_len + io_len + prim_app_len)
/* error checking */
bool are_both_nums(Obj a, Obj b);
void print_error_message(Tag tag, char* source);
// from read.c
int streq(char* str1, char* str2);
#endif