From ad7cab4231db39565c5e9604a0cb640631279c79 Mon Sep 17 00:00:00 2001 From: binrapt Date: Thu, 15 Oct 2009 06:20:39 +0200 Subject: [PATCH] Fixed some further references to "intermediary", moved a few files into more fitting directories, implemented some basic testing code. Got to fix the string/name mixup in the lexer. --- fridh/parser.hpp | 9 +++----- function.cpp => interpreter/function.cpp | 0 symbol.cpp => interpreter/symbol.cpp | 0 main.cpp | 37 ++++++++++++++++++++++++++++++-- parser/control_flow.cpp | 12 +++++------ parser/parser.cpp | 28 ++++++++++++------------ parser/statement.cpp | 2 +- 7 files changed, 59 insertions(+), 29 deletions(-) rename function.cpp => interpreter/function.cpp (100%) rename symbol.cpp => interpreter/symbol.cpp (100%) diff --git a/fridh/parser.hpp b/fridh/parser.hpp index 4bac6e9..1e1d375 100644 --- a/fridh/parser.hpp +++ b/fridh/parser.hpp @@ -76,18 +76,15 @@ namespace fridh std::size_t index; }; - class intermediary_translator + class parser { public: - intermediary_translator(); - bool load_module(std::string const & path, std::string const & name, std::string & error_message); + parser(); + bool process_module(std::string const & path, std::string const & name, module & output, std::string & error_message); private: bool running; - module main_module; - std::vector modules; - std::size_t line_offset, line_end; diff --git a/function.cpp b/interpreter/function.cpp similarity index 100% rename from function.cpp rename to interpreter/function.cpp diff --git a/symbol.cpp b/interpreter/symbol.cpp similarity index 100% rename from symbol.cpp rename to interpreter/symbol.cpp diff --git a/main.cpp b/main.cpp index 4f98506..4a71ebf 100644 --- a/main.cpp +++ b/main.cpp @@ -1,10 +1,13 @@ #include #include #include + #include -#include +#include #include +#include + bool perform_lexer_test(std::string const & input, std::string const & output) { std::string code; @@ -34,6 +37,28 @@ bool perform_lexer_test(std::string const & input, std::string const & output) return true; } +bool perform_parser_test(std::string const & input, std::string const & output) +{ + std::string code; + if(!ail::read_file(input, code)) + { + std::cout << "Unable to read input" << std::endl; + return false; + } + + fridh::module module; + fridh::parser parser; + + std::string error; + if(!parser.process_module(input, "test", module, error)) + { + std::cout << "Error: " << error << std::endl; + return false; + } + + return true; +} + int main(int argc, char ** argv) { if(argc != 4) @@ -43,7 +68,15 @@ int main(int argc, char ** argv) return 1; } - perform_lexer_test(argv[1], argv[2]); + std::string + command = argv[1], + input = argv[2], + output = argv[3]; + + if(command == "lexer") + perform_lexer_test(input, output); + else + perform_parser_test(input, output); return 0; } diff --git a/parser/control_flow.cpp b/parser/control_flow.cpp index 5c07496..008ba9f 100644 --- a/parser/control_flow.cpp +++ b/parser/control_flow.cpp @@ -5,12 +5,12 @@ namespace fridh { - bool intermediary_translator::is_if_statement() + bool parser::is_if_statement() { return lines[line_offset].lexemes[0].type == lexeme_type::division; } - bool intermediary_translator::process_if(executable_unit & output) + bool parser::process_if(executable_unit & output) { lexeme_container & lexemes = lines[line_offset].lexemes; if(is_if_statement()) @@ -55,7 +55,7 @@ namespace fridh return true; } - bool intermediary_translator::process_while(executable_unit & output) + bool parser::process_while(executable_unit & output) { lexeme_container & lexemes = lines[line_offset].lexemes; if(lexemes[0].type != lexeme_type::while_operator) @@ -74,7 +74,7 @@ namespace fridh return true; } - bool intermediary_translator::process_for(executable_unit & output) + bool parser::process_for(executable_unit & output) { lexeme_container & lexemes = lines[line_offset].lexemes; if(lexemes[0].type != lexeme_type::iteration) @@ -117,7 +117,7 @@ namespace fridh return true; } - bool intermediary_translator::process_return(executable_unit & output) + bool parser::process_return(executable_unit & output) { lexeme_container & lexemes = lines[line_offset].lexemes; if(lexemes[0].type != lexeme_type::selection_operator) @@ -131,7 +131,7 @@ namespace fridh return true; } - void intermediary_translator::process_statement(executable_unit & output) + void parser::process_statement(executable_unit & output) { if ( diff --git a/parser/parser.cpp b/parser/parser.cpp index 8486d85..1541d53 100644 --- a/parser/parser.cpp +++ b/parser/parser.cpp @@ -5,12 +5,12 @@ namespace fridh { - intermediary_translator::intermediary_translator(): + parser::parser(): running(false) { } - bool intermediary_translator::load_module(std::string const & path, std::string const & name, std::string & error_message) + bool parser::process_module(std::string const & path, std::string const & name, module & output, std::string & error_message) { std::string content; if(!ail::read_file(path, content)) @@ -27,24 +27,24 @@ namespace fridh return true; } - bool intermediary_translator::name_is_used(std::string const & name) + bool parser::name_is_used(std::string const & name) { return current_node->exists(name); } - std::string const & intermediary_translator::get_declaration_name() + std::string const & parser::get_declaration_name() { return *lines[line_offset].lexemes[1].string; } - void intermediary_translator::name_collision_check() + void parser::name_collision_check() { std::string const & name = get_declaration_name(); if(name_is_used(name)) error("Name \"" + name + "\" has already been used by another function or class in the current scope"); } - symbol_tree_node & intermediary_translator::add_name(symbol::type symbol_type) + symbol_tree_node & parser::add_name(symbol::type symbol_type) { std::string const & name = get_declaration_name(); symbol_tree_node & new_node = *current_node->children[name]; @@ -54,7 +54,7 @@ namespace fridh return new_node; } - void intermediary_translator::process_body(executable_units * output) + void parser::process_body(executable_units * output) { line_offset++; indentation_level++; @@ -87,7 +87,7 @@ namespace fridh } } - bool intermediary_translator::process_class() + bool parser::process_class() { lexeme_container & lexemes = lines[line_offset].lexemes; if(!(lexemes.size() == 2 && lexemes[0].type == lexeme_type::class_operator && lexemes[1].type == lexeme_type::name)) @@ -101,7 +101,7 @@ namespace fridh return true; } - bool intermediary_translator::process_function(function * output) + bool parser::process_function(function * output) { lexeme_container & lexemes = lines[line_offset].lexemes; if(!(lexemes.size() >= 2 && lexemes[0].type == lexeme_type::function_declaration)) @@ -126,7 +126,7 @@ namespace fridh return true; } - void intermediary_translator::process_offset_atomic_statement(parse_tree_node & output, std::size_t offset) + void parser::process_offset_atomic_statement(parse_tree_node & output, std::size_t offset) { lexeme_container & lexemes = lines[line_offset].lexemes; parse_tree_nodes nodes; @@ -135,12 +135,12 @@ namespace fridh line_offset++; } - void intermediary_translator::process_composite_term(parse_tree_node & output) + void parser::process_composite_term(parse_tree_node & output) { process_offset_atomic_statement(output, 1); } - bool intermediary_translator::process_line(executable_unit * output) + bool parser::process_line(executable_unit * output) { line_of_code & current_line = lines[line_offset]; if(current_line.indentation_level > indentation_level) @@ -171,7 +171,7 @@ namespace fridh 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) + bool parser::translate_data(module & target_module, std::string const & data, std::string const & module_name, std::string & error_message_output) { try { @@ -196,7 +196,7 @@ namespace fridh } } - void intermediary_translator::error(std::string const & message) + void parser::error(std::string const & message) { throw ail::exception("Line " + ail::number_to_string(lines[line_offset].line) + ": " + message); } diff --git a/parser/statement.cpp b/parser/statement.cpp index 9d68d82..3436ece 100644 --- a/parser/statement.cpp +++ b/parser/statement.cpp @@ -30,7 +30,7 @@ namespace fridh output.push_back(new_node); } - 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 parser::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; -- 2.11.4.GIT