From 883abd98445ff5e52d3d50a30268c1e59bb6089a Mon Sep 17 00:00:00 2001 From: Harry Li Date: Wed, 30 Oct 2024 12:23:33 +0800 Subject: [PATCH] Generalize `TermDef` for multiple parameter lists The member `params` is renamed to `paramLists` to reflect its new behavior. The type of `paramLists` is changed from `Opt[Ls[Tree]]` to `Ls[Tup]` to reflect its new behavior. It now returns a list of parameter tuples instead of a list of parameters. --- .../src/main/scala/hkmc2/syntax/Tree.scala | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/hkmc2/shared/src/main/scala/hkmc2/syntax/Tree.scala b/hkmc2/shared/src/main/scala/hkmc2/syntax/Tree.scala index 555131789..2c13ce93e 100644 --- a/hkmc2/shared/src/main/scala/hkmc2/syntax/Tree.scala +++ b/hkmc2/shared/src/main/scala/hkmc2/syntax/Tree.scala @@ -204,31 +204,33 @@ private def getName(t: Tree, symNme: Opt[Tree]): (Opt[Tree], Diagnostic \/ Ident trait TermDefImpl: this: TermDef => - lazy val (symName, name, params, typeParams, signature): (Opt[Tree], Diagnostic \/ Ident, Opt[Ls[Tree]], Opt[Tree], Opt[Tree]) = - def rec(t: Tree, symName: Opt[Tree]): - (Opt[Tree], Diagnostic \/ Ident, Opt[Ls[Tree]], Opt[Tree], Opt[Tree]) = - t match - case InfixApp(id: Ident, Keyword.`:`, sign) => - (symName, R(id), N, N, S(sign)) - // show(t: Tree): Str - case InfixApp(App(id: Ident, args), Keyword.`:`, ret) => - (symName, R(id), S(args :: Nil), N, N) - // show[A](t: Tree[A]): Str - case InfixApp(App(App(id: Ident, typeParams: TyTup), args), Keyword.`:`, ret) => - // val sign = S(InfixApp(typeParams, Keyword.`->`, InfixApp(args, Keyword.`->`, ret))) - (symName, R(id), S(args :: Nil), S(typeParams), N) + lazy val (symName, name, paramLists, typeParams, signature): (Opt[Tree], Diagnostic \/ Ident, Ls[Tup], Opt[Tree], Opt[Tree]) = + def rec(t: Tree, symName: Opt[Tree]): (Opt[Tree], Diagnostic \/ Ident, Ls[Tup], Opt[Tree], Opt[Tree]) = t match + // fun f: Int + // fun f(n1: Int): Int + // fun f(n1: Int)(nn: Int): Int + case InfixApp(Apps(id: Ident, paramLists), Keyword.`:`, sign) => + (symName, R(id), paramLists, N, S(sign)) + // fun f[T]: Int + // fun f[T](n1: Int): Int + // fun f[T](n1: Int)(nn: Int): Int + case InfixApp(Apps(App(id: Ident, typeParams: TyTup), paramLists), Keyword.`:`, ret) => + (symName, R(id), paramLists, S(typeParams), N) + case InfixApp(Jux(lhs, rhs), Keyword.`:`, ret) => rec(InfixApp(rhs, Keyword.`:`, ret), S(lhs)) - case id: Ident => - (symName, R(id), N, N, N) - case App(id: Ident, typeParams: TyTup) => - (symName, R(id), N, S(typeParams), N) - case App(id: Ident, args) => - (symName, R(id), S(args :: Nil), N, N) - case App(primary: App, second) => - // TODO: handle the second parameter list - val (sn, id, first, tps, sig) = rec(primary, symName) - (sn, id, S(first.fold(Ls(second))(_ :+ second)), tps, sig) + + // fun f + // fun f(n1: Int) + // fun f(n1: Int)(nn: Int) + case Apps(id: Ident, paramLists) => + (symName, R(id), paramLists, N, N) + // fun f[T] + // fun f[T](n1: Int) + // fun f[T](n1: Int)(nn: Int) + case Apps(App(id: Ident, typeParams: TyTup), paramLists) => + (symName, R(id), paramLists, S(typeParams), N) + case Jux(lhs, rhs) => // happens in `fun (op) nme` form require(symName.isEmpty) // TOOD rec(rhs, S(lhs))