Skip to content

Commit 90adb7a

Browse files
committed
#33 Ternary expressions
1 parent b3c85aa commit 90adb7a

File tree

6 files changed

+83
-7
lines changed

6 files changed

+83
-7
lines changed

Sources/SwiftKotlinFramework/KotlinTokenizer.swift

+47
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,53 @@ public class KotlinTokenizer: SwiftTokenizer {
553553
return tokenize(expression.postfixExpression) + expression.newToken(.symbol, "!!")
554554
}
555555

556+
open override func tokenize(_ expression: TernaryConditionalOperatorExpression) -> [Token] {
557+
return [
558+
[expression.newToken(.keyword, "if")],
559+
tokenize(expression.conditionExpression)
560+
.prefix(with: expression.newToken(.startOfScope, "("))
561+
.suffix(with: expression.newToken(.endOfScope, ")")),
562+
tokenize(expression.trueExpression),
563+
[expression.newToken(.keyword, "else")],
564+
tokenize(expression.falseExpression),
565+
].joined(token: expression.newToken(.space, " "))
566+
}
567+
568+
569+
open override func tokenize(_ expression: SequenceExpression) -> [Token] {
570+
var elementTokens = expression.elements.map({ tokenize($0, node: expression) })
571+
572+
//If there is a ternary, then prefix with if
573+
if let ternaryOperatorIndex = expression.elements.index(where: { $0.isTernaryConditionalOperator }),
574+
ternaryOperatorIndex > 0 {
575+
let assignmentIndex = expression.elements.index(where: { $0.isAssignmentOperator }) ?? -1
576+
let prefixTokens = [
577+
expression.newToken(.keyword, "if"),
578+
expression.newToken(.space, " "),
579+
expression.newToken(.startOfScope, "("),
580+
]
581+
elementTokens[assignmentIndex + 1] =
582+
prefixTokens +
583+
elementTokens[assignmentIndex + 1]
584+
elementTokens[ternaryOperatorIndex - 1] = elementTokens[ternaryOperatorIndex - 1]
585+
.suffix(with: expression.newToken(.endOfScope, ")"))
586+
}
587+
return elementTokens.joined(token: expression.newToken(.space, " "))
588+
}
589+
590+
open override func tokenize(_ element: SequenceExpression.Element, node: ASTNode) -> [Token] {
591+
switch element {
592+
case .ternaryConditionalOperator(let expr):
593+
return [
594+
tokenize(expr),
595+
[node.newToken(.keyword, "else")],
596+
].joined(token: node.newToken(.space, " "))
597+
default:
598+
return super.tokenize(element, node: node)
599+
}
600+
}
601+
602+
556603
// MARK: - Types
557604
open override func tokenize(_ type: ArrayType, node: ASTNode) -> [Token] {
558605
return

Sources/SwiftKotlinFramework/utils/AST+Operations.swift

+18-1
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,21 @@ extension EnumDeclaration.Member {
146146
}
147147
}
148148

149-
149+
extension SequenceExpression.Element {
150+
var isTernaryConditionalOperator: Bool {
151+
switch self {
152+
case .ternaryConditionalOperator:
153+
return true
154+
default:
155+
return false
156+
}
157+
}
158+
var isAssignmentOperator: Bool {
159+
switch self {
160+
case .assignmentOperator:
161+
return true
162+
default:
163+
return false
164+
}
165+
}
166+
}

SwiftKotlin.xcodeproj/xcshareddata/xcschemes/SwiftKotlin-Package.xcscheme

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<BuildableReference
1616
BuildableIdentifier = "primary"
1717
BlueprintIdentifier = "SwiftKotlin::SwiftKotlinCommandLine"
18-
BuildableName = "SwiftKotlinCommandLine"
18+
BuildableName = "swiftkotlin"
1919
BlueprintName = "SwiftKotlinCommandLine"
2020
ReferencedContainer = "container:SwiftKotlin.xcodeproj">
2121
</BuildableReference>
@@ -213,7 +213,7 @@
213213
<BuildableReference
214214
BuildableIdentifier = "primary"
215215
BlueprintIdentifier = "SwiftKotlin::SwiftKotlinCommandLine"
216-
BuildableName = "SwiftKotlinCommandLine"
216+
BuildableName = "swiftkotlin"
217217
BlueprintName = "SwiftKotlinCommandLine"
218218
ReferencedContainer = "container:SwiftKotlin.xcodeproj">
219219
</BuildableReference>

SwiftKotlin.xcodeproj/xcshareddata/xcschemes/SwiftKotlinCommandLine.xcscheme

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<BuildableReference
1616
BuildableIdentifier = "primary"
1717
BlueprintIdentifier = "SwiftKotlin::SwiftKotlinCommandLine"
18-
BuildableName = "SwiftKotlinCommandLine"
18+
BuildableName = "swiftkotlin"
1919
BlueprintName = "SwiftKotlinCommandLine"
2020
ReferencedContainer = "container:SwiftKotlin.xcodeproj">
2121
</BuildableReference>
@@ -34,7 +34,7 @@
3434
<BuildableReference
3535
BuildableIdentifier = "primary"
3636
BlueprintIdentifier = "SwiftKotlin::SwiftKotlinCommandLine"
37-
BuildableName = "SwiftKotlinCommandLine"
37+
BuildableName = "swiftkotlin"
3838
BlueprintName = "SwiftKotlinCommandLine"
3939
ReferencedContainer = "container:SwiftKotlin.xcodeproj">
4040
</BuildableReference>
@@ -58,7 +58,7 @@
5858
<BuildableReference
5959
BuildableIdentifier = "primary"
6060
BlueprintIdentifier = "SwiftKotlin::SwiftKotlinCommandLine"
61-
BuildableName = "SwiftKotlinCommandLine"
61+
BuildableName = "swiftkotlin"
6262
BlueprintName = "SwiftKotlinCommandLine"
6363
ReferencedContainer = "container:SwiftKotlin.xcodeproj">
6464
</BuildableReference>
@@ -77,7 +77,7 @@
7777
<BuildableReference
7878
BuildableIdentifier = "primary"
7979
BlueprintIdentifier = "SwiftKotlin::SwiftKotlinCommandLine"
80-
BuildableName = "SwiftKotlinCommandLine"
80+
BuildableName = "swiftkotlin"
8181
BlueprintName = "SwiftKotlinCommandLine"
8282
ReferencedContainer = "container:SwiftKotlin.xcodeproj">
8383
</BuildableReference>

Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/control_flow.kt

+4
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,7 @@ when (nb) {
7676
}
7777
else -> print("three or more digits")
7878
}
79+
val value = if (isTrue) "yes" else "no"
80+
val label = if (x > 0) "Positive" else "negative"
81+
button.color = if (item.deleted) red else green
82+
val text = label ?: "default"

Tests/SwiftKotlinFrameworkTests/Tests/KotlinTokenizer/control_flow.swift

+8
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,11 @@ switch nb {
5959

6060
default: print("three or more digits")
6161
}
62+
63+
// Ternary and coalescing operators
64+
let value = isTrue ? "yes" : "no"
65+
let label = x > 0 ? "Positive" : "negative"
66+
button.color = item.deleted ? red : green
67+
let text = label ?? "default"
68+
69+

0 commit comments

Comments
 (0)