Amber icon indicating copy to clipboard operation
Amber copied to clipboard

[Feature] Loop used with range should compile to for loop

Open daoanhthanh opened this issue 1 year ago • 3 comments

Hi, I read your documentation and noticed that one of Amber's features is adding Shebang at the top of the compiled file.

However, in my case, it does not work.

My .ab file:

let target = "my friend"

let message = "Hello, {target}! Want some Shawarma?"
let repeated_times = 3

loop i in 0..repeated_times {
    echo message
}

and my .sh file generated by amber input.ab output.sh

__0_target="my friend";
__1_message="Hello, ${__0_target}! Want some Shawarma?";
__2_repeated_times=3;
for i in $(seq 0 $(echo ${__2_repeated_times} '-' 1 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//'))
do
    echo "${__1_message}"
done

Notice that there is no shebang at the top.

Is this a bug, or did I miss some information here?

daoanhthanh avatar May 28 '24 06:05 daoanhthanh

Also, I think the syntax for the for-loop is not optimized, since it relies on the external command (bc). Why don't you use something like:

for ((i=0; i<repeated_times; i++)); do
...

daoanhthanh avatar May 28 '24 06:05 daoanhthanh

@daoanhthanh this is fixed in https://github.com/Ph0enixKM/Amber/pull/107. Stay tuned. The optimisation features will come soon. I'll rename this issue title to be focused on the for-loop optimisation.

Ph0enixKM avatar May 28 '24 08:05 Ph0enixKM

even tho this is fixed since #146, i still made #469 which will fix the for loop being weird

b1ek avatar Sep 07 '24 15:09 b1ek