style: scala format

This commit is contained in:
2025-03-13 13:31:47 +00:00
parent 5141a2369f
commit 6904aa37e4
3 changed files with 48 additions and 25 deletions

View File

@@ -69,7 +69,8 @@ val outputOpt: Opts[Option[Path]] =
.orNone
def frontend(
contents: String, file: File
contents: String,
file: File
): Either[NonEmptyList[Error], microWacc.Program] =
parser.parse(contents) match {
case Failure(msg) => Left(NonEmptyList.one(Error.SyntaxError(msg)))

View File

@@ -134,7 +134,8 @@ object ast {
extends Type
with PairElemType
object ArrayType extends ParserBridgePos2Chain[Int, Type, ArrayType] {
def apply(dimensions: Int, elemType: Type)(pos: Position): ArrayType = ArrayType(elemType, dimensions)(pos)
def apply(dimensions: Int, elemType: Type)(pos: Position): ArrayType =
ArrayType(elemType, dimensions)(pos)
}
case class PairType(fst: PairElemType, snd: PairElemType)(val pos: Position) extends Type
object PairType extends ParserBridgePos2[PairElemType, PairElemType, PairType]
@@ -171,9 +172,12 @@ object ast {
object FuncDecl
extends ParserBridgePos2Chain[
(List[Param], NonEmptyList[Stmt]),
((Type, Ident)), FuncDecl
((Type, Ident)),
FuncDecl
] {
def apply(paramsBody: (List[Param], NonEmptyList[Stmt]), retTyName: (Type, Ident))(pos: Position): FuncDecl =
def apply(paramsBody: (List[Param], NonEmptyList[Stmt]), retTyName: (Type, Ident))(
pos: Position
): FuncDecl =
new FuncDecl(retTyName._1, retTyName._2, paramsBody._1, paramsBody._2)(pos)
}
@@ -261,15 +265,19 @@ object ast {
a => file => this.apply(a(file))(Position(pos._1, pos._2, file))
}
trait ParserBridgePos2Chain[-A, -B, +C] extends ParserSingletonBridgePos[(File => A) => (File => B) => File => C] {
trait ParserBridgePos2Chain[-A, -B, +C]
extends ParserSingletonBridgePos[(File => A) => (File => B) => File => C] {
def apply(a: A, b: B)(pos: Position): C
def apply(a: Parsley[File =>A]): Parsley[(File => B) => File => C] = error(ap1(pos.map(con), a))
def apply(a: Parsley[File => A]): Parsley[(File => B) => File => C] = error(
ap1(pos.map(con), a)
)
override final def con(pos: (Int, Int)): (File => A) => (File => B) => File => C =
a => b => file => this.apply(a(file), b(file))(Position(pos._1, pos._2, file))
}
trait ParserBridgePos2[-A, -B, +C] extends ParserSingletonBridgePos[(File => A, File => B) => File => C] {
trait ParserBridgePos2[-A, -B, +C]
extends ParserSingletonBridgePos[(File => A, File => B) => File => C] {
def apply(a: A, b: B)(pos: Position): C
def apply(a: Parsley[File => A], b: => Parsley[File => B]): Parsley[File => C] = error(
ap2(pos.map(con), a, b)
@@ -279,9 +287,14 @@ object ast {
(a, b) => file => this.apply(a(file), b(file))(Position(pos._1, pos._2, file))
}
trait ParserBridgePos3[-A, -B, -C, +D] extends ParserSingletonBridgePos[(File => A, File => B, File => C) => File => D] {
trait ParserBridgePos3[-A, -B, -C, +D]
extends ParserSingletonBridgePos[(File => A, File => B, File => C) => File => D] {
def apply(a: A, b: B, c: C)(pos: Position): D
def apply(a: Parsley[File => A], b: => Parsley[File => B], c: => Parsley[File => C]): Parsley[File => D] = error(
def apply(
a: Parsley[File => A],
b: => Parsley[File => B],
c: => Parsley[File => C]
): Parsley[File => D] = error(
ap3(pos.map(con), a, b, c)
)

View File

@@ -76,8 +76,8 @@ object parser {
// Expressions
private lazy val `<expr>`: FParsley[Expr] = precedence {
// SOps(InfixR)(Or from "||") +:
// SOps(InfixR)(And from "&&") +:
SOps(InfixN)(Eq from "==", Neq from "!=") +:
// SOps(InfixR)(And from "&&") +:
SOps(InfixN)(Eq from "==", Neq from "!=") +:
SOps(InfixN)(
Less from "<",
LessEq from "<=",
@@ -161,11 +161,15 @@ object parser {
)
private lazy val `<program>` = Program(
"begin" ~> (
fList(many(
fPair(atomic(
`<type>`.label("function declaration") <~> `<ident>` <~ "("
)) <**> `<partial-func-decl>`
).label("function declaration")) |
fList(
many(
fPair(
atomic(
`<type>`.label("function declaration") <~> `<ident>` <~ "("
)
) <**> `<partial-func-decl>`
).label("function declaration")
) |
atomic(`<ident>` <~ "(").verifiedExplain("function declaration is missing return type")
),
`<stmt>`.label(
@@ -174,18 +178,23 @@ object parser {
)
private lazy val `<partial-func-decl>` =
FuncDecl(
fPair((fList(sepBy(`<param>`, ",")) <~ ")" <~ "is") <~>
(`<stmt>`.guardAgainst {
// TODO: passing in an arbitrary file works but is ugly
case stmts if !(stmts(File("."))).isReturning => Seq("all functions must end in a returning statement")
} <~ "end"))
fPair(
(fList(sepBy(`<param>`, ",")) <~ ")" <~ "is") <~>
(`<stmt>`.guardAgainst {
// TODO: passing in an arbitrary file works but is ugly
case stmts if !(stmts(File("."))).isReturning =>
Seq("all functions must end in a returning statement")
} <~ "end")
)
)
private lazy val `<param>` = Param(`<type>`, `<ident>`)
private lazy val `<stmt>`: FParsley[NonEmptyList[Stmt]] =
fNonEmptyList((
`<basic-stmt>`.label("main program body"),
(many(";" ~> `<basic-stmt>`.label("statement after ';'"))) </> Nil
).zipped(NonEmptyList.apply))
fNonEmptyList(
(
`<basic-stmt>`.label("main program body"),
(many(";" ~> `<basic-stmt>`.label("statement after ';'"))) </> Nil
).zipped(NonEmptyList.apply)
)
private lazy val `<basic-stmt>` =
(Skip from "skip")