Ph0enixKM / Amber

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

Conditional checks on Boolean value #236

Open s3than opened 6 days ago

s3than commented 6 days ago

When creating a conditional on a boolean value the resulting bash returns the following

[: !=: unary operator expected

Boolean conditional

    if result == false {
      echo "result is false"
    }

The resulting bash shows as

if [ $(echo ${result} '==' 0 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
            echo "result is false"
fi

If the function was rewritten as follows

if [ $(echo ${result:-0} '==' 0 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//') != 0 ]; then
            echo "result is false"
fi

Same for true

if [ "$(echo "${result:-0}" '==' 1 | bc -l | sed '/\./ s/\.\{0,1\}0\{1,\}$//')" != 0 ]; then
            echo "result is true"
fi

Additionally the following would be good to have as an option

    if not result {
      echo "result is false"
    }

and

    if result {
      echo "result is true"
    }

This only appears to affect Boolean checks

b1ek commented 5 days ago

it could be conditionally generated for boolean types like this

if [ "$value" != "0" ]; then ... fi

instead of all that bc and sed stuff

Ph0enixKM commented 2 days ago

This is also an optimisation (for bash code readability) issue as well

Ph0enixKM commented 2 days ago

Compiler wise - I don't see any error. What's the problem here? It's not a bug. Am I missing something?

Screenshot 2024-06-27 at 11 43 42