A tiny interpreter in Elm that evaluates non-negative integer constants and establishes the interpreter structure reused throughout Tiny Interpreters.
Read CONST: The Structure of a Tiny Interpreter in Elm for a guided explanation of how it works.
flowchart TD
A["123"] -->|parse| B["Program (Const 123)"]
B -->|evaluate| C["VNumber 123"]
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 CONST.Interpreter as I
I.run "123"
-- Ok (VNumber 123)CONST supports one kind of expression: a non-negative integer constant.
123The parser converts the source text into an abstract syntax tree:
Program (Const 123)The interpreter then evaluates the AST to a value:
VNumber 123CONST introduces the structure that the later interpreters build on:
source text → AST → valueAlthough the language contains only constants, the project includes the same main parts that will remain as the language grows:
- a grammar that describes valid programs
- a lexer that recognizes non-negative integer literals
- an AST that represents the program in Elm
- a parser that turns source text into an AST
- evaluation logic that turns the AST into a value
- tests that describe the behaviour of the language
CONST is the first interpreter in Tiny Interpreters, a blog about learning how programming languages work by building tiny interpreters.