-
Notifications
You must be signed in to change notification settings - Fork 0
/
LispObj.h
211 lines (154 loc) · 3.91 KB
/
LispObj.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
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
#ifndef H_LISP_OBJ
#define H_LISP_OBJ
#include <cassert>
#include <ostream>
#include <vector>
#include <map>
#include <string>
#include <algorithm>
#include <boost/variant.hpp>
#include <boost/shared_ptr.hpp>
namespace Lisp {
class LispEnv;
typedef boost::shared_ptr<LispEnv> LispEnvRef;
typedef signed char s8;
typedef signed short s16;
typedef signed int s32;
typedef signed long s64;
typedef signed long long s128;
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned int u32;
typedef unsigned long u64;
typedef unsigned long long u128;
typedef float f32;
typedef double f64;
// forward declaration
class LispObj;
typedef boost::shared_ptr<LispObj> LispObjRef;
typedef s32 CFixnum;
typedef u8 CChar;
typedef std::string CString;
typedef f32 CFloatnum;
// placeholder type
class NullType {
private:
const u32 dead_;
public:
NullType() :dead_(0xDEADBEEF) {
}
};
/* This is a template for an unboxed type that can be represented by a simple scalar */
template <typename T> class TLispType {
private:
T data_;
public:
typedef T CType;
TLispType<T>() {
}
explicit TLispType<T>(const T& other) : data_(other) {
}
T& operator=(const T& other) {
data_ = other;
}
TLispType(const TLispType<T>& other) : data_(other.data_) {
}
T& operator=(const TLispType<T>& other) {
data_ = other.data_;
return *this;
}
// unboxed types can be freely converted to C++ type
operator T() {
return data_;
}
};
/**
* LispPrimitive is a pointer to a function that returns a reference to
* a lisp obj and takes in a lisp obj.
*/
typedef LispObjRef (*CPrim)(LispObjRef args, LispEnvRef env);
/* symbol : a name that references an object */
struct LispSymbol
{
std::string name;
LispObjRef value;
LispSymbol(const std::string& n, const LispObjRef& v) : name(n), value(v)
{
};
LispSymbol(const LispSymbol& sym) : name(sym.name), value(sym.value)
{
}
};
/* byte */
typedef TLispType<CChar> CharType;
/* fixnum */
typedef TLispType<CFixnum> FixnumType;
/* float */
typedef TLispType<CFloatnum> FloatnumType;
/* string */
typedef TLispType< CString > StringType;
/* symbol */
typedef TLispType< LispSymbol > SymbolType;
/* cons cell */
typedef std::pair< LispObjRef, LispObjRef > CCons;
typedef TLispType< CCons > ConsType;
/** primitive */
typedef TLispType< CPrim > PrimType;
/* lambda */
typedef struct Lambda { LispObjRef args; LispObjRef body; Lambda() : args(LispObjRef()), body(LispObjRef()) {}; Lambda(LispObjRef a, LispObjRef b) : args(a), body(b) {}; } CLambda;
typedef TLispType< CLambda > LambdaType;
enum LispObjectType {
NIL = 0,
CHAR,
FIXNUM,
FLOATNUM,
SYMBOL,
STRING,
CONS,
PRIM,
LAMBDA
};
typedef boost::variant< NullType, CharType, FixnumType, FloatnumType, SymbolType, StringType, ConsType, PrimType, LambdaType > LispObjBase;
class LispObj : public LispObjBase {
public:
LispObj() : LispObjBase() {
}
template <typename T>
LispObj(const T& obj) : LispObjBase(TLispType<T>(obj)) {
}
LispObj(const FixnumType& fnum) : LispObjBase(fnum) {
}
LispObj(const FloatnumType& fnum) : LispObjBase(fnum) {
}
LispObj(const CharType& ch) : LispObjBase(ch) {
}
LispObj(const ConsType& cons) : LispObjBase(cons) {
}
LispObj(const StringType& string) : LispObjBase(string) {
}
LispObj(const SymbolType& symbol) : LispObjBase(symbol) {
}
LispObj(const PrimType& primitive) : LispObjBase(primitive) {
}
LispObj(const LambdaType& lambda) : LispObjBase(lambda) {
}
};
template <typename T>
inline LispObjRef make_literal(const T& literal)
{
return boost::shared_ptr<LispObj>(new LispObj(literal));
}
typedef const char *cstrptr;
template <>
inline LispObjRef make_literal(const cstrptr& literal)
{
std::string str(literal);
return boost::shared_ptr<LispObj>(new LispObj(str));
}
template <class LT>
inline typename LT::CType get_ctype(LispObjRef obj)
{
return ((typename LT::CType)(boost::get<LT>(*obj)));
}
} // end namespace lisp
#endif