Added a lame implementation of the iterator symbol
[fridhskrift.git] / intermediary / precedence.cpp
blobe10479b6b8e260a225b888025412340902438d0a
1 #include <map>
2 #include <fridh/intermediary.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;
15 bool initialised_maps = false;
18 void initialise_maps()
20 if(initialised_maps)
21 return;
23 unary_operator_precedence_map[unary_operator_type::negation] = operator_precedence::negation;
24 unary_operator_precedence_map[unary_operator_type::logical_not] = operator_precedence::logical_not;
25 unary_operator_precedence_map[unary_operator_type::binary_not] = operator_precedence::binary_not;
27 binary_operator_precedence_map[binary_operator_type::addition] = operator_precedence::addition;
28 binary_operator_precedence_map[binary_operator_type::subtraction] = operator_precedence::subtraction;
29 binary_operator_precedence_map[binary_operator_type::multiplication] = operator_precedence::multiplication;
30 binary_operator_precedence_map[binary_operator_type::division] = operator_precedence::division;
31 binary_operator_precedence_map[binary_operator_type::modulo] = operator_precedence::modulo;
32 binary_operator_precedence_map[binary_operator_type::exponentiation] = operator_precedence::exponentiation;
34 binary_operator_precedence_map[binary_operator_type::less_than] = operator_precedence::less_than;
35 binary_operator_precedence_map[binary_operator_type::less_than_or_equal] = operator_precedence::less_than_or_equal;
36 binary_operator_precedence_map[binary_operator_type::greater_than] = operator_precedence::greater_than;
37 binary_operator_precedence_map[binary_operator_type::greater_than_or_equal] = operator_precedence::greater_than_or_equal;
38 binary_operator_precedence_map[binary_operator_type::unequal] = operator_precedence::unequal;
39 binary_operator_precedence_map[binary_operator_type::equal] = operator_precedence::equal;
41 binary_operator_precedence_map[binary_operator_type::logical_and] = operator_precedence::logical_and;
42 binary_operator_precedence_map[binary_operator_type::logical_or] = operator_precedence::logical_or;
44 binary_operator_precedence_map[binary_operator_type::shift_left] = operator_precedence::shift_left;
45 binary_operator_precedence_map[binary_operator_type::shift_right] = operator_precedence::shift_right;
47 binary_operator_precedence_map[binary_operator_type::binary_and] = operator_precedence::binary_and;
48 binary_operator_precedence_map[binary_operator_type::binary_or] = operator_precedence::binary_or;
49 binary_operator_precedence_map[binary_operator_type::binary_xor] = operator_precedence::binary_xor;
51 binary_operator_precedence_map[binary_operator_type::selection] = operator_precedence::selection;
53 initialised_maps = true;
56 word get_unary_operator_precedence(unary_operator_type::type input)
58 initialise_maps();
60 unary_operator_precedence_map_type::iterator iterator = unary_operator_precedence_map.find(input);
61 if(iterator == unary_operator_precedence_map.end())
62 throw ail::exception("Invalid unary operator value specified for operator precedence lookup");
63 return static_cast<word>(iterator->second);
66 word get_binary_operator_precedence(binary_operator_type::type input)
68 initialise_maps();
70 binary_operator_precedence_map_type::iterator iterator = binary_operator_precedence_map.find(input);
71 if(iterator == binary_operator_precedence_map.end())
72 throw ail::exception("Invalid binary operator value specified for operator precedence lookup");
73 return static_cast<word>(iterator->second);
76 bool get_parse_tree_node_precedence(parse_tree_node & input, word & output)
78 switch(input.type)
80 case parse_tree_node_type::call:
81 case parse_tree_node_type::call_operator:
82 output = static_cast<word>(operator_precedence::call);
83 break;
85 case parse_tree_node_type::unary_operator_node:
86 output = get_unary_operator_precedence(input.unary_operator_pointer->type);
87 break;
89 case parse_tree_node_type::binary_operator_node:
90 output = get_binary_operator_precedence(input.binary_operator_pointer->type);
91 break;
93 default:
94 return false;
97 return true;
100 bool is_right_to_left_operator(parse_tree_node & input)
102 return input.type == parse_tree_node_type::unary_operator_node;