From 3443d8d8bce4ca233af2fe62534d3a7e259ae320 Mon Sep 17 00:00:00 2001 From: Angel Luis Garcia Date: Fri, 8 Nov 2019 21:15:02 +0100 Subject: [PATCH] #82 Fixed guard condition --- Assets/Tests/KotlinTokenizer/control_flow.kt | 4 + .../Tests/KotlinTokenizer/control_flow.swift | 1 + .../KotlinTokenizer.swift | 76 ++++++++++--------- 3 files changed, 45 insertions(+), 36 deletions(-) diff --git a/Assets/Tests/KotlinTokenizer/control_flow.kt b/Assets/Tests/KotlinTokenizer/control_flow.kt index 6e5ef07..f45f2ab 100644 --- a/Assets/Tests/KotlinTokenizer/control_flow.kt +++ b/Assets/Tests/KotlinTokenizer/control_flow.kt @@ -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") diff --git a/Assets/Tests/KotlinTokenizer/control_flow.swift b/Assets/Tests/KotlinTokenizer/control_flow.swift index 80e51ad..f37da37 100644 --- a/Assets/Tests/KotlinTokenizer/control_flow.swift +++ b/Assets/Tests/KotlinTokenizer/control_flow.swift @@ -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 { diff --git a/Sources/SwiftKotlinFramework/KotlinTokenizer.swift b/Sources/SwiftKotlinFramework/KotlinTokenizer.swift index b34a8b4..3ab998b 100644 --- a/Sources/SwiftKotlinFramework/KotlinTokenizer.swift +++ b/Sources/SwiftKotlinFramework/KotlinTokenizer.swift @@ -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 }