Skip to content

Commit 07d90e6

Browse files
committed
initial commit
0 parents  commit 07d90e6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+3361
-0
lines changed

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.DS_Store
2+
/.build
3+
/Packages
4+
xcuserdata/
5+
DerivedData/
6+
.swiftpm/configuration/registries.json
7+
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
8+
.netrc

Examples/blackjack/code.swift

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import Foundation
2+
3+
struct BlackjackCard {
4+
// nested Suit enumeration
5+
enum Suit: Character {
6+
case spades = ""
7+
case hearts = ""
8+
case diamonds = ""
9+
case clubs = ""
10+
}
11+
12+
// nested Rank enumeration
13+
enum Rank: Int {
14+
case two = 2
15+
case three
16+
case four
17+
case five
18+
case six
19+
case seven
20+
case eight
21+
case nine
22+
case ten
23+
case jack
24+
case queen
25+
case king
26+
case ace
27+
28+
struct Values {
29+
let first: Int, second: Int?
30+
}
31+
32+
var values: Values {
33+
switch self {
34+
case .ace:
35+
return Values(first: 1, second: 11)
36+
case .jack, .queen, .king:
37+
return Values(first: 10, second: nil)
38+
default:
39+
return Values(first: self.rawValue, second: nil)
40+
}
41+
}
42+
}
43+
44+
// BlackjackCard properties and methods
45+
let rank: Rank
46+
let suit: Suit
47+
var description: String {
48+
var output = "suit is \(suit.rawValue),"
49+
output += " value is \(rank.values.first)"
50+
if let second = rank.values.second {
51+
output += " or \(second)"
52+
}
53+
return output
54+
}
55+
}

Examples/blackjack/dsl.swift

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import SwiftBuilder
2+
3+
// Example of generating a BlackjackCard struct with a nested Suit enum
4+
let structExample = Struct("BlackjackCard") {
5+
Enum("Suit") {
6+
EnumCase("spades").equals("")
7+
EnumCase("hearts").equals("")
8+
EnumCase("diamonds").equals("")
9+
EnumCase("clubs").equals("")
10+
}
11+
.inherits("Character")
12+
13+
Enum("Rank") {
14+
EnumCase("two").equals(2)
15+
EnumCase("three")
16+
EnumCase("four")
17+
EnumCase("five")
18+
EnumCase("six")
19+
EnumCase("seven")
20+
EnumCase("eight")
21+
EnumCase("nine")
22+
EnumCase("ten")
23+
EnumCase("jack")
24+
EnumCase("queen")
25+
EnumCase("king")
26+
EnumCase("ace")
27+
Struct("Values") {
28+
Variable(.let, name: "first", type: "Int")
29+
Variable(.let, name: "second", type: "Int?")
30+
}
31+
ComputedProperty("values") {
32+
Switch("self") {
33+
SwitchCase(".ace") {
34+
Return{
35+
Init("Values") {
36+
Parameter(name: "first", value: "1")
37+
Parameter(name: "second", value: "11")
38+
}
39+
}
40+
}
41+
SwitchCase(".jack", ".queen", ".king") {
42+
Return{
43+
Init("Values") {
44+
Parameter(name: "first", value: "10")
45+
Parameter(name: "second", value: "nil")
46+
}
47+
}
48+
}
49+
Default {
50+
Return{
51+
Init("Values") {
52+
Parameter(name: "first", value: "self.rawValue")
53+
Parameter(name: "second", value: "nil")
54+
}
55+
}
56+
}
57+
}
58+
}
59+
}
60+
.inherits("Int")
61+
62+
Variable(.let, name: "rank", type: "Rank")
63+
Variable(.let, name: "suit", type: "Suit")
64+
65+
ComputedProperty("description") {
66+
VariableDecl(.var, name: "output", equals: "suit is \(suit.rawValue),")
67+
PlusAssign("output", " value is \(rank.values.first)")
68+
If(Let("second", "rank.values.second"), then: {
69+
PlusAssign("output", " or \(second)")
70+
})
71+
Return {
72+
VariableExp("output")
73+
}
74+
}
75+
}
76+
77+
// Generate and print the code
78+
print(structExample.generateCode())

