Skip to content

Commit 4ce39df

Browse files
committed
Added using for switch statements
1 parent 8c9ca0a commit 4ce39df

4 files changed

Lines changed: 27 additions & 2 deletions

File tree

Sources/Core/Ast.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,6 +1071,7 @@ init(keyword: Pos, match: [Expr], colon: Pos, block: Block, label: Entity!) {
10711071
class Switch: Node, Stmt {
10721072
var keyword: Pos
10731073
var match: Expr?
1074+
var usingMatch: Bool
10741075
var cases: [CaseClause]
10751076
var rbrace: Pos
10761077

@@ -1080,9 +1081,10 @@ class Switch: Node, Stmt {
10801081
var end: Pos { return rbrace }
10811082

10821083
// sourcery:inline:auto:Switch.Init
1083-
init(keyword: Pos, match: Expr?, cases: [CaseClause], rbrace: Pos, label: Entity!) {
1084+
init(keyword: Pos, match: Expr?, usingMatch: Bool, cases: [CaseClause], rbrace: Pos, label: Entity!) {
10841085
self.keyword = keyword
10851086
self.match = match
1087+
self.usingMatch = usingMatch
10861088
self.cases = cases
10871089
self.rbrace = rbrace
10881090
self.label = label

Sources/Core/Checker.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,21 @@ extension Checker {
930930
reportError("Can only switch on integer and enum types", at: match.start)
931931
return dependencies
932932
}
933+
934+
if sw.usingMatch {
935+
guard let type = baseType(match.type) as? ty.Enum else {
936+
reportError("using is invalid on \(operand)", at: match.start)
937+
return dependencies
938+
}
939+
940+
for c in type.cases.orderedValues {
941+
let entity = newEntity(ident: c.ident, type: type, flags: [.field, .constant], owningScope: context.scope)
942+
entity.constant = c.constant ?? UInt64(c.number)
943+
declare(entity)
944+
}
945+
}
946+
} else if sw.usingMatch {
947+
reportError("Using expects an entity", at: sw.start)
933948
}
934949

935950
var seenDefault = false

Sources/Core/Parser.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,13 @@ extension Parser {
938938
mutating func parseSwitchStmt() -> Switch {
939939
let keyword = eatToken()
940940
var match: Expr?
941+
942+
var usingMatch = false
943+
if tok == .using {
944+
next()
945+
usingMatch = true
946+
}
947+
941948
if tok != .lbrace && tok != .semicolon {
942949
match = parseExpr()
943950
}
@@ -952,7 +959,7 @@ extension Parser {
952959
}
953960
let rbrace = expect(.rbrace)
954961
expectTerm()
955-
return Switch(keyword: keyword, match: match, cases: cases, rbrace: rbrace, label: nil)
962+
return Switch(keyword: keyword, match: match, usingMatch: usingMatch, cases: cases, rbrace: rbrace, label: nil)
956963
}
957964

958965
mutating func parseCaseClause() -> CaseClause {

Sources/Core/generated/Copy.generated.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,7 @@ func copy(_ node: Switch) -> Switch {
685685
return Switch(
686686
keyword: node.keyword,
687687
match: node.match.map(copy),
688+
usingMatch: node.usingMatch,
688689
cases: copy(node.cases),
689690
rbrace: node.rbrace,
690691
label: node.label

0 commit comments

Comments
 (0)