Skip to content

Commit

Permalink
fix(compiler): Fix if with brackets parsing (#812)
Browse files Browse the repository at this point in the history
  • Loading branch information
InversionSpaces authored Jul 25, 2023
1 parent 50ba194 commit 4c3c32b
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
4 changes: 3 additions & 1 deletion parser/src/main/scala/aqua/parser/expr/func/ArrowExpr.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ object ArrowExpr extends Expr.AndIndented {
PushToStreamExpr ::
ForExpr ::
Expr.defer(OnExpr) ::
CallArrowExpr ::
// It is important for IfExpr to be before CallArrowExpr
// because `if (1 + 1) == 2` is parsed as if `if(1 + 1)` is an arrow call
IfExpr ::
CallArrowExpr ::
TryExpr ::
ElseOtherwiseExpr ::
CatchExpr ::
Expand Down
25 changes: 25 additions & 0 deletions parser/src/test/scala/aqua/parser/FuncExprSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import aqua.parser.lift.LiftParser.Implicits.idLiftParser
import aqua.types.ScalarType.*
import cats.Id
import cats.data.{Chain, NonEmptyList}
import cats.data.Chain.*
import cats.free.Cofree
import cats.syntax.foldable.*
import cats.data.Validated.{Invalid, Valid}
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.{Inside, Inspectors}
import org.scalatest.matchers.should.Matchers
import cats.~>
import cats.Eval

import scala.collection.mutable
import scala.language.implicitConversions
Expand Down Expand Up @@ -286,4 +288,27 @@ class FuncExprSpec extends AnyFlatSpec with Matchers with Inside with Inspectors

parser.parseAll(script).value.toEither.isRight shouldBe true
}

"function with if" should "be parsed correctly" in {
val script =
"""func ifTest():
| if (1 + 1) == 2:
| Test.test("one")
|
| if 2 < 3 != (1 > 2):
| Test.test("two")
|""".stripMargin

inside(parser.parseAll(script).value) { case Valid(ast) =>
ast
.cata[Int]((expr, results) =>
// Count `if`s inside the tree
Eval.later(results.sumAll + (expr match {
case IfExpr(_, _, _) => 1
case _ => 0
}))
)
.value shouldBe 2
}
}
}

0 comments on commit 4c3c32b

Please sign in to comment.