Fixed bracket/array parsing, empty calls and empty arrays are now permitted
[fridhskrift.git] / variable / variable.cpp
blobd53e735ac33d82512ef187485ed718269da15333
1 #include <ail/string.hpp>
2 #include <fridh/symbol.hpp>
4 namespace fridh
6 variable::variable():
7 type(variable_type_identifier::undefined)
11 variable::variable(variable const & other)
13 copy(other);
16 variable::~variable()
18 destroy();
21 variable & variable::operator=(variable const & other)
23 destroy();
24 copy(other);
25 return *this;
28 void variable::copy(variable const & other)
30 type = other.type;
32 #define COPY_MEMBER(type) \
33 case variable_type_identifier::type: \
34 type = other.type; \
35 break;
37 #define COPY_MEMBER_POINTER(type, member_type, member) \
38 case variable_type_identifier::type: \
39 member = new member_type(*other.member); \
40 break;
42 switch(type)
44 COPY_MEMBER(boolean)
45 COPY_MEMBER(signed_integer)
46 COPY_MEMBER(unsigned_integer)
47 COPY_MEMBER(floating_point_value)
49 COPY_MEMBER_POINTER(string, std::string, string)
50 COPY_MEMBER_POINTER(array, types::vector, array)
51 COPY_MEMBER_POINTER(map, types::map, map)
54 #undef COPY_MEMBER_POINTER
55 #undef COPY_MEMBER
59 void variable::destroy()
62 #define DELETE_MEMBER(type) \
63 case variable_type_identifier::type: \
64 delete type; \
65 break;
67 switch(type)
69 DELETE_MEMBER(string)
70 DELETE_MEMBER(array)
71 DELETE_MEMBER(map)
74 type = variable_type_identifier::undefined;
76 #undef DELETE_MEMBER
80 types::floating_point_value variable::get_floating_point_value() const
82 switch(type)
84 case variable_type_identifier::signed_integer:
85 return signed_integer;
87 case variable_type_identifier::unsigned_integer:
88 return unsigned_integer;
90 case variable_type_identifier::floating_point_value:
91 return floating_point_value;
94 throw ail::exception("Failed to retrieve floating point value");
97 std::string variable::get_string_representation() const
99 switch(type)
101 case variable_type_identifier::boolean:
102 return ail::bool_to_string(boolean);
104 case variable_type_identifier::signed_integer:
105 return ail::number_to_string<types::signed_integer>(signed_integer);
107 case variable_type_identifier::unsigned_integer:
108 return ail::number_to_string<types::unsigned_integer>(unsigned_integer);
110 case variable_type_identifier::floating_point_value:
111 return ail::number_to_string<types::floating_point_value>(floating_point_value);
113 case variable_type_identifier::string:
114 return *string;
117 unary_argument_type_error("String representation", type);
119 //should never get here, suppress warnings
120 return "Error";
123 bool variable::get_boolean_value() const
125 switch(type)
127 case variable_type_identifier::boolean:
128 return boolean;
130 case variable_type_identifier::signed_integer:
131 return signed_integer != 0;
133 case variable_type_identifier::unsigned_integer:
134 return unsigned_integer != 0;
137 unary_argument_type_error("Boolean representation", type);
139 //should never get here, suppress warnings
140 return false;
143 bool variable::is_zero() const
145 switch(type)
147 case variable_type_identifier::signed_integer:
148 return signed_integer == 0;
150 case variable_type_identifier::unsigned_integer:
151 return unsigned_integer == 0;
153 case variable_type_identifier::floating_point_value:
154 return floating_point_value == 0.0;
157 throw ail::exception("Unable to check if variable is zero");
160 bool variable::operator<(variable const & other) const
162 if(is_numeric_type() && other.is_numeric_type())
166 type == variable_type_identifier::floating_point_value ||
167 other.type == variable_type_identifier::floating_point_value
169 return get_floating_point_value() < other.get_floating_point_value();
170 else if
172 type == variable_type_identifier::unsigned_integer &&
173 other.type == variable_type_identifier::unsigned_integer
175 return unsigned_integer < other.unsigned_integer;
176 else
177 return signed_integer < other.signed_integer;
179 else if(type != other.type)
180 return static_cast<word>(type) < static_cast<word>(other.type);
181 else
183 if(type == variable_type_identifier::string)
184 return *string < *other.string;
185 else
186 return hash() < other.hash();
190 std::string get_type_string(variable_type type)
192 switch(type)
194 case variable_type_identifier::undefined:
195 return "undefined";
197 case variable_type_identifier::nil:
198 return "nil";
200 case variable_type_identifier::none:
201 return "none";
203 case variable_type_identifier::boolean:
204 return "boolean";
206 case variable_type_identifier::signed_integer:
207 return "integer";
209 case variable_type_identifier::unsigned_integer:
210 return "unsigned-integer";
212 case variable_type_identifier::floating_point_value:
213 return "float";
215 case variable_type_identifier::string:
216 return "string";
218 case variable_type_identifier::array:
219 return "array";
221 case variable_type_identifier::map:
222 return "map";
224 case variable_type_identifier::function:
225 return "function";
227 case variable_type_identifier::object:
228 return "object";
231 return "unknown";