From f747d91efeae3bf45f0cfa0675a4d398e5344916 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Tue, 5 Dec 2023 23:54:26 +0800 Subject: [PATCH] :zap: optimize code --- module/json.cpp | 4 ++-- module/keyboard.cpp | 4 ++++ module/nasocket.cpp | 4 ++++ src/nasal.h | 10 +++++----- src/nasal_lexer.cpp | 6 +++--- src/nasal_misc.cpp | 22 +++++++++++++--------- src/nasal_parse.cpp | 2 +- src/nasal_type.cpp | 2 +- src/nasal_vm.h | 4 ++-- src/repl.h | 2 +- 10 files changed, 36 insertions(+), 24 deletions(-) diff --git a/module/json.cpp b/module/json.cpp index ef4bf4f..3d29c1f 100644 --- a/module/json.cpp +++ b/module/json.cpp @@ -241,7 +241,7 @@ void json::vector_member(nas_vec& vec, gc* ngc) { vec.elems.push_back(ngc->newstr(this_token.content)); next(); } else if (this_token.type==token_type::tok_num) { - vec.elems.push_back(var::num(str2num(this_token.content.c_str()))); + vec.elems.push_back(var::num(str_to_num(this_token.content.c_str()))); next(); } } @@ -280,7 +280,7 @@ void json::hash_member(nas_hash& hash, gc* ngc) { hash.elems.insert({name, ngc->newstr(this_token.content)}); next(); } else if (this_token.type==token_type::tok_num) { - hash.elems.insert({name, var::num(str2num(this_token.content.c_str()))}); + hash.elems.insert({name, var::num(str_to_num(this_token.content.c_str()))}); next(); } } diff --git a/module/keyboard.cpp b/module/keyboard.cpp index 27df676..6fbf05b 100644 --- a/module/keyboard.cpp +++ b/module/keyboard.cpp @@ -7,6 +7,10 @@ #include #endif +#ifdef _MSC_VER +#pragma warning (disable:4996) +#endif + #ifdef _WIN32 #include #else diff --git a/module/nasocket.cpp b/module/nasocket.cpp index 282dc2c..12e6fe2 100644 --- a/module/nasocket.cpp +++ b/module/nasocket.cpp @@ -6,6 +6,10 @@ #include #endif +#ifdef _MSC_VER +#pragma warning (disable:4996) +#endif + #ifdef _WIN32 #include #pragma comment(lib,"ws2_32") diff --git a/src/nasal.h b/src/nasal.h index ea69761..ccfd9b7 100644 --- a/src/nasal.h +++ b/src/nasal.h @@ -39,19 +39,19 @@ bool is_superh(); // virtual machine stack depth, both global depth and value stack depth const u32 STACK_DEPTH = 4096; -f64 hex2f(const char*); -f64 oct2f(const char*); +f64 hex_to_f64(const char*); +f64 oct_to_f64(const char*); // we have the same reason not using atof here // just as andy's interpreter does. // it is not platform independent, and may have strange output. // so we write a new function here to convert str to number manually. // but this also makes 0.1+0.2==0.3, // not another result that you may get in other languages. -f64 dec2f(const char*); +f64 dec_to_f64(const char*); -f64 str2num(const char*); +f64 str_to_num(const char*); i32 utf8_hdchk(const char); -std::string chrhex(const char); +std::string char_to_hex(const char); std::string rawstr(const std::string&, const usize maxlen = 0); namespace fs { diff --git a/src/nasal_lexer.cpp b/src/nasal_lexer.cpp index 55b2490..628ab53 100644 --- a/src/nasal_lexer.cpp +++ b/src/nasal_lexer.cpp @@ -61,7 +61,7 @@ void lexer::err_char() { char c = res[ptr++]; err.err("lexer", {line, column-1, line, column, filename}, - "invalid character 0x"+chrhex(c) + "invalid character 0x" + char_to_hex(c) ); ++invalid_char; } @@ -120,9 +120,9 @@ std::string lexer::utf8_gen() { // utf8 character's total length is 1+nbytes if (tmp.length()!=1+nbytes) { ++column; - std::string utf_info = "0x"+chrhex(tmp[0]); + std::string utf_info = "0x" + char_to_hex(tmp[0]); for(u32 i = 1; i +#ifdef _MSC_VER +#pragma warning (disable:4996) +#endif + namespace nasal { bool is_windows() { @@ -93,7 +97,7 @@ bool is_superh() { #endif } -f64 hex2f(const char* str) { +f64 hex_to_f64(const char* str) { f64 ret = 0; for(; *str; ++str) { if ('0'<=*str && *str<='9') { @@ -109,7 +113,7 @@ f64 hex2f(const char* str) { return ret; } -f64 oct2f(const char* str) { +f64 oct_to_f64(const char* str) { f64 ret = 0; while('0'<=*str && *str<'8') { ret = ret*8+(*str++-'0'); @@ -126,7 +130,7 @@ f64 oct2f(const char* str) { // so we write a new function here to convert str to number manually. // but this also makes 0.1+0.2==0.3, // not another result that you may get in other languages. -f64 dec2f(const char* str) { +f64 dec_to_f64(const char* str) { f64 ret = 0, num_pow = 0; bool negative = false; while('0'<=*str && *str<='9') { @@ -172,7 +176,7 @@ f64 dec2f(const char* str) { ret*std::pow(10, num_pow-1)*10; } -f64 str2num(const char* str) { +f64 str_to_num(const char* str) { bool negative = false; f64 res = 0; if (*str=='-' || *str=='+') { @@ -182,11 +186,11 @@ f64 str2num(const char* str) { return nan(""); } if (str[0]=='0' && str[1]=='x') { - res = hex2f(str+2); + res = hex_to_f64(str+2); } else if (str[0]=='0' && str[1]=='o') { - res = oct2f(str+2); + res = oct_to_f64(str+2); } else { - res = dec2f(str); + res = dec_to_f64(str); } return negative? -res:res; } @@ -206,7 +210,7 @@ i32 utf8_hdchk(const char head) { return 0; } -std::string chrhex(const char c) { +std::string char_to_hex(const char c) { const char hextbl[] = "0123456789abcdef"; return {hextbl[(c&0xf0)>>4], hextbl[c&0x0f]}; } @@ -216,7 +220,7 @@ std::string rawstr(const std::string& str, const usize maxlen) { for(auto i : str) { // windows doesn't output unicode normally, so we output the hex if (is_windows() && i<=0) { - ret += "\\x"+chrhex(i); + ret += "\\x" + char_to_hex(i); continue; } switch(i) { diff --git a/src/nasal_parse.cpp b/src/nasal_parse.cpp index e62ff28..20994cb 100644 --- a/src/nasal_parse.cpp +++ b/src/nasal_parse.cpp @@ -223,7 +223,7 @@ nil_expr* parse::nil() { number_literal* parse::num() { auto node = new number_literal(toks[ptr].loc, - str2num(toks[ptr].str.c_str())); + str_to_num(toks[ptr].str.c_str())); match(tok::num); return node; } diff --git a/src/nasal_type.cpp b/src/nasal_type.cpp index 6d4e207..498b699 100644 --- a/src/nasal_type.cpp +++ b/src/nasal_type.cpp @@ -242,7 +242,7 @@ void nas_val::clear() { } f64 var::to_num() { - return type!=vm_type::vm_str? val.num:str2num(str().c_str()); + return type!=vm_type::vm_str? val.num:str_to_num(str().c_str()); } std::string var::to_str() { diff --git a/src/nasal_vm.h b/src/nasal_vm.h index 99b8ad5..8653818 100644 --- a/src/nasal_vm.h +++ b/src/nasal_vm.h @@ -198,7 +198,7 @@ inline bool vm::cond(var& val) { if (val.is_num()) { return val.num(); } else if (val.is_str()) { - const f64 num = str2num(val.str().c_str()); + const f64 num = str_to_num(val.str().c_str()); return std::isnan(num)? !val.str().empty():num; } return false; @@ -308,7 +308,7 @@ inline void vm::o_lnot() { case vm_type::vm_nil: ctx.top[0] = one; break; case vm_type::vm_num: ctx.top[0] = val.num()? zero:one; break; case vm_type::vm_str: { - const f64 num = str2num(val.str().c_str()); + const f64 num = str_to_num(val.str().c_str()); if (std::isnan(num)) { ctx.top[0] = var::num(static_cast(val.str().empty())); } else { diff --git a/src/repl.h b/src/repl.h index a7afdd3..ba8d1c7 100644 --- a/src/repl.h +++ b/src/repl.h @@ -15,7 +15,7 @@ namespace repl { struct info { bool in_repl_mode = false; - std::string repl_file_name = ""; + const std::string repl_file_name = ""; std::string repl_file_source = ""; // singleton