From 939a7a62b70eb247fc68dc9da75884d96c7cdefc Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Sun, 11 Oct 2020 05:28:27 -0700 Subject: [PATCH] update 'import' --- version3.0/lib.nas | 5 ++ version3.0/main.cpp | 108 +++++++++++----------- version3.0/nasal.h | 1 + version3.0/nasal_builtin.h | 18 ++-- version3.0/nasal_import.h | 174 ++++++++++++++++++++++++++++++++++++ version3.0/nasal_lexer.h | 7 +- version3.0/nasal_parse.h | 1 - version3.0/nasal_resource.h | 42 ++------- version3.0/nasal_runtime.h | 4 + 9 files changed, 263 insertions(+), 97 deletions(-) create mode 100644 version3.0/nasal_import.h diff --git a/version3.0/lib.nas b/version3.0/lib.nas index 1c362db..da3f9e6 100644 --- a/version3.0/lib.nas +++ b/version3.0/lib.nas @@ -1,3 +1,8 @@ +var import=func(filename) +{ + nasal_call_import(filename); + return nil; +} var print=func(elements...) { nasal_call_builtin_std_cout(elements); diff --git a/version3.0/main.cpp b/version3.0/main.cpp index 84ce37b..f324603 100644 --- a/version3.0/main.cpp +++ b/version3.0/main.cpp @@ -3,20 +3,19 @@ nasal_resource resource; nasal_lexer lexer; nasal_parse parse; +nasal_import preprocessor; std::string command; +std::string inputfile="null"; nasal_runtime runtime; void help() { - std::cout<<">> [\'file\'] input a file."<> [cls ] clear the screen."<> [\"file\"] input a file."<> [clear ] clear the screen."<> [del ] clear the resource code."<> [lib ] add lib file."<> [rs ] print resource code."<> [lex ] turn code into tokens."<> [par ] turn tokens into abstract syntax tree."<> [ast ] check the abstract syntax tree."<> [del ] clear the source code."<> [rs ] print source code."<> [lex ] use lexer to turn code into tokens."<> [ast ] do parsing and check the abstract syntax tree."<> [run ] run code."<> [logo ] print logo of nasal ."<> [exit ] quit nasal interpreter."<> [Delete] complete."<> ["<: error(s) occurred,stop."<> [lexer] error(s) occurred,stop.\n"; - return; -} - -void par_func() -{ - lexer.scanner(resource.get_file()); - if(!lexer.get_error()) + if(lexer.get_error()) { - parse.set_toklist(lexer.get_token_list()); - parse.main_process(); - if(parse.get_error()) - std::cout<<">> [parse] error(s) occurred,stop.\n"; + die("lexer",inputfile); + return; } - else - std::cout<<">> [lexer] error(s) occurred,stop.\n"; + lexer.print_token(); return; } void ast_print() { lexer.scanner(resource.get_file()); - if(!lexer.get_error()) + if(lexer.get_error()) { - parse.set_toklist(lexer.get_token_list()); - parse.main_process(); - if(parse.get_error()) - std::cout<<">> [parse] error(s) occurred,stop.\n"; - else - parse.get_root().print_ast(0); + die("lexer",inputfile); + return; } - else - std::cout<<">> [lexer] error(s) occurred,stop.\n"; + parse.set_toklist(lexer.get_token_list()); + parse.main_process(); + if(parse.get_error()) + { + die("parse",inputfile); + return; + } + parse.get_root().print_ast(0); return; } void runtime_start() { lexer.scanner(resource.get_file()); - if(!lexer.get_error()) + if(lexer.get_error()) { - parse.set_toklist(lexer.get_token_list()); - parse.main_process(); - if(parse.get_error()) - std::cout<<">> [parse] error(s) occurred,stop.\n"; - else - { - runtime.set_root(parse.get_root()); - runtime.run(); - } + die("lexer",inputfile); + return; } - else - std::cout<<">> [lexer] error(s) occurred,stop.\n"; + parse.set_toklist(lexer.get_token_list()); + parse.main_process(); + if(parse.get_error()) + { + die("parse",inputfile); + return; + } + preprocessor.preprocessing(parse.get_root()); + if(preprocessor.get_error()) + { + die("import",inputfile); + return; + } + runtime.set_root(preprocessor.get_root()); + runtime.run(); return; } @@ -110,7 +111,6 @@ int main() system("chcp 65001"); system("cls"); #endif - // this curve looks really cool logo(); #ifdef _WIN32 std::cout<<">> [system] Windows system."<> [system] MacOS system."<> Nasal interpreter ver 3.0 ."<> Code: https://github.com/ValKmjolnir/Nasal-Interpreter"<> More info: http://wiki.flightgear.org/Nasal_scripting_language"<> Info: http://wiki.flightgear.org/Nasal_scripting_language"<> Input \"help\" to get help ."<>command; if(command=="help") help(); - else if(command=="cls" || command=="clear") + else if(command=="clear") { #ifdef _WIN32 system("cls"); @@ -145,14 +146,10 @@ int main() } else if(command=="del") del_func(); - else if(command=="lib") - resource.load_lib(); else if(command=="rs") resource.print_file(); else if(command=="lex") lex_func(); - else if(command=="par") - par_func(); else if(command=="ast") ast_print(); else if(command=="run") @@ -162,7 +159,10 @@ int main() else if(command=="exit") break; else - resource.input_file(command); + { + inputfile=command; + resource.input_file(inputfile); + } } return 0; } \ No newline at end of file diff --git a/version3.0/nasal.h b/version3.0/nasal.h index 49a46c8..fcef3c1 100644 --- a/version3.0/nasal.h +++ b/version3.0/nasal.h @@ -24,6 +24,7 @@ #include "nasal_lexer.h" #include "nasal_ast.h" #include "nasal_parse.h" +#include "nasal_import.h" #include "nasal_gc.h" #include "nasal_runtime.h" #include "nasal_builtin.h" diff --git a/version3.0/nasal_builtin.h b/version3.0/nasal_builtin.h index 0fd20b4..8a2457c 100644 --- a/version3.0/nasal_builtin.h +++ b/version3.0/nasal_builtin.h @@ -8,7 +8,7 @@ int nasal_runtime::builtin_print(int local_scope_addr) { // get arguments int vector_value_addr=in_builtin_find("elements"); - if(vector_value_addr<0 || nasal_vm.gc_get(vector_value_addr).get_type()!=vm_vector) + if(vector_value_addr<0 || !in_builtin_check(vector_value_addr,vm_vector)) { std::cout<<">> [runtime] builtin_print: cannot find values or wrong value type."<> [runtime] builtin_append: cannot find values or wrong value type."<> [runtime] builtin_append: cannot find values or wrong value type."<> [runtime] cannot use import when running."< filename_table; + int error; + void die(std::string,std::string); + void init(); + bool check_import(nasal_ast&); + bool check_exist(std::string); + void linker(nasal_ast&,nasal_ast&); + nasal_ast file_import(nasal_ast&); + nasal_ast load(nasal_ast&); +public: + nasal_import(); + int get_error(); + void preprocessing(nasal_ast&); + nasal_ast& get_root(); +}; + +nasal_import::nasal_import() +{ + import_src.clear(); + import_lex.clear(); + import_par.clear(); + import_ast.clear(); + filename_table.clear(); + return; +} + +void nasal_import::die(std::string filename,std::string error_stage) +{ + ++error; + std::cout<<">> [import] in <\""<: error(s) occurred in "<& ref_vec=node.get_children(); + if(ref_vec.size()!=2) + return false; + if(ref_vec[0].get_str()!="import") + return false; + if(ref_vec[1].get_type()!=ast_call_func) + return false; + if(ref_vec[1].get_children().size()!=1 || ref_vec[1].get_children()[0].get_type()!=ast_string) + return false; + return true; +} + +bool nasal_import::check_exist(std::string filename) +{ + int size=filename_table.size(); + for(int i=0;i& ref_vec=add_root.get_children(); + int size=ref_vec.size(); + for(int i=0;i& ref_vec=root.get_children(); + int size=ref_vec.size(); + for(int i=0;i& res,int& ptr,int& line) { int res_size=res.size(); bool scientific_notation=false;// numbers like 1e8 are scientific_notation + bool is_hex=(ptr& res) else if(IS_NOTE_HEAD(res[ptr])) { // avoid note - while(ptr& res) ++ptr; } } - std::cout<<">> [lexer] complete scanning. "<> [parse] complete generation. "< res; public: - void input_file(std::string); - void load_lib(); - void clear(); - void print_file(); + bool input_file(std::string); + void clear(); + void print_file(); std::vector& get_file(); }; -void nasal_resource::input_file(std::string filename) +bool nasal_resource::input_file(std::string filename) { + res.clear(); std::ifstream fin(filename,std::ios::binary); if(fin.fail()) { - std::cout<<">> [resource] cannot open file \'"<> [resource] cannot open file \""<input_file(lib_filename[i]); - return; + return true; } void nasal_resource::clear() diff --git a/version3.0/nasal_runtime.h b/version3.0/nasal_runtime.h index 5dac346..38159b4 100644 --- a/version3.0/nasal_runtime.h +++ b/version3.0/nasal_runtime.h @@ -99,6 +99,7 @@ private: int builtin_contains(int); int builtin_delete(int); int builtin_getkeys(int); + int builtin_import(int); void load_builtin_function(); public: nasal_runtime(); @@ -163,6 +164,7 @@ void nasal_runtime::load_builtin_function() {"nasal_call_builtin_contains", nasal_runtime::builtin_contains}, {"nasal_call_builtin_delete", nasal_runtime::builtin_delete}, {"nasal_call_builtin_get_keys", nasal_runtime::builtin_getkeys}, + {"nasal_call_import", nasal_runtime::builtin_import}, {"", NULL} }; for(int i=0;builtin_func_table[i].func_pointer;++i) @@ -271,6 +273,7 @@ int nasal_runtime::main_progress() case ast_while:case ast_for:case ast_forindex:case ast_foreach: ret_state=loop_progress(root.get_children()[i],-1,false);break; case ast_nil:case ast_number:case ast_string:case ast_function:break; + case ast_identifier: case ast_vector:case ast_hash: case ast_call: case ast_equal:case ast_add_equal:case ast_sub_equal:case ast_mult_equal:case ast_div_equal:case ast_link_equal: @@ -325,6 +328,7 @@ int nasal_runtime::block_progress(nasal_ast& node,int local_scope_addr,bool allo case ast_while:case ast_for:case ast_forindex:case ast_foreach: ret_state=loop_progress(tmp_node,local_scope_addr,allow_return);break; case ast_nil:case ast_number:case ast_string:case ast_function:break; + case ast_identifier: case ast_vector:case ast_hash: case ast_call: case ast_equal:case ast_add_equal:case ast_sub_equal:case ast_mult_equal:case ast_div_equal:case ast_link_equal: