From 04f3ba6e36d83b587e17975066e4145cf4f36697 Mon Sep 17 00:00:00 2001 From: binrapt Date: Thu, 15 Oct 2009 00:07:08 +0200 Subject: [PATCH] Fixed all of the syntax problems. Dealing with 3 bizarre linking problems now. --- fridh/function.hpp | 2 +- fridh/lexer.hpp | 6 +-- fridh/variable.hpp | 6 ++- intermediary/control_flow.cpp | 9 +++- intermediary/intermediary.cpp | 30 +++++++---- intermediary/node.cpp | 6 +-- intermediary/operator.cpp | 12 ++--- intermediary/precedence.cpp | 2 +- lexer/comment.cpp | 2 +- lexer/group.cpp | 4 +- lexer/lexeme.cpp | 118 +++++++++++++++++++++--------------------- lexer/lexer.cpp | 8 +-- lexer/string.cpp | 2 +- variable/comparison.cpp | 14 ++--- variable/logical_operator.cpp | 10 +--- variable/operator.cpp | 25 +++++---- variable/type.cpp | 2 +- variable/variable.cpp | 8 ++- 18 files changed, 138 insertions(+), 128 deletions(-) diff --git a/fridh/function.hpp b/fridh/function.hpp index 5f9b81d..001f623 100644 --- a/fridh/function.hpp +++ b/fridh/function.hpp @@ -190,7 +190,7 @@ namespace fridh struct for_each_statement { - parse_tree_symbol container; + parse_tree_node container; executable_units body; }; diff --git a/fridh/lexer.hpp b/fridh/lexer.hpp index 1f15c9a..7aee501 100644 --- a/fridh/lexer.hpp +++ b/fridh/lexer.hpp @@ -126,7 +126,7 @@ namespace fridh explicit lexeme(types::signed_integer signed_integer); explicit lexeme(types::unsigned_integer unsigned_integer); explicit lexeme(types::floating_point_value floating_point_value); - explicit lexeme(lexeme_type::type type, std::string const & string); + explicit lexeme(std::string const & string); std::string to_string() const; }; @@ -169,10 +169,10 @@ namespace fridh void parse_string(line_of_code & output); bool parse_number(line_of_code & output); void parse_name(line_of_code & output); - void parse_comment(std::string & error_message); + void parse_comment(); void lexer_error(std::string const & message, uword error_line = 0); - void number_parsing_error(std::string const & message, bool & error_occured); + void number_parsing_error(std::string const & message); bool is_name_char(char input); bool string_match(std::string const & target); diff --git a/fridh/variable.hpp b/fridh/variable.hpp index 4aea730..c6659be 100644 --- a/fridh/variable.hpp +++ b/fridh/variable.hpp @@ -93,6 +93,8 @@ namespace fridh DECLARE_UNARY_OPERATOR(binary_not) + DECLARE_UNARY_OPERATOR(negation) + #undef DECLARE_UNARY_OPERATOR #undef DECLARE_BINARY_OPERATOR @@ -116,7 +118,7 @@ namespace fridh variable_type type; - bool is_floating_point_operation(variable & argument) const; + bool is_floating_point_operation(variable const & argument) const; bool is_integer_type() const; bool is_numeric_type() const; bool is_zero() const; @@ -125,7 +127,7 @@ namespace fridh std::string get_string_representation() const; bool get_boolean_value() const; - bool variable::array_addition(variable const & argument) const; + bool array_addition(variable const & argument, variable & output) const; bool string_addition(variable const & argument, variable & output) const; bool array_equality(variable const & other) const; diff --git a/intermediary/control_flow.cpp b/intermediary/control_flow.cpp index 7b39fae..9fff970 100644 --- a/intermediary/control_flow.cpp +++ b/intermediary/control_flow.cpp @@ -109,7 +109,7 @@ namespace fridh output.type = executable_unit_type::for_each_statement; for_each_statement * & for_each_pointer = output.for_each_pointer; for_each_pointer = new for_each_statement; - process_composite_term(for_each_pointer->conditional_term); + process_composite_term(for_each_pointer->container); process_body(&for_each_pointer->body); } @@ -143,6 +143,11 @@ namespace fridh process_return(output) ) ) - process_offset_atomic_statement(output); + { + output.type = executable_unit_type::statement; + parse_tree_node * & node = output.statement_pointer; + node = new parse_tree_node; + process_offset_atomic_statement(*node); + } } } diff --git a/intermediary/intermediary.cpp b/intermediary/intermediary.cpp index 1dc5e85..ceab402 100644 --- a/intermediary/intermediary.cpp +++ b/intermediary/intermediary.cpp @@ -47,7 +47,7 @@ namespace fridh symbol_tree_node & intermediary_translator::add_name(symbol::type symbol_type) { std::string const & name = get_declaration_name(); - symbol_tree_node & new_node = current_node->children[name]; + symbol_tree_node & new_node = *current_node->children[name]; new_node = symbol_tree_node(symbol_type); new_node.parent = current_node; current_node = &new_node; @@ -57,7 +57,7 @@ namespace fridh void intermediary_translator::process_body(executable_units * output) { line_offset++; - indentation++; + indentation_level++; bool is_class = (output == 0); @@ -66,10 +66,19 @@ namespace fridh while(line_offset < line_end) { - if(process_line(output)) + bool end; + if(is_class) + end = process_line(0); + else + { + executable_unit new_unit; + process_line(&new_unit); + output->push_back(new_unit); + } + if(end) { - if(indentation > 0) - indentation--; + if(indentation_level > 0) + indentation_level--; if(is_class) nested_class_level--; current_node = current_node->parent; @@ -108,7 +117,7 @@ namespace fridh if(output) current_function = output; else - current_function = *add_name(symbol::class_symbol).function_pointer; + current_function = add_name(symbol::class_symbol).function_pointer; for(std::size_t i = 2, end = lexemes.size(); i < end; i++) current_function->arguments.push_back(*lexemes[i].string); @@ -134,7 +143,7 @@ namespace fridh bool intermediary_translator::process_line(executable_unit * output) { line_of_code & current_line = lines[line_offset]; - if(current_line.indentation_level > indentation) + if(current_line.indentation_level > indentation_level) error("Unexpected increase in the indentation level"); if(!process_class()) @@ -159,7 +168,7 @@ namespace fridh line_of_code & next_line = lines[line_offset]; //end of block? - return next_line.indentation_level < indentation; + return next_line.indentation_level < indentation_level; } bool intermediary_translator::translate_data(module & target_module, std::string const & data, std::string const & module_name, std::string & error_message_output) @@ -167,7 +176,10 @@ namespace fridh try { lines = lines_of_code(); - lexer current_lexer(data, lines, error_message); + lexer current_lexer(data, lines); + + if(!current_lexer.parse(error_message_output)) + return false; current_node = &target_module.symbols; indentation_level = 0; diff --git a/intermediary/node.cpp b/intermediary/node.cpp index 1dc8056..ad3d314 100644 --- a/intermediary/node.cpp +++ b/intermediary/node.cpp @@ -17,7 +17,7 @@ namespace fridh binary_lexeme_map_type binary_lexeme_map; } - void initialise_data() + void initialise_maps() { if(maps_are_initialised) return; @@ -113,7 +113,7 @@ namespace fridh void lexeme_to_unary_operator_node(lexeme & input, parse_tree_node & output) { - initialise_data(); + initialise_maps(); unary_lexeme_map_type::iterator iterator = unary_lexeme_map.find(input.type); if(iterator == unary_lexeme_map.end()) @@ -127,7 +127,7 @@ namespace fridh void lexeme_to_binary_operator_node(lexeme & input, parse_tree_node & output) { - initialise_data(); + initialise_maps(); binary_lexeme_map_type::iterator iterator = binary_lexeme_map.find(input.type); if(iterator == binary_lexeme_map.end()) diff --git a/intermediary/operator.cpp b/intermediary/operator.cpp index 2c7028b..03e9143 100644 --- a/intermediary/operator.cpp +++ b/intermediary/operator.cpp @@ -5,14 +5,14 @@ namespace fridh { - void intermediary_translator::operator_resolution(parse_tree_nodes & input, parse_tree_node & output) + void call_check(std::size_t extremum_offset) { - void call_check(std::size_t extremum_offset) - { - if(extremum_offset != 1) - throw ail::exception("Invalid call offset encountered during operator resolution"); - } + if(extremum_offset != 1) + throw ail::exception("Invalid call offset encountered during operator resolution"); + } + void operator_resolution(parse_tree_nodes & input, parse_tree_node & output) + { if(input.size() != 1) { output = input[0]; diff --git a/intermediary/precedence.cpp b/intermediary/precedence.cpp index 254b052..41f3642 100644 --- a/intermediary/precedence.cpp +++ b/intermediary/precedence.cpp @@ -116,7 +116,7 @@ namespace fridh if ( - input.type == parse_tree_node_type::binary_operator_node) && + input.type == parse_tree_node_type::binary_operator_node && binary_right_to_left_operators.find(input.binary_operator_pointer->type) != binary_right_to_left_operators.end() ) return true; diff --git a/lexer/comment.cpp b/lexer/comment.cpp index f195932..cb71fd6 100644 --- a/lexer/comment.cpp +++ b/lexer/comment.cpp @@ -2,7 +2,7 @@ namespace fridh { - void lexer::parse_comment(std::string & error_message) + void lexer::parse_comment() { std::string const multi_line_comment = ";;", diff --git a/lexer/group.cpp b/lexer/group.cpp index 16bf35b..ad71fc1 100644 --- a/lexer/group.cpp +++ b/lexer/group.cpp @@ -42,7 +42,7 @@ namespace fridh case binary_or: case binary_xor: - output = lexeme_group::binary_argument; + output = lexeme_group::binary_operator; return true; case logical_not: @@ -52,7 +52,7 @@ namespace fridh case increment: case decrement: - output = lexeme_group::unary_argument; + output = lexeme_group::unary_operator; return true; default: diff --git a/lexer/lexeme.cpp b/lexer/lexeme.cpp index 2e1c299..3d86faa 100644 --- a/lexer/lexeme.cpp +++ b/lexer/lexeme.cpp @@ -7,198 +7,196 @@ namespace fridh { } - lexeme::lexeme(lexeme::type type): + lexeme::lexeme(lexeme_type::type type): type(type) { } lexeme::lexeme(types::boolean boolean): - type(boolean), + type(lexeme_type::boolean), boolean(boolean) { } lexeme::lexeme(types::signed_integer signed_integer): - type(signed_integer), + type(lexeme_type::signed_integer), signed_integer(signed_integer) { } lexeme::lexeme(types::unsigned_integer unsigned_integer): - type(unsigned_integer), + type(lexeme_type::unsigned_integer), unsigned_integer(unsigned_integer) { } lexeme::lexeme(types::floating_point_value floating_point_value): - type(floating_point_value), + type(lexeme_type::floating_point_value), floating_point_value(floating_point_value) { } - lexeme::lexeme(lexeme::type type, std::string const & string): - type(type), + lexeme::lexeme(std::string const & string): + type(lexeme_type::string), string(new std::string(string)) { } std::string lexeme::to_string() const { - using namespace lexeme_type; - switch(type) { - case name: + case lexeme_type::name: return "name: " + *string; - case boolean: + case lexeme_type::boolean: return "boolean: " + ail::bool_to_string(boolean); - case signed_integer: + case lexeme_type::signed_integer: return "integer: " + ail::number_to_string(signed_integer); - case unsigned_integer: + case lexeme_type::unsigned_integer: return "unsigned-integer: " + ail::number_to_string(unsigned_integer); - case floating_point_value: + case lexeme_type::floating_point_value: return "float: " + ail::number_to_string(floating_point_value); - case string: + case lexeme_type::string: return "string: " + ail::replace_string(*string, "\n", "\\n"); - case addition: + case lexeme_type::addition: return "+"; - case subtraction: + case lexeme_type::subtraction: return "-"; - case multiplication: + case lexeme_type::multiplication: return "*"; - case division: + case lexeme_type::division: return "/"; - case modulo: + case lexeme_type::modulo: return "%"; - case assignment: + case lexeme_type::assignment: return "="; - case addition_assignment: + case lexeme_type::addition_assignment: return "+="; - case subtraction_assignment: + case lexeme_type::subtraction_assignment: return "-="; - case multiplication_assignment: + case lexeme_type::multiplication_assignment: return "*="; - case division_assignment: + case lexeme_type::division_assignment: return "/="; - case modulo_assignment: + case lexeme_type::modulo_assignment: return "%="; - case exponentiation_assignment: + case lexeme_type::exponentiation_assignment: return "**="; - case increment: + case lexeme_type::increment: return "++"; - case decrement: + case lexeme_type::decrement: return "--"; - case exponentiation: + case lexeme_type::exponentiation: return "**"; - case less_than: + case lexeme_type::less_than: return "<"; - case less_than_or_equal: + case lexeme_type::less_than_or_equal: return "<="; - case greater_than: + case lexeme_type::greater_than: return ">"; - case greater_than_or_equal: + case lexeme_type::greater_than_or_equal: return ">="; - case not_equal: + case lexeme_type::not_equal: return "!="; - case equal: + case lexeme_type::equal: return "=="; - case logical_not: + case lexeme_type::logical_not: return "!"; - case logical_and: + case lexeme_type::logical_and: return "&"; - case logical_or: + case lexeme_type::logical_or: return "|"; - case shift_left: + case lexeme_type::shift_left: return "<<"; - case shift_right: + case lexeme_type::shift_right: return ">>"; - case binary_and: + case lexeme_type::binary_and: return "&&"; - case binary_or: + case lexeme_type::binary_or: return "||"; - case binary_xor: + case lexeme_type::binary_xor: return "^"; - case binary_not: + case lexeme_type::binary_not: return "~"; - case bracket_start: + case lexeme_type::bracket_start: return "bracket: start"; - case bracket_end: + case lexeme_type::bracket_end: return "bracket: end"; - case array_start: + case lexeme_type::array_start: return "array: start"; - case array_end: + case lexeme_type::array_end: return "array: end"; - case scope_start: + case lexeme_type::scope_start: return "scope: start"; - case scope_end: + case lexeme_type::scope_end: return "scope: end"; - case iteration: + case lexeme_type::iteration: return "iteration"; - case iterator: + case lexeme_type::iterator: return "iterator"; - case while_operator: + case lexeme_type::while_operator: return "while"; - case function_declaration: + case lexeme_type::function_declaration: return "function"; - case anonymous_function_declaration: + case lexeme_type::anonymous_function_declaration: return "anonymous function"; - case class_operator: + case lexeme_type::class_operator: return "class operator"; - case selection_operator: + case lexeme_type::selection_operator: return "."; - case call_operator: + case lexeme_type::call_operator: return ","; - case scope_operator: + case lexeme_type::scope_operator: return ":"; default: diff --git a/lexer/lexer.cpp b/lexer/lexer.cpp index 9dff0e5..d12dedf 100644 --- a/lexer/lexer.cpp +++ b/lexer/lexer.cpp @@ -12,13 +12,13 @@ namespace fridh { } - operator_lexeme_type::operator_lexeme(lexeme_type::type lexeme, std::string const & string): + operator_lexeme::operator_lexeme(lexeme_type::type lexeme, std::string const & string): lexeme(lexeme), string(string) { } - bool operator_lexeme_type::operator<(operator_lexeme const & other) const + bool operator_lexeme::operator<(operator_lexeme const & other) const { return string.length() > other.string.length(); } @@ -80,7 +80,7 @@ namespace fridh else if(name == "false") current_lexeme = lexeme(false); else - current_lexeme = lexeme(name, name); + current_lexeme = lexeme(name); output.lexemes.push_back(current_lexeme); } @@ -201,7 +201,7 @@ namespace fridh { try { - parse(); + parse_lexemes(); return true; } catch(ail::exception & exception) diff --git a/lexer/string.cpp b/lexer/string.cpp index c501761..10bfa11 100644 --- a/lexer/string.cpp +++ b/lexer/string.cpp @@ -60,7 +60,7 @@ namespace fridh case '"': if(byte == string_character) { - output.lexemes.push_back(lexeme(lexeme_type::string, string)); + output.lexemes.push_back(lexeme(string)); i++; return; } diff --git a/variable/comparison.cpp b/variable/comparison.cpp index eb65679..34900ca 100644 --- a/variable/comparison.cpp +++ b/variable/comparison.cpp @@ -3,7 +3,7 @@ namespace fridh { #define NUMERIC_COMPARISON(name, description, operator) \ - void variable::name(variable const & argument, variable & output) \ + void variable::name(variable const & argument, variable & output) const \ { \ if(is_numeric_type() && argument.is_numeric_type()) \ { \ @@ -15,7 +15,7 @@ namespace fridh output.new_boolean(signed_integer operator argument.signed_integer); \ } \ else \ - binary_argument_type_error(name_of_operation, type, argument.other.type); \ + binary_argument_type_error(description, type, argument.type); \ } NUMERIC_COMPARISON(less_than, "Less than", <) @@ -25,20 +25,16 @@ namespace fridh void variable::not_equal(variable const & argument, variable & output) const { - output.new_boolean(operator!=(argument.other)); + output.new_boolean(operator!=(argument)); } void variable::equal(variable const & argument, variable & output) const { - output.new_boolean(operator==(argument.other)); + output.new_boolean(operator==(argument)); } void variable::logical_not(variable & output) const { - bool value; - if(get_boolean_value(value)) - output.new_boolean(!value); - else - unary_argument_type_error("Logical not", type); + output.new_boolean(!get_boolean_value()); } } diff --git a/variable/logical_operator.cpp b/variable/logical_operator.cpp index 2a4eecc..d5aa049 100644 --- a/variable/logical_operator.cpp +++ b/variable/logical_operator.cpp @@ -3,15 +3,9 @@ namespace fridh { #define LOGICAL_OPERATOR(name, description, operator) \ - void variable::name(variable const & argument, variable & output) \ + void variable::name(variable const & argument, variable & output) const \ { \ - bool \ - left_value, \ - right_value; \ - if(get_boolean_value(left_value) && argument.get_boolean_value(right_value)) \ - output.new_boolean(left_value operator right_value); \ - else \ - unary_argument_type_error(description, type); \ + output.new_boolean(get_boolean_value() operator argument.get_boolean_value()); \ } LOGICAL_OPERATOR(logical_and, "Logical and", &&) diff --git a/variable/operator.cpp b/variable/operator.cpp index fafc7cb..edd095f 100644 --- a/variable/operator.cpp +++ b/variable/operator.cpp @@ -2,31 +2,36 @@ namespace fridh { + namespace + { + std::string const zero_division_error_message = "Zero division error"; + } + bool variable::array_addition(variable const & argument, variable & output) const { bool left_is_array = type == variable_type_identifier::array; - bool right_is_array = argument.other.type == variable_type_identifier::array; + bool right_is_array = argument.type == variable_type_identifier::array; if(left_is_array || right_is_array) { - argument.output.type = variable_type_identifier::array; - argument.output.array = new types::vector; - types::vector & vector = *argument.output.array; + output.type = variable_type_identifier::array; + output.array = new types::vector; + types::vector & vector = *output.array; if(left_is_array && right_is_array) { vector = *array; - types::vector & right_vector = *argument.other.array; + types::vector & right_vector = *argument.array; vector.insert(vector.end(), right_vector.begin(), right_vector.end()); } else if(left_is_array && !right_is_array) { vector = *array; - vector.push_back(argument.other); + vector.push_back(argument); } else if(!left_is_array && right_is_array) { - vector = *argument.other.array; + vector = *argument.array; vector.push_back(*this); } @@ -39,7 +44,7 @@ namespace fridh bool variable::string_addition(variable const & argument, variable & output) const { bool left_is_string = type == variable_type_identifier::string; - bool right_is_string = argument.other.type == variable_type_identifier::string; + bool right_is_string = argument.type == variable_type_identifier::string; if(left_is_string || right_is_string) { @@ -86,14 +91,14 @@ namespace fridh ARITHMETIC_OPERATION("Multiplication", *) } - void variable::addition(variable const & argument, variable & output) const + void variable::division(variable const & argument, variable & output) const { if(argument.is_zero()) throw ail::exception(zero_division_error_message); ARITHMETIC_OPERATION("Division", /) } - void variable::addition(variable const & argument, variable & output) const + void variable::modulo(variable const & argument, variable & output) const { if(argument.is_zero()) throw ail::exception(zero_division_error_message); diff --git a/variable/type.cpp b/variable/type.cpp index 2501ac1..3e04eb7 100644 --- a/variable/type.cpp +++ b/variable/type.cpp @@ -7,7 +7,7 @@ namespace fridh return type; } - bool variable::is_floating_point_operation(variable & argument) const + bool variable::is_floating_point_operation(variable const & argument) const { return type == variable_type_identifier::floating_point_value || argument.type == variable_type_identifier::floating_point_value; } diff --git a/variable/variable.cpp b/variable/variable.cpp index 51a7b3d..21775e4 100644 --- a/variable/variable.cpp +++ b/variable/variable.cpp @@ -3,11 +3,6 @@ namespace fridh { - namespace - { - std::string const zero_division_error_message = "Zero division error"; - } - variable::variable(): type(variable_type_identifier::undefined) { @@ -124,6 +119,9 @@ namespace fridh } unary_argument_type_error("Boolean representation", type); + + //should never get here, suppress warnings + return false; } bool variable::is_zero() const -- 2.11.4.GIT