summaryrefslogtreecommitdiffstats
path: root/sca-cpp/trunk/kernel/value.hpp
blob: d602b306239303bd09ed9a67a6fc84593e22a631 (plain)
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

/* $Rev$ $Date$ */

#ifndef tuscany_value_hpp
#define tuscany_value_hpp

/**
 * Generic value type.
 */

#include <string>
#include <iostream>
#include <sstream>
#include "gc.hpp"
#include "function.hpp"
#include "list.hpp"

namespace tuscany
{

long int countValues = 0;
long int countEValues = 0;
long int countCValues = 0;
long int countVValues = 0;

bool resetValueCounters() {
    countValues = countEValues = countCValues = countVValues = 0;
    return true;
}

bool printValueCounters() {
    std::cout << "countValues " << countValues << std::endl;
    std::cout << "countEValues " << countEValues << std::endl;
    std::cout << "countCValues " << countCValues << std::endl;
    std::cout << "countVValues " << countVValues << std::endl;
    return true;
}

class value;

class value {
public:

    enum ValueType {
        Undefined, Symbol, String, List, Number, Bool, Char, Lambda, Ptr, PoolPtr
    };

    value() :
        type(value::Undefined) {
        countValues++;
        countEValues++;
    }

    value(const value& v) {
        countValues++;
        countCValues++;
        type = v.type;
        switch(type) {
        case value::List:
            lst() = v.lst();
        case value::Lambda:
            func() = v.func();
        case value::Symbol:
            str() = v.str();
        case value::String:
            str() = v.str();
        case value::Number:
            num() = v.num();
        case value::Bool:
            boo() = v.boo();
        case value::Char:
            chr() = v.chr();
        case value::Ptr:
            ptr() = v.ptr();
        case value::PoolPtr:
            poolptr() = v.poolptr();
        default:
            break;
        }
    }

    const value& operator=(const value& v) {
        if(this == &v)
            return *this;
        type = v.type;
        switch(type) {
        case value::List:
            lst() = v.lst();
        case value::Lambda:
            func() = v.func();
        case value::Symbol:
            str() = v.str();
        case value::String:
            str() = v.str();
        case value::Number:
            num() = v.num();
        case value::Bool:
            boo() = v.boo();
        case value::Char:
            chr() = v.chr();
        case value::Ptr:
            ptr() = v.ptr();
        case value::PoolPtr:
            poolptr() = v.poolptr();
        default:
            break;
        }
        return *this;
    }

    virtual ~value() {
        countValues--;
    }

    value(const lambda<value(list<value>&)>& func) :
        type(value::Lambda), data(vdata(func)) {
        countValues++;
        countVValues++;
    }

    value(const std::string& str) :
        type(value::String), data(vdata(result(str))) {
        countValues++;
        countVValues++;
    }

    value(const char* str) :
        type(value::Symbol), data(vdata(result(std::string(str)))) {
        countValues++;
        countVValues++;
    }

    value(const list<value>& lst) :
        type(value::List), data(vdata(result(lst))) {
        countValues++;
        countVValues++;
    }

    value(const list<list<value> >& l) :
        type(value::List), data(vdata(result(listOfValues(l)))) {
        countValues++;
        countVValues++;
    }

    value(const double num) :
        type(value::Number), data(vdata(result(num))) {
        countValues++;
        countVValues++;
    }

    value(const int num) :
        type(value::Number), data(vdata(result((double)num))) {
        countValues++;
        countVValues++;
    }

    value(const bool boo) :
        type(value::Bool), data(vdata(result(boo))) {
        countValues++;
        countVValues++;
    }

    value(const char chr) :
        type(value::Char), data(vdata(result(chr))) {
        countValues++;
        countVValues++;
    }

    value(const gc_ptr<value> ptr) :
        type(value::Ptr), data(vdata(result(ptr))) {
        countValues++;
        countVValues++;
    }

    value(const gc_pool_ptr<value> ptr) :
        type(value::PoolPtr), data(vdata(result(ptr))) {
        countValues++;
        countVValues++;
    }

    const bool operator!=(const value& v) const {
        return !this->operator==(v);
    }

    const bool operator==(const value& v) const {
        if(this == &v)
            return true;
        if(type != v.type)
            return false;
        switch(type) {
        case value::Undefined:
            return true;
        case value::List:
            return lst()() == v.lst()();
        case value::Lambda:
            return func() == v.func();
        case value::Symbol:
            return str()() == v.str()();
        case value::String:
            return str()() == v.str()();
        case value::Number:
            return num()() == v.num()();
        case value::Bool:
            return boo()() == v.boo()();
        case value::Char:
            return chr()() == v.chr()();
        case value::Ptr:
            return ptr()() == v.ptr()();
        case value::PoolPtr:
            return poolptr()() == v.poolptr()();
        default:
            return false;
        }
    }

    const value operator()(list<value>& args) const {
        return func()(args);
    }

