From dad9db00b713f5e3eac29180232da3477338a882 Mon Sep 17 00:00:00 2001 From: binrapt Date: Tue, 6 Oct 2009 20:17:13 +0200 Subject: [PATCH] Implemented further unary/binary operator lexeme to parse tree node conversion functions --- frith/intermediary.hpp | 2 ++ intermediary/node.cpp | 78 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 78 insertions(+), 2 deletions(-) diff --git a/frith/intermediary.hpp b/frith/intermediary.hpp index 5e89588..5f58365 100644 --- a/frith/intermediary.hpp +++ b/frith/intermediary.hpp @@ -114,4 +114,6 @@ namespace frith }; void lexeme_to_argument_node(lexeme & input, parse_tree_node & output); + void lexeme_to_unary_operator_node(lexeme & input, parse_tree_node & output, parse_tree_node & argument); + void lexeme_to_binary_operator_node(lexeme & input, parse_tree_node & output, parse_tree_node & left_argument, parse_tree_node & right_argument); } diff --git a/intermediary/node.cpp b/intermediary/node.cpp index d411eb2..52912f3 100644 --- a/intermediary/node.cpp +++ b/intermediary/node.cpp @@ -1,11 +1,58 @@ +#include #include namespace frith { - void lexeme_to_argument_node(lexeme & input, parse_tree_node & output) + typedef std::map unary_lexeme_map_type; + typedef std::map binary_lexeme_map_type; + + using namespace lexeme_type; + + namespace + { + bool maps_are_initialised = false; + + unary_lexeme_map_type unary_lexeme_map; + binary_lexeme_map_type binary_lexeme_map; + } + + void initialise_maps() { - using namespace lexeme_type; + if(maps_are_initialised) + return; + + unary_lexeme_map[logical_not] = unary_operator_type::logical_not; + unary_lexeme_map[binary_not] = unary_operator_type::binary_not; + + binary_lexeme_map[addition] = binary_operator_type::; + binary_lexeme_map[subtraction] = binary_operator_type::subtraction; + binary_lexeme_map[multiplication] = binary_operator_type::multiplication; + binary_lexeme_map[division] = binary_operator_type::division; + binary_lexeme_map[modulo] = binary_operator_type::modulo; + binary_lexeme_map[exponentiation] = binary_operator_type::exponentiation; + + binary_lexeme_map[less_than] = binary_operator_type::less_than; + binary_lexeme_map[less_than_or_equal] = binary_operator_type::less_than_or_equal; + binary_lexeme_map[greater_than] = binary_operator_type::greater_than; + binary_lexeme_map[greater_than_or_equal] = binary_operator_type::greater_than_or_equal; + binary_lexeme_map[unequal] = binary_operator_type::unequal; + binary_lexeme_map[equal] = binary_operator_type::equal; + + binary_lexeme_map[logical_and] = binary_operator_type::logical_and; + binary_lexeme_map[logical_or] = binary_operator_type::logical_or; + + binary_lexeme_map[shift_left] = binary_operator_type::shift_left; + binary_lexeme_map[shift_right] = binary_operator_type::shift_right; + + binary_lexeme_map[binary_and] = binary_operator_type::binary_and; + binary_lexeme_map[binary_or] = binary_operator_type::binary_or; + binary_lexeme_map[binary_xor] = binary_operator_type::binary_xor; + maps_are_initialised = true; + } + + void lexeme_to_argument_node(lexeme & input, parse_tree_node & output) + { if(input.type == name) { output.type = parse_tree_node_type::symbol; @@ -49,4 +96,31 @@ namespace frith output.variable_pointer = new_variable; } } + + void lexeme_to_unary_operator_node(lexeme & input, parse_tree_node & output, parse_tree_node & argument) + { + unary_lexeme_map_type::iterator iterator = unary_lexeme_map.find(input.type); + if(iterator == unary_lexeme_map.end()) + throw ail::exception("Unknown lexeme type encountered while trying to match it to a corresponding unary operator parse tree node type"); + + output.type = parse_tree_node_type::unary_operator_node; + parse_tree_unary_operator_node * & unary_operator_pointer = output.unary_operator_pointer; + unary_operator_pointer = new parse_tree_unary_operator_node; + unary_operator_pointer->type = iterator->second; + unary_operator_pointer->argument = argument; + } + + void lexeme_to_binary_operator_node(lexeme & input, parse_tree_node & output, parse_tree_node & left_argument, parse_tree_node & right_argument) + { + binary_lexeme_map_type::iterator iterator = binary_lexeme_map.find(input.type); + if(iterator == binary_lexeme_map.end()) + throw ail::exception("Unknown lexeme type encountered while trying to match it to a corresponding binary operator parse tree node type"); + + output.type = parse_tree_node_type::binary_operator_node; + parse_tree_binary_operator_node * & binary_operator_pointer = output.binary_operator_pointer; + binary_operator_pointer = new parse_tree_binary_operator_node; + binary_operator_pointer->type = iterator->second; + unary_operator_pointer->left_argument = left_argument; + unary_operator_pointer->right_argument = right_argument; + } } -- 2.11.4.GIT