-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdsl.swift
More file actions
74 lines (66 loc) · 1.92 KB
/
dsl.swift
File metadata and controls
74 lines (66 loc) · 1.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
Variable(.var, name: "vendingMachine", equals: Init("VendingMachine"))
Assignment("vendingMachine.coinsDeposited", Literal.integer(8))
Do {
Call("buyFavoriteSnack") {
ParameterExp(name: "person", value: Literal.string("Alice"))
ParameterExp(name: "vendingMachine", value: Literal.ref("vendingMachine"))
}.throwing()
Call("print") {
ParameterExp(unlabeled: Literal.string("Success! Yum."))
}
} catch: {
Catch(EnumCase("invalidSelection")) {
Call("print") {
ParameterExp(unlabeled: Literal.string("Invalid Selection."))
}
}
Catch(EnumCase("outOfStock")) {
Call("print") {
ParameterExp(unlabeled: Literal.string("Out of Stock."))
}
}
Catch(
EnumCase("insufficientFunds").associatedValue(
"coinsNeeded", type: "Int")
) {
Call("print") {
ParameterExp(
unlabeled: Literal.string(
"Insufficient funds. Please insert an additional \\(coinsNeeded) coins."))
}
}
Catch {
Call("print") {
ParameterExp(unlabeled: Literal.string("Unexpected error: \\(error)."))
}
}
}
Function("summarize") {
Parameter(name: "ratings", type: "[Int]")
} _: {
Guard{
VariableExp("ratings").property("isEmpty").not()
} else: {
Throw(EnumCase("noRatings"))
}
}.throws("StatisticsError")
Do {
Variable(.let, name: "data") {
Call("fetchUserData") {
ParameterExp(name: "id", value: Literal.integer(1))
}
}.async()
Variable(.let, name: "posts") {
Call("fetchUserPosts") {
ParameterExp(name: "id", value: Literal.integer(1))
}
}.async()
TupleAssignment(["fetchedData", "fetchedPosts"], equals: Tuple {
VariableExp("data")
VariableExp("posts")
}).async().throwing()
} catch: {
Catch(EnumCase("fetchError")) {
// Example catch for async/await
}
}