From 8e0d1e18e19f1ae11e61ca4b33cd93a3293ef5cd Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Thu, 7 Dec 2023 00:04:41 +0800 Subject: [PATCH] :bug: fix bug in json.cpp & add std/argparse.nas --- makefile | 1 + module/json.cpp | 10 +++---- src/nasal_vm.cpp | 6 ++-- std/argparse.nas | 55 +++++++++++++++++++++++++++++++++++++ std/lib.nas | 14 +++++++--- std/mat.nas | 1 + std/phi.nas | 8 +++--- std/props.nas | 1 + std/utils.nas | 2 ++ test/argparse_test.nas | 11 ++++++++ test/auto_crash.nas | 1 + test/bp.nas | 1 + test/burningship.nas | 1 + test/console3D.nas | 1 + test/donuts.nas | 1 + test/feigenbaum.nas | 1 + test/mcpu.nas | 1 + test/md5_self.nas | 1 + test/md5compare.nas | 1 + test/occupation.nas | 1 + test/prime.nas | 2 ++ test/scalar.nas | 1 + tools/andy_gc_test.nas | 1 + tools/fgfs_props_getter.nas | 6 +++- tools/file2ppm.nas | 1 + tools/search_file.nas | 1 + 26 files changed, 115 insertions(+), 16 deletions(-) create mode 100644 std/argparse.nas create mode 100644 test/argparse_test.nas diff --git a/makefile b/makefile index 3b1fc37..e1d3c94 100644 --- a/makefile +++ b/makefile @@ -237,6 +237,7 @@ clean: .PHONY: test test:nasal + @ ./nasal test/argparse_test.nas @ ./nasal -e test/ascii-art.nas @ ./nasal -t -d test/bfs.nas @ ./nasal -t test/bigloop.nas diff --git a/module/json.cpp b/module/json.cpp index 3d29c1f..e1d4268 100644 --- a/module/json.cpp +++ b/module/json.cpp @@ -69,7 +69,7 @@ private: bool check(char c) { return c=='{' || c=='}' || c=='[' || c==']' || c==':' || c==',' || c=='"' || c=='\'' || - is_num(c) || is_id(c); + c=='-' || c=='+' || is_num(c) || is_id(c); } void next(); void match(token_type); @@ -155,10 +155,10 @@ void json::next() { while(ptr(&func) << std::dec; return result + out.str(); } @@ -337,7 +338,8 @@ std::string vm::report_special_call_lack_arguments( result = result.substr(0, result.length()-2); result += ") "; std::stringstream out; - out << "{entry: 0x" << std::hex << func.entry << std::dec << "}"; + const auto& code = bytecode[func.entry]; + out << "{ entry: " << files[code.fidx] << ":" << code.line << " }"; out << " @ 0x" << std::hex << reinterpret_cast(&func) << std::dec; return result + out.str(); } diff --git a/std/argparse.nas b/std/argparse.nas new file mode 100644 index 0000000..e17d161 --- /dev/null +++ b/std/argparse.nas @@ -0,0 +1,55 @@ + +var _arg = globals.arg; + +var new = func(description) { + var parser = { + description: description, + subparser: [], + command_list: [], + add_command: func(long, short, help) { + return _add_command(parser, long, short, help); + }, + add_subparser: func(name, help) { + return _add_subparser(parser, name, help); + }, + parse: func() { + return _parse(parser); + } + }; + return parser; +} + +var _help = func(parser) { + println(parser.description); + println("Subcommand:"); + foreach(var cmd; parser.subparser) { + println(cmd.name, "\t", cmd.parser.description); + } + println("Options:"); + foreach(var cmd; parser.command_list) { + println(cmd.full_name, " ", cmd.short_name, "\t", cmd.help); + } +} + +var _parse = func(parser) { + if (size(_arg)==0) { + _help(parser); + } +} + +var _add_command = func(parser, long, short, help) { + var new_command = { + full_name: long, + short_name: short, + help: help + }; + append(parser.command_list, new_command); +} + +var _add_subparser = func(parser, name, help) { + var new_subparser = { + name: name, + parser: new(help) + }; + append(parser.subparser, new_subparser); +} \ No newline at end of file diff --git a/std/lib.nas b/std/lib.nas index 60d7eca..e4b556a 100644 --- a/std/lib.nas +++ b/std/lib.nas @@ -1,6 +1,5 @@ # lib.nas # 2019 ValKmjolnir -use std.math; # print is used to print all things in nasal, try and see how it works. # this function uses std::cout to output logs. @@ -271,7 +270,14 @@ var isint = func(x) { } var isnum = func(x) { - return typeof(x)=="num" or !math.isnan(num(x)); + if (typeof(x)=="num") { + return true; + } + x = num(x); + if (!__isnan(x) and x!=nil) { + return true; + } + return false; } var isscalar = func(s) { @@ -347,8 +353,8 @@ var md5 = func(str) { } # important global constants -var D2R = math.pi / 180; # degree to radian -var R2D = 180 / math.pi; # radian to degree +var D2R = 3.14159265358979323846264338327950288 / 180; # degree to radian +var R2D = 180 / 3.14159265358979323846264338327950288; # radian to degree var FT2M = 0.3048; # feet to meter var M2FT = 1 / FT2M; diff --git a/std/mat.nas b/std/mat.nas index 42289c9..d872470 100644 --- a/std/mat.nas +++ b/std/mat.nas @@ -1,3 +1,4 @@ +use std.math; var mat = func(width,height) { var res=[]; diff --git a/std/phi.nas b/std/phi.nas index 2602d39..c3eb07c 100644 --- a/std/phi.nas +++ b/std/phi.nas @@ -4,7 +4,7 @@ # 2023/11/06 ValKmjolnir use module.libsock; -use std.json; +use module.libjson; use std.os; use std.unix; @@ -87,9 +87,9 @@ var new = func(hostname, port) { var data = substr(total_source, begin_position, length); # parse this json and return - var props = json.parse(data); - if (json.get_error()) { - println("encounter error when parsing \"", path, "\""); + var props = libjson.parse(data); + if (size(libjson.get_error())>0) { + println("encounter error when parsing \"", path, "\":\n", libjson.get_error()); logprint(LOG_DEBUG, _raw_str(data)); return {path: path}; } diff --git a/std/props.nas b/std/props.nas index ffb693f..1a31dba 100644 --- a/std/props.nas +++ b/std/props.nas @@ -9,6 +9,7 @@ # local node, there is no equivalent of the "relative path" variants # available in C++; just use node.getNode(path).whatever() instead. # +use std.math; var _new = func { return { diff --git a/std/utils.nas b/std/utils.nas index 0d2bf41..e309483 100644 --- a/std/utils.nas +++ b/std/utils.nas @@ -1,6 +1,8 @@ # utils.nas # 2023 by ValKmjolnir +use std.math; +# when count can be divided exactly by times, return true var times_trigger = func(times, count) { return math.mod(times, count)==0; } \ No newline at end of file diff --git a/test/argparse_test.nas b/test/argparse_test.nas new file mode 100644 index 0000000..af23e8e --- /dev/null +++ b/test/argparse_test.nas @@ -0,0 +1,11 @@ +# use module.libjson; + +# libjson.stringify(math.tan); + +# println(libjson.get_error()); +use std.argparse; + +var args = argparse.new("test cli"); +args.add_command("--what", "-w", "what-what"); +args.add_subparser("what", "what-what"); +args.parse(); diff --git a/test/auto_crash.nas b/test/auto_crash.nas index a18c4af..db925d9 100644 --- a/test/auto_crash.nas +++ b/test/auto_crash.nas @@ -1,5 +1,6 @@ # Road check and auto pilot by ValKmjolnir use std.fg_env; +use std.math; var props = fg_env.props; var geodinfo = fg_env.geodinfo; diff --git a/test/bp.nas b/test/bp.nas index 198ac38..5611faa 100644 --- a/test/bp.nas +++ b/test/bp.nas @@ -1,4 +1,5 @@ use std.mat; +use std.math; rand(time(0)); diff --git a/test/burningship.nas b/test/burningship.nas index c3a5b68..357b131 100644 --- a/test/burningship.nas +++ b/test/burningship.nas @@ -1,6 +1,7 @@ use std.process_bar; use std.os; use std.io; +use std.math; var ppm = func(filename, width, height, RGB) { # P3 use ASCII number diff --git a/test/console3D.nas b/test/console3D.nas index 0703a46..7c137b3 100644 --- a/test/console3D.nas +++ b/test/console3D.nas @@ -23,6 +23,7 @@ # SOFTWARE. use module.libmat; use std.runtime; +use std.math; func() { # allocate more spaces diff --git a/test/donuts.nas b/test/donuts.nas index 8d8bcc3..340c730 100644 --- a/test/donuts.nas +++ b/test/donuts.nas @@ -1,4 +1,5 @@ use std.runtime; +use std.math; var mod = math.mod; diff --git a/test/feigenbaum.nas b/test/feigenbaum.nas index 760bd50..d56d315 100644 --- a/test/feigenbaum.nas +++ b/test/feigenbaum.nas @@ -1,6 +1,7 @@ use std.process_bar; use std.os; use std.io; +use std.math; var ppm = func(filename, width, height, RGB) { # P3 use ASCII number diff --git a/test/mcpu.nas b/test/mcpu.nas index d1ade9b..859ad6f 100644 --- a/test/mcpu.nas +++ b/test/mcpu.nas @@ -1,5 +1,6 @@ use std.bits; use std.os; +use std.math; var inst={ inst_stop:0, diff --git a/test/md5_self.nas b/test/md5_self.nas index 692a22a..38e8467 100644 --- a/test/md5_self.nas +++ b/test/md5_self.nas @@ -1,4 +1,5 @@ use std.bits; +use std.math; var check = func(x) { if (x<0x100000000) diff --git a/test/md5compare.nas b/test/md5compare.nas index 1fcfda7..f5cb242 100644 --- a/test/md5compare.nas +++ b/test/md5compare.nas @@ -3,6 +3,7 @@ use std.process_bar; use std.file; use std.os; use std.io; +use std.math; srand(); diff --git a/test/occupation.nas b/test/occupation.nas index 577539e..6774b63 100644 --- a/test/occupation.nas +++ b/test/occupation.nas @@ -5,6 +5,7 @@ use std.runtime; use std.os; use std.io; use std.unix; +use std.math; var is_windows_platform=os.platform()=="windows"; var is_macos_platform=os.platform()=="macOS"; diff --git a/test/prime.nas b/test/prime.nas index 50f6b82..063baec 100644 --- a/test/prime.nas +++ b/test/prime.nas @@ -1,3 +1,5 @@ +use std.math; + var is_prime = func(x) { for(var i=2;i