Skip to content

Commit

Permalink
Update to Scala 3.4
Browse files Browse the repository at this point in the history
  • Loading branch information
lrlucena committed Mar 18, 2024
1 parent 6f9b47c commit 2b4766a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 40 deletions.
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
ThisBuild / scalaVersion := "3.3.3"
ThisBuild / scalaVersion := "3.4.0"
ThisBuild / organization := "com.github.lrlucena"
ThisBuild / version := "1.3.3"
ThisBuild / version := "1.3.4"

enablePlugins(Antlr4Plugin)

Expand Down
2 changes: 1 addition & 1 deletion project/assembly.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.1.1")
addSbtPlugin("com.eed3si9n" % "sbt-assembly" % "2.2.0")
22 changes: 12 additions & 10 deletions src/main/scala/whilelang/compiler/Semantics.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,29 @@ given Ids = MSet[String]()
def vars(ids: Ids) =
if ids.nonEmpty then s"var ${ids.mkString(", ")} = 0" else ""

extension (src: Any)(using ids: Ids)
extension (src: Element)(using ids: Ids)
def meaning: String = m

private def op(s: String)(lhs: Element, rhs: Element) = s"(${lhs.m} $s ${rhs.m})"

private def m: String = src match
case If(cond, tSmt, eSmt) => s"if ${cond.m} then\n ${tSmt.m}\nelse\n ${eSmt.m}"
case Write(exp) => s"println(${exp.m})"
case Print(text) => s"println(\"$text\")"
case While(cond, doSmt) => s"while ${cond.m} do\n ${doSmt.m}"
case SeqStatement(stmts) => stmts.map(_.m).mkString("\n").replaceAll("\n", "\n ")
case Attrib(id, exp) => ids.add(id); s"$id = ${exp.m}"
case Program(seq) => val main = seq.m
s"@main def main() =\n ${vars(ids)}\n $main"
case Program(seq) => s"@main def main() =\n ${vars(ids)}\n ${seq.m}"
case Skip => "()"
case Read => "readInt()"
case Id(id) => id
case Integer(value) => s"$value"
case ExpSum(lhs, rhs) => s"(${lhs.m} + ${rhs.m})"
case ExpSub(lhs, rhs) => s"(${lhs.m} - ${rhs.m})"
case ExpMult(lhs, rhs) => s"(${lhs.m} * ${rhs.m})"
case Boole(b) => s"$b"
case ExpEq(lhs, rhs) => s"(${lhs.m} == ${rhs.m})"
case ExpLe(lhs, rhs) => s"(${lhs.m} <= ${rhs.m})"
case Not(b) => s"!(${b.m})"
case And(lhs, rhs) => s"(${lhs.m} && ${rhs.m})"
case Not(b) => s"(!${b.m})"
case ExpSum(lhs, rhs) => op("+")(lhs, rhs)
case ExpSub(lhs, rhs) => op("-")(lhs, rhs)
case ExpMult(lhs, rhs) => op("*")(lhs, rhs)
case ExpEq(lhs, rhs) => op("==")(lhs, rhs)
case ExpLe(lhs, rhs) => op("<=")(lhs, rhs)
case And(lhs, rhs) => op("&&")(lhs, rhs)
case _ => "~~~ Not Implemented ~~~"
43 changes: 22 additions & 21 deletions src/main/scala/whilelang/parser/MyListener.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package whilelang.parser

import scala.compiletime.uninitialized
import whilelang.parser.Bool.*
import whilelang.parser.Expression.*
import whilelang.parser.Statement.*
Expand All @@ -9,68 +10,68 @@ import whilelang.util.ContextValue

class MyListener extends BaseListener with ContextValue:

var program: Program = _
var program: Program = uninitialized

override def exitProgram(ctx: ProgramContext): Unit = program = Program:
ctx.seqStatement.value
override def exitProgram(ctx: ProgramContext) =
program = Program(ctx.seqStatement.value)

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

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

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

override def exitIf(ctx: IfContext): Unit = 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): Unit = ctx.value_= :
override def exitWhile(ctx: WhileContext) = ctx.value_= :
While(ctx.bool.value, ctx.statement.value)

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

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

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

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

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

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

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

override def exitBinOp(ctx: BinOpContext): Unit = 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): Unit = ctx.value_= :
override def exitNot(ctx: NotContext) = ctx.value_= :
Not(ctx.bool.value)

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

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

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

override def exitRelOp(ctx: RelOpContext): Unit = 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
8 changes: 5 additions & 3 deletions src/main/scala/whilelang/parser/Syntax.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package whilelang.parser

enum Statement:
trait Element

enum Statement extends Element:
case Skip
case If(condition: Bool, thenSmt: Statement, elseSmt: Statement)
case Write(exp: Expression)
Expand All @@ -10,15 +12,15 @@ enum Statement:
case Attrib(id: String, exp: Expression)
case Program(statements: SeqStatement)

enum Expression:
enum Expression extends Element:
case Read
case Id(id: String)
case Integer(exp: Int)
case ExpSum(lhs: Expression, rhs: Expression)
case ExpSub(lhs: Expression, rhs: Expression)
case ExpMult(lhs: Expression, rhs: Expression)

enum Bool:
enum Bool extends Element:
case Boole(b: Boolean)
case ExpEq(lhs: Expression, rhs: Expression)
case ExpLe(lhs: Expression, rhs: Expression)
Expand Down
5 changes: 3 additions & 2 deletions src/main/scala/whilelang/util/ContextValue.scala
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package whilelang.util

import org.antlr.v4.runtime.tree.{ParseTree, ParseTreeProperty as Property}
import org.antlr.v4.runtime.tree.{ParseTree, ParseTreeProperty}
import scala.jdk.CollectionConverters.ListHasAsScala
import whilelang.parser.Element

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

extension (tree: ParseTree)
def apply(i: Int): ParseTree = tree.getChild(i)
Expand Down
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.3.3/WhileLang-assembly-1.3.3.jar $1
java -Xmx1879m -jar ./target/scala-3.4.0/WhileLang-assembly-1.3.4.jar $1

0 comments on commit 2b4766a

Please sign in to comment.