🚀 add a new way to import other files:

`import("./stl/queue.nas");` now has same function as `import.stl.queue;`
This commit is contained in:
ValKmjolnir 2022-07-06 22:21:01 +08:00
parent b638708722
commit bd5044add2
14 changed files with 60 additions and 57 deletions

View File

@ -1,11 +1,5 @@
# lib.nas # 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. # print is used to print all things in nasal, try and see how it works.
# this function uses std::cout/printf to output logs. # this function uses std::cout/printf to output logs.
var print=func(elems...){ var print=func(elems...){

View File

@ -158,7 +158,7 @@ std::string rawstr(const std::string& str)
{ {
#ifdef _WIN32 #ifdef _WIN32
// windows ps or cmd doesn't output unicode normally // 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) if(i<=0)
{ {
ret+="\\x"; ret+="\\x";
@ -171,15 +171,15 @@ std::string rawstr(const std::string& str)
case '\0': ret+="\\0"; break; case '\0': ret+="\\0"; break;
case '\a': ret+="\\a"; break; case '\a': ret+="\\a"; break;
case '\b': ret+="\\b"; break; case '\b': ret+="\\b"; break;
case '\e': ret+="\\e"; break;
case '\t': ret+="\\t"; break; case '\t': ret+="\\t"; break;
case '\n': ret+="\\n"; break; case '\n': ret+="\\n"; break;
case '\v': ret+="\\v"; break; case '\v': ret+="\\v"; break;
case '\f': ret+="\\f"; break; case '\f': ret+="\\f"; break;
case '\r': ret+="\\r"; break; case '\r': ret+="\\r"; break;
case '\\': ret+="\\\\";break; case '\e': ret+="\\e"; break;
case '\'': ret+="\\\'";break;
case '\"': ret+="\\\"";break; case '\"': ret+="\\\"";break;
case '\'': ret+="\\\'";break;
case '\\': ret+="\\\\";break;
default: ret+=i; break; default: ret+=i; break;
} }
} }

View File

@ -51,7 +51,6 @@ nas_native(builtin_time);
nas_native(builtin_contains); nas_native(builtin_contains);
nas_native(builtin_delete); nas_native(builtin_delete);
nas_native(builtin_keys); nas_native(builtin_keys);
nas_native(builtin_import);
nas_native(builtin_die); nas_native(builtin_die);
nas_native(builtin_find); nas_native(builtin_find);
nas_native(builtin_type); nas_native(builtin_type);
@ -158,7 +157,6 @@ struct
{"__builtin_contains",builtin_contains}, {"__builtin_contains",builtin_contains},
{"__builtin_delete", builtin_delete }, {"__builtin_delete", builtin_delete },
{"__builtin_keys", builtin_keys }, {"__builtin_keys", builtin_keys },
{"__builtin_import", builtin_import },
{"__builtin_die", builtin_die }, {"__builtin_die", builtin_die },
{"__builtin_find", builtin_find }, {"__builtin_find", builtin_find },
{"__builtin_type", builtin_type }, {"__builtin_type", builtin_type },
@ -653,24 +651,13 @@ nasal_ref builtin_keys(nasal_ref* local,nasal_gc& gc)
--gc.top; --gc.top;
return gc.top[1]; 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 builtin_die(nasal_ref* local,nasal_gc& gc)
{ {
nasal_ref str=local[1]; nasal_ref str=local[1];
if(str.type!=vm_str) if(str.type!=vm_str)
return builtin_err("die","\"str\" must be string"); return builtin_err("die","\"str\" must be string");
std::cerr<<"[vm] error: "<<str.str()<<'\n'; std::cerr<<"[vm] error: "<<str.str()<<'\n';
return nasal_ref(vm_none); return {vm_none};
} }
nasal_ref builtin_find(nasal_ref* local,nasal_gc& gc) nasal_ref builtin_find(nasal_ref* local,nasal_gc& gc)
{ {

View File

@ -7,11 +7,12 @@ private:
bool lib_loaded; bool lib_loaded;
nasal_err& nerr; nasal_err& nerr;
std::vector<std::string> files; std::vector<std::string> files;
bool check_import(const nasal_ast&); bool imptchk(const nasal_ast&);
bool check_exist(const std::string&); bool exist(const std::string&);
void linker(nasal_ast&,nasal_ast&&); void linker(nasal_ast&,nasal_ast&&);
nasal_ast file_import(nasal_ast&); std::string path(const nasal_ast&);
nasal_ast lib_import(); nasal_ast fimpt(nasal_ast&);
nasal_ast libimpt();
nasal_ast load(nasal_ast&,uint16_t); nasal_ast load(nasal_ast&,uint16_t);
public: public:
nasal_import(nasal_err& e):lib_loaded(false),nerr(e){} nasal_import(nasal_err& e):lib_loaded(false),nerr(e){}
@ -19,10 +20,38 @@ public:
const std::vector<std::string>& get_file() const {return files;} const std::vector<std::string>& 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<node.size();++i)
#ifndef _WIN32
fpath+="/"+node[i].str();
#else
fpath+="\\"+node[i].str();
#endif
fpath+=".nas";
return fpath;
}
bool nasal_import::imptchk(const nasal_ast& node)
{
// only these two kinds of node can be recognized as 'import':
/*
call
|_id:import
|_callh:stl
|_callh:file
*/
if(node.type()==ast_call && node.size()>=2 && node[0].str()=="import" && node[1].type()==ast_callh)
{
for(size_t i=1;i<node.size();++i)
if(node[i].type()!=ast_callh)
return false;
return true;
}
/* /*
only this kind of node can be recognized as 'import':
call call
|_id:import |_id:import
|_call_func |_call_func
@ -38,7 +67,7 @@ only this kind of node can be recognized as 'import':
); );
} }
bool nasal_import::check_exist(const std::string& file) bool nasal_import::exist(const std::string& file)
{ {
// avoid importing the same file // avoid importing the same file
for(auto& fname:files) for(auto& fname:files)
@ -55,16 +84,16 @@ void nasal_import::linker(nasal_ast& root,nasal_ast&& add_root)
root.add(std::move(i)); root.add(std::move(i));
} }
nasal_ast nasal_import::file_import(nasal_ast& node) nasal_ast nasal_import::fimpt(nasal_ast& node)
{ {
nasal_lexer lex(nerr); nasal_lexer lex(nerr);
nasal_parse par(nerr); nasal_parse par(nerr);
// get filename and set node to ast_null // get filename and set node to ast_null
std::string filename=node[1][0].str(); std::string filename=path(node);
node.clear(); node.clear();
// avoid infinite loading loop // avoid infinite loading loop
if(check_exist(filename)) if(exist(filename))
return {0,ast_root}; return {0,ast_root};
if(access(filename.c_str(),F_OK)==-1) if(access(filename.c_str(),F_OK)==-1)
{ {
@ -80,7 +109,7 @@ nasal_ast nasal_import::file_import(nasal_ast& node)
return load(tmp,files.size()-1); return load(tmp,files.size()-1);
} }
nasal_ast nasal_import::lib_import() nasal_ast nasal_import::libimpt()
{ {
nasal_lexer lex(nerr); nasal_lexer lex(nerr);
nasal_parse par(nerr); nasal_parse par(nerr);
@ -113,7 +142,7 @@ nasal_ast nasal_import::lib_import()
} }
// avoid infinite loading loop // avoid infinite loading loop
if(check_exist(filename)) if(exist(filename))
return {0,ast_root}; return {0,ast_root};
// start importing... // start importing...
@ -129,12 +158,12 @@ nasal_ast nasal_import::load(nasal_ast& root,uint16_t fileindex)
nasal_ast new_root(0,ast_root); nasal_ast new_root(0,ast_root);
if(!lib_loaded) if(!lib_loaded)
{ {
linker(new_root,lib_import()); linker(new_root,libimpt());
lib_loaded=true; lib_loaded=true;
} }
for(auto& i:root.child()) for(auto& i:root.child())
if(check_import(i)) if(imptchk(i))
linker(new_root,file_import(i)); linker(new_root,fimpt(i));
// add root to the back of new_root // add root to the back of new_root
nasal_ast file_head(0,ast_file); nasal_ast file_head(0,ast_file);
file_head.set_num(fileindex); file_head.set_num(fileindex);

View File

@ -1,11 +1,5 @@
# lib.nas # 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. # print is used to print all things in nasal, try and see how it works.
# this function uses std::cout/printf to output logs. # this function uses std::cout/printf to output logs.
var print=func(elems...){ var print=func(elems...){

View File

@ -1,5 +1,5 @@
# Road check and auto pilot by ValKmjolnir # Road check and auto pilot by ValKmjolnir
import("stl/fg_env.nas"); import.stl.fg_env;
var dt=0.01; var dt=0.01;
var intergral=0; var intergral=0;

View File

@ -1,5 +1,3 @@
import("lib.nas");
var mandelbrot= var mandelbrot=
"[A mandelbrot set fractal viewer in brainf*** written by Erik Bosman] "[A mandelbrot set fractal viewer in brainf*** written by Erik Bosman]
+++++++++++++[->++>>>+++++>++>+<<<<<<]>>>>>++++++>--->>>>>>>>>>+++++++++++++++[[ +++++++++++++[->++>>>+++++>++>+<<<<<<]>>>>>++++++>--->>>>>>>>>>+++++++++++++++[[

View File

@ -1,4 +1,4 @@
import("stl/queue.nas"); import.stl.queue;
rand(time(0)); rand(time(0));
var pixel=[' ','#','.','*']; var pixel=[' ','#','.','*'];

View File

@ -1,6 +1,7 @@
# coroutine.nas by ValKmjolnir # coroutine.nas by ValKmjolnir
# 2022/5/19 # 2022/5/19
import("stl/process_bar.nas"); import.stl.process_bar;
var fib=func(){ var fib=func(){
var (a,b)=(1,1); var (a,b)=(1,1);
coroutine.yield(a); coroutine.yield(a);

View File

@ -1,4 +1,4 @@
import("module/libsock.nas"); import.module.libsock;
var http=func(){ var http=func(){
var sd=nil; var sd=nil;

View File

@ -1,5 +1,5 @@
import("test/md5.nas"); import.test.md5;
import("stl/process_bar.nas"); import.stl.process_bar;
srand(); srand();
var compare=func(){ var compare=func(){

View File

@ -1,5 +1,5 @@
import("stl/process_bar.nas"); import.stl.process_bar;
import("module/libkey.nas"); import.module.libkey;
var cpu_stat=func(){ var cpu_stat=func(){
var cpu=split("\n",io.fin("/proc/stat"))[0]; var cpu=split("\n",io.fin("/proc/stat"))[0];

View File

@ -1,4 +1,4 @@
import("./module/libkey.nas"); import.module.libkey;
var list=func(){ var list=func(){
var (begin,end,len)=(nil,nil,0); var (begin,end,len)=(nil,nil,0);
return{ return{

View File

@ -1,4 +1,4 @@
import("./module/libkey.nas"); import.module.libkey;
var color=[ var color=[
"\e[31m","\e[32m","\e[33m","\e[34m","\e[35m","\e[36m", "\e[31m","\e[32m","\e[33m","\e[34m","\e[35m","\e[36m",
"\e[91m","\e[92m","\e[93m","\e[94m","\e[95m","\e[96m", "\e[91m","\e[92m","\e[93m","\e[94m","\e[95m","\e[96m",