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

✨ Subscript syntax should work for any expression that is of type array #211

Open CymDeveloppement opened 1 week ago

CymDeveloppement commented 1 week ago

Hi, we have many project of bash script, and i think amber is very cool

when i use the function directly as array, i have a parser error : But i seem is incorrect.

fun command_exist(command: Text):Bool {
    echo command
    return true
}

command_exist(split("foo bar", " ")[0])

Parser :

 ERROR 
Variable 'command_exist' does not exist
at test.ab:10:2

9 |     
10|     command_exist(split("foo bar", " ")[0])
11| 

and if i store function result in variable :

let splitText = split("foo bar", " ")[0]
command_exist(splitText)
ERROR 
1st argument 'command' of function 'command_exist' expects type 'Text', but '[Text]' was given
at test.ab:10:2

9 |     let splitText = split("foo bar", " ")[0]
10|     command_exist(splitText)

thx

Ph0enixKM commented 1 week ago

I think that the [0] doesn't work on expressions as of right now. You'd have to assign it to a variable first. This is something that has to be improved soon. Thanks for the issue! I'll add a warning in the docs so that people can see this

CymDeveloppement commented 1 week ago

for a more comprehensive code, i created a function and it works great.

fun array_value(array, index) {
    return array[index]
}

main {
    let splitText = array_value(split("foo bar", " "), 0)
    command_exist(splitText)
        command_exist(array_value(split("foobar", " "), 0))
}
Ph0enixKM commented 1 week ago

Thank you for creating this issue @CymDeveloppement