optimize code

This commit is contained in:
ValKmjolnir 2023-12-05 23:54:26 +08:00
parent 3b1659e7ee
commit f747d91efe
10 changed files with 36 additions and 24 deletions

View File

@ -241,7 +241,7 @@ void json::vector_member(nas_vec& vec, gc* ngc) {
vec.elems.push_back(ngc->newstr(this_token.content)); vec.elems.push_back(ngc->newstr(this_token.content));
next(); next();
} else if (this_token.type==token_type::tok_num) { } 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(); next();
} }
} }
@ -280,7 +280,7 @@ void json::hash_member(nas_hash& hash, gc* ngc) {
hash.elems.insert({name, ngc->newstr(this_token.content)}); hash.elems.insert({name, ngc->newstr(this_token.content)});
next(); next();
} else if (this_token.type==token_type::tok_num) { } 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(); next();
} }
} }

View File

@ -7,6 +7,10 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef _MSC_VER
#pragma warning (disable:4996)
#endif
#ifdef _WIN32 #ifdef _WIN32
#include <conio.h> #include <conio.h>
#else #else

View File

@ -6,6 +6,10 @@
#include <unistd.h> #include <unistd.h>
#endif #endif
#ifdef _MSC_VER
#pragma warning (disable:4996)
#endif
#ifdef _WIN32 #ifdef _WIN32
#include <winsock.h> #include <winsock.h>
#pragma comment(lib,"ws2_32") #pragma comment(lib,"ws2_32")

View File

