Skip to content

Commit

Permalink
evaluator: optimize the cost of defining a variable in a scope
Browse files Browse the repository at this point in the history
  • Loading branch information
azenla committed Sep 5, 2023
1 parent 63a90a5 commit 290d8d0
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 20 deletions.

This file was deleted.

5 changes: 3 additions & 2 deletions evaluator/src/main/kotlin/gay/pizza/pork/evaluator/Scope.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ class Scope(val parent: Scope? = null, inherits: List<Scope> = emptyList()) {
private val variables = mutableMapOf<String, Any>()

fun define(name: String, value: Any) {
if (variables.containsKey(name)) {
val previous = variables.put(name, value)
if (previous != null) {
variables[name] = previous
throw RuntimeException("Variable '${name}' is already defined")
}
variables[name] = value
}

fun value(name: String): Any {
Expand Down

0 comments on commit 290d8d0

Please sign in to comment.