Skip to content

Commit de009c9

Browse files
committed
WIP: pattern in expr
1 parent 7c8d8a4 commit de009c9

File tree

4 files changed

+29
-19
lines changed

4 files changed

+29
-19
lines changed

src/main/scala/plus/meow/MeowRust/grammar/expr.scala

+5-10
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ case class CallExpr(recv: Expr, method: Option[String], params: List[Expr]) exte
5454
case class FieldExpr(owner: Expr, field: Expr) extends Expr
5555
case class ArrayIndexExpr(owner: Expr, index: Expr) extends Expr
5656
case class TupleIndexExpr(owner: Expr, index: BigInt) extends Expr
57-
// TODO: pattern
58-
case class ClosureExpr(params: List[Any], body: Expr) extends Expr
57+
case class ClosureExpr(params: List[Pattern], body: Expr) extends Expr
5958
case class FlowCtrlExpr(ctrl: FlowCtrl, label: Option[String], param: Option[Expr]) extends Expr
6059
case class RangeExpr(from: Option[Expr], to: Option[Expr], includeTo: Boolean) extends Expr
6160

@@ -68,18 +67,14 @@ case class BinaryOpExpr(op: BinaryOp, lhs: Expr, rhs: Expr) extends Expr
6867

6968
// Loops
7069
case class InftyLoopExpr(label: Option[String], body: Expr) extends Expr
71-
// TODO: pattern
72-
case class ForLoopExpr(label: Option[String], pat: Any, collection: Expr, body: Expr) extends Expr
70+
case class ForLoopExpr(label: Option[String], pat: Pattern, collection: Expr, body: Expr) extends Expr
7371
case class WhileExpr(label: Option[String], cond: Expr, body: Expr) extends Expr
74-
// TODO: pattern
75-
case class WhileLetExpr(label: Option[String], pat: List[Any], unwrapped: Expr, body: Expr) extends Expr
72+
case class WhileLetExpr(label: Option[String], pat: List[Pattern], unwrapped: Expr, body: Expr) extends Expr
7673

7774
// Conditionals
7875
case class IfExpr(cond: Expr, body: Expr, otherwise: Option[Expr]) extends Expr
79-
// TODO: pattern
80-
case class IfLetExpr(pat: List[Any], unwrapped: Expr, body: Expr, otherwise: Option[Expr]) extends Expr
76+
case class IfLetExpr(pat: List[Pattern], unwrapped: Expr, body: Expr, otherwise: Option[Expr]) extends Expr
8177

8278
// Matches
83-
// TODO: pattern
84-
case class MatchArm(pat: Any, guardCond: Option[Expr], body: Expr)
79+
case class MatchArm(pat: List[Pattern], guardCond: Option[Expr], body: Expr)
8580
case class MatchExpr(input: Expr, arms: List[MatchArm]) extends Expr
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package plus.meow.MeowRust.grammar
2+
3+
abstract class Pattern
4+
case class LiteralPattern(literal: Literal) extends Pattern
5+
case class IdentifierPattern(ident: String, ref: Boolean, mut: Boolean) extends Pattern
6+
case class BoundPattern(ident: IdentifierPattern, pat: Pattern) extends Pattern
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
package plus.meow.MeowRust.parser
22
import com.codecommit.gll.RegexParsers
3+
import plus.meow.MeowRust.grammar._
4+
import plus.meow.MeowRust.grammar
35

46
trait Pattern extends RegexParsers with Literal with Identifier {
57
// TODO: support other types of pattern
6-
lazy val PATTERN: Parser[Any] = (
8+
lazy val PATTERN: Parser[grammar.Pattern] = (
79
LITERAL_PATTERN
810
| IDENTIFIER_PATTERN
911
)
1012

1113
// TODO: support negative literals
12-
lazy val LITERAL_PATTERN: Parser[Any] = LITERAL;
14+
lazy val LITERAL_PATTERN: Parser[LiteralPattern] = LITERAL ^^ LiteralPattern
1315

14-
lazy val IDENTIFIER_PATTERN: Parser[Any] =
15-
("ref"?) ~ ("mut"?) ~ IDENTIFIER ~ (("@" ~> PATTERN)?)
16-
}
16+
lazy val IDENTIFIER_PATTERN: Parser[grammar.Pattern] =
17+
("ref"?) ~ ("mut"?) ~ IDENTIFIER ~ (("@" ~> PATTERN)?) ^^ { (ref, mut, id, bounded) => {
18+
val idpat = IdentifierPattern(id, ref.isDefined, mut.isDefined)
19+
bounded match {
20+
case None => idpat
21+
case Some(value) => BoundPattern(idpat, value)
22+
}
23+
}}
24+
}

