Skip to content

Commit

Permalink
LoopType enum only has two cases now since there is no difference bet…
Browse files Browse the repository at this point in the history
…ween `for` and `while` loops regarding resolving of `break`s and `continue`s.
  • Loading branch information
quephird committed Mar 17, 2024
1 parent 18b1578 commit 3d7fd4c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
7 changes: 3 additions & 4 deletions slox/Resolver.swift
Expand Up @@ -22,8 +22,7 @@ struct Resolver {

private enum LoopType {
case none
case `while`
case `for`
case loop
}

private var scopeStack: [[String: Bool]] = []
Expand Down Expand Up @@ -256,7 +255,7 @@ struct Resolver {

mutating private func handleWhile(conditionExpr: Expression, bodyStmt: Statement) throws -> ResolvedStatement {
let previousLoopType = currentLoopType
currentLoopType = .while
currentLoopType = .loop
defer {
currentLoopType = previousLoopType
}
Expand All @@ -272,7 +271,7 @@ struct Resolver {
incrementExpr: Expression?,
bodyStmt: Statement) throws -> ResolvedStatement {
let previousLoopType = currentLoopType
currentLoopType = .for
currentLoopType = .loop
defer {
currentLoopType = previousLoopType
}
Expand Down
2 changes: 1 addition & 1 deletion slox/ResolverError.swift
Expand Up @@ -46,7 +46,7 @@ enum ResolverError: CustomStringConvertible, Equatable, LocalizedError {
case .cannotBreakOutsideLoop:
return "Can only `break` from inside a `while` or `for` loop"
case .cannotContinueOutsideLoop:
return "Can only `continue` while inside a loop"
return "Can only `continue` from inside a `while` or `for` loop"
}
}
}

0 comments on commit 3d7fd4c

Please sign in to comment.