Implemented the operator/argument resolution
[fridhskrift.git] / variable / variable.cpp
bloba2a78e496c6c6f020cd40bccf25ddfbac104dbba
1 #include <ail/string.hpp>
2 #include <frith/symbol.hpp>
4 namespace frith
6 namespace
8 std::string const zero_division_error_message = "Zero division error";
11 unary_argument::unary_argument(variable & output, std::string & error_message):
12 output(output),
13 error_message(error_message)
17 binary_argument::binary_argument(variable const & other, variable & output, std::string & error_message):
18 other(other),
19 output(output),
20 error_message(error_message)
24 variable::variable():
25 type(variable_type_identifier::undefined)
29 variable::variable(variable const & other):
30 type(other.type)
32 switch(type)
34 case variable_type_identifier::boolean:
35 boolean = other.boolean;
36 break;
38 case variable_type_identifier::signed_integer:
39 signed_integer = other.signed_integer;
40 break;
42 case variable_type_identifier::unsigned_integer:
43 unsigned_integer = other.unsigned_integer;
44 break;
46 case variable_type_identifier::floating_point_value:
47 floating_point_value = other.floating_point_value;
48 break;
50 case variable_type_identifier::string:
51 string = new std::string(*other.string);
52 break;
54 case variable_type_identifier::array:
55 array = new types::vector(*other.array);
56 break;
58 case variable_type_identifier::map:
59 map = new types::map(*other.map);
60 break;
64 variable::~variable()
66 switch(type)
68 case variable_type_identifier::string:
69 delete string;
70 break;
72 case variable_type_identifier::array:
73 delete array;
74 break;
76 case variable_type_identifier::map:
77 delete map;
78 break;
82 types::floating_point_value variable::get_floating_point_value() const
84 switch(type)
86 case variable_type_identifier::signed_integer:
87 return signed_integer;
89 case variable_type_identifier::unsigned_integer:
90 return unsigned_integer;
92 case variable_type_identifier::floating_point_value:
93 return floating_point_value;
96 throw ail::exception("Failed to retrieve floating point value");
99 bool variable::get_string_representation(std::string & output) const
101 switch(type)
103 case variable_type_identifier::boolean:
104 output = ail::bool_to_string(boolean);
105 break;
107 case variable_type_identifier::signed_integer:
108 output = ail::number_to_string<types::signed_integer>(signed_integer);
109 break;
111 case variable_type_identifier::unsigned_integer:
112 output = ail::number_to_string<types::unsigned_integer>(unsigned_integer);
113 break;
115 case variable_type_identifier::floating_point_value:
116 output = ail::number_to_string<types::floating_point_value>(floating_point_value);
117 break;
119 default:
120 return false;
123 return true;
126 bool variable::get_boolean_value(bool & output) const
128 switch(type)
130 case variable_type_identifier::boolean:
131 output = boolean;
132 break;
134 case variable_type_identifier::signed_integer:
135 output = signed_integer != 0;
136 break;
138 case variable_type_identifier::unsigned_integer:
139 output = unsigned_integer != 0;
140 break;
142 default:
143 return false;
146 return true;
149 bool variable::is_zero() const
151 switch(type)
153 case variable_type_identifier::signed_integer:
154 return signed_integer == 0;
156 case variable_type_identifier::unsigned_integer:
157 return unsigned_integer == 0;
159 case variable_type_identifier::floating_point_value:
160 return floating_point_value == 0.0;
163 throw ail::exception("Unable to check if variable is zero");
166 bool variable::operator<(variable const & other) const
168 if(is_numeric_type() && other.is_numeric_type())
172 type == variable_type_identifier::floating_point_value ||
173 other.type == variable_type_identifier::floating_point_value
175 return get_floating_point_value() < other.get_floating_point_value();
176 else if
178 type == variable_type_identifier::unsigned_integer &&
179 other.type == variable_type_identifier::unsigned_integer
181 return unsigned_integer < other.unsigned_integer;
182 else
183 return signed_integer < other.signed_integer;
185 else if(type != other.type)
186 return static_cast<word>(type) < static_cast<word>(other.type);
187 else
189 if(type == variable_type_identifier::string)
190 return *string < *other.string;
191 else
192 return hash() < other.hash();
196 std::string get_type_string(variable_type type)
198 switch(type)
200 case variable_type_identifier::undefined:
201 return "undefined";
203 case variable_type_identifier::nil:
204 return "nil";
206 case variable_type_identifier::none:
207 return "none";
209 case variable_type_identifier::boolean:
210 return "boolean";
212 case variable_type_identifier::signed_integer:
213 return "integer";
215 case variable_type_identifier::unsigned_integer:
216 return "unsigned-integer";
218 case variable_type_identifier::floating_point_value:
219 return "float";
221 case variable_type_identifier::string:
222 return "string";
224 case variable_type_identifier::array:
225 return "array";
227 case variable_type_identifier::map:
228 return "map";
230 case variable_type_identifier::function:
231 return "function";
233 case variable_type_identifier::object:
234 return "object";
237 return "unknown";