risor-io / risor

Fast and flexible scripting for Go developers and DevOps.
https://risor.io
Apache License 2.0
576 stars 24 forks source link

multiple variables assignment error #227

Closed runsisi closed 2 months ago

runsisi commented 2 months ago

example 1

func main() {
    ctx := context.Background()
    script := `
a, b := [2, 3]
a, b = [3, 4]
[a, b]`
    result, err := risor.Eval(ctx, script)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("result is:", result)
}
parse error: unexpected = while parsing declaration statement (expected :=)

example 2

func main() {
    ctx := context.Background()
    script := `
a, b := [2, 3]
a, b := [3, 4]
[a, b]`
    result, err := risor.Eval(ctx, script)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Println("result is:", result)
}
compile error: variable "b" already exists

i think the assignment op in the first example should be allowed?

runsisi commented 2 months ago

@myzie thanks!

is it possible risor could support blank identifier _ ?

func main() {
    x := `
    _, a := [1, 2]
    _, b := [98, 99]
    `
    _, err := risor.Eval(context.Background(), x)
    if err != nil {
        fmt.Println(err)
    }
}

atm the code above has compile error:

compile error: variable "_" already exists
myzie commented 2 months ago

Thanks for filing the bug report @runsisi.

Yeah, the _ behavior should be supported. I'll look at this.