amber-lang / amber

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

[Bug] Function calls fail when the called function is defined later #142

Open arapower opened 4 months ago

arapower commented 4 months ago

The following code produces an error:

fun call(key){
    if key == "some" {
        return some()
    } else {
        return other()
    }
}

fun some(){
    echo "SOME"
    return call("END")
}

fun other(){
    echo "OTHER"
    return call("END")
}

main {
    let keyword = "some"
    echo call(keyword)
}
$ amber ./sample.ab
 ERROR 
Function 'some' does not exist
at ./sample.ab:3:16

2|     if key == "some" {
3|         return some()
4|     } else {

It seems that when the function call tries to call functions some and other, which are defined later, an error occurs. If the called function is not defined before the calling function, it results in an error. This prevents writing recursive processes across multiple functions.

arapower commented 4 months ago

Related: #83