Skip to content

Commit

Permalink
fixes some errors
Browse files Browse the repository at this point in the history
  • Loading branch information
lrlucena committed Oct 10, 2023
1 parent 935b388 commit 668966e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
13 changes: 7 additions & 6 deletions src/main/scala/whilelang/parser/MyListener.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ class MyListener extends BaseListener with ContextValue:
Skip

override def exitIf(ctx: IfContext): Unit = ctx.value_= :
If(ctx.bool.value, ctx.statement(0).value, ctx.statement(1).value)
val Seq(thenStat, elseStat) = ctx.statement.map(_.value[Statement])
If(ctx.bool.value, thenStat, elseStat)

override def exitWhile(ctx: WhileContext): Unit = ctx.value_= :
While(ctx.bool.value, ctx.statement.value)
Expand All @@ -51,11 +52,11 @@ class MyListener extends BaseListener with ContextValue:
Integer(ctx.text.toInt)

override def exitBinOp(ctx: BinOpContext): Unit = ctx.value_= :
val Seq(lhs, rhs): Seq[Expression] = ctx.expression.map(_.value)
val Seq(lhs, rhs) = ctx.expression.map(_.value[Expression])
ctx(1).text match
case "*" => ExpMult(lhs, rhs)
case "-" => ExpSub(lhs, rhs)
case "+" | _ => ExpSum(lhs, rhs)
case _ => ExpSum(lhs, rhs)

override def exitNot(ctx: NotContext): Unit = ctx.value_= :
Not(ctx.bool.value)
Expand All @@ -70,7 +71,7 @@ class MyListener extends BaseListener with ContextValue:
ctx.bool.value

override def exitRelOp(ctx: RelOpContext): Unit = ctx.value_= :
val Seq(lhs, rhs): Seq[Expression] = ctx.expression.map(_.value)
val Seq(lhs, rhs) = ctx.expression.map(_.value[Expression])
ctx(1).text match
case "=" => ExpEq(lhs, rhs)
case "<=" | _ => ExpLe(lhs, rhs)
case "=" => ExpEq(lhs, rhs)
case _ => ExpLe(lhs, rhs)
4 changes: 2 additions & 2 deletions src/main/scala/whilelang/util/ContextValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import org.antlr.v4.runtime.tree.{ParseTree, ParseTreeProperty as Property}
import scala.jdk.CollectionConverters.ListHasAsScala

trait ContextValue:
given Property[Any] = Property[Any]()
private val values = Property[Any]()

extension (tree: ParseTree)(using values: Property[Any])
extension (tree: ParseTree)
def apply(i: Int): ParseTree = tree.getChild(i)
def text: String = tree.getText
def value[E <: Any]: E = values.get(tree).asInstanceOf[E]
Expand Down

0 comments on commit 668966e

Please sign in to comment.