Skip to content

Commit

Permalink
Refactoring ctx.value
Browse files Browse the repository at this point in the history
  • Loading branch information
lrlucena committed Apr 24, 2024
1 parent 871f02c commit 33836c3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ThisBuild / scalaVersion := "3.4.0"
ThisBuild / scalaVersion := "3.4.1"
ThisBuild / organization := "com.github.lrlucena"
ThisBuild / version := "1.3.4"

Expand Down
36 changes: 18 additions & 18 deletions src/main/scala/whilelang/parser/MyListener.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,63 +15,63 @@ class MyListener extends BaseListener with ContextValue:
override def exitProgram(ctx: ProgramContext) =
program = Program(ctx.seqStatement.value)

override def exitSeqStatement(ctx: SeqStatementContext) = ctx.value_= :
override def exitSeqStatement(ctx: SeqStatementContext) = ctx.value :
SeqStatement(ctx.statement.map(_.value[Statement]))

override def exitAttrib(ctx: AttribContext) = ctx.value_= :
override def exitAttrib(ctx: AttribContext) = ctx.value :
Attrib(ctx.ID.text, ctx.expression.value)

override def exitSkip(ctx: SkipContext) = ctx.value_= :
override def exitSkip(ctx: SkipContext) = ctx.value :
Skip

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

override def exitWhile(ctx: WhileContext) = ctx.value_= :
override def exitWhile(ctx: WhileContext) = ctx.value :
While(ctx.bool.value, ctx.statement.value)

override def exitPrint(ctx: PrintContext) = ctx.value_= :
override def exitPrint(ctx: PrintContext) = ctx.value :
Print(ctx.Text.text.drop(1).dropRight(1))

override def exitWrite(ctx: WriteContext) = ctx.value_= :
override def exitWrite(ctx: WriteContext) = ctx.value :
Print(ctx.expression.value)

override def exitBlock(ctx: BlockContext) = ctx.value_= :
override def exitBlock(ctx: BlockContext) = ctx.value :
ctx.seqStatement.value

override def exitRead(ctx: ReadContext) = ctx.value_= :
override def exitRead(ctx: ReadContext) = ctx.value :
Read

override def exitId(ctx: IdContext) = ctx.value_= :
override def exitId(ctx: IdContext) = ctx.value :
Id(ctx.ID.text)

override def exitExpParen(ctx: ExpParenContext) = ctx.value_= :
override def exitExpParen(ctx: ExpParenContext) = ctx.value :
ctx.expression.value

override def exitInt(ctx: IntContext) = ctx.value_= :
override def exitInt(ctx: IntContext) = ctx.value :
Integer(ctx.text.toInt)

override def exitBinOp(ctx: BinOpContext) = ctx.value_= :
override def exitBinOp(ctx: BinOpContext) = ctx.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)

override def exitNot(ctx: NotContext) = ctx.value_= :
override def exitNot(ctx: NotContext) = ctx.value :
Not(ctx.bool.value)

override def exitBoolean(ctx: BooleanContext) = ctx.value_= :
override def exitBoolean(ctx: BooleanContext) = ctx.value :
Boole(ctx.text == "true")

override def exitAnd(ctx: AndContext) = ctx.value_= :
override def exitAnd(ctx: AndContext) = ctx.value :
And(ctx.bool(0).value, ctx.bool(1).value)

override def exitBoolParen(ctx: BoolParenContext) = ctx.value_= :
override def exitBoolParen(ctx: BoolParenContext) = ctx.value :
ctx.bool.value

override def exitRelOp(ctx: RelOpContext) = ctx.value_= :
override def exitRelOp(ctx: RelOpContext) = ctx.value :
val Seq(lhs, rhs) = ctx.expression.map(_.value[Expression])
ctx(1).text match
case "=" => ExpEq(lhs, rhs)
Expand Down
5 changes: 4 additions & 1 deletion src/main/scala/whilelang/util/ContextValue.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ trait ContextValue:
def apply(i: Int): ParseTree = tree.getChild(i)
def text: String = tree.getText
def value[E <: Any]: E = values.get(tree).asInstanceOf[E]
def value_=(v: Any): Unit = values.put(tree, v)
def value(v: Any): Unit = values.put(tree, v)
//def apply(v: Any): Unit = values.put(tree, v)
//def `=`(v: Any): Unit = values.put(tree, v)
//def value_=(v: Any): Unit = values.put(tree, v)

extension[E] (list: java.util.List[E])
def map[T](f: E => T): Seq[T] = list.asScala.toSeq.map(f)
2 changes: 1 addition & 1 deletion while
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

java -Xmx1879m -jar ./target/scala-3.4.0/WhileLang-assembly-1.3.4.jar $1
java -Xmx1879m -jar ./target/scala-3.4.1/WhileLang-assembly-1.3.4.jar $1

0 comments on commit 33836c3

Please sign in to comment.