🐛 fix bug in special argument "arg"

if defining a parameter named "arg", this will cause
error value of the last local variable of this function
This commit is contained in:
ValKmjolnir 2023-07-11 19:09:33 +08:00
parent 94b6e84693
commit 4a7a2ce11e
2 changed files with 16 additions and 1 deletions

View File

@ -219,7 +219,12 @@ void codegen::func_gen(function* node) {
// then the arg is [2, 3], because 1 is accepted by "a"
// so in fact "f" is the same as:
// var f = func(a, arg...) {return(arg)}
add_symbol("arg");
auto arg = std::string("arg");
// this is used to avoid confliction with defined parameter
while(local_find(arg)>=0) {
arg = "0" + arg;
}
add_symbol(arg);
in_iterloop.push(0);
block_gen(block);

View File

@ -22,6 +22,16 @@ var f = func() {
f(1, 2, 3);
var a = func(arg, b) {
println(arg, " ", b);
}
var b = func(a) {
println(a, " ", arg);
}
a(1, 2, 3, 4); # 1 2
b(1, 2, 3, 4); # 1 [2 3 4]
# command line arguments
println(arg);
println(globals.arg);