The dot lexeme is now properly converted to a binary selection operator
[fridhskrift.git] / lexer / lexeme.cpp
blobf38b90af768b6365756197ba7896fe4a451dab8f
1 #include <fridh/lexer.hpp>
2 #include <ail/string.hpp>
4 namespace fridh
5 {
6 lexeme::lexeme():
7 type(lexeme_type::uninitialised)
11 lexeme::lexeme(lexeme const & other)
13 copy(other);
16 lexeme::lexeme(lexeme_type::type type):
17 type(type)
21 lexeme::lexeme(types::boolean boolean):
22 type(lexeme_type::boolean),
23 boolean(boolean)
27 lexeme::lexeme(types::signed_integer signed_integer):
28 type(lexeme_type::signed_integer),
29 signed_integer(signed_integer)
33 lexeme::lexeme(types::unsigned_integer unsigned_integer):
34 type(lexeme_type::unsigned_integer),
35 unsigned_integer(unsigned_integer)
39 lexeme::lexeme(types::floating_point_value floating_point_value):
40 type(lexeme_type::floating_point_value),
41 floating_point_value(floating_point_value)
45 lexeme::lexeme(std::string const & string):
46 type(lexeme_type::string),
47 string(new std::string(string))
51 lexeme::~lexeme()
53 destroy();
56 std::string lexeme::to_string() const
58 switch(type)
60 case lexeme_type::name:
61 return "name: " + *string;
63 case lexeme_type::nil:
64 return "nil";
66 case lexeme_type::boolean:
67 return "boolean: " + ail::bool_to_string(boolean);
69 case lexeme_type::signed_integer:
70 return "integer: " + ail::number_to_string(signed_integer);
72 case lexeme_type::unsigned_integer:
73 return "unsigned-integer: " + ail::number_to_string(unsigned_integer);
75 case lexeme_type::floating_point_value:
76 return "float: " + ail::number_to_string(floating_point_value);
78 case lexeme_type::string:
79 return "string: " + ail::replace_string(*string, "\n", "\\n");
81 case lexeme_type::addition:
82 return "+";
84 case lexeme_type::subtraction:
85 return "-";
87 case lexeme_type::multiplication:
88 return "*";
90 case lexeme_type::division:
91 return "/";
93 case lexeme_type::modulo:
94 return "%";
96 case lexeme_type::assignment:
97 return "=";
99 case lexeme_type::addition_assignment:
100 return "+=";
102 case lexeme_type::subtraction_assignment:
103 return "-=";
105 case lexeme_type::multiplication_assignment:
106 return "*=";
108 case lexeme_type::division_assignment:
109 return "/=";
111 case lexeme_type::modulo_assignment:
112 return "%=";
114 case lexeme_type::exponentiation_assignment:
115 return "**=";
117 case lexeme_type::increment:
118 return "++";
120 case lexeme_type::decrement:
121 return "--";
123 case lexeme_type::exponentiation:
124 return "**";
126 case lexeme_type::less_than:
127 return "<";
129 case lexeme_type::less_than_or_equal:
130 return "<=";
132 case lexeme_type::greater_than:
133 return ">";
135 case lexeme_type::greater_than_or_equal:
136 return ">=";
138 case lexeme_type::not_equal:
139 return "!=";
141 case lexeme_type::equal:
142 return "==";
144 case lexeme_type::logical_not:
145 return "!";
147 case lexeme_type::logical_and:
148 return "&";
150 case lexeme_type::logical_or:
151 return "|";
153 case lexeme_type::shift_left:
154 return "<<";
156 case lexeme_type::shift_right:
157 return ">>";
159 case lexeme_type::binary_and:
160 return "&&";
162 case lexeme_type::binary_or:
163 return "||";
165 case lexeme_type::binary_xor:
166 return "^";
168 case lexeme_type::binary_not:
169 return "~";
171 case lexeme_type::bracket_start:
172 return "bracket: start";
174 case lexeme_type::bracket_end:
175 return "bracket: end";
177 case lexeme_type::array_start:
178 return "array: start";
180 case lexeme_type::array_end:
181 return "array: end";
183 case lexeme_type::scope_start:
184 return "scope: start";
186 case lexeme_type::scope_end:
187 return "scope: end";
189 case lexeme_type::iteration:
190 return "iteration";
192 case lexeme_type::iterator:
193 return "iterator";
195 case lexeme_type::while_operator:
196 return "while";
198 case lexeme_type::function_declaration:
199 return "function";
201 case lexeme_type::anonymous_function_declaration:
202 return "anonymous function";
204 case lexeme_type::class_operator:
205 return "class operator";
207 case lexeme_type::dot:
208 return ".";
210 case lexeme_type::call_operator:
211 return ",";
213 case lexeme_type::scope_operator:
214 return ":";
216 default:
217 return "unknown";
221 lexeme & lexeme::operator=(lexeme const & other)
223 destroy();
224 copy(other);
225 return *this;
228 void lexeme::copy(lexeme const & other)
230 type = other.type;
231 if(is_string())
232 string = new std::string(*other.string);
235 void lexeme::destroy()
237 if(is_string())
238 delete string;
240 type = lexeme_type::uninitialised;
243 bool lexeme::is_string() const
245 return
246 type == lexeme_type::name ||
247 type == lexeme_type::string