From 4757dd220ae1ec134541e7014422090bf0bb2c45 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Tue, 31 Oct 2023 19:53:01 +0800 Subject: [PATCH] :memo: add check for use statement --- src/nasal_codegen.cpp | 8 +++++++- tools/push.nas | 6 ++++-- tools/search_file.nas | 35 ++++++++++++++++++++++++++++++----- 3 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/nasal_codegen.cpp b/src/nasal_codegen.cpp index 37d4906..a3c5afc 100644 --- a/src/nasal_codegen.cpp +++ b/src/nasal_codegen.cpp @@ -1166,7 +1166,13 @@ void codegen::repl_mode_info_output_gen(expr* node) { void codegen::block_gen(code_block* node) { for(auto tmp : node->get_expressions()) { switch(tmp->get_type()) { - case expr_type::ast_use: break; + case expr_type::ast_use: + if (!local.empty()) { + die("module import is not allowed here.", + tmp->get_location() + ); + } + break; case expr_type::ast_null: break; case expr_type::ast_id: if (need_repl_output && local.empty()) { diff --git a/tools/push.nas b/tools/push.nas index 13918d8..51c45d3 100644 --- a/tools/push.nas +++ b/tools/push.nas @@ -1,7 +1,9 @@ -println("[",os.time(),"] auto push, please wait..."); +println("[",os.time(),"] (=.=) auto push, please wait..."); while(system("git push")!=0) { - println("[",os.time(),"] failed to push, retrying..."); + println("[",os.time(),"] (ToT) failed to push, retrying..."); unix.sleep(0.5); } + +println("[",os.time(),"] (^o^) auto push complete."); diff --git a/tools/search_file.nas b/tools/search_file.nas index 5329025..bce4c1e 100644 --- a/tools/search_file.nas +++ b/tools/search_file.nas @@ -1,7 +1,17 @@ import.std.file; -if (size(arg)!=1) { +var tips = func() { + println("usage:"); + println(" nasal search_file.nas [key]"); +} + +if (size(arg)<1) { println("need a key string to search files."); + tips(); + exit(-1); +} else if (size(arg)>1) { + println("too many arguments."); + tips(); exit(-1); } @@ -12,7 +22,7 @@ var do_flat = func(vec) { var bfs = [vec]; while(size(bfs)) { var d = pop(bfs); - foreach(var f;d.files) { + foreach(var f; d.files) { if (ishash(f)) { append(bfs,f); continue; @@ -20,12 +30,12 @@ var do_flat = func(vec) { append(flat, d.dir~"/"~f); } } - sort(flat, func(a,b){return cmp(a,b)<0}); + sort(flat, func(a, b){return cmp(a, b)<0}); return flat; } var count = 0; -foreach(var f;do_flat(file.recursive_find_files("."))) { +foreach(var f; do_flat(file.recursive_find_files("."))) { var pos = find(needle, f); if (pos == -1) { continue; @@ -33,7 +43,22 @@ foreach(var f;do_flat(file.recursive_find_files("."))) { count += 1; var begin = substr(f, 0, pos); var end = pos+size(needle)>=size(f)? "":substr(f, pos+size(needle), size(f)); - println(begin, "\e[95;1m", needle, "\e[0m", end); + var file_size = fstat(f).st_size; + var unit = "b"; + if (file_size>1024) { + file_size/=1024; + unit = "kb"; + } + if (file_size>1024) { + file_size/=1024; + unit = "mb"; + } + if (file_size>1024) { + file_size/=1024; + unit = "gb"; + } + file_size = int(file_size); + println(begin, "\e[95;1m", needle, "\e[0m", end, " | ", file_size, " ", unit); } println("\n", count, " result(s)."); \ No newline at end of file