A scope is now only left after a function or a class - I erroneously went up when...
[fridhskrift.git] / parser / precedence.cpp
blob79d66f5f257fa689e34d14547b75a3fcc92887b6
1 #include <map>
2 #include <fridh/parser.hpp>
3 #include <fridh/symbol.hpp>
5 namespace fridh
7 typedef std::map<unary_operator_type::type, operator_precedence::type> unary_operator_precedence_map_type;
8 typedef std::map<binary_operator_type::type, operator_precedence::type> binary_operator_precedence_map_type;
10 namespace
12 unary_operator_precedence_map_type unary_operator_precedence_map;
13 binary_operator_precedence_map_type binary_operator_precedence_map;
14 std::set<binary_operator_type::type> binary_right_to_left_operators;
16 bool initialised_data = false;
19 void initialise_data()
21 if(initialised_data)
22 return;
24 unary_operator_precedence_map[unary_operator_type::negation] = operator_precedence::negation;
25 unary_operator_precedence_map[unary_operator_type::logical_not] = operator_precedence::logical_not;
26 unary_operator_precedence_map[unary_operator_type::binary_not] = operator_precedence::binary_not;
28 unary_operator_precedence_map[unary_operator_type::increment] = operator_precedence::increment;
29 unary_operator_precedence_map[unary_operator_type::decrement] = operator_precedence::decrement;
31 binary_operator_precedence_map[binary_operator_type::addition] = operator_precedence::addition;
32 binary_operator_precedence_map[binary_operator_type::subtraction] = operator_precedence::subtraction;
33 binary_operator_precedence_map[binary_operator_type::multiplication] = operator_precedence::multiplication;
34 binary_operator_precedence_map[binary_operator_type::division] = operator_precedence::division;
35 binary_operator_precedence_map[binary_operator_type::modulo] = operator_precedence::modulo;
36 binary_operator_precedence_map[binary_operator_type::exponentiation] = operator_precedence::exponentiation;
38 binary_operator_precedence_map[binary_operator_type::less_than] = operator_precedence::less_than;
39 binary_operator_precedence_map[binary_operator_type::less_than_or_equal] = operator_precedence::less_than_or_equal;
40 binary_operator_precedence_map[binary_operator_type::greater_than] = operator_precedence::greater_than;
41 binary_operator_precedence_map[binary_operator_type::greater_than_or_equal] = operator_precedence::greater_than_or_equal;
42 binary_operator_precedence_map[binary_operator_type::not_equal] = operator_precedence::not_equal;
43 binary_operator_precedence_map[binary_operator_type::equal] = operator_precedence::equal;
45 binary_operator_precedence_map[binary_operator_type::logical_and] = operator_precedence::logical_and;
46 binary_operator_precedence_map[binary_operator_type::logical_or] = operator_precedence::logical_or;
48 binary_operator_precedence_map[binary_operator_type::shift_left] = operator_precedence::shift_left;
49 binary_operator_precedence_map[binary_operator_type::shift_right] = operator_precedence::shift_right;
51 binary_operator_precedence_map[binary_operator_type::binary_and] = operator_precedence::binary_and;
52 binary_operator_precedence_map[binary_operator_type::binary_or] = operator_precedence::binary_or;
53 binary_operator_precedence_map[binary_operator_type::binary_xor] = operator_precedence::binary_xor;
55 binary_operator_precedence_map[binary_operator_type::selection] = operator_precedence::selection;
57 binary_operator_precedence_map[binary_operator_type::assignment] = operator_precedence::assignment;
58 binary_operator_precedence_map[binary_operator_type::addition_assignment] = operator_precedence::addition_assignment;
59 binary_operator_precedence_map[binary_operator_type::subtraction_assignment] = operator_precedence::subtraction_assignment;
60 binary_operator_precedence_map[binary_operator_type::multiplication_assignment] = operator_precedence::multiplication_assignment;
61 binary_operator_precedence_map[binary_operator_type::division_assignment] = operator_precedence::division_assignment;
62 binary_operator_precedence_map[binary_operator_type::modulo_assignment] = operator_precedence::modulo_assignment;
63 binary_operator_precedence_map[binary_operator_type::exponentiation_assignment] = operator_precedence::exponentiation_assignment;
66 using namespace binary_operator_type;
67 for(int i = static_cast<int>(assignment); i <= static_cast<int>(exponentiation_assignment); i++)
68 binary_right_to_left_operators.insert(static_cast<binary_operator_type::type>(i));
71 initialised_data = true;
74 word get_unary_operator_precedence(unary_operator_type::type input)
76 initialise_data();
78 unary_operator_precedence_map_type::iterator iterator = unary_operator_precedence_map.find(input);
79 if(iterator == unary_operator_precedence_map.end())
80 throw ail::exception("Invalid unary operator value specified for operator precedence lookup");
81 return static_cast<word>(iterator->second);
84 word get_binary_operator_precedence(binary_operator_type::type input)
86 initialise_data();
88 binary_operator_precedence_map_type::iterator iterator = binary_operator_precedence_map.find(input);
89 if(iterator == binary_operator_precedence_map.end())
90 throw ail::exception("Invalid binary operator value specified for operator precedence lookup");
91 return static_cast<word>(iterator->second);
94 bool get_parse_tree_node_precedence(parse_tree_node & input, word & output)
96 switch(input.type)
98 case parse_tree_node_type::call:
99 case parse_tree_node_type::call_operator:
100 output = static_cast<word>(operator_precedence::call);
101 break;
103 case parse_tree_node_type::unary_operator_node:
104 output = get_unary_operator_precedence(input.unary_operator_pointer->type);
105 break;
107 case parse_tree_node_type::binary_operator_node:
108 output = get_binary_operator_precedence(input.binary_operator_pointer->type);
109 break;
111 default:
112 return false;
115 return true;
118 bool is_right_to_left_operator(parse_tree_node & input)
120 initialise_data();
122 if(input.type == parse_tree_node_type::unary_operator_node)
123 return true;
127 input.type == parse_tree_node_type::binary_operator_node &&
128 binary_right_to_left_operators.find(input.binary_operator_pointer->type) != binary_right_to_left_operators.end()
130 return true;
132 return false;