Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return values got broken in nested function calls #251

Closed
genkami opened this issue Jun 28, 2024 · 2 comments
Closed

Return values got broken in nested function calls #251

genkami opened this issue Jun 28, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@genkami
Copy link

genkami commented Jun 28, 2024

Environment:

$ amber --version
amber 0.3.3-alpha

When calling the same function more then once as an argument to another function, the return value of the function seems to get overwritten by the last function call.

Example:

fun double(n: Num): Num {
    return n * 2
}

fun add(a: Num, b: Num): Num {
    return a + b
}

echo add(double(2), double(3))
// Expected output: 4 + 6 == 10
// Actual output: 6 + 6 == 12

Compiled bash code:

#!/usr/bin/env bash
# Written in [Amber](https://amber-lang.com/)

function double__0_v0 {
    local n=$1
    __AF_double0_v0=$(echo ${n} '*' 2 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//');
    return 0
}
function add__1_v0 {
    local a=$1
    local b=$2
    __AF_add1_v0=$(echo ${a} '+' ${b} | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//');
    return 0
}
double__0_v0 2;
__AF_double0_v0__9=$__AF_double0_v0;
double__0_v0 3;
__AF_double0_v0__9=$__AF_double0_v0;
add__1_v0 $__AF_double0_v0__9 $__AF_double0_v0__9;
__AF_add1_v0__9=$__AF_add1_v0;
echo $__AF_add1_v0__9

This is because the same variable __AF_double0_v0__9 is used to store both of the function calls in this case.

@Mte90 Mte90 added the bug Something isn't working label Jun 28, 2024
@b1ek b1ek added this to the Stable release milestone Jun 28, 2024
@Ph0enixKM
Copy link
Member

This was fixed in #169

@Ph0enixKM
Copy link
Member

New Amber version will be published this week

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants