L1VM - loop macros

I wrote a new include file: “loops.l1h”:

>> loops.l1h
>> loops definitions
>> needs f and one to be defined by set
>>
>> thread safe macros, which set the f variable as argument:
#func thr-for-each-in (PVAR, X, SIZE, F) :(33 PVAR SIZE 0 intr0)@#((SIZE zero +) SIZE =)@#(for-loop)@#(((X SIZE <) F =) F for)
#func thr-for (X, M, OP, F) :(for-loop)@#(((X M OP) F =) F for)
#func thr-while (X, M, OP, F) :(((X M OP) F =) F while)

#func for-each-in (PVAR, X, SIZE) :(33 PVAR SIZE 0 intr0)@#((SIZE zero +) SIZE =)@#(for-loop)@#(((X SIZE <) f =) f for)
#func next-in (I, X, TYPE) :((I one +) I =)@#{X = (I * TYPE)}@#(next)
#func for (X, M, OP) :(for-loop)@#(((X M OP) f =) f for)
#func while (X, M, OP) :(((X M OP) f =) f while)

Update: now with thread safe macros! It has some loop definitions which make them more easy to write. So you had to write this before:

(for-loop)
(((i max <) f =) f for)

Now you can use this:

for (i, max, <)

You just need to define “f” and “one” variables to use the macros.

Here is a full example:

// hello-for-macro.l1com - Hello world! for loop
//
#include <intr.l1h>
#include <vartypes.l1h>
#include <loops.l1h>
(main func)
    (set int64 1 zero 0)
    (set int64 1 one 1)
    (set int64 1 loop 0)
    (set int64 1 loop_foo 0)
    (set int64 1 maxloop 10)
    (set int64 1 f 0)
    (set int64 6 array 24, 12, 7, 9, 32, 7)
    (set const-int64 1 Parray 0)
    (set int64 1 ind 0)
    (set int64 1 realind 0)
    (set int64 1 a 0)
    (set int64 1 asize 0)
    // set string length automatic, by setting "s"
    (set string s hello "Hello world!")
    (set string s foo "foobar!")
    (array Parray pointer)
    // print string
    // for
    (zero loop =)
    for (loop, maxloop, <)
        print_lns (hello)
        (zero loop_foo =)
        for (loop_foo, maxloop, <)
            print_lns (foo)
            ((loop_foo one +) loop_foo =)
        (next)
        ((loop one +) loop =)
    (next)

    // array print
    for-each-in (Parray, realind, asize)
        (array [ realind ] a =)
        print_lni (a)
    next-in (ind, realind, int64_type)

    print_n
    (zero ind =)
    (zero realind =)
    thr-for-each-in (Parray, realind, asize, f)
        (array [ realind ] a =)
        print_lni (a)
    next-in (ind, realind, int64_type)

    exit (zero)
(funcend)