src/main/scala/plus/meow/MeowRust/parser/statement.scala

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package plus.meow.MeowRust.parser
22
import com.codecommit.gll.RegexParsers
33
import plus.meow.MeowRust.grammar._
4+
import plus.meow.MeowRust.grammar
45

56
trait Statement extends RegexParsers with Literal with Pattern with Identifier with Label {
67
lazy val STATEMENT: Parser[Any] = (
@@ -147,7 +148,7 @@ trait Statement extends RegexParsers with Literal with Pattern with Identifier w
147148

148149
// TODO: add type ascription ( -> TYPE BlockExpression
149150
lazy val CLOSURE_EXPRESSION = (("||" ^^^ { List() }) | "|" ~> CLOSURE_PARAMETERS <~ "|") ~ EXPRESSION ^^ ClosureExpr
150-
lazy val CLOSURE_PARAMETERS: Parser[List[Any]] = CLOSURE_PARAM ~ (("," ~> CLOSURE_PARAM)*) <~ (","?) ^^ { _ :: _ }
151+
lazy val CLOSURE_PARAMETERS: Parser[List[grammar.Pattern]] = CLOSURE_PARAM ~ (("," ~> CLOSURE_PARAM)*) <~ (","?) ^^ { _ :: _ }
151152
// TODO: add type ascription
152153
lazy val CLOSURE_PARAM = PATTERN
153154

@@ -175,7 +176,7 @@ trait Statement extends RegexParsers with Literal with Pattern with Identifier w
175176
lazy val IF_LET_EXPRESSION: Parser[IfLetExpr] = (("if" ~ "let") ~> MATCH_ARM_PATTERNS <~ "=") ~ EXPRESSION ~ BLOCK_EXPRESSION ~(ELSE_ARM?) ^^ { IfLetExpr(_, _, _, _) }
176177

177178
lazy val MATCH_EXPRESSION: Parser[MatchExpr] = ("match" ~> EXPRESSION <~ "{") ~ (MATCH_ARMS?) <~ "}" ^^ { (e, arms) => MatchExpr(e, arms getOrElse List()) }
178-
val armParser = (spec: (List[Any], Option[Expr]), body: Expr) => MatchArm(spec._1, spec._2, body)
179+
val armParser = (spec: (List[grammar.Pattern], Option[Expr]), body: Expr) => MatchArm(spec._1, spec._2, body)
179180
lazy val MATCH_ARMS: Parser[List[MatchArm]] = (
180181
(
181182
(MATCH_ARM <~ "=>") ~ ((BLOCK_EXPRESSION <~ (","?)) | (EXPRESSION <~ ",")) ^^ armParser)*
@@ -184,8 +185,8 @@ trait Statement extends RegexParsers with Literal with Pattern with Identifier w
184185
) ^^ {
185186
_ :+ _
186187
}
187-
lazy val MATCH_ARM: Parser[(List[Any], Option[Expr])] = MATCH_ARM_PATTERNS ~ (("if" ~> EXPRESSION)?) ^^ { (_, _) }
188-
lazy val MATCH_ARM_PATTERNS: Parser[List[Any]] = ("|"?) ~> PATTERN ~ (("|" ~> PATTERN)*) ^^ { (e, l) => e :: l }
188+
lazy val MATCH_ARM: Parser[(List[grammar.Pattern], Option[Expr])] = MATCH_ARM_PATTERNS ~ (("if" ~> EXPRESSION)?) ^^ { (_, _) }
189+
lazy val MATCH_ARM_PATTERNS: Parser[List[grammar.Pattern]] = ("|"?) ~> PATTERN ~ (("|" ~> PATTERN)*) ^^ { (e, l) => e :: l }
189190

190191
lazy val RETURN_EXPRESSION = "return" ~> (EXPRESSION?) ^^ { FlowCtrlExpr(Return(), None, _) }
191192
}

0 commit comments

Comments
 (0)