Skip to content

Commit bd4b12d

Browse files
committed
spliting for loops
1 parent 83c67ef commit bd4b12d

File tree

4 files changed

+241
-0
lines changed

4 files changed

+241
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// MARK: - Basic For-in Loop
2+
// Simple for-in loop over an array
3+
let names = ["Alice", "Bob", "Charlie"]
4+
for name in names {
5+
print("Hello, \(name)!")
6+
}
7+
8+
// MARK: - For-in with Enumerated
9+
// For-in loop with enumerated() to get index and value
10+
print("\n=== For-in with Enumerated ===")
11+
for (index, name) in names.enumerated() {
12+
print("Index: \(index), Name: \(name)")
13+
}
14+
15+
// MARK: - For-in with Where Clause
16+
// For-in loop with where clause
17+
print("\n=== For-in with Where Clause ===")
18+
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
19+
for number in numbers where number % 2 == 0 {
20+
print("Even number: \(number)")
21+
}
22+
23+
// MARK: - For-in with Dictionary
24+
// For-in loop over dictionary
25+
print("\n=== For-in with Dictionary ===")
26+
let scores = ["Alice": 95, "Bob": 87, "Charlie": 92]
27+
for (name, score) in scores {
28+
print("\(name): \(score)")
29+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import SyntaxKit
2+
3+
// MARK: - For Loops Examples
4+
Group {
5+
// MARK: - Basic For-in Loop
6+
Variable(.let, name: "names", equals: Literal.array([Literal.string("Alice"), Literal.string("Bob"), Literal.string("Charlie")]))
7+
.comment {
8+
Line("MARK: - Basic For-in Loop")
9+
Line("Simple for-in loop over an array")
10+
}
11+
12+
For {
13+
VariableExp("name")
14+
} in: {
15+
VariableExp("names")
16+
} then: {
17+
Call("print") {
18+
ParameterExp(name: "", value: "\"Hello, \\(name)!\"")
19+
}
20+
}
21+
22+
// MARK: - For-in with Enumerated
23+
Call("print") {
24+
ParameterExp(name: "", value: "\"\\n=== For-in with Enumerated ===\"")
25+
}
26+
.comment {
27+
Line("MARK: - For-in with Enumerated")
28+
Line("For-in loop with enumerated() to get index and value")
29+
}
30+
For {
31+
Tuple.pattern([VariableExp("index"), VariableExp("name")])
32+
} in: {
33+
VariableExp("names").call("enumerated")
34+
} then: {
35+
Call("print") {
36+
ParameterExp(name: "", value: "\"Index: \\(index), Name: \\(name)\"")
37+
}
38+
}
39+
40+
// MARK: - For-in with Where Clause
41+
Call("print") {
42+
ParameterExp(name: "", value: "\"\\n=== For-in with Where Clause ===\"")
43+
}
44+
.comment {
45+
Line("MARK: - For-in with Where Clause")
46+
Line("For-in loop with where clause")
47+
}
48+
Variable(.let, name: "numbers", equals: Literal.array([Literal.integer(1), Literal.integer(2), Literal.integer(3), Literal.integer(4), Literal.integer(5), Literal.integer(6), Literal.integer(7), Literal.integer(8), Literal.integer(9), Literal.integer(10)]))
49+
50+
For {
51+
VariableExp("number")
52+
} in: {
53+
VariableExp("numbers")
54+
} where: {
55+
Infix("%") {
56+
VariableExp("number")
57+
Literal.integer(2)
58+
}
59+
} then: {
60+
Call("print") {
61+
ParameterExp(name: "", value: "\"Even number: \\(number)\"")
62+
}
63+
}
64+
65+
// MARK: - For-in with Dictionary
66+
Call("print") {
67+
ParameterExp(name: "", value: "\"\\n=== For-in with Dictionary ===\"")
68+
}
69+
.comment {
70+
Line("MARK: - For-in with Dictionary")
71+
Line("For-in loop over dictionary")
72+
}
73+
Variable(.let, name: "scores", equals: Literal.dictionary([(Literal.string("Alice"), Literal.integer(95)), (Literal.string("Bob"), Literal.integer(87)), (Literal.string("Charlie"), Literal.integer(92))]))
74+
75+
For {
76+
Tuple.pattern([VariableExp("name"), VariableExp("score")])
77+
} in: {
78+
VariableExp("scores")
79+
} then: {
80+
Call("print") {
81+
ParameterExp(name: "", value: "\"\\(name): \\(score)\"")
82+
}
83+
}
84+
}

Examples/Completed/for_loops/syntax.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import Foundation
2+
import Testing
3+
4+
@testable import SyntaxKit
5+
6+
@Suite internal struct ForLoopsExampleTests {
7+
@Test("Completed for loops DSL generates expected Swift code", .disabled())
8+
internal func testCompletedForLoopsExample() throws {
9+
// Build DSL equivalent of Examples/Completed/for_loops/dsl.swift
10+
11+
let program = Group {
12+
// MARK: - Basic For-in Loop
13+
Variable(.let, name: "names", equals: Literal.array([Literal.string("Alice"), Literal.string("Bob"), Literal.string("Charlie")]))
14+
.comment {
15+
Line("MARK: - Basic For-in Loop")
16+
Line("Simple for-in loop over an array")
17+
}
18+
//
19+
// For {
20+
// VariableExp("name")
21+
// } in: VariableExp("names") then: {
22+
// Call("print") {
23+
// ParameterExp(unlabeled: "\"Hello, \\(name)!\"")
24+
// }
25+
// }
26+
//
27+
// // MARK: - For-in with Enumerated
28+
// Call("print") {
29+
// ParameterExp(unlabeled: "\"\\n=== For-in with Enumerated ===\"")
30+
// }
31+
// .comment {
32+
// Line("MARK: - For-in with Enumerated")
33+
// Line("For-in loop with enumerated() to get index and value")
34+
// }
35+
// For {
36+
// Tuple.pattern([VariableExp("index"), VariableExp("name")])
37+
// } in: VariableExp("names").call("enumerated") then: {
38+
// Call("print") {
39+
// ParameterExp(unlabeled: "\"Index: \\(index), Name: \\(name)\"")
40+
// }
41+
// }
42+
//
43+
// // MARK: - For-in with Where Clause
44+
// Call("print") {
45+
// ParameterExp(unlabeled: "\"\\n=== For-in with Where Clause ===\"")
46+
// }
47+
// .comment {
48+
// Line("MARK: - For-in with Where Clause")
49+
// Line("For-in loop with where clause")
50+
// }
51+
// Variable(.let, name: "numbers", equals: Literal.array([Literal.integer(1), Literal.integer(2), Literal.integer(3), Literal.integer(4), Literal.integer(5), Literal.integer(6), Literal.integer(7), Literal.integer(8), Literal.integer(9), Literal.integer(10)]))
52+
//
53+
// For {
54+
// VariableExp("number")
55+
// } in: VariableExp("numbers") where: {
56+
// Infix("%") {
57+
// VariableExp("number")
58+
// Literal.integer(2)
59+
// }
60+
// } then: {
61+
// Call("print") {
62+
// ParameterExp(unlabeled: "\"Even number: \\(number)\"")
63+
// }
64+
// }
65+
//
66+
// // MARK: - For-in with Dictionary
67+
// Call("print") {
68+
// ParameterExp(unlabeled: "\"\\n=== For-in with Dictionary ===\"")
69+
// }
70+
// .comment {
71+
// Line("MARK: - For-in with Dictionary")
72+
// Line("For-in loop over dictionary")
73+
// }
74+
// Variable(.let, name: "scores", equals: Literal.dictionary([(Literal.string("Alice"), Literal.integer(95)), (Literal.string("Bob"), Literal.integer(87)), (Literal.string("Charlie"), Literal.integer(92))]))
75+
//
76+
// For {
77+
// Tuple.pattern([VariableExp("name"), VariableExp("score")])
78+
// } in: VariableExp("scores") then: {
79+
// Call("print") {
80+
// ParameterExp(unlabeled: "\"\\(name): \\(score)\"")
81+
// }
82+
// }
83+
}
84+
85+
// Generate Swift from DSL
86+
var generated = program.generateCode()
87+
88+
// Remove type annotations like ": Int =" for comparison to example code
89+
generated = generated.normalize()
90+
91+
// Use the expected Swift code as a string literal
92+
let expected = """
93+
// MARK: - Basic For-in Loop
94+
// Simple for-in loop over an array
95+
let names = ["Alice", "Bob", "Charlie"]
96+
for name in names {
97+
print("Hello, \\(name)!")
98+
}
99+
100+
// MARK: - For-in with Enumerated
101+
// For-in loop with enumerated() to get index and value
102+
print("\\n=== For-in with Enumerated ===")
103+
for (index, name) in names.enumerated() {
104+
print("Index: \\(index), Name: \\(name)")
105+
}
106+
107+
// MARK: - For-in with Where Clause
108+
// For-in loop with where clause
109+
print("\\n=== For-in with Where Clause ===")
110+
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
111+
for number in numbers where number % 2 == 0 {
112+
print("Even number: \\(number)")
113+
}
114+
115+
// MARK: - For-in with Dictionary
116+
// For-in loop over dictionary
117+
print("\\n=== For-in with Dictionary ===")
118+
let scores = ["Alice": 95, "Bob": 87, "Charlie": 92]
119+
for (name, score) in scores {
120+
print("\\(name): \\(score)")
121+
}
122+
"""
123+
.normalize()
124+
125+
#expect(generated == expected)
126+
}
127+
}

0 commit comments

Comments
 (0)