A scope is now only left after a function or a class - I erroneously went up when...
[fridhskrift.git] / parser / node.cpp
blob77704ddf9ca60e33a9677fb5f07258ac0462a4c6
1 #include <map>
2 #include <fridh/symbol.hpp>
3 #include <fridh/lexer.hpp>
5 namespace fridh
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;
12 namespace
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)
23 return;
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;
77 else
79 variable * & new_variable = output.variable_pointer;
80 new_variable = new variable;
82 switch(input.type)
84 case nil:
85 new_variable->nil();
86 break;
88 case boolean:
89 new_variable->new_boolean(input.boolean);
90 break;
92 case signed_integer:
93 new_variable->new_signed_integer(input.signed_integer);
94 break;
96 case unsigned_integer:
97 new_variable->new_unsigned_integer(input.unsigned_integer);
98 break;
100 case floating_point_value:
101 new_variable->new_floating_point_value(input.floating_point_value);
102 break;
104 case string:
105 new_variable->new_string(*input.string);
106 break;
108 default:
109 delete new_variable;
110 throw ail::exception("Unknown lexeme type encountered while trying to produce a parse tree node for a variable");
113 output.type = parse_tree_node_type::variable;
117 void lexeme_to_unary_operator_node(lexeme & input, parse_tree_node & output)
119 initialise_maps();
121 unary_lexeme_map_type::iterator iterator = unary_lexeme_map.find(input.type);
122 if(iterator == unary_lexeme_map.end())
123 throw ail::exception("Unknown lexeme type encountered while trying to match it to a corresponding unary operator parse tree node type");
125 output.type = parse_tree_node_type::unary_operator_node;
126 parse_tree_unary_operator_node * & unary_operator_pointer = output.unary_operator_pointer;
127 unary_operator_pointer = new parse_tree_unary_operator_node;
128 unary_operator_pointer->type = iterator->second;
131 void lexeme_to_binary_operator_node(lexeme & input, parse_tree_node & output)
133 initialise_maps();
135 binary_lexeme_map_type::iterator iterator = binary_lexeme_map.find(input.type);
136 if(iterator == binary_lexeme_map.end())
137 throw ail::exception("Unknown lexeme type encountered while trying to match it to a corresponding binary operator parse tree node type");
139 output.type = parse_tree_node_type::binary_operator_node;
140 parse_tree_binary_operator_node * & binary_operator_pointer = output.binary_operator_pointer;
141 binary_operator_pointer = new parse_tree_binary_operator_node;
142 binary_operator_pointer->type = iterator->second;