aboutsummaryrefslogtreecommitdiff
path: root/builtin.h
blob: 666b48e1ae43b3392c0d103826336ab28c538e76 (plain) (blame)
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
#ifndef BUILTIN_H
#define BUILTIN_H

#include "model.h"
#include "types.h"
#include <string>

using std::string;

const int EQUAL_QUEUE_SIZE = 262144;

/** @class SpecialOptIf
 * The implementation of `if` operator
 */
class SpecialOptIf: public SpecialOptObj {/*{{{*/
    private:
        unsigned char state; /**< 0 for prepared, 1 for pre_called */
    public:
        /** Construct a `if` operator */
        SpecialOptIf();
        /** Prevent <condition> and <consequence> from being evaluated */
        void prepare(Pair *pc);
        /** When it's invoked at the first time, it will determined which of
         * <condition> and <consequence> should be evaluated.  Then when it's
         * invoked again, it will tell the system the corresponding result.*/
        Pair *call(Pair *args, Environment * &envt,
                Continuation * &cont, FrameObj ** &top_ptr);
};/*}}}*/

/** @class SpecialOptLambda
 * The implementation of `lambda` operator
 */
class SpecialOptLambda: public SpecialOptObj {/*{{{*/
    public:
        /** Construct a `lambda` operator */
        SpecialOptLambda();
        /** Prevent all parts of the expression being evaluated */
        void prepare(Pair *pc);
        /** Make up a ProcObj and push into the stack */
        Pair *call(Pair *args, Environment * &envt,
                Continuation * &cont, FrameObj ** &top_ptr);

};/*}}}*/

/** @class SpecialOptDefine
 * The implementation of `define` operator
 */
class SpecialOptDefine: public SpecialOptObj {/*{{{*/
    public:
        /** Construct a `define` operator */
        SpecialOptDefine();
        /** Prevent some parts from being evaluated */
        void prepare(Pair *pc);
        /** See `SpecialOptLambda` */
        Pair *call(Pair *args, Environment * &envt,
                Continuation * &cont, FrameObj ** &top_ptr);
};/*}}}*/

/** @class SpecialOptSet
 * The implementation of `set!` operator
 */
class SpecialOptSet: public SpecialOptObj {/*{{{*/
    public:
        /** Construct a `set!` operator */
        SpecialOptSet();
        /** See `SpecialOptDefine */
        void prepare(Pair *pc);
        /** See `SpecialOptDefine */
        Pair *call(Pair *args, Environment * &envt,
                Continuation * &cont, FrameObj ** &top_ptr);
};/*}}}*/

/** @class SpecialOptLambda
 * The implementation of `lambda` operator
 */
class SpecialOptQuote: public SpecialOptObj {/*{{{*/
    public:
        /** Construct a `quote` operator */
        SpecialOptQuote();
        /** Prevent the literal part from being evaluated */
        void prepare(Pair *pc);
        /** Return the literal */
        Pair *call(Pair *args, Environment * &envt,
                Continuation * &cont, FrameObj ** &top_ptr);

};/*}}}*/

/** @class SpecialOptEval
 * The implementation of `eval` operator
 */
class SpecialOptEval: public SpecialOptObj {/*{{{*/
    private:
        unsigned char state; /**< 0 for prepared, 1 for pre_called */
    public:
        /** Construct an `eval` operator */
        SpecialOptEval();
        /** Set state to 0 */
        void prepare(Pair *pc);
        /** Behaves like the one in `SpecialOptIf` */
        Pair *call(Pair *args, Environment * &envt,
                Continuation * &cont, FrameObj ** &top_ptr);

};/*}}}*/

/** @class SpecialOptAnd
 * The implementation of `and` operator
 */
class SpecialOptAnd: public SpecialOptObj {/*{{{*/
    public:
        /** Construct an `and` operator */
        SpecialOptAnd();
        /** Prevent all parts from being evaluated */
        void prepare(Pair *pc);
        /** Acts like `SpecialOptIf` */
        Pair *call(Pair *args, Environment * &envt,
                Continuation * &cont, FrameObj ** &top_ptr);

};/*}}}*/

/** @class SpecialOptOr
 * The implementation of `and` operator
 */
class SpecialOptOr: public SpecialOptObj {/*{{{*/
    public:
        /** Construct an `or` operator */
        SpecialOptOr();
        /** See `SpecialOptAnd` */
        void prepare(Pair *pc);
        /** See `SpecialOptAnd` */
        Pair *call(Pair *args, Environment * &envt,
                Continuation * &cont, FrameObj ** &top_ptr);

};/*}}}*/

