Thursday | 21 NOV 2024
[ previous ]
[ next ]

The Template Language - LOOP

Title:
Date: 2023-06-19

I've now finished the implementation of the LOOP command which completes the last command that I wanted functioning in my template engine. This was relatively straightforward after the IF statement. This involved a similar chunk of code, I process the entire loop block and then split it up into a block that runs before the conditional, and another block that runs after the conditional.

I also implemented the LOOP UNTIL single line version of the LOOP statement which I think fell out of the design nicely.

The code is still rough but it works. I could now rewrite the FOR loop as a version of the LOOP but I'll leave it as is for now. I may want to do some unrolling of loops in the future but for now this is all fine.

I can now handle the below template:

{{ X = 0 }}
{{ LOOP }}
    {{ X = X + 1 }}
{{ UNTIL X > 10 DO }}
    {{ Y = 1 }}
    {{ LOOP UNTIL Y > 3 DO }}
        {{ Y = Y + 1 }}
        {{ X }} - {{ Y }}
    {{ REPEAT }}
{{ REPEAT }}

I need to fix up my OPEN and READ statements so that they work with the else clause as that will be useful. It might also be worth it to implement the block version of the commands.

I've also realized that using the MATCH statement or a regex engine might be worth it. {Specifying each of the statements as a regex} [Future goal] with capture groups would get rid of my janky string munging that I'm currently doing.