-
Notifications
You must be signed in to change notification settings - Fork 0
/
Value.h
331 lines (271 loc) · 8.51 KB
/
Value.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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
/**
* Copyright 2010 by Benjamin J. Land (a.k.a. BenLand100)
*
* This file is part of CPascal.
*
* CPascal is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CPascal is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with CPascal. If not, see <http://www.gnu.org/licenses/>.
*/
class Value;
class StringValue;
class IntegerValue;
class RealValue;
class CharValue;
class BooleanValue;
class ArrayValue;
class RecordValue;
class PointerValue;
#include <string>
#include <stack>
#include "Element.h"
#include "Type.h"
#include "Exceptions.h"
#include "Interpreter.h"
#ifndef _VALUE_H
#define _VALUE_H
typedef void* (*MALLOC)(int size) __attribute__((stdcall));
typedef void* (*REALLOC)(void* mem, int size) __attribute__((stdcall));
typedef void (*FREE)(void* mem) __attribute__((stdcall));
typedef struct {
int refcount;
char data[0];
} fpc_memory;
typedef struct {
int refcount;
int size;
char data[0];
} fpc_array;
class Value : public Element {
public:
static MALLOC malloc;
static REALLOC realloc;
static FREE free;
static Value* decref(Value *val) throw(int,InterpEx*);
static Value* incref(Value *val) throw(int,InterpEx*);
static Value* fromType(Type* type) throw(int,InterpEx*);
static Value* fromTypeMem(Type* type, void* mem) throw(int,InterpEx*);
const Type* typeObj;
const int type;
Value();
virtual void set(Value* val) throw(int,InterpEx*);
virtual Value* clone();
virtual Value* getRef() throw(int,InterpEx*);
virtual void setRef(Value* ref) throw(int,InterpEx*);
virtual void negate() throw(int,InterpEx*);
virtual void incr() throw(int,InterpEx*);
virtual void decr() throw(int,InterpEx*);
virtual Value* getField(int name) throw(int,InterpEx*);
virtual void setField(int name, Value* val) throw(int,InterpEx*);
virtual int size() throw(int,InterpEx*);
virtual void resize(int size) throw(int,InterpEx*);
virtual void setIndex(int index, Value* val) throw(int,InterpEx*);
virtual Value* getIndex(int index) throw(int,InterpEx*);
virtual char* asString() throw(int,InterpEx*);
virtual char asChar() throw(int,InterpEx*);
virtual double asReal() throw(int,InterpEx*);
virtual int asInteger() throw(int,InterpEx*);
virtual bool asBoolean() throw(int,InterpEx*);
virtual Value* invoke(Value** args, unsigned int numArgs, Frame* frame) throw (int,InterpEx*);
virtual int argSize();
virtual void refArg(void* mem) throw(int,InterpEx*);
virtual void valArg(void* mem) throw(int,InterpEx*);
virtual void read_c(void* res) throw(int,InterpEx*);
virtual void read_fpc(void* res) throw(int,InterpEx*);
protected:
Value(int impl_type, Type* impl_typeObj, bool ownsmem);
virtual ~Value();
unsigned int refcount;
bool owns_mem;
private:
Value(Value &val);
};
class MethodValue : public Value {
friend class PointerValue;
public:
MethodValue(void* mem, Meth* type);
MethodValue(Method* meth);
Value* clone();
void set(Value* val) throw(int,InterpEx*);
Value* invoke(Value** args, unsigned int numArgs, Frame* frame) throw (int,InterpEx*);
protected:
~MethodValue();
Method** meth;
};
class IntegerValue : public Value {
public:
IntegerValue(void* mem);
IntegerValue(int i);
Value* clone();
void set(Value* val) throw(int,InterpEx*);
void negate() throw(int,InterpEx*);
void incr() throw(int,InterpEx*);
void decr() throw(int,InterpEx*);
double asReal() throw(int,InterpEx*);
int asInteger() throw(int,InterpEx*);
int argSize();
void refArg(void* mem) throw(int,InterpEx*);
void valArg(void* mem) throw(int,InterpEx*);
void read_c(void* res) throw(int,InterpEx*);
void read_fpc(void* res) throw(int,InterpEx*);
protected:
~IntegerValue();
private:
int* integer;
};
class StringValue : public Value {
public:
StringValue(void* mem);
StringValue(char* str);
StringValue(std::string str);
Value* clone();
void set(Value* val) throw(int,InterpEx*);
char* asString() throw(int,InterpEx*);
Value* getIndex(int index) throw(int,InterpEx*);
void setIndex(int index, Value* val) throw(int,InterpEx*);
void resize(int size) throw(int,InterpEx*);
int size() throw(int,InterpEx*);
int argSize();
void refArg(void* mem) throw(int,InterpEx*);
void valArg(void* mem) throw(int,InterpEx*);
void read_c(void* res) throw(int,InterpEx*);
void read_fpc(void* res) throw(int,InterpEx*);
protected:
~StringValue();
private:
char** data;
};
class RealValue : public Value {
public:
RealValue(void* mem);
RealValue(double real);
Value* clone();
void set(Value* val) throw(int,InterpEx*);
void negate() throw(int,InterpEx*);
void incr() throw(int,InterpEx*);
void decr() throw(int,InterpEx*);
double asReal() throw(int,InterpEx*);
int argSize();
void refArg(void* mem) throw(int,InterpEx*);
void valArg(void* mem) throw(int,InterpEx*);
protected:
~RealValue();
private:
double* real;
};
class CharValue : public Value {
public:
CharValue(void* mem);
CharValue(char c);
Value* clone();
void set(Value* val) throw(int,InterpEx*);
void incr() throw(int,InterpEx*);
void decr() throw(int,InterpEx*);
char asChar() throw(int,InterpEx*);
int asInteger() throw(int,InterpEx*);
char* asString() throw(int,InterpEx*);
int argSize();
void refArg(void* mem) throw(int,InterpEx*);
void valArg(void* mem) throw(int,InterpEx*);
void read_c(void* res) throw(int,InterpEx*);
void read_fpc(void* res) throw(int,InterpEx*);
protected:
~CharValue();
private:
char* chr;
char** str; //temp
};
class BooleanValue : public Value {
public:
BooleanValue(void* mem);
BooleanValue(bool b);
Value* clone();
void set(Value* val) throw(int,InterpEx*);
bool asBoolean() throw(int,InterpEx*);
int asInteger() throw(int,InterpEx*);
int argSize();
void refArg(void* mem) throw(int,InterpEx*);
void valArg(void* mem) throw(int,InterpEx*);
void read_c(void* res) throw(int,InterpEx*);
void read_fpc(void* res) throw(int,InterpEx*);
protected:
~BooleanValue();
private:
char* boolean;
};
class ArrayValue : public Value {
public:
ArrayValue(Array* arr, void* mem);
ArrayValue(Array* arr);
Value* clone();
void set(Value* val) throw(int,InterpEx*);
int size() throw(int,InterpEx*);
void resize(int size) throw(int,InterpEx*);
Value* getIndex(int index) throw(int,InterpEx*);
void setIndex(int index, Value* val) throw(int,InterpEx*);
int argSize();
void refArg(void* mem) throw(int,InterpEx*);
void valArg(void* mem) throw(int,InterpEx*);
protected:
~ArrayValue();
private:
Type *elemType;
bool dynamic;
int start;
int elemsz;
char** data;
Value** array;
ArrayValue(Array* arr, bool internal);
};
class PointerValue : public Value {
public:
PointerValue(Pointer* pt, void* mem);
PointerValue(Pointer* pt);
PointerValue(Value* ref);
Value* clone();
void set(Value* val) throw(int,InterpEx*);
Value* getRef() throw(int,InterpEx*);
void setRef(Value* ref) throw(int,InterpEx*);
Value* invoke(Value** args, int numArgs, Frame* frame) throw (int,InterpEx*);
int argSize();
void refArg(void* mem) throw(int,InterpEx*);
void valArg(void* mem) throw(int,InterpEx*);
protected:
~PointerValue();
private:
Value* ref;
void** pas_ref;
Type* refType;
PointerValue(Pointer* pt, bool internal);
};
class RecordValue : public Value {
public:
RecordValue(Record* rec, void* mem);
RecordValue(Record* rec);
Value* clone();
void set(Value* val) throw(int,InterpEx*);
Value* getField(int slot) throw(int,InterpEx*);
void setField(int slot, Value* value) throw(int,InterpEx*);
int argSize();
void refArg(void* mem) throw(int,InterpEx*);
void valArg(void* mem) throw(int,InterpEx*);
protected:
~RecordValue();
private:
int memsize;
char* mem;
int len;
int* indexes;
Value** slots;
RecordValue(Record* rec, bool internal);
};
#endif /* _VALUE_H */