@ -39,19 +39,19 @@ bool is_superh();
// virtual machine stack depth, both global depth and value stack depth // virtual machine stack depth, both global depth and value stack depth
const u32 STACK_DEPTH = 4096; const u32 STACK_DEPTH = 4096;
f64 hex2f(const char*); f64 hex_to_f64(const char*);
f64 oct2f(const char*); f64 oct_to_f64(const char*);
// we have the same reason not using atof here // we have the same reason not using atof here
// just as andy's interpreter does. // just as andy's interpreter does.
// it is not platform independent, and may have strange output. // it is not platform independent, and may have strange output.
// so we write a new function here to convert str to number manually. // so we write a new function here to convert str to number manually.
// but this also makes 0.1+0.2==0.3, // but this also makes 0.1+0.2==0.3,
// not another result that you may get in other languages. // 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); 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); std::string rawstr(const std::string&, const usize maxlen = 0);
namespace fs { namespace fs {

View File

@ -61,7 +61,7 @@ void lexer::err_char() {
char c = res[ptr++]; char c = res[ptr++];
err.err("lexer", err.err("lexer",
{line, column-1, line, column, filename}, {line, column-1, line, column, filename},
"invalid character 0x"+chrhex(c) "invalid character 0x" + char_to_hex(c)
); );
++invalid_char; ++invalid_char;
} }
@ -120,9 +120,9 @@ std::string lexer::utf8_gen() {
// utf8 character's total length is 1+nbytes // utf8 character's total length is 1+nbytes
if (tmp.length()!=1+nbytes) { if (tmp.length()!=1+nbytes) {
++column; ++column;
std::string utf_info = "0x"+chrhex(tmp[0]); std::string utf_info = "0x" + char_to_hex(tmp[0]);
for(u32 i = 1; i<tmp.size(); ++i) { for(u32 i = 1; i<tmp.size(); ++i) {
utf_info += " 0x"+chrhex(tmp[i]); utf_info += " 0x" + char_to_hex(tmp[i]);
} }
err.err("lexer", err.err("lexer",
{line, column-1, line, column, filename}, {line, column-1, line, column, filename},

View File

@ -7,6 +7,10 @@
#endif #endif
#include <sys/stat.h> #include <sys/stat.h>
#ifdef _MSC_VER
#pragma warning (disable:4996)
#endif
namespace nasal { namespace nasal {
bool is_windows() { bool is_windows() {
@ -93,7 +97,7 @@ bool is_superh() {
#endif #endif
} }
f64 hex2f(const char* str) { f64 hex_to_f64(const char* str) {
f64 ret = 0; f64 ret = 0;
for(; *str; ++str) { for(; *str; ++str) {
if ('0'<=*str && *str<='9') { if ('0'<=*str && *str<='9') {
@ -109,7 +113,7 @@ f64 hex2f(const char* str) {
return ret; return ret;
} }
f64 oct2f(const char* str) { f64 oct_to_f64(const char* str) {
f64 ret = 0; f64 ret = 0;
while('0'<=*str && *str<'8') { while('0'<=*str && *str<'8') {
ret = ret*8+(*str++-'0'); 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. // so we write a new function here to convert str to number manually.
// but this also makes 0.1+0.2==0.3, // but this also makes 0.1+0.2==0.3,
// not another result that you may get in other languages. // 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; f64 ret = 0, num_pow = 0;
bool negative = false; bool negative = false;
while('0'<=*str && *str<='9') { while('0'<=*str && *str<='9') {
@ -172,7 +176,7 @@ f64 dec2f(const char* str) {
ret*std::pow(10, num_pow-1)*10; ret*std::pow(10, num_pow-1)*10;
} }
f64 str2num(const char* str) { f64 str_to_num(const char* str) {
bool negative = false; bool negative = false;
f64 res = 0; f64 res = 0;
if (*str=='-' || *str=='+') { if (*str=='-' || *str=='+') {
@ -182,11 +186,11 @@ f64 str2num(const char* str) {
return nan(""); return nan("");
} }
if (str[0]=='0' && str[1]=='x') { 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') { } else if (str[0]=='0' && str[1]=='o') {
res = oct2f(str+2); res = oct_to_f64(str+2);
} else { } else {
res = dec2f(str); res = dec_to_f64(str);
} }
return negative? -res:res; return negative? -res:res;
} }
@ -206,7 +210,7 @@ i32 utf8_hdchk(const char head) {
return 0; return 0;
} }
std::string chrhex(const char c) { std::string char_to_hex(const char c) {
const char hextbl[] = "0123456789abcdef"; const char hextbl[] = "0123456789abcdef";
return {hextbl[(c&0xf0)>>4], hextbl[c&0x0f]}; 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) { for(auto i : str) {
// windows doesn't output unicode normally, so we output the hex // windows doesn't output unicode normally, so we output the hex
if (is_windows() && i<=0) { if (is_windows() && i<=0) {
ret += "\\x"+chrhex(i); ret += "\\x" + char_to_hex(i);
continue; continue;
} }
switch(i) { switch(i) {

View File

@ -223,7 +223,7 @@ nil_expr* parse::nil() {
number_literal* parse::num() { number_literal* parse::num() {
auto node = new number_literal(toks[ptr].loc, 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); match(tok::num);
return node; return node;
} }

View File

@ -242,7 +242,7 @@ void nas_val::clear() {
} }
f64 var::to_num() { 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() { std::string var::to_str() {

View File

@ -198,7 +198,7 @@ inline bool vm::cond(var& val) {
if (val.is_num()) { if (val.is_num()) {
return val.num(); return val.num();
} else if (val.is_str()) { } 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 std::isnan(num)? !val.str().empty():num;
} }
return false; 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_nil: ctx.top[0] = one; break;
case vm_type::vm_num: ctx.top[0] = val.num()? zero:one; break; case vm_type::vm_num: ctx.top[0] = val.num()? zero:one; break;
case vm_type::vm_str: { 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)) { if (std::isnan(num)) {
ctx.top[0] = var::num(static_cast<f64>(val.str().empty())); ctx.top[0] = var::num(static_cast<f64>(val.str().empty()));
} else { } else {

View File

@ -15,7 +15,7 @@ namespace repl {
struct info { struct info {
bool in_repl_mode = false; bool in_repl_mode = false;
std::string repl_file_name = "<nasal-repl>"; const std::string repl_file_name = "<nasal-repl>";
std::string repl_file_source = ""; std::string repl_file_source = "";
// singleton // singleton