A tiny interpreter in Elm that adds variable expressions and environments, showing how variable lookup makes evaluation depend on context.
VAR builds on IF by allowing programs to refer to predefined values by name.
Read VAR: Adding Variables and Environments to a Tiny Interpreter in Elm for a guided explanation of how it works.
flowchart TD
A["x"] -->|parse| B["Program (Var #quot;x#quot;)"]
B -->|evaluate| C["VNumber 10"]
You'll need Nix with flakes enabled.
Enter the development environment and start the Elm REPL:
nix develop
elm replImport the interpreter and run a program:
import VAR.Interpreter as I
I.run "x"
-- Ok (VNumber 10)VAR supports the constants, difference expressions, zero? expressions, and conditional expressions introduced by the previous interpreters.
It also adds variable expressions. An identifier contains one or more lowercase letters:
x
value
onetwothreeThe words if, then, and else are reserved and cannot be used as variable names.
Variables can also appear inside larger expressions:
I.run "if zero?(-(5, v)) then i else v"
-- Ok (VNumber 1)The AST for a variable expression stores the name being referenced:
Var "x"It does not store the value associated with that name. The evaluator finds the value by looking up the name in an environment.
VAR evaluates programs using this initial environment:
x ↦ VNumber 10
v ↦ VNumber 5
i ↦ VNumber 1Evaluating Var "x" looks up x and returns VNumber 10.
A valid identifier that is not present in the environment produces an identifier-not-found runtime error.
Only variable expressions inspect the environment directly, but the evaluator passes the environment through every recursive call so that variables can appear anywhere an expression is expected.
VAR does not extend the environment during evaluation. Programs can refer to predefined names, but they cannot introduce new names themselves.
VAR is part of Tiny Interpreters, where we learn how programming languages work by building tiny interpreters.