Skip to content

Commit

Permalink
continuing to refactor Wander and it's test suite, #410
Browse files Browse the repository at this point in the history
  • Loading branch information
almibe committed May 6, 2023
1 parent 731cecc commit 1795858
Show file tree
Hide file tree
Showing 17 changed files with 117 additions and 130 deletions.
26 changes: 9 additions & 17 deletions wander/shared/src/main/scala/dev/ligature/wander/Bindings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,25 @@

package dev.ligature.wander

import dev.ligature.wander.parser.{Name, ScriptError, WanderValue}
import dev.ligature.wander.parser.FunctionDefinition
import dev.ligature.wander.parser.NativeFunction
import dev.ligature.wander.{Name, ScriptError, WanderValue}
import dev.ligature.wander.NativeFunction

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

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

def bindVariable(
name: Name,
wanderValue: WanderValue
): Either[ScriptError, Bindings] = {
val currentScope = this.scopes.last
if (currentScope.variables.contains(name)) {
if (currentScope.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 newVariables = currentScope + (name -> wanderValue)
val oldScope = this.scopes.dropRight(1)
Right(
Bindings(oldScope.appended(Scope(newVariables)))
Bindings(oldScope.appended(newVariables))
)
}
}
Expand All @@ -35,15 +31,11 @@ case class Bindings(scopes: List[Scope] = List(Scope(Map()))) {
var currentScopeOffset = this.scopes.length - 1
while (currentScopeOffset >= 0) {
val currentScope = this.scopes(currentScopeOffset)
if (currentScope.variables.contains(name)) {
return Right(currentScope.variables(name))
if (currentScope.contains(name)) {
return Right(currentScope(name))
}
currentScopeOffset -= 1
}
Left(ScriptError(s"Could not find $name in scope."))
}
}

//TODO this function will probably be used once I allow for ad-hoc polymorphism with functions.
def createFunctionDelegate(): NativeFunction =
???
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package dev.ligature.wander

import dev.ligature.wander.parser.Script
import dev.ligature.wander.Script

case class TypeError(message: String)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import dev.ligature.gaze.{
}
import dev.ligature.lig.LigNibblers
import dev.ligature.LigatureError
import dev.ligature.wander.parser.ScriptError
import dev.ligature.wander.ScriptError
import dev.ligature.Identifier

enum Token:
Expand Down
16 changes: 5 additions & 11 deletions wander/shared/src/main/scala/dev/ligature/wander/Modes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@

package dev.ligature.wander

import dev.ligature.wander.parser.{
Name,
NativeFunction,
Parameter,
BooleanValue,
ScriptResult,
ScriptError,
WanderValue
}
import dev.ligature.wander.WanderValue
import dev.ligature.wander.{Name, NativeFunction, Parameter, BooleanValue, ScriptResult, ScriptError}

import dev.ligature.{Ligature, Dataset}
import dev.ligature.wander.parser.WanderType
import dev.ligature.wander.parser.LigatureValue
import dev.ligature.wander.WanderType
import dev.ligature.wander.LigatureValue
import dev.ligature.Identifier

def instanceMode(instance: Ligature): Bindings = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

package dev.ligature.wander.parser
package dev.ligature.wander

import dev.ligature.gaze.{
Gaze,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

package dev.ligature.wander.parser
package dev.ligature.wander

import dev.ligature.wander.Bindings
import cats.effect.IO
Expand Down
16 changes: 8 additions & 8 deletions wander/shared/src/main/scala/dev/ligature/wander/Wander.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@

package dev.ligature.wander

import dev.ligature.wander.tokenize
import dev.ligature.wander.parser.{parse, Nothing, Script, ScriptError, ScriptResult}
import dev.ligature.wander.{NativeFunction, BooleanValue, WanderFunction, WanderValue, ScriptError, Nothing, Script, ResultStream, LigatureValue, ScriptResult}
import dev.ligature.wander.parse
import dev.ligature.{Dataset, Ligature}
import dev.ligature.lig.writeValue
import dev.ligature.wander.parser.WanderValue
import dev.ligature.wander.parser.BooleanValue
import dev.ligature.wander.parser.LigatureValue
import dev.ligature.wander.parser.NativeFunction
import dev.ligature.wander.parser.ResultStream
import dev.ligature.wander.parser.WanderFunction
import dev.ligature.wander.WanderValue
import dev.ligature.wander.BooleanValue
import dev.ligature.wander.LigatureValue
import dev.ligature.wander.NativeFunction
import dev.ligature.wander.ResultStream
import dev.ligature.wander.WanderFunction

def run(
script: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package dev.ligature.wander

import dev.ligature.IntegerLiteral
import dev.ligature.wander.Token
import dev.ligature.wander.parser.{
import dev.ligature.wander.{
BooleanValue,
LetStatement,
LigatureValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package dev.ligature.wander

import dev.ligature.wander.parser.{LigatureValue, Name}
import dev.ligature.wander.{LigatureValue, Name}
import dev.ligature.{Identifier, StringLiteral}
import munit.FunSuite

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package dev.ligature.wander

import dev.ligature.wander.Token
import dev.ligature.wander.parser.{BooleanValue, FunctionCall, Name, Script, ScriptResult}
import dev.ligature.wander.{BooleanValue, FunctionCall, Name, Script, ScriptResult}

val booleanExpression = List(
TestInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package dev.ligature.wander

import dev.ligature.{Identifier, IntegerLiteral, StringLiteral}
import dev.ligature.wander.Token
import dev.ligature.wander.parser.{
import dev.ligature.wander.{
FunctionCall,
LetStatement,
LigatureValue,
Expand All @@ -17,7 +17,7 @@ import dev.ligature.wander.parser.{
ScriptResult,
WanderFunction
}
import dev.ligature.wander.parser.WanderType
import dev.ligature.wander.WanderType

val closureTestData = List(
TestInstance(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package dev.ligature.wander

import dev.ligature.Dataset
import dev.ligature.wander.parser.parse
import dev.ligature.wander.parse
import dev.ligature.wander.tokenize
import dev.ligature.wander.common

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package dev.ligature.wander

import dev.ligature.IntegerLiteral
import dev.ligature.wander.Token
import dev.ligature.wander.parser.{
import dev.ligature.wander.{
BooleanValue,
Else,
ElseIf,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package dev.ligature.wander

import dev.ligature.wander.parser.{LigatureValue, Name}
import dev.ligature.wander.{LigatureValue, Name}
import dev.ligature.{Identifier, StringLiteral}
import munit.FunSuite

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,17 @@ package dev.ligature.wander

import dev.ligature.{Identifier, IntegerLiteral, StringLiteral}
import dev.ligature.wander.Token
import dev.ligature.wander.parser.{BooleanValue, LigatureValue, Nothing, Script, ScriptResult}
import dev.ligature.wander.{BooleanValue, LigatureValue, Nothing, Script, ScriptResult}

class PrimitivesSuite extends munit.FunSuite {
test("true boolean primitive") {
val script = "true"
val result = Right(ScriptResult(BooleanValue(true)))
assertEquals(run("true", common()), result)
}
}

val primitivesTestData = List(
TestInstance(
description = "true boolean primitive",
script = "true",
tokens = List(Token.BooleanLiteral(true)),
ast = Script(List(BooleanValue(true))),
result = Right(ScriptResult(BooleanValue(true)))
),
TestInstance(
description = "false boolean primitive",
script = "false",
Expand Down
52 changes: 26 additions & 26 deletions wander/shared/src/test/scala/dev/ligature/wander/TestData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
package dev.ligature.wander

import dev.ligature.wander.Token
import dev.ligature.wander.parser.{
import dev.ligature.wander.{
BooleanValue,
Else,
ElseIf,
Expand Down Expand Up @@ -47,30 +47,30 @@ val errorsExpression = List()
val newLine = "\n" //sys.props("line.separator")

val testData = List(
TestData(
category = "Primitives",
dataset = Dataset.fromString("test").getOrElse(???),
testInstances = primitivesTestData
),
TestData(
category = "Assignment",
dataset = Dataset.fromString("test").getOrElse(???),
testInstances = assignmentTestData
),
TestData(
category = "Closures",
dataset = Dataset.fromString("test").getOrElse(???),
testInstances = closureTestData
),
TestData(
category = "Boolean Functions",
dataset = Dataset.fromString("test").getOrElse(???),
testInstances = booleanExpression
),
TestData(
category = "If Expressions",
dataset = Dataset.fromString("test").getOrElse(???),
testInstances = ifExpression
)
// TestData(
// category = "Primitives",
// dataset = Dataset.fromString("test").getOrElse(???),
// testInstances = primitivesTestData
// ),
// TestData(
// category = "Assignment",
// dataset = Dataset.fromString("test").getOrElse(???),
// testInstances = assignmentTestData
// ),
// TestData(
// category = "Closures",
// dataset = Dataset.fromString("test").getOrElse(???),
// testInstances = closureTestData
// ),
// TestData(
// category = "Boolean Functions",
// dataset = Dataset.fromString("test").getOrElse(???),
// testInstances = booleanExpression
// ),
// TestData(
// category = "If Expressions",
// dataset = Dataset.fromString("test").getOrElse(???),
// testInstances = ifExpression
// )
// TODO add error cases
)
Loading

0 comments on commit 1795858

Please sign in to comment.