Rudimentary for loop

A very basic for loop has been committed.

for value in range(2,6) {
    trace(value)
}
//Output: 2345

Syntactic transformation

This is the first high-level feature which is implemented strictly at the typing level. The underlying IR code does not understand a for loop, receiving only a normal for loop. The above code is rewritten as below.

var iter = range(2,6)
loop iter.is_more() ? {
    var value = iter.get()
    iter.step()
    do {
        trace( value )
    }
}

Range implementation

There is no fundamental iterator, the range function will be provided in the standard library. Naturally an iterator for arrays will also be made standard. It may require changing the code transformation since one would want to iterator an array without a decorator. In any case, here is the code to define range.

class range_iter {
    var begin : integer
    var end : integer
    var cur : integer

    func construct = -> member {
    }

    func is_more = -> ( : boolean ) member {
        return cur < end
    }

    func step = -> member {
        cur = cur + 1
    }

    func get = -> ( : integer ) member {
        return cur
    }
}
func range = ( begin : integer, end : integer ) -> ( ri : range_iter ) {
    ri.begin = begin
    ri.end = end
    ri.cur = begin
}

Problems

The major obstacles encountered had nothing to do with loops at all. The first one was a parsing issue. Instead of continuing to extend the tree parser to have special declarator handling, it instead parses into lists of values now. It is a significant change, but perhaps not so interesting.

The second, and more problematic, obstacle was the building of flow blocks. The demo code above had a structure which was not actually supported yet: the condition expression had a defer block. This brought up an interesting problem of elevating a variable out of a block. Most of the work was done at this level. Several more tests were create which exhibited related errors, and they were all corrected. I can still think of some that will fail and I will have to come back to this issue.

The next step is to get the range function working from a shared library. When I wrote the export system I put a big TODO about classes.