From 67e85688b20b14d999191760e7e846350886cde7 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Thu, 13 Mar 2025 14:03:53 +0000 Subject: [PATCH] refactor: fMap to replace fOption, fList and fNonEmptyList --- src/main/wacc/frontend/parser.scala | 30 +++++++++++++---------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/src/main/wacc/frontend/parser.scala b/src/main/wacc/frontend/parser.scala index 232474f..ce9283c 100644 --- a/src/main/wacc/frontend/parser.scala +++ b/src/main/wacc/frontend/parser.scala @@ -10,11 +10,13 @@ import parsley.errors.combinator._ import parsley.errors.patterns.VerifiedErrors import parsley.syntax.zipped._ import parsley.cats.combinator.{some, sepBy1} +import cats.syntax.all._ import cats.data.NonEmptyList import parsley.errors.DefaultErrorBuilder import parsley.errors.ErrorBuilder import parsley.errors.tokenextractors.LexToken import parsley.expr.GOps +import cats.Functor object parser { import lexer.implicits.implicitSymbol @@ -62,17 +64,11 @@ object parser { private def fParsley[A](p: Parsley[A]): FParsley[A] = p map { a => file => a } - private def fList[A](p: Parsley[List[File => A]]): FParsley[List[A]] = - p map { l => file => l.map(_(file)) } - - private def fNonEmptyList[A](p: Parsley[NonEmptyList[File => A]]): FParsley[NonEmptyList[A]] = - p map { l => file => l.map(_(file)) } - private def fPair[A, B](p: Parsley[(File => A, File => B)]): FParsley[(A, B)] = p map { case (a, b) => file => (a(file), b(file)) } - private def fOption[A](p: Parsley[Option[File => A]]): FParsley[Option[A]] = - p map { l => file => l.map(_(file)) } + private def fMap[A, F[_]: Functor](p: Parsley[F[File => A]]): FParsley[F[A]] = + p map { funcs => file => funcs.map(_(file)) } // Expressions private lazy val ``: FParsley[Expr] = precedence { @@ -118,7 +114,7 @@ object parser { (`` <~ ("(".verifiedExplain( "functions can only be called using 'call' keyword" ) | unit)) <**> (`` identity) - private lazy val `` = ArrayElem(fNonEmptyList(some("[" ~> `` <~ "]"))) + private lazy val `` = ArrayElem(fMap(some("[" ~> `` <~ "]"))) // Types private lazy val ``: FParsley[Type] = @@ -148,21 +144,21 @@ object parser { concern. */ private lazy val `` = PartialProgram( - fList(many(``)), + fMap(many(``)), `` ) private lazy val `` = Import( "import" ~> ``, - "(" ~> fNonEmptyList(sepBy1(``, ",")) <~ ")" + "(" ~> fMap(sepBy1(``, ",")) <~ ")" ) private lazy val `` = ``.label("import file name") private lazy val `` = ImportedFunc( ``.label("imported function name"), - fOption(option("as" ~> ``)).label("imported function alias") + fMap(option("as" ~> ``)).label("imported function alias") ) private lazy val `` = Program( "begin" ~> ( - fList( + fMap( many( fPair( atomic( @@ -180,7 +176,7 @@ object parser { private lazy val `` = FuncDecl( fPair( - (fList(sepBy(``, ",")) <~ ")" <~ "is") <~> + (fMap(sepBy(``, ",")) <~ ")" <~ "is") <~> (``.guardAgainst { // TODO: passing in an arbitrary file works but is ugly case stmts if !(stmts(File("."))).isReturning => @@ -190,7 +186,7 @@ object parser { ) private lazy val `` = Param(``, ``) private lazy val ``: FParsley[NonEmptyList[Stmt]] = - fNonEmptyList( + fMap( ( ``.label("main program body"), (many(";" ~> ``.label("statement after ';'"))) Nil @@ -239,13 +235,13 @@ object parser { `` | Call( "call" ~> `` <~ "(", - fList(sepBy(``, ",")) <~ ")" + fMap(sepBy(``, ",")) <~ ")" ) | ``.labelWithType(LabelType.Expr) private lazy val `` = Fst("fst" ~> ``.label("valid pair")) | Snd("snd" ~> ``.label("valid pair")) private lazy val `` = ArrayLiter( - "[" ~> fList(sepBy(``, ",")) <~ "]" + "[" ~> fMap(sepBy(``, ",")) <~ "]" ) extension (stmts: NonEmptyList[Stmt]) {