Yet Another Useless Language Part 7 — Loop

This is the seventh part of the YAUL series. For your convenience you can find other parts in the table of contents in Part 1 — Introduction

Last time we implemented conditional expression. Today we are going to implement another basic imperative concept — loop.

Table of Contents


We are going to handle while loop. We will also handle common keywords allowing to control execution: break, continue, and goto.


As usual, we start with parser. First, loop:

Looks almost the same as if code. We simply check condition and execute block of code. Let’s see what is exactly the block_or_statement:

The code should be obvious. We simply handle list of statements, where each statement is one of language’s instructions. Let’s now handle keywords for controlling loop:

Looks pretty easy, just a keyword with semicolon at the end. Let’s now see the labels and goto:

Once again, nothing special.


Let’s dig into C# code. First, loop:

First, before visiting the body we create instructions for break and continue instructions. We add them to visitor, so it knows which instructions use to control the loop. Next, we create a loop — it consists of expression for checking condition, expression for loop’s block, and expressions for controlling the loop. Check MSDN for more details. We also need to handle labels in loop’s body.

Let’s now see blocks of code:

No magic here. We simply iterate over statements and collect them into one block. Statement is:

Well, it is very simple. Statements are just particular instructions and they are handled somewhere else, in implementations.

OK, let’s now see control keywords:

We simply extract most recent instructions from visitor or throw exceptions if they do not exist.

And now the last part, gotos:

Since LINQ handles labels and jumps, this is pretty straightforward. We simply create label, store it in visitor, and jump to it.


We are now able to handle loops. Next time we will handle another big thing: functions.