Skip to content

Commit 0ec13f2

Browse files
committed
starting work on generics
1 parent 0b6f191 commit 0ec13f2

1 file changed

Lines changed: 28 additions & 21 deletions

File tree

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,40 @@
11
import SyntaxKit
22

33
// Example of generating a BlackjackCard struct with a nested Suit enum
4-
let structExample = Struct("Stack", generic: "Element") {
5-
Variable(.var, name: "items", type: "[Element]", equals: "[]")
4+
let genericGroup = Group{
5+
Protocol("Stackable") {
6+
AssociatedType("Element")
7+
FunctionRequirement("push", parameters: {
8+
Parameter(name: "item", type: "Element")
9+
}).mutating()
10+
}
11+
Struct("Stack", generic: "Element") {
12+
Variable(.var, name: "items", type: "[Element]", equals: "[]")
613

7-
Function("push") parameters:{
8-
Parameter(name: "item", type: "Element")
9-
} {
10-
VariableExp("output").call("append") {
11-
Parameter(name: "item", value: "item")
14+
Function("push") parameters:{
15+
Parameter(name: "item", type: "Element")
16+
} {
17+
VariableExp("output").call("append") {
18+
Parameter(name: "item", value: "item")
19+
}
1220
}
13-
}
1421

15-
Function("pop", returns: "Element?") {
16-
VariableExp("items").call("popLast")
17-
}
22+
Function("pop", returns: "Element?") {
23+
VariableExp("items").call("popLast")
24+
}
1825

19-
Function("peek", returns: "Element?") {
20-
VariableExp("items").property("last")
21-
}
26+
Function("peek", returns: "Element?") {
27+
VariableExp("items").property("last")
28+
}
2229

23-
ComputedProperty("isEmpty") {
24-
VariableExp("items").property("isEmpty")
25-
}
30+
ComputedProperty("isEmpty") {
31+
VariableExp("items").property("isEmpty")
32+
}
2633

27-
ComputedProperty("count") {
28-
VariableExp("items").property("count")
34+
ComputedProperty("count") {
35+
VariableExp("items").property("count")
36+
}
2937
}
3038
}
31-
3239
// Generate and print the code
33-
print(structExample.generateCode())
40+
print(genericGroup.generateCode())

0 commit comments

Comments
 (0)