Fixed the variable code, replacing it with simpler arguments and using exceptions...
[fridhskrift.git] / variable / variable.cpp
blobf3e67404e0e19dd40818e0e52a0732e6d0f5dfe6
1 #include <ail/string.hpp>
2 #include <fridh/symbol.hpp>
4 namespace fridh
6 namespace
8 std::string const zero_division_error_message = "Zero division error";
11 variable::variable():
12 type(variable_type_identifier::undefined)
16 variable::variable(variable const & other):
17 type(other.type)
19 switch(type)
21 case variable_type_identifier::boolean:
22 boolean = other.boolean;
23 break;
25 case variable_type_identifier::signed_integer:
26 signed_integer = other.signed_integer;
27 break;
29 case variable_type_identifier::unsigned_integer:
30 unsigned_integer = other.unsigned_integer;
31 break;
33 case variable_type_identifier::floating_point_value:
34 floating_point_value = other.floating_point_value;
35 break;
37 case variable_type_identifier::string:
38 string = new std::string(*other.string);
39 break;
41 case variable_type_identifier::array:
42 array = new types::vector(*other.array);
43 break;
45 case variable_type_identifier::map:
46 map = new types::map(*other.map);
47 break;
51 variable::~variable()
53 switch(type)
55 case variable_type_identifier::string:
56 delete string;
57 break;
59 case variable_type_identifier::array:
60 delete array;
61 break;
63 case variable_type_identifier::map:
64 delete map;
65 break;
69 types::floating_point_value variable::get_floating_point_value() const
71 switch(type)
73 case variable_type_identifier::signed_integer:
74 return signed_integer;
76 case variable_type_identifier::unsigned_integer:
77 return unsigned_integer;
79 case variable_type_identifier::floating_point_value:
80 return floating_point_value;
83 throw ail::exception("Failed to retrieve floating point value");
86 std::string variable::get_string_representation() const
88 switch(type)
90 case variable_type_identifier::boolean:
91 return ail::bool_to_string(boolean);
93 case variable_type_identifier::signed_integer:
94 return ail::number_to_string<types::signed_integer>(signed_integer);
96 case variable_type_identifier::unsigned_integer:
97 return ail::number_to_string<types::unsigned_integer>(unsigned_integer);
99 case variable_type_identifier::floating_point_value:
100 return ail::number_to_string<types::floating_point_value>(floating_point_value);
102 case variable_type_identifier::string:
103 return *string;
106 get_unary_argument_type_error("String representation", type);
109 bool variable::get_boolean_value() const
111 switch(type)
113 case variable_type_identifier::boolean:
114 return boolean;
116 case variable_type_identifier::signed_integer:
117 return signed_integer != 0;
119 case variable_type_identifier::unsigned_integer:
120 return unsigned_integer != 0;
123 get_unary_argument_type_error("Boolean representation", type);
126 bool variable::is_zero() const
128 switch(type)
130 case variable_type_identifier::signed_integer:
131 return signed_integer == 0;
133 case variable_type_identifier::unsigned_integer:
134 return unsigned_integer == 0;
136 case variable_type_identifier::floating_point_value:
137 return floating_point_value == 0.0;
140 throw ail::exception("Unable to check if variable is zero");
143 bool variable::operator<(variable const & other) const
145 if(is_numeric_type() && other.is_numeric_type())
149 type == variable_type_identifier::floating_point_value ||
150 other.type == variable_type_identifier::floating_point_value
152 return get_floating_point_value() < other.get_floating_point_value();
153 else if
155 type == variable_type_identifier::unsigned_integer &&
156 other.type == variable_type_identifier::unsigned_integer
158 return unsigned_integer < other.unsigned_integer;
159 else
160 return signed_integer < other.signed_integer;
162 else if(type != other.type)
163 return static_cast<word>(type) < static_cast<word>(other.type);
164 else
166 if(type == variable_type_identifier::string)
167 return *string < *other.string;
168 else
169 return hash() < other.hash();
173 std::string get_type_string(variable_type type)
175 switch(type)
177 case variable_type_identifier::undefined:
178 return "undefined";
180 case variable_type_identifier::nil:
181 return "nil";
183 case variable_type_identifier::none:
184 return "none";
186 case variable_type_identifier::boolean:
187 return "boolean";
189 case variable_type_identifier::signed_integer:
190 return "integer";
192 case variable_type_identifier::unsigned_integer:
193 return "unsigned-integer";
195 case variable_type_identifier::floating_point_value:
196 return "float";
198 case variable_type_identifier::string:
199 return "string";
201 case variable_type_identifier::array:
202 return "array";
204 case variable_type_identifier::map:
205 return "map";
207 case variable_type_identifier::function:
208 return "function";
210 case variable_type_identifier::object:
211 return "object";
214 return "unknown";