Skip to content

Commit

Permalink
getting first Primitives test to pass, #410
Browse files Browse the repository at this point in the history
  • Loading branch information
almibe committed May 6, 2023
1 parent 1795858 commit 0fe678b
Show file tree
Hide file tree
Showing 7 changed files with 830 additions and 827 deletions.
42 changes: 21 additions & 21 deletions wander/shared/src/main/scala/dev/ligature/wander/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def parse(script: Seq[Token]): Either[String, Script] = {
case _ => true
}
val gaze = Gaze(filteredInput)
val res: Option[Seq[Element]] = None //gaze.attempt(scriptNib)
val res: Option[Seq[Term]] = gaze.attempt(scriptNib)
res match {
case None =>
if (gaze.isComplete) {
Expand Down Expand Up @@ -53,7 +53,7 @@ val identifierNib: Nibbler[Token, Term] = gaze =>
case Some(Token.Identifier(i)) => Some(List(Term.IdentifierLiteral(i)))
case _ => None

val intergerNib: Nibbler[Token, Term] = gaze =>
val integerNib: Nibbler[Token, Term] = gaze =>
gaze.next() match
case Some(Token.IntegerLiteral(i)) => Some(List(Term.IntegerLiteral(i)))
case _ => None
Expand Down Expand Up @@ -183,18 +183,18 @@ val nameNib: Nibbler[Token, Term] = gaze =>
// } yield Seq(FunctionCall(name.head, parameters.toList))
// }

// val expressionNib =
// takeFirst(
// ifExpressionNib,
// functionCallNib,
// nameNib,
// scopeNib,
// identifierNib,
// wanderFunctionNib,
// stringNib,
// integerNib,
// booleanNib
// )
val expressionNib =
takeFirst(
// ifExpressionNib,
// functionCallNib,
// nameNib,
// scopeNib,
// identifierNib,
// wanderFunctionNib,
// stringNib,
integerNib,
booleanNib
)

// val equalSignNib = takeCond[Token](_.tokenType == TokenType.EqualSign).map { _ =>
// Seq(EqualSign)
Expand All @@ -214,11 +214,11 @@ val nameNib: Nibbler[Token, Term] = gaze =>
// } yield Seq(LetStatement(name.head, expression.head))
// }

// val elementNib = takeFirst(expressionNib, letStatementNib)
val elementNib = takeFirst(expressionNib)//, letStatementNib)

// val scriptNib =
// optional(
// repeat(
// elementNib
// )
// )
val scriptNib =
optional(
repeat(
elementNib
)
)
15 changes: 9 additions & 6 deletions wander/shared/src/main/scala/dev/ligature/wander/Syntax.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ enum Term:
case BooleanLiteral(value: Boolean)
case FunctionCall(name: String, parameters: Seq[Term])

def eval(term: Term, bindings: Bindings): Either[ScriptError, EvalResult] = ???
def evalTerm(term: Term, bindings: Bindings): Either[ScriptError, EvalResult] =
term match
case Term.BooleanLiteral(value) => Right(EvalResult(BooleanValue(value), bindings))
case _ => ???

/** Represents the union of Statements and Expressions
*/
Expand Down Expand Up @@ -151,12 +154,12 @@ case class NativeFunction(

/** Represents a full script that can be eval'd.
*/
case class Script(val elements: Seq[Element]) {
case class Script(val terms: Seq[Term]) {
def eval(bindings: Bindings): Either[ScriptError, ScriptResult] = {
var result: WanderValue = Nothing
var currentBindings: Bindings = bindings
elements.foreach { element =>
element.eval(currentBindings) match {
terms.foreach { term =>
evalTerm(term,currentBindings) match {
case Left(err) => return Left(err)
case Right(res) =>
result = res.result
Expand All @@ -170,12 +173,12 @@ case class Script(val elements: Seq[Element]) {
/** Represents a scope in Wander that can be eval'd and can contain it's own
* bindings.
*/
case class Scope(val elements: List[Element]) extends Expression {
case class Scope(val elements: List[Term]) extends Expression {
def eval(bindings: Bindings) = {
var currentBindings = bindings.newScope()
var result: WanderValue = Nothing
elements.foreach { element =>
element.eval(currentBindings) match {
evalTerm(element, currentBindings) match {
case Left(err) => return Left(err)
case Right(res) =>
result = res.result
Expand Down
266 changes: 133 additions & 133 deletions wander/shared/src/test/scala/dev/ligature/wander/AssignmentTestData.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,137 +18,137 @@ import dev.ligature.wander.{
}

val assignmentTestData = List(
TestInstance(
description = "basic let",
script = "let x = 5",
tokens = List(
Token.LetKeyword,
Token.Spaces(" "),
Token.Name("x"),
Token.Spaces(" "),
Token.EqualSign,
Token.Spaces(" "),
Token.IntegerLiteral(5)
),
ast = Script(List(LetStatement(Name("x"), LigatureValue(IntegerLiteral(5))))),
result = Right(ScriptResult(Nothing))
),
TestInstance(
description = "make sure keyword parser is greedy",
script = "let trued = true",
tokens = List(
Token.LetKeyword,
Token.Spaces(" "),
Token.Name("trued"),
Token.Spaces(" "),
Token.EqualSign,
Token.Spaces(" "),
Token.BooleanLiteral(true)
),
ast = Script(List(LetStatement(Name("trued"), BooleanValue(true)))),
result = Right(ScriptResult(Nothing))
),
TestInstance(
description = "let with result",
script = """let hello = 5
|hello""".stripMargin,
tokens = List(
Token.LetKeyword,
Token.Spaces(" "),
Token.Name("hello"),
Token.Spaces(" "),
Token.EqualSign,
Token.Spaces(" "),
Token.IntegerLiteral(5),
Token.NewLine,
Token.Name("hello")
),
ast = Script(
List(
LetStatement(Name("hello"), LigatureValue(IntegerLiteral(5))),
Name("hello")
)
),
result = Right(ScriptResult(LigatureValue(IntegerLiteral(5))))
),
TestInstance(
description = "basic scope",
script = """{
| let x = 7
| x
|}""".stripMargin,
tokens = List(
Token.OpenBrace,
Token.NewLine,
Token.Spaces(" "),
Token.LetKeyword,
Token.Spaces(" "),
Token.Name("x"),
Token.Spaces(" "),
Token.EqualSign,
Token.Spaces(" "),
Token.IntegerLiteral(7),
Token.NewLine,
Token.Spaces(" "),
Token.Name("x"),
Token.NewLine,
Token.CloseBrace
),
ast = Script(
List(
Scope(
List(
LetStatement(Name("x"), LigatureValue(IntegerLiteral(7))),
Name("x")
)
)
)
),
result = Right(ScriptResult(LigatureValue(IntegerLiteral(7))))
),
TestInstance(
description = "scope shadowing",
script = """let x = 5
|{
| let x = 7
| x
|}""".stripMargin,
tokens = List(
Token.LetKeyword,
Token.Spaces(" "),
Token.Name("x"),
Token.Spaces(" "),
Token.EqualSign,
Token.Spaces(" "),
Token.IntegerLiteral(5),
Token.NewLine,
Token.OpenBrace,
Token.NewLine,
Token.Spaces(" "),
Token.LetKeyword,
Token.Spaces(" "),
Token.Name("x"),
Token.Spaces(" "),
Token.EqualSign,
Token.Spaces(" "),
Token.IntegerLiteral(7),
Token.NewLine,
Token.Spaces(" "),
Token.Name("x"),
Token.NewLine,
Token.CloseBrace
),
ast = Script(
List(
LetStatement(Name("x"), LigatureValue(IntegerLiteral(5))),
Scope(
List(
LetStatement(Name("x"), LigatureValue(IntegerLiteral(7))),
Name("x")
)
)
)
),
result = Right(ScriptResult(LigatureValue(IntegerLiteral(7))))
)
// TestInstance(
// description = "basic let",
// script = "let x = 5",
// tokens = List(
// Token.LetKeyword,
// Token.Spaces(" "),
// Token.Name("x"),
// Token.Spaces(" "),
// Token.EqualSign,
// Token.Spaces(" "),
// Token.IntegerLiteral(5)
// ),
// ast = Script(List(LetStatement(Name("x"), LigatureValue(IntegerLiteral(5))))),
// result = Right(ScriptResult(Nothing))
// ),
// TestInstance(
// description = "make sure keyword parser is greedy",
// script = "let trued = true",
// tokens = List(
// Token.LetKeyword,
// Token.Spaces(" "),
// Token.Name("trued"),
// Token.Spaces(" "),
// Token.EqualSign,
// Token.Spaces(" "),
// Token.BooleanLiteral(true)
// ),
// ast = Script(List(LetStatement(Name("trued"), BooleanValue(true)))),
// result = Right(ScriptResult(Nothing))
// ),
// TestInstance(
// description = "let with result",
// script = """let hello = 5
// |hello""".stripMargin,
// tokens = List(
// Token.LetKeyword,
// Token.Spaces(" "),
// Token.Name("hello"),
// Token.Spaces(" "),
// Token.EqualSign,
// Token.Spaces(" "),
// Token.IntegerLiteral(5),
// Token.NewLine,
// Token.Name("hello")
// ),
// ast = Script(
// List(
// LetStatement(Name("hello"), LigatureValue(IntegerLiteral(5))),
// Name("hello")
// )
// ),
// result = Right(ScriptResult(LigatureValue(IntegerLiteral(5))))
// ),
// TestInstance(
// description = "basic scope",
// script = """{
// | let x = 7
// | x
// |}""".stripMargin,
// tokens = List(
// Token.OpenBrace,
// Token.NewLine,
// Token.Spaces(" "),
// Token.LetKeyword,
// Token.Spaces(" "),
// Token.Name("x"),
// Token.Spaces(" "),
// Token.EqualSign,
// Token.Spaces(" "),
// Token.IntegerLiteral(7),
// Token.NewLine,
// Token.Spaces(" "),
// Token.Name("x"),
// Token.NewLine,
// Token.CloseBrace
// ),
// ast = Script(
// List(
// Scope(
// List(
// LetStatement(Name("x"), LigatureValue(IntegerLiteral(7))),
// Name("x")
// )
// )
// )
// ),
// result = Right(ScriptResult(LigatureValue(IntegerLiteral(7))))
// ),
// TestInstance(
// description = "scope shadowing",
// script = """let x = 5
// |{
// | let x = 7
// | x
// |}""".stripMargin,
// tokens = List(
// Token.LetKeyword,
// Token.Spaces(" "),
// Token.Name("x"),
// Token.Spaces(" "),
// Token.EqualSign,
// Token.Spaces(" "),
// Token.IntegerLiteral(5),
// Token.NewLine,
// Token.OpenBrace,
// Token.NewLine,
// Token.Spaces(" "),
// Token.LetKeyword,
// Token.Spaces(" "),
// Token.Name("x"),
// Token.Spaces(" "),
// Token.EqualSign,
// Token.Spaces(" "),
// Token.IntegerLiteral(7),
// Token.NewLine,
// Token.Spaces(" "),
// Token.Name("x"),
// Token.NewLine,
// Token.CloseBrace
// ),
// ast = Script(
// List(
// LetStatement(Name("x"), LigatureValue(IntegerLiteral(5))),
// Scope(
// List(
// LetStatement(Name("x"), LigatureValue(IntegerLiteral(7))),
// Name("x")
// )
// )
// )
// ),
// result = Right(ScriptResult(LigatureValue(IntegerLiteral(7))))
// )
)
Loading

0 comments on commit 0fe678b

Please sign in to comment.