Changed the FNV include path
[fridhskrift.git] / parser / precedence.cpp
blob65fddf4f995a115a7afbc6c5157f1ee258d4c232
1 #include <map>
2 #include <fridh/parser.hpp>
3 #include <fridh/symbol.hpp>
5 namespace fridh
7 namespace
9 std::set<binary_operator_type::type> binary_right_to_left_operators;
11 bool initialised_data = false;
14 void initialise_data()
16 if(initialised_data)
17 return;
20 using namespace binary_operator_type;
21 for(int i = static_cast<int>(assignment); i <= static_cast<int>(exponentiation_assignment); i++)
22 binary_right_to_left_operators.insert(static_cast<binary_operator_type::type>(i));
25 initialised_data = true;
28 word get_unary_operator_precedence(unary_operator_type::type input)
30 using namespace unary_operator_type;
32 switch(input)
34 case negation:
35 case logical_not:
36 case binary_not:
37 return 3;
39 case increment:
40 case decrement:
41 return 2;
44 throw ail::exception("Invalid unary operator type");
47 word get_binary_operator_precedence(binary_operator_type::type input)
49 using namespace binary_operator_type;
51 switch(input)
53 case addition:
54 case subtraction:
55 return 6;
57 case multiplication:
58 case division:
59 case modulo:
60 return 5;
62 case exponentiation:
63 //improvised, not from the C++ operators article as such
64 return 4;
66 case less_than:
67 case less_than_or_equal:
68 case greater_than:
69 case greater_than_or_equal:
70 return 8;
72 case not_equal:
73 case equal:
74 return 9;
76 case logical_and:
77 return 13;
79 case logical_or:
80 return 14;
82 case shift_left:
83 case shift_right:
84 return 7;
86 case binary_and:
87 return 10;
89 case binary_or:
90 return 12;
92 case binary_xor:
93 return 11;
95 case selection:
96 return 2;
98 case assignment:
99 case addition_assignment:
100 case subtraction_assignment:
101 case multiplication_assignment:
102 case division_assignment:
103 case modulo_assignment:
104 case exponentiation_assignment:
105 return 16;
108 throw ail::exception("Invalid binary operator type");
111 bool get_parse_tree_node_precedence(parse_tree_node & input, word & output)
113 switch(input.type)
115 case parse_tree_node_type::call:
116 case parse_tree_node_type::call_operator:
117 output = 2;
118 break;
120 case parse_tree_node_type::unary_operator_node:
121 output = get_unary_operator_precedence(input.unary_operator_pointer->type);
122 break;
124 case parse_tree_node_type::binary_operator_node:
125 output = get_binary_operator_precedence(input.binary_operator_pointer->type);
126 break;
128 default:
129 return false;
132 return true;
135 bool is_right_to_left_operator(parse_tree_node & input)
137 initialise_data();
139 if(input.type == parse_tree_node_type::unary_operator_node)
140 return true;
144 input.type == parse_tree_node_type::binary_operator_node &&
145 binary_right_to_left_operators.find(input.binary_operator_pointer->type) != binary_right_to_left_operators.end()
147 return true;
149 return false;