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]],
x4: => Parsley[NonEmptyList[Stmt]]
): Parsley[FuncDecl] =
super.apply(x1, x2, x3, x4).guardAgainst {
case FuncDecl(_, _, _, body) if !body.isReturning =>
Seq("Function must return on all paths")
super.apply(
x1,
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)

View File

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