Back to Blog

Writing zorLang, a Tree Walk Interpreter in Java

Programming LanguagesJavaInterpretersCompilers

Documentation Site: https://zor-lang.vercel.app/

Github Link: https://github.com/sarbagyag/zorLang.git

Before I tried building a virtual machine in C, I wanted to understand interpreters in their simplest form, so I built zorLang as a tree walk interpreter in Java. The goal was never speed. The goal was clarity, and Java's structure made that easier to reason about while I was still learning how all the pieces fit together.

Parsing Into a Tree

I wrote a recursive descent parser by hand that scans and tokenizes source code before building it into a strongly typed abstract syntax tree. From there, the interpreter walks that tree using the visitor pattern, where each node type routes to its own evaluation method. Keeping tree traversal separate from the logic that actually evaluates each node made the whole system far easier to extend later without breaking anything that already worked.

tree-walk

Scope, Blocks and Objects

Variables live inside a hierarchical environment structure, so nested blocks, dynamic typing and closures all fall out of the same underlying scoping model rather than needing separate special cases. Control flow constructs like if, else, while and for all plug into this same structure. On top of that sits a proper object oriented runtime, complete with constructors, instance properties and dynamic method binding, backed by error reporting detailed enough that I could actually debug my own test programs.

What It Taught Me

zorLang will probably never be fast, and that was never the point. What it gave me instead was an intuition for how languages actually work underneath their syntax, an intuition that made every later project, from ghoLang onward, feel far less mysterious than it would have otherwise.