Lesson 15 of 15

Complete Mini-Language

Bringing It All Together

We have built every piece of an interpreter. Now let's combine them into a complete mini-language with while loops and string support.

Adding While Loops

A while loop repeatedly evaluates its body as long as its condition is truthy:

while condition do body;

For example:

let i = 0;
while i < 5 do let i = i + 1;
print i;

AST Node

{ type: "WhileStatement", condition: <expr>, body: <statement> }

Parsing While

if (this.peek().value === "while") {
    this.advance(); // consume 'while'
    const condition = this.parseExpr();
    this.advance(); // consume 'do'
    const body = this.parseStatement();
    return { type: "WhileStatement", condition, body };
}

Evaluating While

if (node.type === "WhileStatement") {
    while (evaluate(node.condition, env) !== 0) {
        evaluate(node.body, env);
    }
    return;
}

String Support

We also add string literals and string concatenation with +:

let greeting = "hello";
let name = "world";
print greeting + " " + name;

Your Task

Build the complete interpreter with: variables, functions, if/else, while loops, strings, comparisons, built-in functions, and error handling. This is the capstone -- your mini-language in action!

Node.js loading...
Loading...
Click "Run" to execute your code.