From 6904aa37e4c36527a1f701a410c9c7914cd8e8e0 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Thu, 13 Mar 2025 13:31:47 +0000 Subject: [PATCH] style: scala format --- src/main/wacc/Main.scala | 3 ++- src/main/wacc/frontend/ast.scala | 29 ++++++++++++++------ src/main/wacc/frontend/parser.scala | 41 ++++++++++++++++++----------- 3 files changed, 48 insertions(+), 25 deletions(-) diff --git a/src/main/wacc/Main.scala b/src/main/wacc/Main.scala index 68688bc..add3eec 100644 --- a/src/main/wacc/Main.scala +++ b/src/main/wacc/Main.scala @@ -69,7 +69,8 @@ val outputOpt: Opts[Option[Path]] = .orNone def frontend( - contents: String, file: File + contents: String, + file: File ): Either[NonEmptyList[Error], microWacc.Program] = parser.parse(contents) match { case Failure(msg) => Left(NonEmptyList.one(Error.SyntaxError(msg))) diff --git a/src/main/wacc/frontend/ast.scala b/src/main/wacc/frontend/ast.scala index 4a397a9..a291070 100644 --- a/src/main/wacc/frontend/ast.scala +++ b/src/main/wacc/frontend/ast.scala @@ -134,7 +134,8 @@ object ast { extends Type with PairElemType object ArrayType extends ParserBridgePos2Chain[Int, Type, ArrayType] { - def apply(dimensions: Int, elemType: Type)(pos: Position): ArrayType = ArrayType(elemType, dimensions)(pos) + def apply(dimensions: Int, elemType: Type)(pos: Position): ArrayType = + ArrayType(elemType, dimensions)(pos) } case class PairType(fst: PairElemType, snd: PairElemType)(val pos: Position) extends Type object PairType extends ParserBridgePos2[PairElemType, PairElemType, PairType] @@ -171,9 +172,12 @@ object ast { object FuncDecl extends ParserBridgePos2Chain[ (List[Param], NonEmptyList[Stmt]), - ((Type, Ident)), FuncDecl + ((Type, Ident)), + FuncDecl ] { - def apply(paramsBody: (List[Param], NonEmptyList[Stmt]), retTyName: (Type, Ident))(pos: Position): FuncDecl = + def apply(paramsBody: (List[Param], NonEmptyList[Stmt]), retTyName: (Type, Ident))( + pos: Position + ): FuncDecl = new FuncDecl(retTyName._1, retTyName._2, paramsBody._1, paramsBody._2)(pos) } @@ -261,15 +265,19 @@ object ast { a => file => this.apply(a(file))(Position(pos._1, pos._2, file)) } - trait ParserBridgePos2Chain[-A, -B, +C] extends ParserSingletonBridgePos[(File => A) => (File => B) => File => C] { + trait ParserBridgePos2Chain[-A, -B, +C] + extends ParserSingletonBridgePos[(File => A) => (File => B) => File => C] { def apply(a: A, b: B)(pos: Position): C - def apply(a: Parsley[File =>A]): Parsley[(File => B) => File => C] = error(ap1(pos.map(con), a)) + def apply(a: Parsley[File => A]): Parsley[(File => B) => File => C] = error( + ap1(pos.map(con), a) + ) override final def con(pos: (Int, Int)): (File => A) => (File => B) => File => C = a => b => file => this.apply(a(file), b(file))(Position(pos._1, pos._2, file)) } - trait ParserBridgePos2[-A, -B, +C] extends ParserSingletonBridgePos[(File => A, File => B) => File => C] { + trait ParserBridgePos2[-A, -B, +C] + extends ParserSingletonBridgePos[(File => A, File => B) => File => C] { def apply(a: A, b: B)(pos: Position): C def apply(a: Parsley[File => A], b: => Parsley[File => B]): Parsley[File => C] = error( ap2(pos.map(con), a, b) @@ -279,9 +287,14 @@ object ast { (a, b) => file => this.apply(a(file), b(file))(Position(pos._1, pos._2, file)) } - trait ParserBridgePos3[-A, -B, -C, +D] extends ParserSingletonBridgePos[(File => A, File => B, File => C) => File => D] { + trait ParserBridgePos3[-A, -B, -C, +D] + extends ParserSingletonBridgePos[(File => A, File => B, File => C) => File => D] { def apply(a: A, b: B, c: C)(pos: Position): D - def apply(a: Parsley[File => A], b: => Parsley[File => B], c: => Parsley[File => C]): Parsley[File => D] = error( + def apply( + a: Parsley[File => A], + b: => Parsley[File => B], + c: => Parsley[File => C] + ): Parsley[File => D] = error( ap3(pos.map(con), a, b, c) ) diff --git a/src/main/wacc/frontend/parser.scala b/src/main/wacc/frontend/parser.scala index 3b8ead4..c9d36f3 100644 --- a/src/main/wacc/frontend/parser.scala +++ b/src/main/wacc/frontend/parser.scala @@ -76,8 +76,8 @@ object parser { // Expressions private lazy val ``: FParsley[Expr] = precedence { // SOps(InfixR)(Or from "||") +: - // SOps(InfixR)(And from "&&") +: - SOps(InfixN)(Eq from "==", Neq from "!=") +: + // SOps(InfixR)(And from "&&") +: + SOps(InfixN)(Eq from "==", Neq from "!=") +: SOps(InfixN)( Less from "<", LessEq from "<=", @@ -161,11 +161,15 @@ object parser { ) private lazy val `` = Program( "begin" ~> ( - fList(many( - fPair(atomic( - ``.label("function declaration") <~> `` <~ "(" - )) <**> `` - ).label("function declaration")) | + fList( + many( + fPair( + atomic( + ``.label("function declaration") <~> `` <~ "(" + ) + ) <**> `` + ).label("function declaration") + ) | atomic(`` <~ "(").verifiedExplain("function declaration is missing return type") ), ``.label( @@ -174,18 +178,23 @@ object parser { ) private lazy val `` = FuncDecl( - fPair((fList(sepBy(``, ",")) <~ ")" <~ "is") <~> - (``.guardAgainst { - // TODO: passing in an arbitrary file works but is ugly - case stmts if !(stmts(File("."))).isReturning => Seq("all functions must end in a returning statement") - } <~ "end")) + fPair( + (fList(sepBy(``, ",")) <~ ")" <~ "is") <~> + (``.guardAgainst { + // TODO: passing in an arbitrary file works but is ugly + case stmts if !(stmts(File("."))).isReturning => + Seq("all functions must end in a returning statement") + } <~ "end") + ) ) private lazy val `` = Param(``, ``) private lazy val ``: FParsley[NonEmptyList[Stmt]] = - fNonEmptyList(( - ``.label("main program body"), - (many(";" ~> ``.label("statement after ';'"))) Nil - ).zipped(NonEmptyList.apply)) + fNonEmptyList( + ( + ``.label("main program body"), + (many(";" ~> ``.label("statement after ';'"))) Nil + ).zipped(NonEmptyList.apply) + ) private lazy val `` = (Skip from "skip")