Skip to content

Commit

Permalink
Move stdlib to top-level lazy property
Browse files Browse the repository at this point in the history
When using the REPL it would otherwise be reloaded every line.
  • Loading branch information
mrjameshamilton committed Apr 17, 2022
1 parent 3b85f8c commit 336e213
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/main/kotlin/eu/jameshamilton/klox/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,8 @@ fun runPrompt() {
}
}

fun parse(code: String): Program? {
hadError = false
hadRuntimeError = false

val stdlib = readFiles("/klox/")
private val stdlib by lazy {
readFiles("/klox/")
.filter { it.fileName.name.endsWith(".lox") }
// ensure that Object.lox is loaded first since it defines the base class for all other classes
.sortedBy { it.fileName.name != "Object.lox" }
Expand All @@ -110,6 +107,11 @@ fun parse(code: String): Program? {
.map { Parser(it).parse() }
.toList()
.reduce { stdlib, lib -> stdlib + lib }
}

fun parse(code: String): Program? {
hadError = false
hadRuntimeError = false

val scanner = Scanner(code)
val tokens = scanner.scanTokens()
Expand Down

0 comments on commit 336e213

Please sign in to comment.