KLox

A Kotlin interpreter for The Lox Programming Language.

Overview

KLox is Tree-Walk Interpreter – Directly executes the resulting AST after applying some semantic analysis – for the Lox Programming Language.

About Lox

Lox is a High-Level, Object Oriented, Dynamic Scripting language with Higher-Order Functions support, Designed by Bob Nystrom.

Implementation Details

KLox is immensly inspired by Nystorm's Crafting Interpreters Java Implementation. With the necessary improvements, simplicity, and advantages that Kotlin, and Functioanl Programming provide.

After thoughts and comments

What makes this implementation different than other Kotlin implementations (aside from my messy git log), is that it doesn't stick to the book in terms of design patterns; I tried to embrace functional Kotlin in this project, yet (unfortunately) I've not even come close to pure Functional Programming principles. The thing is, having mutable global state floating around each pass in the compiler is extremely inconvenient to maintain/debug.

In the broken_immutable_state branch you'll find my obviously-broken attempt at immutable state with Kotlin's implementation of persistent data structures. But without implicit parameters support it was really painful to implement.

However, I didn't completely give up on FP with Kotlin; the Lexer, Parser, Resolver, and Interpreter are all functions. Recursion and pattern-matching (as limited as kotlin's support for it) are heavily used, and I plan to refactor the entire project to render it as functional as possible.

I did consider using ΛRROW , but so far it feels to me like a language inside the language. Perhaps Kotlin is not ready yet to be a pure functional language (on it's own)?


Project on GitHub

Muhammad Hashim