amber-lang / amber

💎 Amber the programming language compiled to Bash
https://amber-lang.com
GNU General Public License v3.0
3.81k stars 81 forks source link

[BUG] function with no return generate useless bash code #371

Open Mte90 opened 1 month ago

Mte90 commented 1 month ago

Take this code:

fun something() {
    echo "yes"
}

something()

It will generated:

#!/usr/bin/env bash
# Written in [Amber](https://amber-lang.com/)
# version: 0.3.4-alpha
# date: 2024-07-30 12:41:18
function something__0_v0 {
    echo "yes"
}
something__0_v0
__AF_something0_v0__5_1="$__AF_something0_v0"
echo "$__AF_something0_v0__5_1" >/dev/null 2>&1

As we can see the function doesn't have any return so the last 2 lines are not needed.

Mte90 commented 1 month ago

The code involved is https://github.com/amber-lang/amber/blob/435d91c4aabce2bd016db77313b221eec38a6a65/src/modules/function/invocation.rs#L158

Basically print that also if the function doesn't have any return.

b1ek commented 1 month ago

i dont know if we can do this without breaking apart the whole compiler. its kind of like google translate, really

Mte90 commented 1 month ago

Yeah this is something for @Ph0enixKM that knows how works.

Ph0enixKM commented 1 month ago

I think that this is the moment when we should add that optimization I explained before:

https://www.youtube.com/watch?v=_caXIMxrYj8&feature=youtu.be

This would solve this issue too.