/** @class SpecialOptApply
 * The implementation of `apply` operator
 */
class SpecialOptApply: public SpecialOptObj {/*{{{*/
    public:
        /** Construct an `apply` operator */
        SpecialOptApply();
        /** Do nothing */
        void prepare(Pair *pc);
        /** Provoke the <proc> with args */
        Pair *call(Pair *args, Environment * &envt,
                Continuation * &cont, FrameObj ** &top_ptr);

};/*}}}*/

/** @class SpecialOptDelay
 * The implementation of `delay` operator
 */
class SpecialOptDelay: public SpecialOptObj {/*{{{*/
    public:
        /** Construct a `delay` operator */
        SpecialOptDelay();
        /** Do nothing */
        void prepare(Pair *pc);
        /** Make up a PromObj and push into the stack */
        Pair *call(Pair *args, Environment * &envt,
                Continuation * &cont, FrameObj ** &top_ptr);

};/*}}}*/

/** @class SpecialOptForce
 * The implementation of `force` operator
 */
class SpecialOptForce: public SpecialOptObj {/*{{{*/
    private:
        unsigned char state;
        PromObj* prom;
    public:
        /** Construct a `force` operator */
        SpecialOptForce();
        /** Set the state to 0 */
        void prepare(Pair *pc);
        /** Force the evaluation of a promise. If the promise has not been
         * evaluated yet, then evaluate and feed the result to its memory,
         * while if it has already been evaluated, just push the result into
         * the stack */
        Pair *call(Pair *args, Environment * &envt,
                Continuation * &cont, FrameObj ** &top_ptr);

};/*}}}*/

#define BUILTIN_PROC_DEF(func)\
    EvalObj *(func)(Pair *args, const string &name)

BUILTIN_PROC_DEF(num_add);
BUILTIN_PROC_DEF(num_sub);
BUILTIN_PROC_DEF(num_mul);
BUILTIN_PROC_DEF(num_div);

BUILTIN_PROC_DEF(num_lt);
BUILTIN_PROC_DEF(num_le);
BUILTIN_PROC_DEF(num_gt);
BUILTIN_PROC_DEF(num_ge);
BUILTIN_PROC_DEF(num_eq);

BUILTIN_PROC_DEF(num_is_exact);
BUILTIN_PROC_DEF(num_is_inexact);
BUILTIN_PROC_DEF(is_number);
BUILTIN_PROC_DEF(is_complex);
BUILTIN_PROC_DEF(is_real);
BUILTIN_PROC_DEF(is_rational);
BUILTIN_PROC_DEF(is_integer);
BUILTIN_PROC_DEF(num_abs);
BUILTIN_PROC_DEF(num_mod);
BUILTIN_PROC_DEF(num_rem);
BUILTIN_PROC_DEF(num_quo);
BUILTIN_PROC_DEF(num_gcd);
BUILTIN_PROC_DEF(num_lcm);

BUILTIN_PROC_DEF(bool_not);
BUILTIN_PROC_DEF(is_boolean);

BUILTIN_PROC_DEF(is_pair);
BUILTIN_PROC_DEF(make_pair);
BUILTIN_PROC_DEF(pair_car);
BUILTIN_PROC_DEF(pair_cdr);
BUILTIN_PROC_DEF(pair_set_car);
BUILTIN_PROC_DEF(pair_set_cdr);
BUILTIN_PROC_DEF(is_null);
BUILTIN_PROC_DEF(is_list);
BUILTIN_PROC_DEF(make_list);
BUILTIN_PROC_DEF(length);
BUILTIN_PROC_DEF(append);
BUILTIN_PROC_DEF(reverse);
BUILTIN_PROC_DEF(list_tail);

BUILTIN_PROC_DEF(is_eqv);
BUILTIN_PROC_DEF(is_equal);

BUILTIN_PROC_DEF(display);
BUILTIN_PROC_DEF(is_string);
BUILTIN_PROC_DEF(is_symbol);
BUILTIN_PROC_DEF(string_lt);
BUILTIN_PROC_DEF(string_le);
BUILTIN_PROC_DEF(string_gt);
BUILTIN_PROC_DEF(string_ge);
BUILTIN_PROC_DEF(string_eq);

BUILTIN_PROC_DEF(make_vector);
BUILTIN_PROC_DEF(vector_set);
BUILTIN_PROC_DEF(vector_ref);
BUILTIN_PROC_DEF(vector_length);

BUILTIN_PROC_DEF(gc_status);
BUILTIN_PROC_DEF(set_gc_resolve_threshold);

#endif