Skip to content

AssignmentStatement, potential generation problems and fixes #3

@Mutoxicated

Description

@Mutoxicated

So due to lua's quirkiness, there are are few concerns with the assignStmt. Here they are:

Problem 1

Generation of:

a, b = b, a

in lua this would swap the variables as shown in the bytecode:

1 [1] VARARGPREP 0
--
2 [1] LOADI 0 0    ; a = 0
3 [1] LOADI 1 1    ; b = 1
4 [2] MOVE 2 1     ; c = 1
5 [2] MOVE 1 0     ; b = 0
6 [2] MOVE 0 2     ; a = c 
7 [2] RETURN 2 1 1 ; 0 out

the generator however would unroll the assignment variables into their corresponding value.

-- the generated lua code
a = b
b = a

which would NOT swap the variables.

Problem 2

Calls can correspond to multiple values and lua does not handle those very well in assignment. For instance:

a,b,c = call_with_2_return_values(), 0

a would get set to the first returned value of the call and b would get set to 0.

The original fix for this was that the generator would create temporary variables for the values and then assign them to the actual variables they were supposed to be assigned to.
E.g.

a,b,c = call_with_2_return_values(), 0 
-- the generated lua code
H0, H1 =call_with_2_return_values()
a = H0
b = H1
c = 0

Fix for both

Make the assignmentStatement and potentially also the variable declaration NEVER "unroll" and still keep the original fix for the function call.

Compound assignment statements with multiple variables could also work in that case.

Example 1

Hybroid:

a,b = b,a

Lua:

a,b = b,a

Example 2

Hybroid:

a,b += b, a

Lua:

a,b = (a+b), (b+a) -- they equal the same

Example 3

Hybroid:

a,b,c = call_with_2_return_values(), 0 

Lua:

H0, H1 = call_with_2_return_values()
a ,b, c = H0, H1, 0

Benefits

We are maximizing flexibility for our users and allowing lua's variable swap feature while simultaneously taking care of lua's downsides with the function calls under the hood.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions