Skip to content

Commit

Permalink
#33 Ternary expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
angelolloqui committed Nov 1, 2017
1 parent b3c85aa commit 90adb7a
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 7 deletions.
47 changes: 47 additions & 0 deletions Sources/SwiftKotlinFramework/KotlinTokenizer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,53 @@ public class KotlinTokenizer: SwiftTokenizer {
return tokenize(expression.postfixExpression) + expression.newToken(.symbol, "!!")
}

open override func tokenize(_ expression: TernaryConditionalOperatorExpression) -> [Token] {
return [
[expression.newToken(.keyword, "if")],
tokenize(expression.conditionExpression)
.prefix(with: expression.newToken(.startOfScope, "("))
.suffix(with: expression.newToken(.endOfScope, ")")),
tokenize(expression.trueExpression),
[expression.newToken(.keyword, "else")],
tokenize(expression.falseExpression),
].joined(token: expression.newToken(.space, " "))
}


open override func tokenize(_ expression: SequenceExpression) -> [Token] {
var elementTokens = expression.elements.map({ tokenize($0, node: expression) })

//If there is a ternary, then prefix with if
if let ternaryOperatorIndex = expression.elements.index(where: { $0.isTernaryConditionalOperator }),
ternaryOperatorIndex > 0 {
let assignmentIndex = expression.elements.index(where: { $0.isAssignmentOperator }) ?? -1
let prefixTokens = [
expression.newToken(.keyword, "if"),
expression.newToken(.space, " "),
expression.newToken(.startOfScope, "("),
]
elementTokens[assignmentIndex + 1] =
prefixTokens +
elementTokens[assignmentIndex + 1]
elementTokens[ternaryOperatorIndex - 1] = elementTokens[ternaryOperatorIndex - 1]
.suffix(with: expression.newToken(.endOfScope, ")"))
}
return elementTokens.joined(token: expression.newToken(.space, " "))
}

open override func tokenize(_ element: SequenceExpression.Element, node: ASTNode) -> [Token] {
switch element {
case .ternaryConditionalOperator(let expr):
return [
tokenize(expr),
[node.newToken(.keyword, "else")],
].joined(token: node.newToken(.space, " "))
default:
return super.tokenize(element, node: node)
}
}


// MARK: - Types
open override func tokenize(_ type: ArrayType, node: ASTNode) -> [Token] {
return
Expand Down
19 changes: 18 additions & 1 deletion Sources/SwiftKotlinFramework/utils/AST+Operations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,21 @@ extension EnumDeclaration.Member {
}
}


extension SequenceExpression.Element {
var isTernaryConditionalOperator: Bool {
switch self {
case .ternaryConditionalOperator:
return true
default:
return false
}
}
var isAssignmentOperator: Bool {
switch self {
case .assignmentOperator:
return true
default:
return false
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SwiftKotlin::SwiftKotlinCommandLine"
BuildableName = "SwiftKotlinCommandLine"
BuildableName = "swiftkotlin"
BlueprintName = "SwiftKotlinCommandLine"
ReferencedContainer = "container:SwiftKotlin.xcodeproj">
</BuildableReference>
Expand Down Expand Up @@ -213,7 +213,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SwiftKotlin::SwiftKotlinCommandLine"
BuildableName = "SwiftKotlinCommandLine"
BuildableName = "swiftkotlin"
BlueprintName = "SwiftKotlinCommandLine"
ReferencedContainer = "container:SwiftKotlin.xcodeproj">
</BuildableReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SwiftKotlin::SwiftKotlinCommandLine"
BuildableName = "SwiftKotlinCommandLine"
BuildableName = "swiftkotlin"
BlueprintName = "SwiftKotlinCommandLine"
ReferencedContainer = "container:SwiftKotlin.xcodeproj">
</BuildableReference>
Expand All @@ -34,7 +34,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SwiftKotlin::SwiftKotlinCommandLine"
BuildableName = "SwiftKotlinCommandLine"
BuildableName = "swiftkotlin"
BlueprintName = "SwiftKotlinCommandLine"
ReferencedContainer = "container:SwiftKotlin.xcodeproj">
</BuildableReference>
Expand All @@ -58,7 +58,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SwiftKotlin::SwiftKotlinCommandLine"
BuildableName = "SwiftKotlinCommandLine"
BuildableName = "swiftkotlin"
BlueprintName = "SwiftKotlinCommandLine"
ReferencedContainer = "container:SwiftKotlin.xcodeproj">
</BuildableReference>
Expand All @@ -77,7 +77,7 @@
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "SwiftKotlin::SwiftKotlinCommandLine"
BuildableName = "SwiftKotlinCommandLine"
BuildableName = "swiftkotlin"
BlueprintName = "SwiftKotlinCommandLine"
ReferencedContainer = "container:SwiftKotlin.xcodeproj">
</BuildableReference>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,7 @@ when (nb) {
}
else -> print("three or more digits")
}
val value = if (isTrue) "yes" else "no"
val label = if (x > 0) "Positive" else "negative"
button.color = if (item.deleted) red else green
val text = label ?: "default"
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,11 @@ switch nb {

default: print("three or more digits")
}

// Ternary and coalescing operators
let value = isTrue ? "yes" : "no"
let label = x > 0 ? "Positive" : "negative"
button.color = item.deleted ? red : green
let text = label ?? "default"


0 comments on commit 90adb7a

Please sign in to comment.