aboutsummaryrefslogtreecommitdiff
path: root/builtin.h
blob: ab52d5a4f966db84cc2560123fff7b6e87bed463 (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
#ifndef BUILTIN_H
#define BUILTIN_H

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

using std::string;



/** @class InexactNumObj
 * Inexact number implementation (using doubles)
 */
class InexactNumObj: public NumObj {
    public:
        InexactNumObj(NumLvl level);
};

/** @class CompNumObj
 * Complex numbers
 */
class CompNumObj: public InexactNumObj {
    public:
        double real, imag;

        /** Construct a complex number */
        CompNumObj(double _real, double _imag);
        /** Try to construct an RealNumObj object 
         * @return NULL if failed
         */
        static CompNumObj *from_string(string repr);
        /** Convert to a complex number from other numeric types */
        CompNumObj *convert(NumObj* obj);

        NumObj *plus(NumObj *r);
        NumObj *minus(NumObj *r);
        NumObj *multi(NumObj *r);
        NumObj *div(NumObj *r);
        bool lt(NumObj *r);
        bool gt(NumObj *r);
        bool eq(NumObj *r);
        string ext_repr();
};

/** @class RealNumObj
 * Real numbers
 */
class RealNumObj: public InexactNumObj {
    public:
        double real;
        /** Construct a real number */
        RealNumObj(double _real);
        /** Try to construct an RealNumObj object 
         * @return NULL if failed
         */
        static RealNumObj *from_string(string repr);
        /** Convert to a real number from other numeric types */
        RealNumObj *convert(NumObj* obj);

        NumObj *plus(NumObj *r);
        NumObj *minus(NumObj *r);
        NumObj *multi(NumObj *r);
        NumObj *div(NumObj *r);
        bool lt(NumObj *r);
        bool gt(NumObj *r);
        bool eq(NumObj *r);
        string ext_repr();

};


/** @class ExactNumObj
 * Exact number implementation (using gmp)
 */
class ExactNumObj: public NumObj {
    public:
        ExactNumObj(NumLvl level);
};

/** @class RatNumObj
 * Rational numbers
 */
class RatNumObj: public ExactNumObj {
    public:
        int a, b;
        /** Construct a rational number */
        RatNumObj(int _a, int _b);
        /** Try to construct an RealNumObj object 
         * @return NULL if failed
         */
        static RatNumObj *from_string(string repr);
        /** Convert to a Rational number from other numeric types */
        RatNumObj *convert(NumObj* obj);

        NumObj *plus(NumObj *r);
        NumObj *minus(NumObj *r);
        NumObj *multi(NumObj *r);
        NumObj *div(NumObj *r);
        bool lt(NumObj *r);
        bool gt(NumObj *r);
        bool eq(NumObj *r);
        string ext_repr();
};

/** @class IntNumObj
 * Integers
 */
class IntNumObj: public ExactNumObj {
    public:
        int val;
        /** Construct a integer */
        IntNumObj(int _val);
        /** Try to construct an IntNumObj object 
         * @return NULL if failed
         */
        static IntNumObj *from_string(string repr);
        /** Convert to a integer from other numeric types */
        IntNumObj *convert(NumObj* obj);

        NumObj *plus(NumObj *r);
        NumObj *minus(NumObj *r);
        NumObj *multi(NumObj *r);
        NumObj *div(NumObj *r);
        bool lt(NumObj *r);
        bool gt(NumObj *r);
        bool eq(NumObj *r);
        string ext_repr();
};


/** @class SpecialOptIf
 * The implementation of `if` operator
 */
class SpecialOptIf: public SpecialOptObj {
    private:
        unsigned char state; /**< 0 for prepared, 1 for pre_called */
        /** 
         * The evaluator will call this after the <condition> exp is evaluated.
         * And this function tells the evaluator which of <consequence> and
         * <alternative> should be evaluted. */
        void pre_call(ArgList *args, Cons *pc, 
                Environment *envt);
        /** The system will call this again after the desired result is
         * evaluated, so just return it to let the evaluator know the it's the
         * answer.
         */
        EvalObj *post_call(ArgList *args, Cons *pc, 
                Environment *envt);
    public:
        SpecialOptIf();
        void prepare(Cons *pc);
        Cons *call(ArgList *args, Environment * &envt, 
                    Continuation * &cont, FrameObj ** &top_ptr);
        string ext_repr();
};

/** @class SpecialOptLambda
 * The implementation of `lambda` operator
 */
class SpecialOptLambda: public SpecialOptObj {
    public:
        SpecialOptLambda();
        void prepare(Cons *pc);
        Cons *call(ArgList *args, Environment * &envt, 
                    Continuation * &cont, FrameObj ** &top_ptr);

        string ext_repr();
};

/** @class SpecialOptDefine
 * The implementation of `define` operator
 */
class SpecialOptDefine: public SpecialOptObj {
    public:
        SpecialOptDefine(); 
        void prepare(Cons *pc);
        Cons *call(ArgList *args, Environment * &envt, 
                    Continuation * &cont, FrameObj ** &top_ptr);
        string ext_repr();
};

/** @class SpecialOptSet
 * The implementation of `set!` operator
 */
class SpecialOptSet: public SpecialOptObj {
    public:
        SpecialOptSet();
        void prepare(Cons *pc);
        Cons *call(ArgList *args, Environment * &envt, 
                    Continuation * &cont, FrameObj ** &top_ptr);
        string ext_repr();
};

EvalObj *builtin_plus(ArgList *);
EvalObj *builtin_minus(ArgList *);
EvalObj *builtin_multi(ArgList *);
EvalObj *builtin_div(ArgList *);
EvalObj *builtin_exact(ArgList *);
EvalObj *builtin_inexact(ArgList *);
EvalObj *builtin_lt(ArgList *);
EvalObj *builtin_gt(ArgList *);
EvalObj *builtin_arithmetic_eq(ArgList *);

EvalObj *builtin_display(ArgList *);
EvalObj *builtin_cons(ArgList *);
EvalObj *builtin_car(ArgList *);
EvalObj *builtin_cdr(ArgList *);
EvalObj *builtin_list(ArgList *);

#endif