abs-lang / abs

Home of the ABS programming language: the joy of shell scripting.
https://www.abs-lang.org
MIT License
516 stars 35 forks source link

A difference between while and for #464

Open s5unty opened 2 years ago

s5unty commented 2 years ago
#!/usr/bin/env abs

f foo() {
    for t=2; t>0; t=t-1 {
        if arg(2) != "ok" {
            sleep(100)
            continue
        }
        return [0, "ok"]
    }
    return [1, "ng"]
}

f bar() {
    t = 2
    while t > 0 {
        t = t - 1
        if arg(2) != "ok" {
            sleep(100)
            continue
        }
        return [0, "ok"]
    }
    return [1, "ng"]
}

a, b = foo()
echo("foo(%s): %s", a, b)

x, y = bar()
echo("bar(%s): %s", x, y)
% ./bug.abs ok
foo(0): ok
bar(1): ng

% ./bug.abs ng
foo(1): ng
ERROR: identifier not found: x
    [33:21] echo("bar(%s): %s", x, y)
AndrewSav commented 1 year ago

continue does not have a special meaning for while loops that it has for for loops, therefore, in bar continue simply terminates the function.