-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalue.h
179 lines (142 loc) · 4.54 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
/*
* Copyright (C) 2012-2014 Impossible Language
* by Jacopo Santoni (Jack)
* License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
* Created on 12/21/12
*/
#ifndef _VALUE_H_
#define _VALUE_H_
#include "defines.h"
#include "vm/heap.h"
#include "types/traits.h"
#include "types/range.h"
#include "types/lazy.h"
#include <cmath>
#include <string>
#include <list>
#include <vector>
#include <unordered_map>
#include <unordered_set>
#define TYPES(x, y) (x << 8 | y)
using opcode_t = Opcode;
class TCollection;
class String;
class Regex;
class Tuple;
class Array;
class List;
class Stack;
class Queue;
class Set;
class Map;
class Range;
class LazyArray;
class Code;
class Error;
union value_data
{
opcode_t op;
integral_t i;
real_t f;
char c;
bool b;
managed_object* ptr;
value_data(opcode_t op) : op(op) { }
value_data(integral_t i) : i(i) { }
value_data(real_t f) : f(f) { }
value_data(char c) : c(c) { }
value_data(bool b) : b(b) { }
value_data(managed_object* ptr) : ptr(ptr) { }
bool operator==(const value_data& o) const { return i == o.i; }
};
#if IMPOSSIBLE_USE_PACKED_VALUES
class __attribute__((packed)) Value final
#else
class Value final
#endif
{
public:
value_data data;
TypeInfo type;
public:
Value() : type(TYPE_NIL), data(0LL) { }
Value(const Value& other) : type(other.type), data(other.data) { }
Value(Type type) : type(type), data(nullptr) { }
Value(Type type, managed_object* ptr) : type(type), data(ptr) { }
//Value(int value) : Value((integral_t)value) { }
Value(integral_t value) : type(TYPE_INT), data(value) { }
Value(real_t value) : type(TYPE_FLOAT), data(value) { }
Value(char value) : type(TYPE_CHAR), data(value) { }
Value(bool value) : type(TYPE_BOOL), data(value) { }
Value(opcode_t value) : type(TYPE_OPCODE), data(value) { }
Value(String* string);
Value(Regex* string);
Value(Tuple* tuple);
Value(List* list);
Value(Stack* list);
Value(Queue* list);
Value(Array* array);
Value(Map* map);
Value(Set* set);
Value(Lambda* lambda);
Value(Range* range);
Value(LazyArray* lazyArray);
Value(Error* error);
Value& operator=(const Value& other) { this->data = other.data; this->type = other.type; return *this; }
bool valid() const { return type != TYPE_INVALID; }
bool operator<(const Value& other) const;
bool operator==(const Value& value) const { return type.traits().equal_to(*this, value); }
std::string svalue() const { return type.traits().to_string(*this); }
bool operator>(const Value& other) const { return !operator==(other) && !operator<(other); }
bool operator!=(const Value& value) const { return !this->operator==(value); }
integral_t integral() const { return data.i; }
real_t real() const { return data.f; }
char character() const { return data.c; }
bool boolean() const { return data.b; }
opcode_t opcode() const { return data.op; }
//TODO: we should find a variant way to handle this
numeric_t number() const { return type == TYPE_INT ? data.i : data.f; }
template<typename T> auto number() const -> typename std::enable_if<std::is_same<T, integral_t>::value, T>::type { return data.i; }
template<typename T> auto number() const -> typename std::enable_if<std::is_same<T, real_t>::value, T>::type { return data.f; }
template<typename T, typename R> R traitOrElse(const std::function<R(T*)>& t, R f) const {
auto* ptr = dynamic_cast<T*>(data.ptr);
return ptr ? t(ptr) : f;
}
template<typename T> T* object() const
{
T* ptr = dynamic_cast<T*>(data.ptr);
assert(ptr);
return ptr;
}
TCollection* collection() const;
String* string() const;
Regex* regex() const;
Tuple* tuple() const;
List* list() const;
Stack* stack() const;
Queue* queue() const;
Set* set() const;
Map* map() const;
Array* array() const;
Range* range() const;
LazyArray* lazyArray() const;
Error* error() const;
template<typename T> T* as() const;
Traits::Indexable* indexable() const;
Traits::Iterable* iterable() const;
Traits::Appendable* appendable() const;
Traits::Poppable* poppable() const;
Lambda* lambda() const;
static Value INVALID;
struct hash { size_t operator()(const Value&) const; };
};
template<> inline String* Value::as<String>() const { return string(); }
template<> inline Array* Value::as<Array>() const { return array(); }
template<> inline List* Value::as<List>() const { return list(); }
class TCollection : public managed_object, public Traits::Countable
{
public:
virtual void put(Value value) = 0;
virtual bool empty() const = 0;
};
#endif