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

ERROR: Failed expression must be followed by a block or statement #209

Closed llagerlof closed 1 week ago

llagerlof commented 1 week ago
amber 0.3.3-alpha
EndeavourOS
bash
Running as normal user.

I saw that exists the issue 208 for the same error. I got the code below from the docs (https://docs.amber-lang.com/advanced_syntax/as_cast):

import { parse } from "std"

let a = "12"
let b = parse(a)

echo b + 12

Another thing (or related?), I am not sure about the std import though. Do I need to download some std file or it is already embedded in Amber executable?

Ph0enixKM commented 1 week ago

std is embedded into the Amber executable. This weird documentation error should be fixed. You can handle it this way:

import { parse } from "std"

let a = "12"
let b = parse(a) failed {
    echo "Variable `a` is not a number."
}

echo b + 12
Ph0enixKM commented 1 week ago

@llagerlof fixed: https://docs.amber-lang.com/advanced_syntax/as_cast

llagerlof commented 1 week ago

@Ph0enixKM Thank you for the update. I have some notes to share.

Some notes:

After using failed, I received a message indicating that I needed to install bc. I installed it, and now it's working. It's strange that the lang didn't show this dependency error before adding the failed to the code.

A question:

I'm not convinced I fully understood failed. According to the docs:

Failed - the recommended way to handle failing, enabling you to write specific logic to run when a command fails.

Assuming failed is for exception handling (although I could be wrong), when I run the new code (the example you provided above), the output is 12. If I don't use failed, that error occurs. I mean, if the parse isn't failing, why doesn't it work without using failed?

Ph0enixKM commented 1 week ago

If I don't use failed, that error occurs. I mean, if the parse isn't failing, why doesn't it work without using failed?

The function doesn't know what if the bash command invoked inside will fail or not. Hence we have to handle the potential failure. The parse function is a failing function (something can fail in the execution process) so we have to potentially handle the failure even if we know it's not going to fail. For the times when you know that it's not going to fail - you can use unsafe keyword before the function call. This however results currently in undefined behaviour and is fixed in this PR: https://github.com/Ph0enixKM/Amber/pull/203. Stay tuned for that fix.

llagerlof commented 1 week ago

Thank you.

I have one suggestion (I don't want to add this as a new "suggestion issue", isn't so important):

When the code is using a failing function without failed, the error message could be clearer.

Instead:

[user@pc test]$ amber program1.ab
[src/modules/condition/failed.rs:64:21] "FAILED" = "FAILED"
 ERROR
Failed expression must be followed by a block or statement
at program1.ab:6:1

Could be:

[user@pc test]$ amber program1.ab
 ERROR
The function 'parse' requires a 'failed' block or statement to handle errors
at program1.ab:6:1

But it's just a detail.

Ph0enixKM commented 1 week ago

@llagerlof That's definitely a good point. I think that you can indeed create an issue for that!