Skip to content

Commit

Permalink
removing some unneeded classes
Browse files Browse the repository at this point in the history
  • Loading branch information
almibe committed May 11, 2023
1 parent de554bd commit 840e498
Show file tree
Hide file tree
Showing 14 changed files with 123 additions and 121 deletions.
6 changes: 3 additions & 3 deletions lig/shared/src/main/scala/dev/ligature/lig/LigReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ def parseIdentifier(
}

def handleIdGenId(input: String): Either[LigError, Identifier] =
Identifier.fromString(genIdId(input)).left.map(err => LigError(err.message))
Identifier.fromString(genIdId(input)).left.map(err => LigError(err.userMessage))

def genIdId(input: String): String = {
val itr = input.toCharArray.iterator
Expand All @@ -283,7 +283,7 @@ def handlePrefixedId(
Identifier
.fromString(prefixValue + postfix)
.left
.map(err => LigError(err.message))
.map(err => LigError(err.userMessage))
}
}

Expand All @@ -299,7 +299,7 @@ def handlePrefixedGenId(
Identifier
.fromString(genIdId(prefixValue + postfix))
.left
.map(err => LigError(err.message))
.map(err => LigError(err.userMessage))
}
}

Expand Down
2 changes: 1 addition & 1 deletion ligature/shared/src/main/scala/dev/ligature/Ligature.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ object Identifier {
}
}

final case class LigatureError(message: String)
case class LigatureError(val userMessage: String) extends Throwable

enum LigatureLiteral:
case StringLiteral(value: String)
Expand Down
11 changes: 6 additions & 5 deletions wander/shared/src/main/scala/dev/ligature/wander/Bindings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@

package dev.ligature.wander

import dev.ligature.wander.{ScriptError, WanderValue}
import dev.ligature.LigatureError
import dev.ligature.wander.{WanderValue}

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

