feat: show exact statement which must be returning at the end of a function

This commit is contained in:
Gleb Koval 2025-02-02 00:00:09 +00:00
parent a71045867a
commit cb9796fa87
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
2 changed files with 15 additions and 9 deletions

View File

@ -105,10 +105,16 @@ object ast {
x3: => Parsley[List[Param]], x3: => Parsley[List[Param]],
x4: => Parsley[NonEmptyList[Stmt]] x4: => Parsley[NonEmptyList[Stmt]]
): Parsley[FuncDecl] = ): Parsley[FuncDecl] =
super.apply(x1, x2, x3, x4).guardAgainst { super.apply(
case FuncDecl(_, _, _, body) if !body.isReturning => x1,
Seq("Function must return on all paths") x2,
} x3,
x4.guardAgainst {
case body if !body.isReturning =>
println(body)
Seq("All functions must end in a returning statement")
}
)
} }
case class Param(paramType: Type, name: Ident) case class Param(paramType: Type, name: Ident)

View File

@ -75,15 +75,15 @@ object parser {
// Statements // Statements
private lazy val `<program>` = Program( private lazy val `<program>` = Program(
"begin" ~> many(atomic(`<func>`)), "begin" ~> many(`<func>`),
`<stmt>` <~ "end" `<stmt>` <~ "end"
) )
private lazy val `<func>` = FuncDecl( private lazy val `<func>` = FuncDecl(
`<type>`, atomic(`<type>`),
`<ident>` <~ "(", atomic(`<ident>`) <~ "(",
sepBy(`<param>`, ",") <~ ")" <~ "is", sepBy(`<param>`, ",") <~ ")" <~ "is",
`<stmt>` <~ "end" `<stmt>`
) ) <~ "end"
private lazy val `<param>` = Param(`<type>`, `<ident>`) private lazy val `<param>` = Param(`<type>`, `<ident>`)
private lazy val `<stmt>`: Parsley[NonEmptyList[Stmt]] = private lazy val `<stmt>`: Parsley[NonEmptyList[Stmt]] =
sepBy1(`<basic-stmt>`, ";") sepBy1(`<basic-stmt>`, ";")