2 #include <fridh/symbol.hpp>
3 #include <fridh/lexer.hpp>
7 typedef std::map
<lexeme_type::type
, unary_operator_type::type
> unary_lexeme_map_type
;
8 typedef std::map
<lexeme_type::type
, binary_operator_type::type
> binary_lexeme_map_type
;
10 using namespace lexeme_type
;
14 bool maps_are_initialised
= false;
16 unary_lexeme_map_type unary_lexeme_map
;
17 binary_lexeme_map_type binary_lexeme_map
;
20 void initialise_maps()
22 if(maps_are_initialised
)
25 unary_lexeme_map
[logical_not
] = unary_operator_type::logical_not
;
26 unary_lexeme_map
[binary_not
] = unary_operator_type::binary_not
;
28 unary_lexeme_map
[increment
] = unary_operator_type::increment
;
29 unary_lexeme_map
[decrement
] = unary_operator_type::decrement
;
31 binary_lexeme_map
[addition
] = binary_operator_type::addition
;
32 binary_lexeme_map
[subtraction
] = binary_operator_type::subtraction
;
33 binary_lexeme_map
[multiplication
] = binary_operator_type::multiplication
;
34 binary_lexeme_map
[division
] = binary_operator_type::division
;
35 binary_lexeme_map
[modulo
] = binary_operator_type::modulo
;
36 binary_lexeme_map
[exponentiation
] = binary_operator_type::exponentiation
;
38 binary_lexeme_map
[less_than
] = binary_operator_type::less_than
;
39 binary_lexeme_map
[less_than_or_equal
] = binary_operator_type::less_than_or_equal
;
40 binary_lexeme_map
[greater_than
] = binary_operator_type::greater_than
;
41 binary_lexeme_map
[greater_than_or_equal
] = binary_operator_type::greater_than_or_equal
;
42 binary_lexeme_map
[not_equal
] = binary_operator_type::not_equal
;
43 binary_lexeme_map
[equal
] = binary_operator_type::equal
;
45 binary_lexeme_map
[logical_and
] = binary_operator_type::logical_and
;
46 binary_lexeme_map
[logical_or
] = binary_operator_type::logical_or
;
48 binary_lexeme_map
[shift_left
] = binary_operator_type::shift_left
;
49 binary_lexeme_map
[shift_right
] = binary_operator_type::shift_right
;
51 binary_lexeme_map
[binary_and
] = binary_operator_type::binary_and
;
52 binary_lexeme_map
[binary_or
] = binary_operator_type::binary_or
;
53 binary_lexeme_map
[binary_xor
] = binary_operator_type::binary_xor
;
55 binary_lexeme_map
[selection_operator
] = binary_operator_type::selection
;
57 binary_lexeme_map
[assignment
] = binary_operator_type::assignment
;
58 binary_lexeme_map
[addition_assignment
] = binary_operator_type::addition_assignment
;
59 binary_lexeme_map
[subtraction_assignment
] = binary_operator_type::subtraction_assignment
;
60 binary_lexeme_map
[multiplication_assignment
] = binary_operator_type::multiplication_assignment
;
61 binary_lexeme_map
[division_assignment
] = binary_operator_type::division_assignment
;
62 binary_lexeme_map
[modulo_assignment
] = binary_operator_type::modulo_assignment
;
63 binary_lexeme_map
[exponentiation_assignment
] = binary_operator_type::exponentiation_assignment
;
65 maps_are_initialised
= true;
68 void lexeme_to_argument_node(lexeme
& input
, parse_tree_node
& output
)
70 if(input
.type
== name
)
72 output
.type
= parse_tree_node_type::symbol
;
73 parse_tree_symbol
* & symbol_pointer
= output
.symbol_pointer
;
74 symbol_pointer
= new parse_tree_symbol
;
75 symbol_pointer
->name
= *input
.string
;
78 std::cout << "lexeme_to_argument_node " << (int)output.type << std::endl;
79 std::cout << output.to_string() << std::endl;
84 variable
* & new_variable
= output
.variable_pointer
;
85 new_variable
= new variable
;
94 new_variable
->new_boolean(input
.boolean
);
98 new_variable
->new_signed_integer(input
.signed_integer
);
101 case unsigned_integer
:
102 new_variable
->new_unsigned_integer(input
.unsigned_integer
);
105 case floating_point_value
:
106 new_variable
->new_floating_point_value(input
.floating_point_value
);
110 new_variable
->new_string(*input
.string
);
115 throw ail::exception("Unknown lexeme type encountered while trying to produce a parse tree node for a variable");
118 output
.type
= parse_tree_node_type::variable
;
122 void lexeme_to_unary_operator_node(lexeme
& input
, parse_tree_node
& output
)
126 unary_lexeme_map_type::iterator iterator
= unary_lexeme_map
.find(input
.type
);
127 if(iterator
== unary_lexeme_map
.end())
128 throw ail::exception("Unknown lexeme type encountered while trying to match it to a corresponding unary operator parse tree node type");
130 output
.type
= parse_tree_node_type::unary_operator_node
;
131 parse_tree_unary_operator_node
* & unary_operator_pointer
= output
.unary_operator_pointer
;
132 unary_operator_pointer
= new parse_tree_unary_operator_node
;
133 unary_operator_pointer
->type
= iterator
->second
;
136 void lexeme_to_binary_operator_node(lexeme
& input
, parse_tree_node
& output
)
140 binary_lexeme_map_type::iterator iterator
= binary_lexeme_map
.find(input
.type
);
141 if(iterator
== binary_lexeme_map
.end())
142 throw ail::exception("Unknown lexeme type encountered while trying to match it to a corresponding binary operator parse tree node type");
144 output
.type
= parse_tree_node_type::binary_operator_node
;
145 parse_tree_binary_operator_node
* & binary_operator_pointer
= output
.binary_operator_pointer
;
146 binary_operator_pointer
= new parse_tree_binary_operator_node
;
147 binary_operator_pointer
->type
= iterator
->second
;