From 0955f19a816f134bcac188ec36f690e3367f30b6 Mon Sep 17 00:00:00 2001 From: Gleb Koval Date: Fri, 7 Feb 2025 15:39:04 +0000 Subject: [PATCH] feat: detect function declarations mid-body --- src/main/wacc/parser.scala | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/wacc/parser.scala b/src/main/wacc/parser.scala index dac9fcd..8098e84 100644 --- a/src/main/wacc/parser.scala +++ b/src/main/wacc/parser.scala @@ -6,6 +6,7 @@ import parsley.Parsley.{atomic, many, notFollowedBy, pure} import parsley.combinator.{countSome, sepBy} import parsley.expr.{precedence, SOps, InfixL, InfixN, InfixR, Prefix, Atoms} import parsley.errors.combinator._ +import parsley.errors.patterns.VerifiedErrors import parsley.syntax.zipped._ import parsley.cats.combinator.{some} import cats.data.NonEmptyList @@ -126,7 +127,7 @@ object parser { FuncDecl( sepBy(``, ",") <~ ")" <~ "is", ``.guardAgainst { - case stmts if !stmts.isReturning => Seq("All functions must end in a returning statement") + case stmts if !stmts.isReturning => Seq("all functions must end in a returning statement") } <~ "end" ) private lazy val `` = Param(``, ``) @@ -151,7 +152,13 @@ object parser { ) | While("while" ~> ``.labelWithType(LabelType.Expr) <~ "do", `` <~ "done") | Block("begin" ~> `` <~ "end") - | VarDecl(``, `` <~ "=", ``.label("valid initial value for variable")) + | VarDecl( + ``, + `` <~ ("=" | "(".verifiedExplain( + "all function declarations must be above the main program body" + )), + ``.label("valid initial value for variable") + ) // TODO: Can we inline the name of the variable in the message | Assign(`` <~ "=", ``) private lazy val ``: Parsley[LValue] =