Skip to content

Commit

Permalink
removing unused code from Bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
almibe committed May 2, 2023
1 parent d74b2a6 commit 756073a
Showing 1 changed file with 6 additions and 63 deletions.
69 changes: 6 additions & 63 deletions wander/shared/src/main/scala/dev/ligature/wander/Bindings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,92 +8,35 @@ import dev.ligature.wander.parser.{Name, ScriptError, WanderValue}
import dev.ligature.wander.parser.FunctionDefinition
import dev.ligature.wander.parser.NativeFunction

case class Scope(
variables: Map[Name, WanderValue],
functions: Map[Name, List[FunctionDefinition]]
)
case class Scope(variables: Map[Name, WanderValue])

case class Bindings(scopes: List[Scope] = List(Scope(Map(), Map()))) {
case class Bindings(scopes: List[Scope] = List(Scope(Map()))) {
def newScope(): Bindings =
Bindings(this.scopes.appended(Scope(Map(), Map())))
Bindings(this.scopes.appended(Scope(Map())))

def bindVariable(
name: Name,
wanderValue: WanderValue
): Either[ScriptError, Bindings] = {
val currentScope = this.scopes.last
if (
currentScope.variables
.contains(name) || currentScope.functions.contains(name)
) {
if (currentScope.variables.contains(name)) {
//TODO probably remove this to allow shadowing?
Left(ScriptError(s"$name is already bound in current scope."))
} else {
val newVariables = currentScope.variables + (name -> wanderValue)
val oldScope = this.scopes.dropRight(1)
Right(
Bindings(oldScope.appended(Scope(newVariables, currentScope.functions)))
Bindings(oldScope.appended(Scope(newVariables)))
)
}
}

def bindFunction(
name: Name,
functionDefinition: FunctionDefinition
): Either[ScriptError, Bindings] = {
val currentScope = this.scopes.last
if (
currentScope.variables
.contains(name) || duplicateFunction(name, functionDefinition)
) {
Left(ScriptError(s"$name is already bound in current scope."))
} else {
if (currentScope.functions.contains(name)) {
val newFunctionList =
currentScope.functions(name).appended(functionDefinition)
val newFunctions = currentScope.functions.updated(name, newFunctionList)
val oldScope = this.scopes.dropRight(1)
Right(
Bindings(
oldScope.appended(Scope(currentScope.variables, newFunctions))
)
)
} else {
val newFunctions =
currentScope.functions.updated(name, List(functionDefinition))
val oldScope = this.scopes.dropRight(1)
Right(
Bindings(
oldScope.appended(Scope(currentScope.variables, newFunctions))
)
)
}
}
}

private def duplicateFunction(
name: Name,
functionDefinition: FunctionDefinition
): Boolean = {
val currentScope = this.scopes.last
if (currentScope.functions.contains(name)) {
val functions = currentScope.functions(name)
val dupe = functions.find { f =>
functionDefinition.parameters == f.parameters
}
dupe.isDefined
} else {
false
}
}

def read(name: Name): Either[ScriptError, WanderValue] = {
var currentScopeOffset = this.scopes.length - 1
while (currentScopeOffset >= 0) {
val currentScope = this.scopes(currentScopeOffset)
if (currentScope.variables.contains(name)) {
return Right(currentScope.variables(name))
} else if (currentScope.functions.contains(name)) {
???
}
currentScopeOffset -= 1
}
Expand Down

0 comments on commit 756073a

Please sign in to comment.