From 3ddfe3d4ebd2088e5f7dc74454b04c19a177f45f Mon Sep 17 00:00:00 2001 From: binrapt Date: Wed, 14 Oct 2009 04:02:05 +0200 Subject: [PATCH] Fixed a considerable amount of compiler errors and changed the lexer error handling from boolean return values to exceptions, at least internally --- fridh/function.hpp | 67 +++++++++++++++++++++------------------- fridh/intermediary.hpp | 11 +++---- fridh/lexer.hpp | 29 +++++++++-------- fridh/symbol.hpp | 9 ++++-- fridh/variable.hpp | 13 ++++---- function.cpp | 8 ++--- intermediary/control_flow.cpp | 2 +- intermediary/intermediary.cpp | 30 ++++++++++-------- intermediary/node.cpp | 3 +- intermediary/precedence.cpp | 2 +- intermediary/statement.cpp | 72 +++++++++++++++++++++---------------------- lexer/comment.cpp | 18 +++-------- lexer/group.cpp | 2 +- lexer/lexeme.cpp | 18 +++++------ lexer/lexer.cpp | 51 +++++++++++++++--------------- lexer/number.cpp | 24 +++------------ lexer/operator.cpp | 2 +- lexer/string.cpp | 35 ++++++++------------- main.cpp | 31 +++++++++++-------- symbol.cpp | 23 +++++++------- variable/comparison.cpp | 2 +- 21 files changed, 217 insertions(+), 235 deletions(-) diff --git a/fridh/function.hpp b/fridh/function.hpp index 02cf2df..5f9b81d 100644 --- a/fridh/function.hpp +++ b/fridh/function.hpp @@ -49,7 +49,7 @@ namespace fridh less_than_or_equal, greater_than, greater_than_or_equal, - unequal, + not_equal, equal, logical_and, @@ -104,15 +104,43 @@ namespace fridh }; } - struct - parse_tree_node, - parse_tree_symbol, - executable_units; + struct parse_tree_node; + struct parse_tree_symbol; + struct executable_unit; typedef std::vector parse_tree_nodes; typedef std::vector parse_tree_symbols; typedef std::vector executable_units; + struct parse_tree_symbol; + struct parse_tree_unary_operator_node; + struct parse_tree_binary_operator_node; + struct parse_tree_call; + struct parse_tree_array; + + struct parse_tree_node + { + parse_tree_node_type::type type; + union + { + variable * variable_pointer; + parse_tree_symbol * symbol_pointer; + parse_tree_unary_operator_node * unary_operator_pointer; + parse_tree_binary_operator_node * binary_operator_pointer; + parse_tree_call * call_pointer; + parse_tree_array * array_pointer; + }; + + parse_tree_node(); + parse_tree_node(parse_tree_node_type::type type); + parse_tree_node(variable * variable_pointer); + parse_tree_node(unary_operator_type::type unary_operator); + parse_tree_node(binary_operator_type::type binary_operator); + parse_tree_node(parse_tree_nodes & elements); + + void is_call(); + }; + struct parse_tree_symbol { std::string name; @@ -123,13 +151,13 @@ namespace fridh struct parse_tree_unary_operator_node { - unary_operator_type type; + unary_operator_type::type type; parse_tree_node argument; }; struct parse_tree_binary_operator_node { - binary_operator_type type; + binary_operator_type::type type; parse_tree_node left_argument, right_argument; @@ -146,29 +174,6 @@ namespace fridh parse_tree_nodes elements; }; - struct parse_tree_node - { - parse_tree_node_type::type type; - union - { - variable * variable_pointer; - parse_tree_symbol * symbol_pointer; - parse_tree_unary_operator_node * unary_operator_pointer; - parse_tree_binary_operator_node * binary_operator_pointer; - parse_tree_call * call_pointer; - parse_tree_array * array_pointer; - } - - parse_tree_node(); - parse_tree_node(parse_tree_node_type::type type); - parse_tree_node(variable * variable_pointer); - parse_tree_node(unary_operator_type::type unary_operator); - parse_tree_node(binary_operator_type::type binary_operator); - parse_tree_node(parse_tree_nodes & elements); - - void is_call(); - }; - struct if_statement { parse_tree_node conditional_term; @@ -216,7 +221,7 @@ namespace fridh for_each_statement * for_each_pointer; for_statement * for_pointer; while_statement * while_pointer; - } + }; }; struct function diff --git a/fridh/intermediary.hpp b/fridh/intermediary.hpp index aad4b0e..e285d70 100644 --- a/fridh/intermediary.hpp +++ b/fridh/intermediary.hpp @@ -1,9 +1,10 @@ #pragma once -#include #include #include +#include #include +#include namespace fridh { @@ -45,6 +46,7 @@ namespace fridh binary_and, binary_or, + binary_xor, logical_and, logical_or, @@ -56,9 +58,6 @@ namespace fridh division_assignment, modulo_assignment, exponentiation_assignment, - - increment, - decrement, }; } @@ -110,11 +109,11 @@ namespace fridh void name_collision_check(); symbol_tree_node & add_name(symbol::type symbol_type); - void intermediary_translator::operator_resolution(parse_tree_nodes & input, parse_tree_node & output); + void operator_resolution(parse_tree_nodes & input, parse_tree_node & output); void process_body(executable_units * output = 0); - void process_atomic_statement(lexeme_container & lexemes, std::size_t & offset, parse_tree_node & output, bool allow_multi_statements = false, lexeme_type::type terminator = lexeme_type::non_terminating_placeholder); + void process_atomic_statement(lexeme_container & lexemes, std::size_t & offset, parse_tree_nodes & output, bool allow_multi_statements = false, lexeme_type::type terminator = lexeme_type::non_terminating_placeholder); void process_offset_atomic_statement(parse_tree_node & output, std::size_t offset = 0); void process_composite_term(parse_tree_node & output); diff --git a/fridh/lexer.hpp b/fridh/lexer.hpp index f818e9c..1f15c9a 100644 --- a/fridh/lexer.hpp +++ b/fridh/lexer.hpp @@ -46,7 +46,7 @@ namespace fridh less_than_or_equal, greater_than, greater_than_or_equal, - unequal, + not_equal, equal, logical_not, @@ -102,9 +102,8 @@ namespace fridh }; } - struct - lexeme, - line_of_code; + struct lexeme; + struct line_of_code; typedef std::vector lexeme_container; typedef std::vector lines_of_code; @@ -152,14 +151,12 @@ namespace fridh class lexer { public: - lexer(std::string const & input, std::vector & lines, std::string & error); - - bool parse(); + lexer(std::string const & input, lines_of_code & lines); + bool parse(std::string & error); private: std::string const & input; - std::vector & lines; - std::string & error; + lines_of_code & lines; uword line; std::size_t @@ -169,20 +166,22 @@ namespace fridh line_of_code current_line; bool parse_operator(line_of_code & output); - bool parse_string(line_of_code & output, std::string & error_message, std::string error_prefix = ""); - bool parse_number(line_of_code & output, bool & error_occured); + void parse_string(line_of_code & output); + bool parse_number(line_of_code & output); void parse_name(line_of_code & output); - bool parse_comment(std::string & error_message); + void parse_comment(std::string & error_message); - std::string lexer_error(std::string const & message, uword error_line = 0); - std::string number_parsing_error(std::string const & message, bool & error_occured); + void lexer_error(std::string const & message, uword error_line = 0); + void number_parsing_error(std::string const & message, bool & error_occured); bool is_name_char(char input); bool string_match(std::string const & target); void process_newline(); + + void parse_lexemes(); }; - std::string visualise_lexemes(std::vector & lines); + std::string visualise_lexemes(lines_of_code & lines); void initialise_tables(); diff --git a/fridh/symbol.hpp b/fridh/symbol.hpp index 127e986..b713e29 100644 --- a/fridh/symbol.hpp +++ b/fridh/symbol.hpp @@ -2,9 +2,6 @@ #include #include -#include -#include -#include namespace fridh { @@ -20,7 +17,11 @@ namespace fridh } struct symbol_tree_node; + struct function; struct class_type; + struct module; + + class variable; typedef std::map node_children; @@ -47,5 +48,7 @@ namespace fridh }; } +#include #include +#include #include diff --git a/fridh/variable.hpp b/fridh/variable.hpp index 304f8e8..4aea730 100644 --- a/fridh/variable.hpp +++ b/fridh/variable.hpp @@ -2,13 +2,13 @@ #include #include #include -#include +#include namespace fridh { namespace variable_type_identifier { - enum variable_type + enum type { undefined, nil, @@ -27,10 +27,9 @@ namespace fridh } class variable; + struct function; - typedef variable_type_identifier::variable_type variable_type; - - class variable; + typedef variable_type_identifier::type variable_type; namespace types { @@ -77,7 +76,7 @@ namespace fridh DECLARE_BINARY_OPERATOR(less_than_or_equal) DECLARE_BINARY_OPERATOR(greater_than) DECLARE_BINARY_OPERATOR(greater_than_or_equal) - DECLARE_BINARY_OPERATOR(unequal) + DECLARE_BINARY_OPERATOR(not_equal) DECLARE_BINARY_OPERATOR(equal) DECLARE_UNARY_OPERATOR(logical_not) @@ -126,7 +125,7 @@ namespace fridh std::string get_string_representation() const; bool get_boolean_value() const; - bool variable::array_addition(variable const & argument) const + bool variable::array_addition(variable const & argument) const; bool string_addition(variable const & argument, variable & output) const; bool array_equality(variable const & other) const; diff --git a/function.cpp b/function.cpp index adf0a3e..f7c2ba3 100644 --- a/function.cpp +++ b/function.cpp @@ -20,21 +20,21 @@ namespace fridh } parse_tree_node::parse_tree_node(unary_operator_type::type unary_operator): - type(parse_tree_node::unary_operator) + type(parse_tree_node_type::unary_operator_node) { unary_operator_pointer = new parse_tree_unary_operator_node; unary_operator_pointer->type = unary_operator; } - parse_tree_node::parse_tree_node(binary_operator_type::type binary_operator) - type(parse_tree_node::binary_operator) + parse_tree_node::parse_tree_node(binary_operator_type::type binary_operator): + type(parse_tree_node_type::binary_operator_node) { binary_operator_pointer = new parse_tree_binary_operator_node; binary_operator_pointer->type = binary_operator; } parse_tree_node::parse_tree_node(parse_tree_nodes & elements): - type(parse_tree_node::array) + type(parse_tree_node_type::array) { array_pointer = new parse_tree_array; array_pointer->elements = elements; diff --git a/intermediary/control_flow.cpp b/intermediary/control_flow.cpp index e12f0e0..7b39fae 100644 --- a/intermediary/control_flow.cpp +++ b/intermediary/control_flow.cpp @@ -58,7 +58,7 @@ namespace fridh bool intermediary_translator::process_while(executable_unit & output) { lexeme_container & lexemes = lines[line_offset].lexemes; - if(lexemes[0].type != lexeme_type::while_statement) + if(lexemes[0].type != lexeme_type::while_operator) return false; parse_tree_node conditional; diff --git a/intermediary/intermediary.cpp b/intermediary/intermediary.cpp index b53bb83..1dc5e85 100644 --- a/intermediary/intermediary.cpp +++ b/intermediary/intermediary.cpp @@ -10,7 +10,7 @@ namespace fridh { } - bool intermediary_translator::load_module(std::string const & path, std::string const & name, std::string & error_message); + bool intermediary_translator::load_module(std::string const & path, std::string const & name, std::string & error_message) { std::string content; if(!ail::read_file(path, content)) @@ -20,7 +20,7 @@ namespace fridh } module module_output; - bool success = translate_data(module_output, content, error_message); + bool success = translate_data(module_output, content, name, error_message); if(!success) return false; @@ -34,7 +34,7 @@ namespace fridh std::string const & intermediary_translator::get_declaration_name() { - return *lines[line_offset][1].string; + return *lines[line_offset].lexemes[1].string; } void intermediary_translator::name_collision_check() @@ -164,18 +164,24 @@ namespace fridh bool intermediary_translator::translate_data(module & target_module, std::string const & data, std::string const & module_name, std::string & error_message_output) { - lines = std::vector(); - lexer current_lexer(data, lines, error_message); - if(!current_lexer.parse()) - return false; + try + { + lines = lines_of_code(); + lexer current_lexer(data, lines, error_message); - current_node = &target_module.symbols; - indentation_level = 0; - nested_class_level = 0; + current_node = &target_module.symbols; + indentation_level = 0; + nested_class_level = 0; - process_function(&target_module.entry_function); + process_function(&target_module.entry_function); - return true; + return true; + } + catch(ail::exception & exception) + { + error_message_output = exception.get_message(); + return false; + } } void intermediary_translator::error(std::string const & message) diff --git a/intermediary/node.cpp b/intermediary/node.cpp index 7e2327f..747b489 100644 --- a/intermediary/node.cpp +++ b/intermediary/node.cpp @@ -1,5 +1,6 @@ #include #include +#include namespace fridh { @@ -38,7 +39,7 @@ namespace fridh 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[not_equal] = binary_operator_type::not_equal; binary_lexeme_map[equal] = binary_operator_type::equal; binary_lexeme_map[logical_and] = binary_operator_type::logical_and; diff --git a/intermediary/precedence.cpp b/intermediary/precedence.cpp index db764ee..254b052 100644 --- a/intermediary/precedence.cpp +++ b/intermediary/precedence.cpp @@ -39,7 +39,7 @@ namespace fridh binary_operator_precedence_map[binary_operator_type::less_than_or_equal] = operator_precedence::less_than_or_equal; binary_operator_precedence_map[binary_operator_type::greater_than] = operator_precedence::greater_than; binary_operator_precedence_map[binary_operator_type::greater_than_or_equal] = operator_precedence::greater_than_or_equal; - binary_operator_precedence_map[binary_operator_type::unequal] = operator_precedence::unequal; + binary_operator_precedence_map[binary_operator_type::not_equal] = operator_precedence::not_equal; binary_operator_precedence_map[binary_operator_type::equal] = operator_precedence::equal; binary_operator_precedence_map[binary_operator_type::logical_and] = operator_precedence::logical_and; diff --git a/intermediary/statement.cpp b/intermediary/statement.cpp index faa50ef..99d7477 100644 --- a/intermediary/statement.cpp +++ b/intermediary/statement.cpp @@ -5,37 +5,37 @@ namespace fridh { - void intermediary_translator::process_atomic_statement(lexeme_container & lexemes, std::size_t & offset, parse_tree_nodes & output, bool allow_multi_statements, lexeme_type::type terminator) + void set_last_group(lexeme_group::type new_last_group, lexeme_group::type & last_group, bool & got_last_group) { - bool got_last_group = false; - lexeme_group::type last_group; + last_group = new_last_group; + got_last_group = true; + } - parse_tree_nodes arguments; + void add_unary_node(lexeme & current_lexeme, parse_tree_nodes & arguments) + { + parse_tree_node unary_operator_node; + lexeme_to_unary_operator_node(current_lexeme, unary_operator_node); + arguments.push_back(unary_operator_node); + } - void set_last_group(lexeme_group::type new_last_group) - { - last_group = new_last_group; - got_last_group = true; - } + void add_negation_lexeme(parse_tree_nodes & arguments) + { + add_unary_node(lexeme(lexeme_type::negation), arguments); + } - void add_unary_node(lexeme & current_lexeme) - { - parse_tree_node unary_operator_node; - lexeme_to_unary_operator_node(current_lexeme, unary_operator_node); - arguments.push_back(unary_operator_node); - } + void process_node_group(parse_tree_nodes & arguments, parse_tree_nodes & output) + { + parse_tree_node new_node; + operator_resolution(arguments, new_node); + output.push_back(new_node); + } - void add_negation_lexeme() - { - add_unary_node(lexeme(lexeme_type::negation)); - } + void intermediary_translator::process_atomic_statement(lexeme_container & lexemes, std::size_t & offset, parse_tree_nodes & output, bool allow_multi_statements, lexeme_type::type terminator) + { + bool got_last_group = false; + lexeme_group::type last_group; - void process_node_group() - { - parse_tree_node new_node; - operator_resolution(arguments, new_node); - output.push_back(new_node); - } + parse_tree_nodes arguments; symbol_prefix::type prefix = symbol_prefix::none; @@ -77,7 +77,7 @@ namespace fridh process_atomic_statement(lexemes, offset, content, false, lexeme_type::bracket_end); arguments.push_back(content[0]); } - set_last_group(lexeme_group::argument); + set_last_group(lexeme_group::argument, last_group, got_last_group); break; } @@ -89,7 +89,7 @@ namespace fridh parse_tree_nodes elements; process_atomic_statement(lexemes, offset, content, true, lexeme_type::array_end); arguments.push_back(parse_tree_node(elements)); - set_last_group(lexeme_group::argument); + set_last_group(lexeme_group::argument, last_group, got_last_group); break; } @@ -98,17 +98,17 @@ namespace fridh case lexeme_type::call_operator: arguments.push_back(parse_tree_node(parse_tree_node_type::call_operator)); - set_last_group(lexeme_group::call_operator); + set_last_group(lexeme_group::call_operator, last_group, got_last_group); continue; case lexeme_type::spaced_call_operator: arguments.push_back(parse_tree_node(parse_tree_node_type::spaced_call_operator)); - set_last_group(lexeme_group::call_operator); + set_last_group(lexeme_group::call_operator, last_group, got_last_group); continue; case lexeme_type::iterator: arguments.push_back(parse_tree_node(parse_tree_node_type::iterator)); - set_last_group(lexeme_group::argument); + set_last_group(lexeme_group::argument, last_group, got_last_group); continue; } @@ -143,7 +143,7 @@ namespace fridh if(allow_multi_statements) { - process_node_group(); + process_node_group(arguments, output); arguments.clear(); got_last_group = false; continue; @@ -154,7 +154,7 @@ namespace fridh case lexeme_group::unary_operator: if(got_last_group && last_group == lexeme_group::argument) error("Encountered an argument followed by an unary operator without a binary operator between them"); - add_unary_node(current_lexeme); + add_unary_node(current_lexeme, arguments); break; case lexeme_group::binary_operator: @@ -167,7 +167,7 @@ namespace fridh case lexeme_group::binary_operator: if(current_lexeme.type == lexeme_type::subtraction) - add_negation_lexeme(); + add_negation_lexeme(arguments); else error("Encountered two sequential binary operators"); break; @@ -176,7 +176,7 @@ namespace fridh else { if(current_lexeme.type == lexeme_type::subtraction) - add_negation_lexeme(); + add_negation_lexeme(arguments); else error("Encountered a binary operator in the beginning of a statement"); break; @@ -187,7 +187,7 @@ namespace fridh break; } - set_last_group(group); + set_last_group(group, last_group, got_last_group); } if(!got_last_group) @@ -196,6 +196,6 @@ namespace fridh if(last_group != lexeme_group::argument) error("An operator is missing an argument"); - process_node_group(); + process_node_group(arguments, output); } } diff --git a/lexer/comment.cpp b/lexer/comment.cpp index d13d325..f195932 100644 --- a/lexer/comment.cpp +++ b/lexer/comment.cpp @@ -2,7 +2,7 @@ namespace fridh { - bool lexer::parse_comment(std::string & error_message) + void lexer::parse_comment(std::string & error_message) { std::string const multi_line_comment = ";;", @@ -37,10 +37,7 @@ namespace fridh i++; } if(!got_end) - { - error_message = lexer_error("Unable to find the end of a multi-line comment", start_of_comment); - return false; - } + lexer_error("Unable to find the end of a multi-line comment", start_of_comment); } else if(string_match(nested_comment_start)) { @@ -69,22 +66,15 @@ namespace fridh } if(comment_depth != 0) - { - error_message = lexer_error("Unable to find the end of a nested comment", start_of_comment); - return false; - } + lexer_error("Unable to find the end of a nested comment", start_of_comment); } else { std::size_t offset = input.find('\n', i); if(offset == std::string::npos) - { - error_message = lexer_error("Unable to find the end of a multi-line comment", start_of_comment); - return false; - } + lexer_error("Unable to find the end of a multi-line comment", start_of_comment); i = offset; process_newline(); } - return true; } } diff --git a/lexer/group.cpp b/lexer/group.cpp index 215d042..16bf35b 100644 --- a/lexer/group.cpp +++ b/lexer/group.cpp @@ -29,7 +29,7 @@ namespace fridh case less_than_or_equal: case greater_than: case greater_than_or_equal: - case unequal: + case not_equal: case equal: case logical_and: diff --git a/lexer/lexeme.cpp b/lexer/lexeme.cpp index bb2946c..2e1c299 100644 --- a/lexer/lexeme.cpp +++ b/lexer/lexeme.cpp @@ -3,46 +3,46 @@ namespace fridh { - lexeme_type::lexeme() + lexeme::lexeme() { } - lexeme_type::lexeme(lexeme_type::type type): + lexeme::lexeme(lexeme::type type): type(type) { } - lexeme_type::lexeme(types::boolean boolean): + lexeme::lexeme(types::boolean boolean): type(boolean), boolean(boolean) { } - lexeme_type::lexeme(types::signed_integer signed_integer): + lexeme::lexeme(types::signed_integer signed_integer): type(signed_integer), signed_integer(signed_integer) { } - lexeme_type::lexeme(types::unsigned_integer unsigned_integer): + lexeme::lexeme(types::unsigned_integer unsigned_integer): type(unsigned_integer), unsigned_integer(unsigned_integer) { } - lexeme_type::lexeme(types::floating_point_value floating_point_value): + lexeme::lexeme(types::floating_point_value floating_point_value): type(floating_point_value), floating_point_value(floating_point_value) { } - lexeme_type::lexeme(lexeme_type::type type, std::string const & string): + lexeme::lexeme(lexeme::type type, std::string const & string): type(type), string(new std::string(string)) { } - std::string lexeme_type::to_string() const + std::string lexeme::to_string() const { using namespace lexeme_type; @@ -123,7 +123,7 @@ namespace fridh case greater_than_or_equal: return ">="; - case unequal: + case not_equal: return "!="; case equal: diff --git a/lexer/lexer.cpp b/lexer/lexer.cpp index d7dae75..9dff0e5 100644 --- a/lexer/lexer.cpp +++ b/lexer/lexer.cpp @@ -23,10 +23,9 @@ namespace fridh return string.length() > other.string.length(); } - lexer::lexer(std::string const & input, std::vector & lines, std::string & error): + lexer::lexer(std::string const & input, lines_of_code & lines): input(input), - lines(lines), - error(error) + lines(lines) { } @@ -52,17 +51,16 @@ namespace fridh return false; } - std::string lexer::lexer_error(std::string const & message, uword error_line) + void lexer::lexer_error(std::string const & message, uword error_line) { if(error_line == 0) error_line = line; - return "Line " + ail::number_to_string(error_line) + ": " + message; + throw ail::exception("Lexer error: Line " + ail::number_to_string(error_line) + ": " + message); } - std::string lexer::number_parsing_error(std::string const & message, bool & error_occured) + void lexer::number_parsing_error(std::string const & message) { - error_occured = true; - return lexer_error(message); + lexer_error(message); } bool lexer::is_name_char(char input) @@ -109,7 +107,7 @@ namespace fridh line_offset = i; } - bool lexer::parse() + void lexer::parse_lexemes() { initialise_tables(); @@ -130,10 +128,7 @@ namespace fridh { case tab: if(current_line.indentation_level > 0) - { - error = lexer_error("Tabs are only permitted in the beginning of a line (offset " + ail::number_to_string(i - line_offset + 1) + ")"); - return false; - } + lexer_error("Tabs are only permitted in the beginning of a line (offset " + ail::number_to_string(i - line_offset + 1) + ")"); for(i++, current_line.indentation_level = 1; i < end && input[i] == tab; i++, current_line.indentation_level++); continue; @@ -152,24 +147,18 @@ namespace fridh case '"': { std::string string; - if(!parse_string(current_line, error)) - return false; + parse_string(current_line); continue; } case ';': - if(!parse_comment(error)) - return false; + parse_comment(); continue; } - bool error_occured; - if(parse_number(current_line, error_occured)) + if(parse_number(current_line)) continue; - if(error_occured) - return false; - parse_name(current_line); } @@ -178,11 +167,9 @@ namespace fridh current_line.line = line; lines.push_back(current_line); } - - return true; } - std::string visualise_lexemes(std::vector & lines) + std::string visualise_lexemes(lines_of_code & lines) { std::string output; @@ -209,4 +196,18 @@ namespace fridh return output; } + + bool lexer::parse(std::string & error) + { + try + { + parse(); + return true; + } + catch(ail::exception & exception) + { + error = exception.get_message(); + return false; + } + } } diff --git a/lexer/number.cpp b/lexer/number.cpp index 630a73f..8821f9c 100644 --- a/lexer/number.cpp +++ b/lexer/number.cpp @@ -3,11 +3,10 @@ namespace fridh { - bool lexer::parse_number(line_of_code & output, bool & error_occured) + bool lexer::parse_number(line_of_code & output) { std::size_t start = i; char byte = input[i]; - error_occured = false; if(ail::is_digit(byte)) { @@ -23,10 +22,7 @@ namespace fridh i++; remaining_bytes = end - i; if(remaining_bytes == 0) - { - error = number_parsing_error("Incomplete hex number at the end of the input", error_occured); - return false; - } + number_parsing_error("Incomplete hex number at the end of the input", error_occured); std::size_t hex_start = i; @@ -34,11 +30,7 @@ namespace fridh std::size_t hex_length = i - hex_start; if(hex_length == 0) - { - error_occured = true; - error = lexer_error("Incomplete hex number"); - return false; - } + lexer_error("Incomplete hex number"); std::string hex_string = input.substr(hex_start, i - end); types::unsigned_integer value = ail::string_to_number(hex_string, std::ios_base::hex); @@ -58,10 +50,7 @@ namespace fridh if(byte == dot) { if(got_dot) - { - error = number_parsing_error("Encountered a floating point value containing multiple dots", error_occured); - return false; - } + number_parsing_error("Encountered a floating point value containing multiple dots", error_occured); else got_dot = true; } @@ -72,10 +61,7 @@ namespace fridh } if(last_byte == dot) - { - error = number_parsing_error("Encountered a floating point value ending with a dot", error_occured); - return false; - } + number_parsing_error("Encountered a floating point value ending with a dot", error_occured); std::string number_string = input.substr(start, i - start); lexeme current_lexeme; diff --git a/lexer/operator.cpp b/lexer/operator.cpp index 19d2791..826df54 100644 --- a/lexer/operator.cpp +++ b/lexer/operator.cpp @@ -31,7 +31,7 @@ namespace fridh operator_lexeme(less_than_or_equal, "<="), operator_lexeme(greater_than, ">"), operator_lexeme(greater_than_or_equal, ">="), - operator_lexeme(unequal, "!="), + operator_lexeme(not_equal, "!="), operator_lexeme(equal, "=="), operator_lexeme(logical_not, "!"), diff --git a/lexer/string.cpp b/lexer/string.cpp index a41d348..c501761 100644 --- a/lexer/string.cpp +++ b/lexer/string.cpp @@ -3,7 +3,7 @@ namespace fridh { - bool lexer::parse_string(line_of_code & output, std::string & error_message, std::string error_prefix) + void lexer::parse_string(line_of_code & output) { std::string string; char string_character = input[i]; @@ -17,10 +17,7 @@ namespace fridh case '\\': { if(end - i < 2) - { - error_message = lexer_error(error_prefix + "Backslash at the end of the input"); - return false; - } + lexer_error("Backslash at the end of the input"); i++; @@ -40,39 +37,32 @@ namespace fridh if(ail::is_hex_digit(next_byte)) { if(end - i < 2) - { - error_message = lexer_error(error_prefix + "Incomplete hex number escape sequence at the end of the input"); - return false; - } + lexer_error("Incomplete hex number escape sequence at the end of the input"); + if(!ail::is_hex_digit(input[i + 1])) - { - error_message = lexer_error(error_prefix + "Invalid hex number escape sequence"); - return false; - } + lexer_error("Invalid hex number escape sequence"); + std::string hex_string = input.substr(i, 2); i++; char new_byte = ail::string_to_number(hex_string, std::ios_base::hex); string.push_back(new_byte); } else - { - error_message = lexer_error(error_prefix + "Invalid escape sequence: " + ail::hex_string_8(static_cast(next_byte))); - return false; - } + lexer_error("Invalid escape sequence: " + ail::hex_string_8(static_cast(next_byte))); break; } case '\n': - error_message = lexer_error(error_prefix + "Detected a newline in a string"); - return false; + lexer_error("Detected a newline in a string"); + break; case '\'': case '"': if(byte == string_character) { - output.lexemes.push_back(lexeme(string, string)); + output.lexemes.push_back(lexeme(lexeme_type::string, string)); i++; - return true; + return; } string.push_back(byte); break; @@ -82,7 +72,6 @@ namespace fridh break; } } - error_message = lexer_error(error_prefix + "String lacks terminator"); - return false; + lexer_error("String lacks terminator"); } } diff --git a/main.cpp b/main.cpp index 2fdd3fc..06803ed 100644 --- a/main.cpp +++ b/main.cpp @@ -5,32 +5,37 @@ #include #include -int main(int argc, char ** argv) +bool perform_lexer_test(std::string const & input, std::string const & output) { - if(argc != 3) - { - std::cout << argv[0] << " " << std::endl; - return 1; - } - std::string code; - if(!ail::read_file(argv[1], code)) + if(!ail::read_file(input, code)) { std::cout << "Unable to read input" << std::endl; - return 1; + return false; } std::vector lines; std::string error; - fridh::lexer lexer(code, lines, error); - if(!lexer.parse(lines)) + fridh::lexer lexer(code, lines); + if(!lexer.parse()) { std::cout << "Error: " << error << std::endl; - std::cin.get(); + return false; + } + + ail::write_file(output, fridh::visualise_lexemes(lines)); + return true; +} + +int main(int argc, char ** argv) +{ + if(argc != 3) + { + std::cout << argv[0] << " " << std::endl; return 1; } - ail::write_file(argv[2], fridh::visualise_lexemes(lines)); + perform_lexer_test(argv[1], argv[2]); return 0; } diff --git a/symbol.cpp b/symbol.cpp index e326c7a..4009bd9 100644 --- a/symbol.cpp +++ b/symbol.cpp @@ -1,5 +1,4 @@ -#include -#include +#include namespace fridh { @@ -14,15 +13,15 @@ namespace fridh { switch(type) { - case variable::variable: + case symbol::variable: variable_pointer = new variable; break; - case variable::function: + case symbol::function: function_pointer = new function; break; - case variable::class_symbol: + case symbol::class_symbol: class_pointer = new class_type; break; } @@ -30,24 +29,24 @@ namespace fridh symbol_tree_node::~symbol_tree_node() { - BOOST_FOREACH(symbol_tree_node * i, children) - delete i; + for(node_children::iterator i = children.begin(), end = children.end(); i != end; i++) + delete i->second; switch(type) { - case variable::variable: + case symbol::variable: delete variable_pointer; break; - case variable::function: + case symbol::function: delete function_pointer; break; - case variable::class_symbol: + case symbol::class_symbol: delete class_pointer; break; - case variable::module: + case symbol::module: delete module_pointer; break; } @@ -69,7 +68,7 @@ namespace fridh return parent->find_entity(name, output); } - output = &iterator->second; + output = iterator->second; return true; } } diff --git a/variable/comparison.cpp b/variable/comparison.cpp index a27d841..eb65679 100644 --- a/variable/comparison.cpp +++ b/variable/comparison.cpp @@ -23,7 +23,7 @@ namespace fridh NUMERIC_COMPARISON(greater_than, "Less than", >) NUMERIC_COMPARISON(greater_than_or_equal, "Less than", >=) - void variable::unequal(variable const & argument, variable & output) const + void variable::not_equal(variable const & argument, variable & output) const { output.new_boolean(operator!=(argument.other)); } -- 2.11.4.GIT