-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.cpp
356 lines (290 loc) · 7.19 KB
/
types.cpp
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
// Copyright (c) 2009, Nicholas "Indy" Ray. All rights reserved.
// See the LICENSE file for usage, modification, and distribution terms.
#include "types.h"
#include "symbol.h"
#include "typeinfo.h"
#include <assert.h>
#include <cstdio>
//TODO: allow compile time code transformation by appending source data (strings, and line info to the type data.
//TODO: Dynamic build this too
// It is essential that this is orderd in the same way the enums are numbered.
static const dynamic_type TypeList[DT_Invalid] =
{
{DT_Pair, sizeof(pair), NULL},
{DT_Symbol, sizeof(symbol), NULL},
{DT_Int, sizeof(int), NULL},
{DT_Real, sizeof(float), NULL},
{DT_String, invalid_size, NULL},
{DT_Char, sizeof(char), NULL},
{DT_TypeInfo, sizeof(type_info_size), typeinfo_finish}
};
const dynamic_type* get_type(dynamic_types typeId)
{
assert(typeId < DT_Invalid);
return &TypeList[typeId];
}
pointer ploy_alloc(const dynamic_type* type, size_t size)
{
assert(size != invalid_size || type->AllocSize != invalid_size);
size_t alloc_size = size;
if(alloc_size == invalid_size)
alloc_size = type->AllocSize;
alloc_size += sizeof(dynamic_types);
void* alloc = malloc(alloc_size);
memset(alloc, 0, alloc_size); // Remove if we implement type initializers
*((dynamic_types*)alloc) = type->Id;
return (pointer)alloc;
}
pointer ploy_alloc(const dynamic_type* type)
{
return ploy_alloc(type, invalid_size);
}
pointer ploy_static_alloc(const dynamic_type* type, size_t size)
{
pointer P = ploy_alloc(type, size);
*((dynamic_types*)P) = (dynamic_types)(type->Id | DT_Static_Flag);
return P;
}
pointer ploy_static_alloc(const dynamic_type* type)
{
return ploy_static_alloc(type, invalid_size);
}
void ploy_free(pointer P)
{
if(P == NIL ||
*(dynamic_types*)P & DT_Static_Flag)
return; // Abort free for static or NIL objects.
const dynamic_type* T = get_type(get_type_id(P));
if(T->finish)
T->finish(P);
free(P);
}
dynamic_types get_type_id(pointer P)
{
return (dynamic_types)((*(dynamic_types*)P) & ~DT_Static_Flag);
}
bool is_type(pointer P, dynamic_types type)
{
return (P != NIL) && (get_type_id(P) == type);
}
void* get_value(pointer P)
{
return (char*)P+sizeof(dynamic_types);
}
pair* get_pair(pointer P)
{
assert(is_type(P, DT_Pair));
return (pair*)get_value(P);
}
pointer create_pair(pointer car, pointer cdr)
{
pointer ret = ploy_alloc(get_type(DT_Pair));
pair* value = get_pair(ret);
value->_car = car;
value->_cdr = cdr;
return ret;
}
pointer pair_car(pointer P)
{
assert(is_type(P, DT_Pair));
return get_pair(P)->_car;
}
pointer pair_cdr(pointer P)
{
assert(is_type(P, DT_Pair));
return get_pair(P)->_cdr;
}
pointer* ref_car(pointer P)
{
assert(is_type(P, DT_Pair));
return &get_pair(P)->_car;
}
pointer* ref_cdr(pointer P)
{
assert(is_type(P, DT_Pair));
return &get_pair(P)->_cdr;
}
// we don't have any complicated trees atm, so we can just free the old when we set
// watch out though, it'll only free the first link in a list.
// also not in the main header so we don't overuse.
void set_car(pointer P, pointer V)
{
assert(is_type(P, DT_Pair));
ploy_free(get_pair(P)->_car);
get_pair(P)->_car = V;
}
void set_cdr(pointer P, pointer V)
{
assert(is_type(P, DT_Pair));
ploy_free(get_pair(P)->_cdr);
get_pair(P)->_cdr = V;
}
void clear_car(pointer P)
{
assert(is_type(P, DT_Pair));
get_pair(P)->_car = NIL;
}
void clear_cdr(pointer P)
{
assert(is_type(P, DT_Pair));
get_pair(P)->_cdr = NIL;
}
void append_in_place(pointer P, pointer V)
{
assert(is_type(P, DT_Pair));
if(cdr(P) == NIL)
set_cdr(P, V);
else
append_in_place(cdr(P), V);
}
symbol* get_symbol(pointer P)
{
assert(is_type(P, DT_Symbol));
return (symbol*)get_value(P);
}
pointer create_symbol(symbol_table* tbl, const char* sym)
{
pointer ret = ploy_alloc(get_type(DT_Symbol));
*get_symbol(ret) = symbol_from_string(tbl, sym);
return ret;
}
pointer create_symbol(symbol_table* tbl, const char* sym, size_t len)
{
pointer ret = ploy_alloc(get_type(DT_Symbol));
*get_symbol(ret) = symbol_from_string(tbl, sym, len);
return ret;
}
int* get_int_ref(pointer P)
{
return (int*)get_value(P);
}
int get_int(pointer P)
{
return *get_int_ref(P);
}
pointer create_int(int i)
{
pointer ret = ploy_alloc(get_type(DT_Int));
*get_int_ref(ret) = i;
return ret;
}
float* get_real_ref(pointer P)
{
assert(is_type(P, DT_Real));
return (float*)get_value(P);
}
float get_real(pointer P)
{
return *get_real_ref(P);
}
pointer create_real(float f)
{
pointer ret = ploy_alloc(get_type(DT_Real));
*get_real_ref(ret) = f;
return ret;
}
char* get_char_ref(pointer P)
{
assert(is_type(P, DT_Char));
return (char*)get_value(P);
}
char get_char(pointer P)
{
return *get_char_ref(P);
}
pointer create_char(char c)
{
pointer ret = ploy_alloc(get_type(DT_Char));
*get_char_ref(ret) = c;
return ret;
}
char* get_string_ref(pointer P)
{
assert(is_type(P, DT_String));
return (char*)get_value(P);
}
const char* get_string(pointer P)
{
return get_string_ref(P);
}
pointer alloc_string(size_t len)
{
pointer ret = ploy_alloc(get_type(DT_String), len+1);
(get_string_ref(ret))[0] = '\0'; // Unneeded if everywhere initializes it directlly.
return ret;
}
pointer create_string(const char* str)
{
return create_string(str, strlen(str));
}
pointer create_string(const char* str, size_t len)
{
pointer ret = ploy_alloc(get_type(DT_String), len+1);
strncpy(get_string_ref(ret), str, len);
(get_string_ref(ret))[len+1] = '\0'; // not strictly nessacary while we are zeroing the mem, but for if/when we remove it.
return ret;
}
void destroy_list(pointer P)
{
if(is_type(P, DT_Pair))
{
if(pair_car(P) != NIL)
destroy_list(pair_car(P));
if(pair_cdr(P) != NIL)
destroy_list(pair_cdr(P));
}
ploy_free(P);
}
void print_object(pointer P, symbol_table* table)
{
FILE* out = stdout;
if(P == NIL)
{
fputs("NIL", out);
return;
}
switch(get_type_id(P))
{
case DT_Pair:
if(is_type(pair_car(P), DT_Pair))
{
putc('(', out);
print_object(pair_car(P), table);
putc(')', out);
}
else
print_object(pair_car(P), table);
putc(' ', out);
if(pair_cdr(P) != NIL)
{
if(!is_type(pair_cdr(P), DT_Pair))
fputs(". ", out);
print_object(pair_cdr(P), table);
}
break;
case DT_Symbol:
fputs(string_from_symbol(table, *get_symbol(P)), out);
break;
case DT_Int:
fprintf(out, "%d", get_int(P));
break;
case DT_Real:
fprintf(out, "%f", get_real(P));
break;
case DT_String:
fputs(get_string(P), out);
break;
case DT_Char:
putc(get_char(P), out);
break;
case DT_TypeInfo:
print_typeinfo(P, table, out);
break;
case DT_Invalid:
fputs("#INVALID#", out);
break;
case DT_Any:
fputs("#ANY#", out);
break;
}
}