def bindVariable(
name: WanderValue.Name,
wanderValue: WanderValue
): Either[ScriptError, Bindings] = {
): Either[LigatureError, Bindings] = {
val currentScope = this.scopes.last
if (currentScope.contains(name)) {
//TODO probably remove this to allow shadowing?
Left(ScriptError(s"$name is already bound in current scope."))
Left(LigatureError(s"$name is already bound in current scope."))
} else {
val newVariables = currentScope + (name -> wanderValue)
val oldScope = this.scopes.dropRight(1)
Expand All @@ -26,7 +27,7 @@ case class Bindings(scopes: List[Map[WanderValue.Name, WanderValue]] = List((Map
}
}

def read(name: WanderValue.Name): Either[ScriptError, WanderValue] = {
def read(name: WanderValue.Name): Either[LigatureError, WanderValue] = {
var currentScopeOffset = this.scopes.length - 1
while (currentScopeOffset >= 0) {
val currentScope = this.scopes(currentScopeOffset)
Expand All @@ -35,6 +36,6 @@ case class Bindings(scopes: List[Map[WanderValue.Name, WanderValue]] = List((Map
}
currentScopeOffset -= 1
}
Left(ScriptError(s"Could not find $name in scope."))
Left(LigatureError(s"Could not find $name in scope."))
}
}
49 changes: 49 additions & 0 deletions wander/shared/src/main/scala/dev/ligature/wander/Interpreter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* 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

import dev.ligature.LigatureError
import dev.ligature.LigatureLiteral

/** Represents a full script that can be eval'd.
*/
def eval(script: Seq[Term], bindings: Bindings): Either[LigatureError, ScriptResult] = {
var result: WanderValue = WanderValue.Nothing
var currentBindings: Bindings = bindings
script.foreach { term =>
evalTerm(term,currentBindings) match {
case Left(err) => return Left(err)
case Right(res) =>
result = res.result
currentBindings = res.bindings
}
}
Right(result)
}

def evalTerm(term: Term, bindings: Bindings): Either[LigatureError, EvalResult] =
term match
case Term.BooleanLiteral(value) => Right(EvalResult(WanderValue.BooleanValue(value), bindings))
case Term.IdentifierLiteral(value) => Right(EvalResult(WanderValue.LigatureValue(value), bindings))
case Term.IntegerLiteral(value) => Right(EvalResult(WanderValue.LigatureValue(LigatureLiteral.IntegerLiteral(value)), bindings))
case Term.StringLiteral(value) => Right(EvalResult(WanderValue.LigatureValue(LigatureLiteral.StringLiteral(value)), bindings))
case Term.Name(value) => ???
case Term.FunctionCall(name, arguments) =>
//TODO val evaldArgs = evalArguments(arguments)
bindings.read(WanderValue.Name(name.value)) match {
case Left(value) => Left(value)
case Right(value) =>
value match {
case WanderValue.NativeFunction(parameters, body, output) => {
body(arguments, bindings).map { value => EvalResult(value, bindings) }
}
case WanderValue.WanderFunction(parameters, body, output) => ???
case _ => ???
}
}
case Term.WanderFunction(parameters, body) => {
???
}
case Term.Scope(terms) => ???
10 changes: 4 additions & 6 deletions wander/shared/src/main/scala/dev/ligature/wander/Lexer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ import dev.ligature.gaze.{
repeat
}
import dev.ligature.lig.LigNibblers
import dev.ligature.LigatureError
import dev.ligature.wander.ScriptError
import dev.ligature.Identifier
import dev.ligature.{Identifier, LigatureError}

enum Token:
case BooleanLiteral(value: Boolean)
Expand All @@ -32,20 +30,20 @@ enum Token:
case OpenBrace, CloseBrace, Colon, OpenParen, CloseParen, NewLine,
Arrow, IfKeyword, ElsifKeyword, ElseKeyword, EqualSign, LetKeyword, Comment

def tokenize(input: String): Either[ScriptError, Seq[Token]] = {
def tokenize(input: String): Either[LigatureError, Seq[Token]] = {
val gaze = Gaze.from(input)
gaze.attempt(tokensNib) match {
case None =>
if (gaze.isComplete) {
Right(List())
} else {
Left(ScriptError("Error"))
Left(LigatureError("Error"))
}
case Some(res) =>
if (gaze.isComplete) {
Right(res)
} else {
Left(ScriptError("Error"))
Left(LigatureError("Error"))
}
}
}
Expand Down
18 changes: 10 additions & 8 deletions wander/shared/src/main/scala/dev/ligature/wander/Modes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
package dev.ligature.wander

import dev.ligature.wander.WanderValue
import dev.ligature.wander.{Parameter, ScriptResult, ScriptError}
import dev.ligature.wander.{Parameter, ScriptResult}

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

def instanceMode(instance: Ligature): Bindings = {
var bindings = common()

bindings = bindings.bindVariable(WanderValue.Name("datasets"), WanderValue.NativeFunction(
List(),
(arguments: Seq[Term], binding: Bindings) => Right(WanderValue.LigatureValue(Identifier.fromString("test").getOrElse(???)))
(arguments: Seq[Term], binding: Bindings) =>
Right(WanderValue.LigatureValue(Identifier.fromString("test").getOrElse(???)))
)).getOrElse(???)

bindings = bindings.bindVariable(WanderValue.Name("addDataset"), WanderValue.NativeFunction(
Expand Down Expand Up @@ -63,12 +65,12 @@ def common(): Bindings = {
List(Parameter(WanderValue.Name("bool"), WanderType.Boolean)),
(arguments: Seq[Term], bindings: Bindings) =>
if arguments.size != 1 then
Left(ScriptError("`not` function requires 1 argument."))
Left(LigatureError("`not` function requires 1 argument."))
else
val evaledArgs = arguments.map(evalTerm(_, bindings))
evaledArgs.headOption match
case Some(Right(EvalResult(b: WanderValue.BooleanValue, _))) => Right(WanderValue.BooleanValue(!b.value))
case _ => Left(ScriptError("`not` function requires 1 boolean argument."))
case _ => Left(LigatureError("`not` function requires 1 boolean argument."))
)
)
.getOrElse(???)
Expand All @@ -89,10 +91,10 @@ def common(): Bindings = {
(left, right) match {
case (Right(EvalResult(l: WanderValue.BooleanValue, _)), Right(EvalResult(r: WanderValue.BooleanValue, _))) =>
Right(WanderValue.BooleanValue(l.value && r.value))
case _ => Left(ScriptError("`and` function requires two booleans"))
case _ => Left(LigatureError("`and` function requires two booleans"))
}
else
Left(ScriptError("`and` function requires two booleans"))
Left(LigatureError("`and` function requires two booleans"))
)
)
.getOrElse(???)
Expand All @@ -113,10 +115,10 @@ def common(): Bindings = {
(left, right) match {
case (Right(EvalResult(l: WanderValue.BooleanValue, _)), Right(EvalResult(r: WanderValue.BooleanValue, _))) =>
Right(WanderValue.BooleanValue(l.value || r.value))
case _ => Left(ScriptError("`or` function requires two booleans"))
case _ => Left(LigatureError("`or` function requires two booleans"))
}
else
Left(ScriptError("`or` function requires two booleans"))
Left(LigatureError("`or` function requires two booleans"))
)
)
.getOrElse(???)
Expand Down
37 changes: 6 additions & 31 deletions wander/shared/src/main/scala/dev/ligature/wander/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import dev.ligature.gaze.{
takeString,
repeat
}
import dev.ligature.{Identifier, LigatureLiteral}
import dev.ligature.{Identifier, LigatureLiteral, LigatureError}
import dev.ligature.wander.Token

enum Term:
Expand All @@ -30,32 +30,7 @@ enum Term:
parameters: Seq[String],
body: Scope)

def evalTerm(term: Term, bindings: Bindings): Either[ScriptError, EvalResult] =
term match
case Term.BooleanLiteral(value) => Right(EvalResult(WanderValue.BooleanValue(value), bindings))
case Term.IdentifierLiteral(value) => Right(EvalResult(WanderValue.LigatureValue(value), bindings))
case Term.IntegerLiteral(value) => Right(EvalResult(WanderValue.LigatureValue(LigatureLiteral.IntegerLiteral(value)), bindings))
case Term.StringLiteral(value) => Right(EvalResult(WanderValue.LigatureValue(LigatureLiteral.StringLiteral(value)), bindings))
case Term.Name(value) => ???
case Term.FunctionCall(name, arguments) =>
//TODO val evaldArgs = evalArguments(arguments)
bindings.read(WanderValue.Name(name.value)) match {
case Left(value) => Left(value)
case Right(value) =>
value match {
case WanderValue.NativeFunction(parameters, body, output) => {
body(arguments, bindings).map { value => EvalResult(value, bindings) }
}
case WanderValue.WanderFunction(parameters, body, output) => ???
case _ => ???
}
}
case Term.WanderFunction(parameters, body) => {
???
}
case Term.Scope(terms) => ???

def parse(script: Seq[Token]): Either[String, Script] = {
def parse(script: Seq[Token]): Either[LigatureError, Seq[Term]] = {
val filteredInput = script.filter {
_ match
case Token.Spaces(_) | Token.NewLine | Token.Comment => false
Expand All @@ -66,16 +41,16 @@ def parse(script: Seq[Token]): Either[String, Script] = {
res match {
case None =>
if (gaze.isComplete) {
Right(Script(Seq()))
Right(Seq())
} else {
Left("No Match")
Left(LigatureError(s"Error Parsing - No Match - Next Token: ${gaze.next()}"))
}
// TODO some case also needs to check if gaze is complete
case Some(res) =>
if (gaze.isComplete) {
Right(Script(res)) // .filter(_.isDefined).map(_.get)))
Right(res)
} else {
Left("No Match")
Left(LigatureError(s"Error Parsing - No Match - Next Token: ${gaze.next()}"))
}
}
}
Expand Down
24 changes: 3 additions & 21 deletions wander/shared/src/main/scala/dev/ligature/wander/Syntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import fs2.Stream
import scala.util.Success
import dev.ligature.Identifier
import cats.Eval
import dev.ligature.LigatureError

/** Represents the union of Statements and Expressions
*/
Expand All @@ -22,8 +23,7 @@ import cats.Eval
*/
//sealed trait Expression extends Element

case class ScriptError(message: String)
case class ScriptResult(result: WanderValue)
type ScriptResult = WanderValue
case class EvalResult(result: WanderValue, bindings: Bindings)

/** Represents a Value in the Wander language.
Expand All @@ -35,7 +35,7 @@ enum WanderValue:
case Nothing
case NativeFunction(
parameters: List[Parameter],
body: (arguments: Seq[Term], bindings: Bindings) => Either[ScriptError, WanderValue],
body: (arguments: Seq[Term], bindings: Bindings) => Either[LigatureError, WanderValue],
output: WanderType = null)
case WanderFunction(
parameters: List[Parameter],
Expand Down Expand Up @@ -92,24 +92,6 @@ enum WanderValue:
// Right(EvalResult(this, binding))
// }

/** Represents a full script that can be eval'd.
*/
case class Script(val terms: Seq[Term]) {
def eval(bindings: Bindings): Either[ScriptError, ScriptResult] = {
var result: WanderValue = WanderValue.Nothing
var currentBindings: Bindings = bindings
terms.foreach { term =>
evalTerm(term,currentBindings) match {
case Left(err) => return Left(err)
case Right(res) =>
result = res.result
currentBindings = res.bindings
}
}
Right(ScriptResult(result))
}
}

/** Represents a scope in Wander that can be eval'd and can contain it's own
* bindings.
*/
Expand Down
21 changes: 8 additions & 13 deletions wander/shared/src/main/scala/dev/ligature/wander/Wander.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,27 @@

package dev.ligature.wander

import dev.ligature.wander.{WanderValue, ScriptError, Script, ScriptResult}
import dev.ligature.wander.{WanderValue, ScriptResult}
import dev.ligature.wander.parse
import dev.ligature.{Dataset, Ligature}
import dev.ligature.lig.writeValue
import dev.ligature.LigatureError

def run(
script: String,
bindings: Bindings
): Either[ScriptError, ScriptResult] =
): Either[LigatureError, ScriptResult] =
for {
tokens <- tokenize(script)
script <- parse(tokens).left.map(ScriptError(_))
result <- interpret(script, bindings)
script <- parse(tokens)
result <- eval(script, bindings)
} yield result

def interpret(
script: Script,
bindings: Bindings
): Either[ScriptError, ScriptResult] = {
script.eval(bindings)
}

def printResult(result: Either[ScriptError, ScriptResult]): String = {
def printResult(result: Either[LigatureError, ScriptResult]): String = {
result match {
case Left(value) => value.message
case Right(ScriptResult(value)) => printWanderValue(value)
case Left(value) => value.userMessage
case Right(value) => printWanderValue(value)
}
}

Expand Down
Loading

0 comments on commit 840e498

Please sign in to comment.