Skip to content

unit ScanTest

bob.junk/ScanTest.em
package bob.junk

from em$distro import BoardC

module ScanTest

private:

    var membuf: uint8[100]

    function init()
    function scan (p: uint8*): uint8

end

def em$startup()
    init()
end

def em$run()
    %%[d+]
    ^^asm ("bkpt")^^
    auto cnt = scan(&membuf[0])
    ^^asm ("bkpt")^^
    %%[d-]
    %%[>cnt]
end

def init()
    auto idx = 0
    for auto i = 0; i < 20; i++
        for auto j = 0; j < 3; j++
            membuf[idx++] = '0' + i
        end
        membuf[idx++] = ','
    end
end

def scan(p)
    auto cnt = 0
    for ; *p != 0; p += 1
        auto c = *p
        cnt += 1 if c >= <uint8>'0' && c <= <uint8>'9'
    end
    return cnt
end

#/*
    fn init() void {
        var idx: usize = 0;
        var i: u8 = 0;
        while (i < 5) : (i += 1) {
            for (0..3) |_| {
                membuf[idx] = '0' + i;
                idx += 1;
            }
            membuf[idx] = ',';
            idx += 1;
        }
    }

    fn scan(buf: [*]u8) u8 {
        var cnt: u8 = 0;
        var p = buf;
        while (p[0] != 0) : (p += 1) {
            const c = p[0];
            if (!em.std.ascii.isDigit(c)) cnt += 1;
        }
        return cnt;
    }
#*/