dictu-lang / Dictu

Dictu is a high-level dynamically typed, multi-paradigm, interpreted programming language.
https://dictu-lang.com
MIT License
267 stars 53 forks source link

[BUG] Local variable in optional argument assignment #742

Open RevengerWizard opened 3 months ago

RevengerWizard commented 3 months ago

Is there an existing issue for this?

Current Behavior

Given this function declaration with various optional arguments:

def call(text, x=0, y=0, r=0, sx=15, sy=sx, ox=0, oy=0)
{
    print(text, x, y, r, sx, sy, ox, oy);
}

call(1, 2);

We can see that the optional argument sy changed unexpectedly:

1  <-  text ok
2  <-  x ok
0  <-  y ok
0  <-  r ok
1  <-  sx ok
0  <-  sy wrong, it changed to 0?
0  <-  ok
0  <-  ok

As far as I can tell it could have something to do to with the assignment to a local in the optional sy=sx. Calling with just 1 as argument doesn't modify the other parameters, the problem seems to start with more arguments given.

Expected Behavior

It should probably let us use safely locals.

The same example in Python directly throws an error telling us name 'sx' is not defined, but optional arguments in Python are already weird to deal with.

On the other hand, Javascript gives us the expected values:

function call(text, x=0, y=0, r=0, sx=1, sy=sx, ox=0, oy=0) {
     console.log(text, x, y, r, sx, sy, ox, oy);
}

call(1, 2, 3)
// 1 2 3 0 1 1 0 0

call(1, 2, 3, 4, 5)
// 1 2 3 4 5 5 0 0

Steps To Reproduce

No response

Anything else?

The handling for optional arguments happens in the opcode DEFINE_OPTIONAL in the function declaration, not sure if it can help or if there might be a fix

Jason2605 commented 1 week ago

This is an interesting one.. Yeah I think if you'd asked me what I'd expect to happen I would say the Python one would be correct in that sx hasn't been defined - this is the route I think we should probably go here too