Amber icon indicating copy to clipboard operation
Amber copied to clipboard

Remove sed for condition

Open Mte90 opened this issue 1 year ago • 10 comments

Ref: #366

In this way we are configuring the scale we want to approximate.

Right now some tests doens't work as we have again issues on bash generation:

function foo__0_v0 {
    local a=$1
    local b=$2
    eval "${a}=$(
        eval "echo "scale=0
        ${!a}/${b}" | bc -l"
    )"
}

Instead it should be:

function foo__0_v0 {
    local a=$1
    local b=$2
    eval "${a}=$(
        eval "echo \"scale=0
        ${!a}/${b}\" | bc -l"
    )"
}

As you can see the eco inside the second eval is not escaped. I guess the issue is there https://github.com/amber-lang/amber/blob/master/src/modules/shorthand/add.rs#L68

But it is a similar issue of https://github.com/amber-lang/amber/pull/376 with missing/wrong escaping.

Mte90 avatar Jul 30 '24 15:07 Mte90

@Mte90 but the scale parameter gets rid of the fraction part in the number

Ph0enixKM avatar Aug 08 '24 16:08 Ph0enixKM

@Mte90 but the scale parameter gets rid of the fraction part in the number

If you see with the tests everything works in this way. We have issues in some cases because:

#!/usr/bin/env bash
# Written in [Amber](https://amber-lang.com/)
# version: 0.3.4-alpha
# date: 2024-08-09 11:26:27
function foo__0_v0 {
    local a=$1
    local b=$2
    eval "${a}=$(
        eval "echo "scale=0
        ${!a}/${b}" | bc -l"
    )"
}
__0_a=15
foo__0_v0 __0_a 3
__AF_foo0_v0__9_1="$__AF_foo0_v0"
echo "$__AF_foo0_v0__9_1" >/dev/null 2>&1
echo ${__0_a}

The echo command is not escaped rightly and the bash script fails. That is part of the other PR as documented. If some cases are not covered for fraction we have to create more tests.

Mte90 avatar Aug 09 '24 09:08 Mte90

@Mte90 there are some tests that were added but are not on this branch. Can you git pull origin master?

Ph0enixKM avatar Aug 13 '24 13:08 Ph0enixKM

If I get internet access sure.

Mte90 avatar Aug 13 '24 14:08 Mte90

So right now there are all the tests, the problem is that there is a wrong escaping of variables in the generated bash so doesn't work.

I wrote about it on https://github.com/amber-lang/amber/pull/381#issuecomment-2277546441

Mte90 avatar Sep 19 '24 08:09 Mte90

@Mte90 This PR has many tests failed

Ph0enixKM avatar Sep 27 '24 17:09 Ph0enixKM

@Ph0enixKM yeah I wrote on https://github.com/amber-lang/amber/pull/381#issuecomment-2360407534 why there are issues that depends on another bug

Mte90 avatar Sep 27 '24 20:09 Mte90

#!/usr/bin/env bash
# Written in [Amber](https://amber-lang.com/)
# version: 0.3.4-alpha
# date: 2024-08-09 11:26:27

function foo__0_v0 {
    local a=$1
    local b=$2
    eval "${a}=$(
        eval "echo "scale=0
        ${!a}/${b}" | bc -l"
    )"
}
__0_a=15
foo__0_v0 __0_a 3
__AF_foo0_v0__9_1="$__AF_foo0_v0"
echo "$__AF_foo0_v0__9_1" >/dev/null 2>&1
echo ${__0_a}

@mte90 Can you provide the Amber code that you used to generate this Bash code? Without it, it'll be hard to decipher what went wrong.

Ph0enixKM avatar Oct 01 '24 08:10 Ph0enixKM

If you try to compile the ./src/tests/validity/variable_ref_div_arithmetic_num.ab test you can see the escaping issue.

I improved the code, now the sed trailing zero there is only for the division in this way the code generated is more clean.

fun foo(ref a) {
    a += 12
}

let a = 24
foo(a)
echo a
foo__0_v0() {
    local a=$1
    local b=$2
    eval "${a}=$(
        eval "echo "scale=3
        ${!a}/${b}" | bc -l| sed "/\./ s/\.\{0,1\}0\{1,\}$//""
    )"
}
__0_a=15
foo__0_v0 __0_a 3
__AF_foo0_v0__9_1="$__AF_foo0_v0"
echo "$__AF_foo0_v0__9_1" >/dev/null 2>&1
echo ${__0_a}

Mte90 avatar Oct 01 '24 10:10 Mte90

This PR has various issues.

The big one that doesn't let me to finish and do all the tests and verify everything is that the bash generated has errors in the escaping as I wrote here https://github.com/amber-lang/amber/pull/381#issuecomment-2385445358

Until that situation is not fixed in Amber itself I can't do all the checks that are need to move on.

Mte90 avatar Oct 04 '24 08:10 Mte90

@Mte90 What does this PR actually do? Do you want to remove the usage of sed inside of relation operators like >, <, >=, <=, ==, != ? There was no issue created to explain the problem that this PR was trying to solve. Could you explain it here?

After reading the description I see that you want to fix some escaping problems in Bash. I believe that would be better solved when we implement the TranslationModules

Ph0enixKM avatar Oct 28 '24 13:10 Ph0enixKM

The plan of this PR is to remove the sed usage to escape strings to number as can be optimized.

Right now as it is the PR it is needed sed only for the modulo operation so it means that in a lot of operation the Bash generated is better. For the escaping issue yeah is a long story.

Mte90 avatar Oct 28 '24 13:10 Mte90

@Mte90 I think that this PR might be closed once we implement the new numeric system (one that does not use bc)

Ph0enixKM avatar Nov 05 '24 11:11 Ph0enixKM

I'm closing this PR for until we introduce better numerical system

Ph0enixKM avatar Dec 20 '24 18:12 Ph0enixKM