    operator const std::string() const {
        switch(type) {
        case value::List:
        case value::Lambda:
        case value::Ptr:
        case value::PoolPtr:
            return "";
        case value::Symbol:
        case value::String:
            return str()();
        case value::Number: {
            std::ostringstream sos;
            sos << num()();
            return sos.str();
        }
        case value::Bool: {
            if(boo()())
                return "true";
            else
                return "false";
        }
        case value::Char: {
            std::ostringstream sos;
            sos << chr()();
            return sos.str();
        }
        default:
            return "";
        }
    }

    operator const double() const {
        return num()();
    }

    operator const int() const {
        return num()();
    }

    operator const bool() const {
        return boo()();
    }

    operator const char() const {
        return chr()();
    }

    operator const gc_ptr<value>() const {
        return ptr()();
    }

    operator const gc_pool_ptr<value>() const {
        return poolptr()();
    }

    operator const list<value>() const {
        return lst()();
    }

    operator const list<list<value> >() const {
        return listOfListOfValues(lst()());
    }

    operator const lambda<value(list<value>&)>() const {
        return func();
    }

    friend std::ostream& operator<<(std::ostream&, const value&);

    ValueType type;
    lambda<char()> data;

private:
    template<typename T> lambda<T>& vdata() const {
        return *reinterpret_cast<lambda<T> *> (const_cast<lambda<char()> *> (&data));
    }

    template<typename T> const lambda<char()>& vdata(const T& v) const {
        return *reinterpret_cast<const lambda<char()> *> (&v);
    }

    lambda<double()>& num() const {
        return vdata<double()> ();
    }

    lambda<bool()>& boo() const {
        return vdata<bool()> ();
    }

    lambda<char()>& chr() const {
        return vdata<char()> ();
    }

    lambda<gc_ptr<value>()>& ptr() const {
        return vdata<gc_ptr<value>()> ();
    }

    lambda<gc_pool_ptr<value>()>& poolptr() const {
        return vdata<gc_pool_ptr<value>()> ();
    }

    lambda<std::string()>& str() const {
        return vdata<std::string()> ();
    }

    lambda<list<value>()>& lst() const {
        return vdata<list<value>()> ();
    }

    lambda<value(list<value>&)>& func() const {
        return vdata<value(list<value>&)> ();
    }

    const list<value> listOfValues(const list<list<value> >& l) const {
        if (isNil(l))
            return list<value>();
        return cons<value>(car(l), listOfValues(cdr(l)));
    }

    const list<list<value> > listOfListOfValues(const list<value>& l) const {
        if (isNil(l))
            return list<list<value> >();
        return cons<list<value> >(list<value>(car(l)), listOfListOfValues(cdr(l)));
    }

};

/**
 * Write a value to a stream.
 */
std::ostream& operator<<(std::ostream& out, const value& v) {
    switch(v.type) {
    case value::List:
        return out << v.lst()();
    case value::Lambda:
        return out << "lambda::" << v.func();
    case value::Symbol:
        return out << v.str()();
    case value::String:
        return out << '\"' << v.str()() << '\"';
    case value::Number:
        return out << v.num()();
    case value::Bool:
        if(v.boo()())
            return out << "true";
        else
            return out << "false";
    case value::Char:
        return out << v.chr()();
    case value::Ptr: {
        const gc_ptr<value> p =  v.ptr()();
        if (p == gc_ptr<value>(NULL))
            return out << "pointer::null";
        return out << "pointer::" << *p;
    }
    case value::PoolPtr: {
        const gc_pool_ptr<value> p =  v.poolptr()();
        if (p == gc_pool_ptr<value>(NULL))
            return out << "pointer::null";
        return out << "pointer::" << *p;
    }
    default:
        return out << "undefined";
    }
}

/**
 * Returns the type of a value.
 */
const value::ValueType type(const value& v) {
    return v.type;
}

/**
 * Returns true if a value is nil.
 */
const bool isNil(const value& value) {
    return value.type == value::Undefined;
}

/**
 * Returns true if a value is a lambda.
 */
const bool isLambda(const value& value) {
    return value.type == value::Lambda;
}

/**
 * Returns true if a value is a string.
 */
const bool isString(const value& value) {
    return value.type == value::String;
}

/**
 * Returns true if a value is a symbol.
 */
const bool isSymbol(const value& value) {
    return value.type == value::Symbol;
}

/**
 * Returns true if a value is a list.
 */
const bool isList(const value& value) {
    return value.type == value::List;
}

/**
 * Returns true if a value is a number.
 */
const bool isNumber(const value& value) {
    return value.type == value::Number;
}

/**
 * Returns true if a value is a boolean.
 */
const bool isBool(const value& value) {
    return value.type == value::Bool;
}

/**
 * Returns true if a value is a character.
 */
const bool isChar(const value& value) {
    return value.type == value::Char;
}

/**
 * Returns true if a value is a pointer.
 */
const bool isPtr(const value& value) {
    return value.type == value::Ptr;
}

/**
 * Returns true if a value is a pooled pointer.
 */
const bool isPoolPtr(const value& value) {
    return value.type == value::PoolPtr;
}

/**
 * Returns true if a value is a tagged list.
 */
const bool isTaggedList(const value& exp, value tag) {
    if(isList(exp) && !isNil((list<value>)exp))
        return car((list<value>)exp) == tag;
    return false;
}

}
#endif /* tuscany_value_hpp */