Skip to content

Commit 04ed565

Browse files
committed
Simplify.
1 parent 67a64c6 commit 04ed565

File tree

1 file changed

+13
-34
lines changed

1 file changed

+13
-34
lines changed

src/main/scala/lambda/Parser.scala

Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ type Parse[Src <: String] =
1515

1616
sealed trait ParseResult
1717
case class ParsedTerm[T <: Term, R <: String](term: T, rest: R) extends ParseResult
18-
case class ParseError[R <: String](reason: R) extends ParseResult with ParseAppResult
19-
20-
sealed trait ParseAppResult
21-
case class ParsedApp[L <: HList, R <: String](list: L, rest: R) extends ParseAppResult
18+
case class ParseError[R <: String](reason: R) extends ParseResult
2219

2320
@annotation.experimental // Length, Matches, Substring
2421
object Parser {
@@ -34,11 +31,7 @@ object Parser {
3431
type ParseExp[Src <: String] <: ParseResult =
3532
SafeSubstring[Src, 0, 1] match {
3633
case "λ" => ParseAbs[HNil, Substring[Src, 1, Length[Src]]]
37-
case _ => ParseApp[Src] match {
38-
case ParsedApp[list, rest] => ParsedTerm[MakeApp[list], rest]
39-
case ParseError[s] => ParseError[s]
40-
case _ => ParseError["unexpected"]
41-
}
34+
case _ => ParseApp[Src]
4235
}
4336

4437
type ParseAbs[Args <: HList, Src <: String] <: ParseResult =
@@ -56,19 +49,19 @@ object Parser {
5649
}
5750
}
5851

59-
type ParseApp[Src <: String] <: ParseAppResult =
52+
type ParseApp[Src <: String] <: ParseResult =
6053
ParsePrimary[Src] match {
61-
case ParsedTerm[t, rest] => rest match {
62-
case "" => ParsedApp[t :+: HNil, ""]
63-
case _ => SafeSubstring[rest, 0, 1] match {
64-
case " " => ParseApp[Substring[rest, 1, Length[rest]]] match {
65-
case ParsedApp[list, rest] => ParsedApp[t :+: list, rest]
66-
case ParseError[s] => ParseError[s]
67-
}
68-
case _ => ParsedApp[t :+: HNil, rest]
69-
}
54+
case ParsedTerm[t, rest] => ParseApp1[t, rest]
55+
case ParseError[s] => ParseError[s]
56+
}
57+
58+
type ParseApp1[Prev <: Term, Src <: String] <: ParseResult =
59+
SafeSubstring[Src, 0, 1] match {
60+
case " " => ParsePrimary[Substring[Src, 1, Length[Src]]] match {
61+
case ParsedTerm[t, rest] => ParseApp1[App[Prev, t], rest]
62+
case ParseError[s] => ParseError[s]
7063
}
71-
case ParseError[s] => ParseError[s]
64+
case _ => ParsedTerm[Prev, Src]
7265
}
7366

7467
type ParsePrimary[Src <: String] <: ParseResult =
@@ -92,20 +85,6 @@ object Parser {
9285
case HNil => T
9386
}
9487

95-
type MakeApp[L <: HList] <: Term =
96-
L match {
97-
case Var[v] :+: HNil => Var[v]
98-
case Abs[v, t] :+: HNil => Abs[v, t]
99-
case App[t1, t2] :+: HNil => App[t1, t2]
100-
case t1 :+: t2 :+: rest => MakeApp1[App[t1, t2], rest]
101-
}
102-
103-
type MakeApp1[T <: Term, L <: HList] <: Term =
104-
L match {
105-
case HNil => T
106-
case t :+: rest => MakeApp1[App[T, t], rest]
107-
}
108-
10988
type SafeSubstring[S <: String, IBeg <: Int, IEnd <: Int] <: String =
11089
boolean.&&[int.>=[Length[S], IBeg], int.>=[Length[S], IEnd]] match {
11190
case true => Substring[S, IBeg, IEnd]

0 commit comments

Comments
 (0)