Skip to content

Commit

Permalink
#82 Fixed guard condition
Browse files Browse the repository at this point in the history
  • Loading branch information
angelolloqui committed Nov 8, 2019
1 parent 7158abc commit 3443d8d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 36 deletions.
4 changes: 4 additions & 0 deletions Assets/Tests/KotlinTokenizer/control_flow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ if (value == null) {
return
}
val value = some.method() ?: throw Exception()
val match = this.interactor.match
if (match == null || !(interactor.userIsOwner || interactor.userIsPlayer)) {
return
}
when (nb) {
in 0 .. 7, 8, 9 -> print("single digit")
10 -> print("double digits")
Expand Down
1 change: 1 addition & 0 deletions Assets/Tests/KotlinTokenizer/control_flow.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ guard let value = some.method() else {
return
}
guard let value = some.method() else { throw Exception() }
guard let match = self.interactor.match, (interactor.userIsOwner || interactor.userIsPlayer) else { return }

// Switch
switch nb {
Expand Down
76 changes: 40 additions & 36 deletions Sources/SwiftKotlinFramework/KotlinTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -958,47 +958,51 @@ public class KotlinTokenizer: SwiftTokenizer {

private func tokenize(_ condition: InvertedCondition, node: ASTNode) -> [Token] {
let tokens = tokenize(condition.condition, node: node)
var invertedTokens = [Token]()
var inverted = false
var lastExpressionIndex = 0
for token in tokens {
if let origin = token.origin, let node = token.node {
if origin is SequenceExpression || origin is BinaryExpression || origin is Condition {
let inversionMap = [
"==": "!=",
"!=": "==",
">": "<=",
">=": "<",
"<": ">=",
"<=": ">",
"is": "!is",
]
if let newValue = inversionMap[token.value] {
inverted = true
invertedTokens.append(origin.newToken(token.kind, newValue, node))
continue
} else if token.value == "&&" || token.value == "||" {
if !inverted {
invertedTokens.insert(origin.newToken(.symbol, "!", node), at: lastExpressionIndex)
if case Condition.expression(let expression) = condition.condition, expression is ParenthesizedExpression {
return tokens.prefix(with: condition.condition.newToken(.symbol, "!", node))
} else {
var invertedTokens = [Token]()
var inverted = false
var lastExpressionIndex = 0
for token in tokens {
if let origin = token.origin, let node = token.node {
if origin is SequenceExpression || origin is BinaryExpression || origin is Condition {
let inversionMap = [
"==": "!=",
"!=": "==",
">": "<=",
">=": "<",
"<": ">=",
"<=": ">",
"is": "!is",
]
if let newValue = inversionMap[token.value] {
inverted = true
invertedTokens.append(origin.newToken(token.kind, newValue, node))
continue
} else if token.value == "&&" || token.value == "||" {
if !inverted {
invertedTokens.insert(origin.newToken(.symbol, "!", node), at: lastExpressionIndex)
}
inverted = false
invertedTokens.append(origin.newToken(token.kind, token.value == "&&" ? "||" : "&&", node))
lastExpressionIndex = invertedTokens.count + 1
continue
}
} else if origin is PrefixOperatorExpression {
if token.value == "!" {
inverted = true
continue
}
inverted = false
invertedTokens.append(origin.newToken(token.kind, token.value == "&&" ? "||" : "&&", node))
lastExpressionIndex = invertedTokens.count + 1
continue
}
} else if origin is PrefixOperatorExpression {
if token.value == "!" {
inverted = true
continue
}
}
invertedTokens.append(token)
}
invertedTokens.append(token)
}
if !inverted {
invertedTokens.insert(condition.newToken(.symbol, "!", node), at: lastExpressionIndex)
if !inverted {
invertedTokens.insert(condition.newToken(.symbol, "!", node), at: lastExpressionIndex)
}
return invertedTokens
}
return invertedTokens
}


Expand Down

0 comments on commit 3443d8d

Please sign in to comment.