gregtour / duck-lang

The Duck Programming Language
http://www.ducklang.org
109 stars 13 forks source link

return in while broken #22

Closed WebFreak001 closed 9 years ago

WebFreak001 commented 9 years ago

this code should actually just output hello:

function foo()
    while(true) do
        return "hello"
    loop
end

duck.println(foo())

Because return breaks out of while loops but in here it doesn't. It should return "hello" right at the first iteration but its being stuck there.

WebFreak001 commented 9 years ago

Update: break in while seems to work just fine

function foo()
    while(true) do
        break
    loop
    return "hello"
end

duck.println(foo())

Maybe add same functionality to return?

gregtour commented 9 years ago

Good catch.

WebFreak001 commented 9 years ago

happened when i was writing an array length function in duck

WebFreak001 commented 9 years ago

Maybe adding more unittests will prevent this?

gregtour commented 9 years ago

Tests would be great. We have a framework for testing, but we don't have any unit tests currently.

I fixed this issue in the latest revision: https://github.com/gregtour/duck-lang/commit/6182733936279ec5f7e00709882ea6bce43a4beb

gregtour commented 9 years ago

As a heads up, automated unit tests have been added between e637ca9 and ece6389.

See: https://github.com/gregtour/duck-lang/blob/master/test/testcases.txt

If you find any more bugs or issues please report them and once fixed we will provide the proper tests to prevent regression.

WebFreak001 commented 9 years ago

I think it would be also useful to have embedded unittests in duck code like this:

unittest
    a = 5
    b = 4
    assert(a + b == 9, "Invalid integer addition")
end

They should be like functions that cant be called manually, but instead get called before everything else when running with a unittest flag or something like that.

gregtour commented 9 years ago

That's fine, but it's possible they might not be tested if the interpreter fails.

In any case, it's possible to use an assert function to quit with an error and still set up tests in this way.