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());
}
const error& codegen::compile(parse& parse, linker& import) {
const error& codegen::compile(parse& parse, linker& import, bool repl) {
init_native_function();
const auto& file = import.filelist();
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_symbol("arg");
find_symbol(parse.tree()); // search symbols first
gen(op_intg, global.size(), parse.tree()->get_location());
block_gen(parse.tree()); // generate main block
// search global symbols first
find_symbol(parse.tree());
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());
// size out of bound check
@ -1162,10 +1166,14 @@ const error& codegen::compile(parse& parse, linker& import) {
"too many constant strings: " +
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",
"too many global variables: " + std::to_string(global.size()));
}
// check generated code size
if (code.size()>0xffffff) {
err.err("code",
"bytecode size overflow: " + std::to_string(code.size()));

View File

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

View File

@ -62,6 +62,8 @@ var ln = func(x) {
}
var _iln10 = 1/ln(10);
# log10 is alias of lg
var log10 = lg;
var sqrt = func(x) {
@ -96,6 +98,14 @@ var min = func(x, arg...) {
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 x = n-int(n/m)*m;
return x<0? x+abs(m):x;

View File

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

View File

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