update std/math

This commit is contained in:
ValKmjolnir 2023-08-03 22:55:59 +08:00
parent 5cc9824a62
commit 56a26b6ab6
5 changed files with 28 additions and 10 deletions

View File

@ -1131,7 +1131,7 @@ void codegen::ret_gen(return_expr* node) {
gen(op_ret, 0, node->get_location()); gen(op_ret, 0, node->get_location());
} }
const error& codegen::compile(parse& parse, linker& import) { const error& codegen::compile(parse& parse, linker& import, bool repl) {
init_native_function(); init_native_function();
const auto& file = import.filelist(); const auto& file = import.filelist();
file_map = {}; file_map = {};
@ -1146,9 +1146,13 @@ const error& codegen::compile(parse& parse, linker& import) {
// add special symbol arg here, which is used to store command line args // add special symbol arg here, which is used to store command line args
add_symbol("arg"); add_symbol("arg");
find_symbol(parse.tree()); // search symbols first // search global symbols first
gen(op_intg, global.size(), parse.tree()->get_location()); find_symbol(parse.tree());
block_gen(parse.tree()); // generate main block gen(op_intg, repl? STACK_DEPTH/2:global.size(), parse.tree()->get_location());
// generate main block
block_gen(parse.tree());
// generate exit operand, vm stops here
gen(op_exit, 0, parse.tree()->get_location()); gen(op_exit, 0, parse.tree()->get_location());
// size out of bound check // size out of bound check
@ -1162,10 +1166,14 @@ const error& codegen::compile(parse& parse, linker& import) {
"too many constant strings: " + "too many constant strings: " +
std::to_string(const_string_table.size())); std::to_string(const_string_table.size()));
} }
if (global.size()>=STACK_DEPTH) {
// check global variables size
if (global.size()>=STACK_DEPTH/2) {
err.err("code", err.err("code",
"too many global variables: " + std::to_string(global.size())); "too many global variables: " + std::to_string(global.size()));
} }
// check generated code size
if (code.size()>0xffffff) { if (code.size()>0xffffff) {
err.err("code", err.err("code",
"bytecode size overflow: " + std::to_string(code.size())); "bytecode size overflow: " + std::to_string(code.size()));

View File

@ -128,7 +128,7 @@ public:
public: public:
codegen() = default; codegen() = default;
const error& compile(parse&, linker&); const error& compile(parse&, linker&, bool repl = false);
void print(std::ostream&); void print(std::ostream&);
void symbol_dump(std::ostream&) const; void symbol_dump(std::ostream&) const;
}; };

View File

@ -62,6 +62,8 @@ var ln = func(x) {
} }
var _iln10 = 1/ln(10); var _iln10 = 1/ln(10);
# log10 is alias of lg
var log10 = lg; var log10 = lg;
var sqrt = func(x) { var sqrt = func(x) {
@ -96,6 +98,14 @@ var min = func(x, arg...) {
return res; return res;
} }
var avg = func(arg...) {
var x = 0;
foreach(var i;arg) {
x += i;
}
return x/size(arg);
}
var mod = func(n, m) { var mod = func(n, m) {
var x = n-int(n/m)*m; var x = n-int(n/m)*m;
return x<0? x+abs(m):x; return x<0? x+abs(m):x;

View File

@ -34,7 +34,7 @@ var hex32str=func(){
}; };
}(); }();
var _md5=func(){ var md5=func(){
var K=[ var K=[
0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501,
0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821,
@ -201,7 +201,7 @@ var md5check=func(){
"ec6d5b197ba019db23c719112f3f70b7" "ec6d5b197ba019db23c719112f3f70b7"
]; ];
forindex(var i;test_set){ forindex(var i;test_set){
var res=_md5(test_set[i]); var res=md5(test_set[i]);
if(cmp(res,result[i])) if(cmp(res,result[i]))
println( println(
"md5 cannot work:\n", "md5 cannot work:\n",

View File

@ -23,7 +23,7 @@ var compare=func() {
} }
byte+=size(s); byte+=size(s);
var res=md5(s); var res=md5(s);
if(cmp(res,md5_self._md5(s))) { if(cmp(res, md5_self.md5(s))) {
die("error: "~str(i)); die("error: "~str(i));
} }
if (i-begin-int((i-begin)/4)*4==0) { if (i-begin-int((i-begin)/4)*4==0) {
@ -74,7 +74,7 @@ var filechecksum=func(){
var f=io.readfile(files[i]); var f=io.readfile(files[i]);
var res=md5(f); var res=md5(f);
byte+=size(f); byte+=size(f);
if(cmp(res,md5_self._md5(f))){ if(cmp(res, md5_self.md5(f))){
die("error: "~files[i]); die("error: "~files[i]);
} }
print( print(