blob: 3b3164fc6ca50e1a9f0afa96c194802e222febb3 (
plain) (
blame)
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
// Test if-else expressions.
---
// Test condition evaluation.
#if 1 < 2 [
One.
]
#if true == false [
{Bad}, but we {dont-care}!
]
---
// Braced condition.
#if {true} [
One.
]
// Template in condition.
#if [] != none [
Two.
]
// Multi-line condition with parens.
#if (
1 + 1
== 1
) [
Nope.
] else {
"Three."
}
// Multiline.
#if false [
Bad.
] else {
let point = "."
"Four" + point
}
// Template can be argument or body depending on whitespace.
{
if "template" == type[b] [Fi] else [Nope]
if "template" == type [Nope] else [ve.]
}
#let i = 3
#if i < 2 [
Five.
] else if i < 4 [
Six.
] else [
Seven.
]
---
// Test else if.
// Ref: false
#let nth(n) = {
str(n)
(if n == 1 { "st" }
else if n == 2 { "nd" }
else if n == 3 { "rd" }
else { "th" })
}
#test(nth(1), "1st")
#test(nth(2), "2nd")
#test(nth(3), "3rd")
#test(nth(4), "4th")
#test(nth(5), "5th")
---
// Value of if expressions.
// Ref: false
{
let x = 1
let y = 2
let z
// Returns if branch.
z = if x < y { "ok" }
test(z, "ok")
// Returns else branch.
z = if x > y { "bad" } else { "ok" }
test(z, "ok")
// Missing else evaluates to none.
z = if x > y { "bad" }
test(z, none)
}
---
// Condition must be boolean.
// If it isn't, neither branch is evaluated.
// Error: 5-14 expected boolean, found string
#if "a" + "b" { nope } else { nope }
---
// Make sure that we don't complain twice.
// Error: 5-12 cannot add integer and string
#if 1 + "2" {}
---
// Error: 4 expected expression
#if
// Error: 4 expected expression
{if}
// Error: 6 expected body
#if x
// Error: 1-6 unexpected keyword `else`
#else {}
// Should output `x`.
// Error: 4 expected expression
#if
x {}
// Should output `something`.
// Error: 6 expected body
#if x something
// Should output `A thing.`
// Error: 19 expected body
A#if false {} else thing
#if a []else [b]
#if a [] else [b]
#if a {} else [b]
|