From bd5044add2fd7c6d526adf429fbcf109329ae640 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Wed, 6 Jul 2022 22:21:01 +0800 Subject: [PATCH] :rocket: add a new way to import other files: `import("./stl/queue.nas");` now has same function as `import.stl.queue;` --- lib.nas | 6 ----- nasal.h | 8 +++--- nasal_builtin.h | 15 +---------- nasal_import.h | 59 +++++++++++++++++++++++++++++++++----------- stl/lib.nas | 6 ----- test/auto_crash.nas | 2 +- test/bfconvertor.nas | 2 -- test/bfs.nas | 2 +- test/coroutine.nas | 3 ++- test/httptest.nas | 2 +- test/md5compare.nas | 4 +-- test/occupation.nas | 4 +-- test/snake.nas | 2 +- test/tetris.nas | 2 +- 14 files changed, 60 insertions(+), 57 deletions(-) diff --git a/lib.nas b/lib.nas index 7000d43..a24fef1 100644 --- a/lib.nas +++ b/lib.nas @@ -1,11 +1,5 @@ # lib.nas -# import is used to link another file, this lib function will do nothing. -# because nasal_import will recognize this and link files before generating bytecode. -var import=func(filename){ - return __builtin_import(filename); -} - # print is used to print all things in nasal, try and see how it works. # this function uses std::cout/printf to output logs. var print=func(elems...){ diff --git a/nasal.h b/nasal.h index 6946ca1..d8c6ab5 100644 --- a/nasal.h +++ b/nasal.h @@ -158,7 +158,7 @@ std::string rawstr(const std::string& str) { #ifdef _WIN32 // windows ps or cmd doesn't output unicode normally - // if 'chcp65001' is not enabled, so we output the hex + // if 'chcp65001' is not enabled, we output the hex if(i<=0) { ret+="\\x"; @@ -171,15 +171,15 @@ std::string rawstr(const std::string& str) case '\0': ret+="\\0"; break; case '\a': ret+="\\a"; break; case '\b': ret+="\\b"; break; - case '\e': ret+="\\e"; break; case '\t': ret+="\\t"; break; case '\n': ret+="\\n"; break; case '\v': ret+="\\v"; break; case '\f': ret+="\\f"; break; case '\r': ret+="\\r"; break; - case '\\': ret+="\\\\";break; - case '\'': ret+="\\\'";break; + case '\e': ret+="\\e"; break; case '\"': ret+="\\\"";break; + case '\'': ret+="\\\'";break; + case '\\': ret+="\\\\";break; default: ret+=i; break; } } diff --git a/nasal_builtin.h b/nasal_builtin.h index 4340462..13450a0 100644 --- a/nasal_builtin.h +++ b/nasal_builtin.h @@ -51,7 +51,6 @@ nas_native(builtin_time); nas_native(builtin_contains); nas_native(builtin_delete); nas_native(builtin_keys); -nas_native(builtin_import); nas_native(builtin_die); nas_native(builtin_find); nas_native(builtin_type); @@ -158,7 +157,6 @@ struct {"__builtin_contains",builtin_contains}, {"__builtin_delete", builtin_delete }, {"__builtin_keys", builtin_keys }, - {"__builtin_import", builtin_import }, {"__builtin_die", builtin_die }, {"__builtin_find", builtin_find }, {"__builtin_type", builtin_type }, @@ -653,24 +651,13 @@ nasal_ref builtin_keys(nasal_ref* local,nasal_gc& gc) --gc.top; return gc.top[1]; } -nasal_ref builtin_import(nasal_ref* local,nasal_gc& gc) -{ - // this function is used in preprocessing. - // this function will return nothing when running. - return builtin_err( - "import", - "\n\tthis function is used to link files together. " - "\n\tmake sure it is used in global scope. " - "\n\tmake sure it has correct argument(only one arg allowed)" - ); -} nasal_ref builtin_die(nasal_ref* local,nasal_gc& gc) { nasal_ref str=local[1]; if(str.type!=vm_str) return builtin_err("die","\"str\" must be string"); std::cerr<<"[vm] error: "< files; - bool check_import(const nasal_ast&); - bool check_exist(const std::string&); + bool imptchk(const nasal_ast&); + bool exist(const std::string&); void linker(nasal_ast&,nasal_ast&&); - nasal_ast file_import(nasal_ast&); - nasal_ast lib_import(); + std::string path(const nasal_ast&); + nasal_ast fimpt(nasal_ast&); + nasal_ast libimpt(); nasal_ast load(nasal_ast&,uint16_t); public: nasal_import(nasal_err& e):lib_loaded(false),nerr(e){} @@ -19,10 +20,38 @@ public: const std::vector& get_file() const {return files;} }; -bool nasal_import::check_import(const nasal_ast& node) +std::string nasal_import::path(const nasal_ast& node) { + if(node[1].type()==ast_callf) + return node[1][0].str(); + std::string fpath="."; + for(size_t i=1;i=2 && node[0].str()=="import" && node[1].type()==ast_callh) + { + for(size_t i=1;i