objeck / objeck-lang

Objeck is a modern object-oriented programming language with functional features tailored for machine learning. It emphasizes expression, simplicity, portability, and scalability. The programming environment consists of a compiler, virtual machine, REPL shell, and command line debugger with IDE plugins.
https://objeck.org
Other
154 stars 11 forks source link

Bug in code for Collections 'Filter' methods #497

Closed objeck closed 4 months ago

objeck commented 4 months ago

In the 'Bar()' method, the code crashed due to an invalid 'POP_INT' instruction. This seems to be caused by a redundant call to 'RogueReturn()'. Testing fix, code below....

use Collection;

class Test {
    function : Main(args : String[]) ~ Nil {
        values := Vector->New()<IntRef>;
        each(i : 10) {
            values->AddBack(i);
        };

        filtered := Foo(values, Filter(IntRef) ~ Bool);
        each(value in filtered) {
            value->PrintLine();
        };

        filtered := Bar(values, Filter(IntRef) ~ Bool);
        each(value in filtered) {
            value->PrintLine();
        };
    }

    function : Foo(values : Vector<IntRef>, func : (IntRef) ~ Bool) ~ Vector<IntRef> {
        filtered := Vector->New()<IntRef>;

        each(value in values) {
            result := func(value);
            if(result) {
                filtered->AddBack(value);
            };
        };

        return filtered;
    }

    function : Bar(values : Vector<IntRef>, func : (IntRef) ~ Bool) ~ Vector<IntRef> {
        filtered := Vector->New()<IntRef>;

        each(value in values) {
            if(func(value)) {
                filtered->AddBack(value);
            };
        };

        return filtered;
    }

    function : Filter(value : IntRef) ~ Bool {
        if(value->Get() < 4) {
            return true;
        };

        return false;
    }
}
objeck commented 4 months ago

Fixed in last push