1 #include <ail/string.hpp>
2 #include <fridh/symbol.hpp>
7 type(variable_type_identifier::undefined
)
11 variable::variable(variable
const & other
)
21 variable
& variable::operator=(variable
const & other
)
28 void variable::copy(variable
const & other_pattern
)
30 variable
const & other
= dynamic_cast<variable
const &>(other_pattern
);
34 #define COPY_MEMBER(type) \
35 case variable_type_identifier::type: \
39 #define COPY_MEMBER_POINTER(type, member_type, member) \
40 case variable_type_identifier::type: \
41 member = new member_type(*other.member); \
47 COPY_MEMBER(signed_integer
)
48 COPY_MEMBER(unsigned_integer
)
49 COPY_MEMBER(floating_point_value
)
51 COPY_MEMBER_POINTER(string
, std::string
, string
)
52 COPY_MEMBER_POINTER(array
, types::vector
, array
)
53 COPY_MEMBER_POINTER(map
, types::map
, map
)
56 #undef COPY_MEMBER_POINTER
61 void variable::destroy()
64 #define DELETE_MEMBER(type) \
65 case variable_type_identifier::type: \
76 type
= variable_type_identifier::undefined
;
82 types::floating_point_value
variable::get_floating_point_value() const
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 std::string
variable::get_string_representation() const
103 case variable_type_identifier::boolean
:
104 return ail::bool_to_string(boolean
);
106 case variable_type_identifier::signed_integer
:
107 return ail::number_to_string
<types::signed_integer
>(signed_integer
);
109 case variable_type_identifier::unsigned_integer
:
110 return ail::number_to_string
<types::unsigned_integer
>(unsigned_integer
);
112 case variable_type_identifier::floating_point_value
:
113 return ail::number_to_string
<types::floating_point_value
>(floating_point_value
);
115 case variable_type_identifier::string
:
119 unary_argument_type_error("String representation", type
);
121 //should never get here, suppress warnings
125 bool variable::get_boolean_value() const
129 case variable_type_identifier::boolean
:
132 case variable_type_identifier::signed_integer
:
133 return signed_integer
!= 0;
135 case variable_type_identifier::unsigned_integer
:
136 return unsigned_integer
!= 0;
139 unary_argument_type_error("Boolean representation", type
);
141 //should never get here, suppress warnings
145 bool variable::is_zero() const
149 case variable_type_identifier::signed_integer
:
150 return signed_integer
== 0;
152 case variable_type_identifier::unsigned_integer
:
153 return unsigned_integer
== 0;
155 case variable_type_identifier::floating_point_value
:
156 return floating_point_value
== 0.0;
159 throw ail::exception("Unable to check if variable is zero");
162 bool variable::operator<(variable
const & other
) const
164 if(is_numeric_type() && other
.is_numeric_type())
168 type
== variable_type_identifier::floating_point_value
||
169 other
.type
== variable_type_identifier::floating_point_value
171 return get_floating_point_value() < other
.get_floating_point_value();
174 type
== variable_type_identifier::unsigned_integer
&&
175 other
.type
== variable_type_identifier::unsigned_integer
177 return unsigned_integer
< other
.unsigned_integer
;
179 return signed_integer
< other
.signed_integer
;
181 else if(type
!= other
.type
)
182 return static_cast<word
>(type
) < static_cast<word
>(other
.type
);
185 if(type
== variable_type_identifier::string
)
186 return *string
< *other
.string
;
188 return hash() < other
.hash();
192 std::string
get_type_string(variable_type type
)
196 case variable_type_identifier::undefined
:
199 case variable_type_identifier::nil
:
202 case variable_type_identifier::none
:
205 case variable_type_identifier::boolean
:
208 case variable_type_identifier::signed_integer
:
211 case variable_type_identifier::unsigned_integer
:
212 return "unsigned-integer";
214 case variable_type_identifier::floating_point_value
:
217 case variable_type_identifier::string
:
220 case variable_type_identifier::array
:
223 case variable_type_identifier::map
:
226 case variable_type_identifier::function
:
229 case variable_type_identifier::object
: