This repository was archived by the owner on Apr 19, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkvalue.h
253 lines (208 loc) · 5.69 KB
/
kvalue.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
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
252
253
//
// Created by John Fourkiotis on 17/11/15.
//
#ifndef KAT_KVALUE_H
#define KAT_KVALUE_H
#include <iostream>
#include <memory>
#include <fstream>
#include <functional>
#include <cstdint>
#define TAG_MASK 0b111
#define PTR_MASK ~TAG_MASK
#define INT_MASK 0b001
#define CHR_MASK 0b010
#define IS_INT(v) (((reinterpret_cast<intptr_t>(v)) & INT_MASK) == INT_MASK)
#define MK_INT(n) (((n) << 1) | 1)
#define TK_INT(v) ((reinterpret_cast<intptr_t>(v)) >> 1)
#define IS_CHR(v) (((reinterpret_cast<intptr_t>(v)) & CHR_MASK) == CHR_MASK)
#define MK_CHR(c) (((c) << CHR_MASK) | CHR_MASK)
#define TK_CHR(v) static_cast<char>(((reinterpret_cast<intptr_t>(v)) >> CHR_MASK))
///////////////////////////////////////////////////////////////////////////////
enum class ValueType
{
BOOLEAN,
STRING,
NIL,
CELL,
SYMBOL,
PRIM_PROC,
COMP_PROC,
INPUT_PORT,
OUTPUT_PORT,
EOF_OBJECT,
MAX
};
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
class Kvm;
class Kgc;
class Value
{
public:
explicit Value(ValueType type) : type_(type) {}
virtual ~Value() {}
ValueType type() const { return type_; }
private:
ValueType type_;
mutable unsigned int marked_ = 0;
const Value* next_ = nullptr;
friend class Kvm;
friend class Kgc;
};
//---------------------------------------------------------------------------
class CompoundProc final : public Value
{
public:
CompoundProc() : Value(ValueType::COMP_PROC) {}
private:
const Value *parameters_ = nullptr;
const Value *body_ = nullptr;
const Value *env_ = nullptr;
friend class Kgc;
friend class Kvm;
};
//---------------------------------------------------------------------------
class Cell final : public Value
{
public:
Cell() : Value(ValueType::CELL) {}
private:
const Value *head_ = nullptr;
const Value *tail_ = nullptr;
friend class Kgc;
friend class Kvm;
friend const Value * car(const Value *);
friend const Value * cdr(const Value *);
friend void set_car(Value *v, const Value *obj);
friend void set_cdr(Value *v, const Value *obj);
};
//---------------------------------------------------------------------------
class InputPort final : public Value
{
public:
InputPort() : Value(ValueType::INPUT_PORT) {}
private:
std::unique_ptr<std::ifstream> input;
friend class Kvm;
};
//---------------------------------------------------------------------------
class OutputPort final : public Value
{
public:
OutputPort() : Value(ValueType::INPUT_PORT) {}
private:
std::unique_ptr<std::ofstream> output;
friend class Kvm;
};
//---------------------------------------------------------------------------
class PrimitiveProc final : public Value
{
public:
explicit PrimitiveProc()
: Value(ValueType::PRIM_PROC) {}
private:
const Value *(*func_)(Kvm *, const Value *) = nullptr;
friend class Kvm;
};
//---------------------------------------------------------------------------
class Nil final : public Value
{
public:
Nil() : Value(ValueType::NIL) {}
};
//---------------------------------------------------------------------------
class Eof final : public Value
{
public:
Eof() : Value(ValueType::EOF_OBJECT) {}
};
//---------------------------------------------------------------------------
template<typename T, ValueType V>
class PrimitiveValue final : public Value
{
public:
PrimitiveValue() : Value(V) {}
private:
T value_{};
friend class Kvm;
};
using String = PrimitiveValue<const char *, ValueType::STRING>;
using Boolean = PrimitiveValue<bool, ValueType::BOOLEAN>;
using Symbol = PrimitiveValue<const char *, ValueType::SYMBOL>;
//---------------------------------------------------------------------------
inline bool isBoolean(const Value *v)
{
return v->type() == ValueType::BOOLEAN;
}
inline bool isFixnum(const Value *v)
{
return IS_INT(v);
}
inline bool isCharacter(const Value *v)
{
return IS_CHR(v);
}
inline bool isString(const Value *v)
{
if (isFixnum(v) || isCharacter(v)) return false;
return v->type() == ValueType::STRING;
}
inline bool isCell(const Value *v)
{
if (isFixnum(v) || isCharacter(v)) return false;
return v->type() == ValueType::CELL;
}
inline bool isSymbol(const Value *v)
{
if (isFixnum(v) || isCharacter(v)) return false;
return v->type() == ValueType::SYMBOL;
}
inline bool isPrimitiveProc(const Value *v)
{
if (isFixnum(v) || isCharacter(v)) return false;
return v->type() == ValueType::PRIM_PROC;
}
inline bool isCompoundProc(const Value *v)
{
if (isFixnum(v) || isCharacter(v)) return false;
return v->type() == ValueType::COMP_PROC;
}
inline bool isInputPort(const Value *v)
{
if (isFixnum(v) || isCharacter(v)) return false;
return v->type() == ValueType::INPUT_PORT;
}
inline bool isOutputPort(const Value *v)
{
if (isFixnum(v) || isCharacter(v)) return false;
return v->type() == ValueType::OUTPUT_PORT;
}
inline bool isEof(const Value *v)
{
if (isFixnum(v) || isCharacter(v)) return false;
return v->type() == ValueType::EOF_OBJECT;
}
inline const Value* car(const Value *v)
{
return static_cast<const Cell *>(v)->head_;
}
inline const Value* cdr(const Value *v)
{
return static_cast<const Cell *>(v)->tail_;
}
inline const Value* cadr(const Value *v)
{
return car(cdr(v));
}
inline const Value* cddr(const Value *v)
{
return cdr(cdr(v));
}
const Value* caddr(const Value *v);
const Value* cdadr(const Value *v);
const Value* cadddr(const Value *v);
const Value* cdddr(const Value *v);
void set_car(Value *v, const Value *obj);
void set_cdr(Value *v, const Value *obj);
#endif //KAT_KVALUE_H