From 5361373c017b72fe086b7c673687ba51ab40d536 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Sat, 1 Feb 2025 17:15:14 +0000 Subject: [PATCH 1/3] refactor: non-recursive statements and array types --- src/main/wacc/ast.scala | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main/wacc/ast.scala b/src/main/wacc/ast.scala index 59fb9fa..4af5b1d 100644 --- a/src/main/wacc/ast.scala +++ b/src/main/wacc/ast.scala @@ -78,8 +78,10 @@ object ast { case object BoolType extends BaseType with ParserBridge0[BoolType.type] case object CharType extends BaseType with ParserBridge0[CharType.type] case object StringType extends BaseType with ParserBridge0[StringType.type] - case class ArrayType(elemType: Type) extends Type with PairElemType - object ArrayType extends ParserBridge1[Type, ArrayType] + case class ArrayType(elemType: Type, dimensions: Int) + extends Type + with PairElemType + object ArrayType extends ParserBridge2[Type, Int, ArrayType] case class PairType(fst: PairElemType, snd: PairElemType) extends Type object PairType extends ParserBridge2[PairElemType, PairElemType, PairType] @@ -89,18 +91,18 @@ object ast { with ParserBridge0[UntypedPairType.type] // waccadoodledo - case class Program(funcs: List[FuncDecl], main: Stmt) - object Program extends ParserBridge2[List[FuncDecl], Stmt, Program] + case class Program(funcs: List[FuncDecl], main: List[Stmt]) + object Program extends ParserBridge2[List[FuncDecl], List[Stmt], Program] // Function Definitions case class FuncDecl( returnType: Type, name: Ident, params: List[Param], - body: Stmt + body: List[Stmt] ) object FuncDecl - extends ParserBridge4[Type, Ident, List[Param], Stmt, FuncDecl] + extends ParserBridge4[Type, Ident, List[Param], List[Stmt], FuncDecl] case class Param(paramType: Type, name: Ident) object Param extends ParserBridge2[Type, Ident, Param] @@ -122,14 +124,13 @@ object ast { object Exit extends ParserBridge1[Expr, Exit] case class Print(expr: Expr, newline: Boolean) extends Stmt object Print extends ParserBridge2[Expr, Boolean, Print] - case class If(cond: Expr, thenStmt: Stmt, elseStmt: Stmt) extends Stmt - object If extends ParserBridge3[Expr, Stmt, Stmt, If] - case class While(cond: Expr, body: Stmt) extends Stmt - object While extends ParserBridge2[Expr, Stmt, While] - case class Block(stmt: Stmt) extends Stmt - object Block extends ParserBridge1[Stmt, Block] - case class SeqStmt(stmt1: Stmt, stmt2: Stmt) extends Stmt - object SeqStmt extends ParserBridge2[Stmt, Stmt, SeqStmt] + case class If(cond: Expr, thenStmt: List[Stmt], elseStmt: List[Stmt]) + extends Stmt + object If extends ParserBridge3[Expr, List[Stmt], List[Stmt], If] + case class While(cond: Expr, body: List[Stmt]) extends Stmt + object While extends ParserBridge2[Expr, List[Stmt], While] + case class Block(stmt: List[Stmt]) extends Stmt + object Block extends ParserBridge1[List[Stmt], Block] sealed trait LValue From 7320052939a6d7113f95075116140c92bb101e98 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Sat, 1 Feb 2025 20:33:27 +0000 Subject: [PATCH 2/3] refactor: increase max line length to 100 --- .scalafmt.conf | 2 ++ src/test/wacc/examples.scala | 5 +---- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/.scalafmt.conf b/.scalafmt.conf index 2f0c08c..765f3f8 100644 --- a/.scalafmt.conf +++ b/.scalafmt.conf @@ -1,2 +1,4 @@ version = 3.8.6 runner.dialect = scala3 + +maxColumn = 100 diff --git a/src/test/wacc/examples.scala b/src/test/wacc/examples.scala index ef48195..0016630 100644 --- a/src/test/wacc/examples.scala +++ b/src/test/wacc/examples.scala @@ -5,10 +5,7 @@ import org.scalatest.flatspec.AnyFlatSpec import org.scalatest.Inspectors.forEvery import parsley.{Success, Failure} -class ParallelExamplesSpec - extends AnyFlatSpec - with BeforeAndAfterAll - with ParallelTestExecution { +class ParallelExamplesSpec extends AnyFlatSpec with BeforeAndAfterAll with ParallelTestExecution { val files = allWaccFiles("wacc-examples/valid").map { p => (p.toString, List(0)) From 1d78072c22478e96447319bca99e71f37ff51351 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Sat, 1 Feb 2025 20:39:56 +0000 Subject: [PATCH 3/3] feat: use NoneEmptyList from cats to enfroce non-empty statements --- project.scala | 1 + src/main/wacc/ast.scala | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/project.scala b/project.scala index 7a42a9c..e6fb151 100644 --- a/project.scala +++ b/project.scala @@ -3,6 +3,7 @@ // dependencies //> using dep com.github.j-mie6::parsley::5.0.0-M10 +//> using dep com.github.j-mie6::parsley-cats::1.3.0 //> using dep com.lihaoyi::os-lib::0.11.3 //> using dep com.github.scopt::scopt::4.1.0 //> using test.dep org.scalatest::scalatest::3.2.19 diff --git a/src/main/wacc/ast.scala b/src/main/wacc/ast.scala index d5c2cca..c6e743e 100644 --- a/src/main/wacc/ast.scala +++ b/src/main/wacc/ast.scala @@ -1,6 +1,7 @@ package wacc import parsley.generic._ +import cats.data.NonEmptyList object ast { // Expressions @@ -24,8 +25,8 @@ object ast { case object PairLiter extends Expr6 with ParserBridge0[PairLiter.type] case class Ident(v: String) extends Expr6 with LValue object Ident extends ParserBridge1[String, Ident] - case class ArrayElem(name: Ident, indices: List[Expr]) extends Expr6 with LValue - object ArrayElem extends ParserBridge2[Ident, List[Expr], ArrayElem] + case class ArrayElem(name: Ident, indices: NonEmptyList[Expr]) extends Expr6 with LValue + object ArrayElem extends ParserBridge2[Ident, NonEmptyList[Expr], ArrayElem] case class Parens(expr: Expr) extends Expr6 object Parens extends ParserBridge1[Expr, Parens] @@ -85,17 +86,17 @@ object ast { case object UntypedPairType extends PairElemType with ParserBridge0[UntypedPairType.type] // waccadoodledo - case class Program(funcs: List[FuncDecl], main: List[Stmt]) - object Program extends ParserBridge2[List[FuncDecl], List[Stmt], Program] + case class Program(funcs: List[FuncDecl], main: NonEmptyList[Stmt]) + object Program extends ParserBridge2[List[FuncDecl], NonEmptyList[Stmt], Program] // Function Definitions case class FuncDecl( returnType: Type, name: Ident, params: List[Param], - body: List[Stmt] + body: NonEmptyList[Stmt] ) - object FuncDecl extends ParserBridge4[Type, Ident, List[Param], List[Stmt], FuncDecl] + object FuncDecl extends ParserBridge4[Type, Ident, List[Param], NonEmptyList[Stmt], FuncDecl] case class Param(paramType: Type, name: Ident) object Param extends ParserBridge2[Type, Ident, Param] @@ -117,12 +118,12 @@ object ast { object Exit extends ParserBridge1[Expr, Exit] case class Print(expr: Expr, newline: Boolean) extends Stmt object Print extends ParserBridge2[Expr, Boolean, Print] - case class If(cond: Expr, thenStmt: List[Stmt], elseStmt: List[Stmt]) extends Stmt - object If extends ParserBridge3[Expr, List[Stmt], List[Stmt], If] - case class While(cond: Expr, body: List[Stmt]) extends Stmt - object While extends ParserBridge2[Expr, List[Stmt], While] - case class Block(stmt: List[Stmt]) extends Stmt - object Block extends ParserBridge1[List[Stmt], Block] + case class If(cond: Expr, thenStmt: NonEmptyList[Stmt], elseStmt: NonEmptyList[Stmt]) extends Stmt + object If extends ParserBridge3[Expr, NonEmptyList[Stmt], NonEmptyList[Stmt], If] + case class While(cond: Expr, body: NonEmptyList[Stmt]) extends Stmt + object While extends ParserBridge2[Expr, NonEmptyList[Stmt], While] + case class Block(stmt: NonEmptyList[Stmt]) extends Stmt + object Block extends ParserBridge1[NonEmptyList[Stmt], Block] sealed trait LValue