From d6a1d9a43cb45798d593423af89db967dd610e3c Mon Sep 17 00:00:00 2001 From: binrapt Date: Sat, 3 Oct 2009 04:50:22 +0200 Subject: [PATCH] The omnipotent crawling chaos, Portal! --- frith/class.hpp | 3 +- frith/function.hpp | 18 +++++---- frith/interpreter.hpp | 32 +++++++++++++-- frith/module.hpp | 4 +- interpreter/interpreter.cpp | 98 +++++++++++++++++++++++++++------------------ 5 files changed, 103 insertions(+), 52 deletions(-) diff --git a/frith/class.hpp b/frith/class.hpp index f4a9fa3..4ce38fc 100644 --- a/frith/class.hpp +++ b/frith/class.hpp @@ -6,7 +6,6 @@ namespace frith struct class_type { std::string name; - symbol_tree_node * class_scope; - //std::vector functions; + symbol_tree_node symbols; }; } diff --git a/frith/function.hpp b/frith/function.hpp index 003e6d9..e478dce 100644 --- a/frith/function.hpp +++ b/frith/function.hpp @@ -5,17 +5,21 @@ namespace frith { - enum executable_unit_type + namespace executable_unit { - executable_unit_type_statement, - executable_unit_type_if, - executable_unit_type_for, - executable_unit_type_while - }; + enum type + { + statement, + assignment, + if_statement, + for_statement, + while_statement, + }; + } struct executable_unit { - line_of_code line; + executable_unit_type type; }; struct function diff --git a/frith/interpreter.hpp b/frith/interpreter.hpp index 35f12b3..788fae7 100644 --- a/frith/interpreter.hpp +++ b/frith/interpreter.hpp @@ -6,6 +6,26 @@ namespace frith { + namespace match_result + { + enum type + { + no_match, + match, + error, + }; + } + + namespace process_line_result + { + enum type + { + ok, + end_of_block, + error, + }; + } + class interpreter { public: @@ -22,18 +42,22 @@ namespace frith line_offset, line_end; + uword indentation_level; + std::size_t lexeme_offset, lexeme_end; + std::string error_message; + + bool in_a_class; + std::vector lines; symbol_tree_node * current_node; - bool parse_class(std::string & error_message); - - bool translate_data(module & target_module, std::string const & data, std::string const & module_name, std::string & error_message); + bool translate_data(module & target_module, std::string const & data, std::string const & module_name, std::string & error_message_output); - void error(std::string const & message, std::string & output); + void error(std::string const & message); }; } diff --git a/frith/module.hpp b/frith/module.hpp index 4f80497..82e17df 100644 --- a/frith/module.hpp +++ b/frith/module.hpp @@ -1,10 +1,12 @@ #include +#include namespace frith { struct module { std::string path; - symbol_tree_node module_scope; + symbol_tree_node symbols; + function entry_function; }; } diff --git a/interpreter/interpreter.cpp b/interpreter/interpreter.cpp index 6ae3429..c0a31cc 100644 --- a/interpreter/interpreter.cpp +++ b/interpreter/interpreter.cpp @@ -56,59 +56,81 @@ namespace frith } } - bool interpreter::translate_data(module & target_module, std::string const & data, std::string const & module_name, std::string & error_message) + match_result::type interpreter::read_class() { - lines = std::vector(); - lexer current_lexer(data, lines, error_message); - if(!current_lexer.parse()) - return false; + } - current_node = 0; + match_result::type interpreter::read_function(function & current_function) + { + } + + bool interpreter::read_statement(function & current_function) + { + } - uword indentation_level = 0; - bool expected_indentation = false; + process_line_result::type interpreter::process_line(function & active_function) + { + line_of_code & current_line = lines[line_offset]; + if(current_line.indentation_level > indentation) + { + error = error("Unexpected increase in the indentation level"); + return process_line_result::error; + } - for(line_offset = 0, line_end = lines.size(); line_offset < line_end;) + match_result::type result = read_class(); + if(result == match_result::error) + return process_line_result::error; + else if(result == match_result::no_match) { - line_of_code & current_line = lines[line_offset]; - word difference = current_line.indentation_level - indentation_level; - if(difference > 1) - { - error("Invalid jump in the indentation level (difference is " + ail::number_to_string(difference) + ")", error_message); - return false; - } - else if(expected_indentation && difference != 1) + result = read_function(current_function); + if(result == match_result::error) + return process_line_result::error; + else { - error("Expected an indentation of 1", error_message); - return false; + if(!read_statement(active_function)) + return process_line_result::error; } + } - std::vector & current_lexemes = current_line.lexemes; + line_offset++; - for(lexeme_offset = 0, lexeme_end = current_lexemes.size(); lexeme_offset < lexeme_end;) - { - lexeme & current_lexeme = current_lexemes[lexeme_offset]; - switch(current_lexeme.type) - { - case lexeme_type_class_operator: - continue; - - default: - error("Unprocessed lexeme type encountered: " + current_lexeme.to_string(), error_message); - return false; - } - - lexeme_offset++; - } + if(line_offset == line_end) + { + //end of file -> end of the module entry function block + return process_line_result::end_of_block; + } - line_offset++; + line_of_code & next_line = lines[line_offset]; + + if(next_line.indentation_level < indentation) + { + //end of block + return process_line_result::end_of_block; + } + else + return process_line_result::ok; + } + + bool interpreter::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; + + current_node = 0; + indentation_level = 0; + in_a_class = false; + + while(line_offset < line_end) + { } return true; } - void interpreter::error(std::string const & message, std::string & output) + void interpreter::error(std::string const & message) { - output = "Line " + ail::number_to_string(lines[line_offset].line) + ": " + message; + error_message = "Line " + ail::number_to_string(lines[line_offset].line) + ": " + message; } } -- 2.11.4.GIT