Examples/card_game/code.swift

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import Foundation
2+
3+
// MARK: - Models
4+
/// Represents a playing card in a standard 52-card deck
5+
///
6+
/// A card has a rank (2-10, J, Q, K, A) and a suit (hearts, diamonds, clubs, spades).
7+
/// Each card can be compared to other cards based on its rank.
8+
struct Card: Comparable {
9+
/// The rank of the card (2-10, J, Q, K, A)
10+
let rank: Rank
11+
/// The suit of the card (hearts, diamonds, clubs, spades)
12+
let suit: Suit
13+
}
14+
15+
// MARK: - Enums
16+
/// Represents the possible ranks of a playing card
17+
enum Rank: Int, CaseIterable {
18+
case two = 2
19+
case three
20+
case four
21+
case five
22+
case six
23+
case seven
24+
case eight
25+
case nine
26+
case ten
27+
case jack
28+
case queen
29+
case king
30+
case ace
31+
32+
/// Returns a string representation of the rank
33+
var description: String {
34+
switch self {
35+
case .jack: return "J"
36+
case .queen: return "Q"
37+
case .king: return "K"
38+
case .ace: return "A"
39+
default: return "\(rawValue)"
40+
}
41+
}
42+
}
43+
44+
/// Represents the four suits in a standard deck of cards
45+
enum Suit: String, CaseIterable {
46+
case hearts = ""
47+
case diamonds = ""
48+
case clubs = ""
49+
case spades = ""
50+
}

Examples/card_game/dsl.swift

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
import SwiftBuilder
2+
3+
// Example of generating a BlackjackCard struct with a nested Suit enum
4+
let structExample = Group {
5+
Struct("Card") {
6+
Variable(.let, name: "rank", type: "Rank")
7+
.comment{
8+
Line(.doc, "The rank of the card (2-10, J, Q, K, A)")
9+
}
10+
Variable(.let, name: "suit", type: "Suit")
11+
.comment{
12+
Line(.doc, "The suit of the card (hearts, diamonds, clubs, spades)")
13+
}
14+
}
15+
.inherits("Comparable")
16+
.comment{
17+
Line("MARK: - Models")
18+
Line(.doc, "Represents a playing card in a standard 52-card deck")
19+
Line(.doc)
20+
Line(.doc, "A card has a rank (2-10, J, Q, K, A) and a suit (hearts, diamonds, clubs, spades).")
21+
Line(.doc, "Each card can be compared to other cards based on its rank.")
22+
}
23+
24+
Enum("Rank") {
25+
EnumCase("two").equals(2)
26+
EnumCase("three")
27+
EnumCase("four")
28+
EnumCase("five")
29+
EnumCase("six")
30+
EnumCase("seven")
31+
EnumCase("eight")
32+
EnumCase("nine")
33+
EnumCase("ten")
34+
EnumCase("jack")
35+
EnumCase("queen")
36+
EnumCase("king")
37+
EnumCase("ace")
38+
Struct("Values") {
39+
Variable(.let, name: "first", type: "Int")
40+
Variable(.let, name: "second", type: "Int?")
41+
}
42+
ComputedProperty("description") {
43+
Switch("self") {
44+
SwitchCase(".jack") {
45+
Return{
46+
Literal("\"J\"")
47+
}
48+
}
49+
SwitchCase(".queen") {
50+
Return{
51+
Literal("\"Q\"")
52+
}
53+
}
54+
SwitchCase(".king") {
55+
Return{
56+
Literal("\"K\"")
57+
}
58+
}
59+
SwitchCase(".ace") {
60+
Return{
61+
Literal("\"A\"")
62+
}
63+
}
64+
Default {
65+
Return{
66+
Literal("\\(rawValue)")
67+
}
68+
}
69+
}
70+
}
71+
.comment{
72+
Line(.doc, "Returns a string representation of the rank")
73+
}
74+
}
75+
.inherits("Int")
76+
.inherits("CaseIterable")
77+
.comment{
78+
Line("MARK: - Enums")
79+
Line(.doc, "Represents the possible ranks of a playing card")
80+
}
81+
82+
Enum("Suit") {
83+
EnumCase("spades").equals("")
84+
EnumCase("hearts").equals("")
85+
EnumCase("diamonds").equals("")
86+
EnumCase("clubs").equals("")
87+
}
88+
.inherits("String")
89+
.inherits("CaseIterable")
90+
.comment{
91+
Line(.doc, "Represents the possible suits of a playing card")
92+
}
93+
94+
}
95+
96+
97+
// Generate and print the code
98+
print(structExample.generateCode())

Examples/card_game/syntax.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)