@@ -866,14 +866,14 @@ object IfBodyHelpers {
866
866
s " $scrutinee is $className"
867
867
case IfBodyHelpers .Condition .MatchTuple (scrutinee, arity, fields) =>
868
868
s " $scrutinee is Tuple# $arity"
869
- }.mkString(" " , " and " , s " => $term" ))
869
+ }.mkString(" « " , " » and « " , s " » => $term" ))
870
870
}
871
871
}
872
872
}
873
873
874
874
abstract class MutCaseOf {
875
- def append (branch : Ls [IfBodyHelpers .Condition ] -> Term ): Unit
876
- def toTerm ( implicit fallback : Opt [ Term ]) : Term
875
+ def merge (branch : Ls [IfBodyHelpers .Condition ] -> Term )( implicit raise : Diagnostic => Unit ): Unit
876
+ def toTerm : Term
877
877
}
878
878
879
879
object MutCaseOf {
@@ -933,38 +933,39 @@ object MutCaseOf {
933
933
934
934
// A short-hand for pattern matchings with only true and false branches.
935
935
final case class IfThenElse (condition : Term , var whenTrue : MutCaseOf , var whenFalse : MutCaseOf ) extends MutCaseOf {
936
- override def append (branch : Ls [Condition ] -> Term ): Unit = branch match {
937
- case Nil -> term =>
938
- whenFalse match {
939
- case Consequent (_) => ??? // duplicated branch
940
- case MissingCase => whenFalse = Consequent (term)
941
- case _ => whenFalse.append(branch)
942
- }
943
- case (Condition .BooleanTest (test) :: tail) -> term =>
944
- if (test === condition) {
945
- whenTrue.append(tail -> term)
946
- } else {
936
+ override def merge (branch : Ls [Condition ] -> Term )(implicit raise : Diagnostic => Unit ): Unit =
937
+ branch match {
938
+ case Nil -> term =>
947
939
whenFalse match {
948
940
case Consequent (_) => ??? // duplicated branch
949
941
case MissingCase => whenFalse = Consequent (term)
950
- case _ => whenFalse.append (branch)
942
+ case _ => whenFalse.merge (branch)
951
943
}
952
- }
953
- case _ =>
954
- whenFalse match {
955
- case Consequent (_) => ??? // duplicated branch
956
- case MissingCase => whenFalse = buildBranch(branch._1, branch._2)
957
- case _ => whenFalse.append(branch)
958
- }
959
- }
960
- override def toTerm (implicit fallback : Opt [Term ]): Term = {
944
+ case (Condition .BooleanTest (test) :: tail) -> term =>
945
+ if (test === condition) {
946
+ whenTrue.merge(tail -> term)
947
+ } else {
948
+ whenFalse match {
949
+ case Consequent (_) => ??? // duplicated branch
950
+ case MissingCase => whenFalse = Consequent (term)
951
+ case _ => whenFalse.merge(branch)
952
+ }
953
+ }
954
+ case _ =>
955
+ whenFalse match {
956
+ case Consequent (_) => ??? // duplicated branch
957
+ case MissingCase => whenFalse = buildBranch(branch._1, branch._2)
958
+ case _ => whenFalse.merge(branch)
959
+ }
960
+ }
961
+ override def toTerm : Term = {
961
962
val falseBranch = Wildcard (whenFalse.toTerm)
962
963
val trueBranch = Case (Var (" true" ), whenTrue.toTerm, falseBranch)
963
964
CaseOf (condition, trueBranch)
964
965
}
965
966
}
966
967
final case class Match (scrutinee : Term , val branches : Buffer [Branch ]) extends MutCaseOf {
967
- override def append (branch : Ls [Condition ] -> Term ): Unit = {
968
+ override def merge (branch : Ls [Condition ] -> Term )( implicit raise : Diagnostic => Unit ): Unit = {
968
969
branch match {
969
970
case (Condition .MatchClass (scrutinee2, className, fields) :: tail) -> term if scrutinee2 === scrutinee =>
970
971
branches.find(_.matches(className)) match {
@@ -973,7 +974,7 @@ object MutCaseOf {
973
974
branches += Branch (S (className -> Buffer .from(fields)), buildBranch(tail, term))
974
975
case S (branch) =>
975
976
branch.addFields(fields)
976
- branch.consequent.append (tail -> term)
977
+ branch.consequent.merge (tail -> term)
977
978
}
978
979
case (Condition .MatchTuple (scrutinee2, arity, fields) :: tail) -> term if scrutinee2 === scrutinee =>
979
980
val tupleClassName = Var (s " Tuple# $arity" )
@@ -983,19 +984,29 @@ object MutCaseOf {
983
984
branches += Branch (S (tupleClassName -> Buffer .from(indexFields)), buildBranch(tail, term))
984
985
case S (branch) =>
985
986
branch.addFields(indexFields)
986
- branch.consequent.append(tail -> term)
987
+ branch.consequent.merge(tail -> term)
988
+ }
989
+ // A wild card case. We should propagate wildcard to every default positions.
990
+ case Nil -> term =>
991
+ var hasWildcard = false
992
+ branches.foreach {
993
+ case Branch (_, _ : Consequent | MissingCase ) => ()
994
+ case Branch (patternFields, consequent) =>
995
+ consequent.merge(branch)
996
+ hasWildcard &&= patternFields.isEmpty
987
997
}
998
+ if (! hasWildcard) branches += Branch (N , Consequent (term))
988
999
case conditions -> term =>
989
1000
// Locate the wildcard case.
990
1001
branches.find(_.isWildcard) match {
991
1002
// No wildcard. We will create a new one.
992
1003
case N => branches += Branch (N , buildBranch(conditions, term))
993
- case S (Branch (N , consequent)) => consequent.append (conditions -> term)
1004
+ case S (Branch (N , consequent)) => consequent.merge (conditions -> term)
994
1005
case S (_) => ??? // Impossible case. What we find should be N.
995
1006
}
996
1007
}
997
1008
}
998
- override def toTerm ( implicit fallback : Opt [ Term ]) : Term = {
1009
+ override def toTerm : Term = {
999
1010
def rec (xs : Ls [Branch ]): CaseBranches =
1000
1011
xs match {
1001
1012
case Nil => NoCases
@@ -1010,13 +1021,14 @@ object MutCaseOf {
1010
1021
}
1011
1022
}
1012
1023
final case class Consequent (term : Term ) extends MutCaseOf {
1013
- override def append (branch : Ls [Condition ] -> Term ): Unit = ???
1014
- override def toTerm (implicit fallback : Opt [Term ]): Term = term
1024
+ override def merge (branch : Ls [Condition ] -> Term )(implicit raise : Diagnostic => Unit ): Unit =
1025
+ raise(WarningReport (Message .fromStr(" duplicated branch" ) -> N :: Nil ))
1026
+ override def toTerm : Term = term
1015
1027
}
1016
1028
final case object MissingCase extends MutCaseOf {
1017
- override def append (branch : Ls [Condition ] -> Term ): Unit = ???
1018
- override def toTerm ( implicit fallback : Opt [ Term ]) : Term =
1019
- fallback.getOrElse( throw new IfDesugaringException (" missing a default branch" ) )
1029
+ override def merge (branch : Ls [Condition ] -> Term )( implicit raise : Diagnostic => Unit ): Unit = ???
1030
+ override def toTerm : Term =
1031
+ throw new IfDesugaringException (" missing a default branch" )
1020
1032
}
1021
1033
1022
1034
private def buildBranch (conditions : Ls [Condition ], term : Term ): MutCaseOf = {
@@ -1040,12 +1052,12 @@ object MutCaseOf {
1040
1052
rec(conditions)
1041
1053
}
1042
1054
1043
- def build (cnf : Ls [Ls [Condition ] -> Term ]): MutCaseOf = {
1055
+ def build (cnf : Ls [Ls [Condition ] -> Term ])( implicit raise : Diagnostic => Unit ) : MutCaseOf = {
1044
1056
cnf match {
1045
1057
case Nil => ???
1046
1058
case (conditions -> term) :: next =>
1047
1059
val root = MutCaseOf .buildBranch(conditions, term)
1048
- next.foreach(root.append (_))
1060
+ next.foreach(root.merge (_))
1049
1061
root
1050
1062
}
1051
1063
}
0 commit comments