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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
|
package org.shedlang.compiler.parser;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.shedlang.compiler.ast.untyped.Untyped;
import org.shedlang.compiler.ast.untyped.UntypedBlockNode;
import org.shedlang.compiler.ast.untyped.UntypedFunctionStatementBehaviour;
import org.shedlang.compiler.parser.errors.InconsistentBlockBehaviourError;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.shedlang.compiler.ast.untyped.UntypedNodeMatchers.*;
import static org.shedlang.compiler.parser.ParserTesting.parseString;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.equalTo;
import static org.zwobble.precisely.Matchers.isSequence;
public class ParserExpressionStatementTests {
@Test
public void canParseExpressionStatement() {
var source = "f;";
var node = parseString(source, Parser::parseFunctionStatement);
assertThat(node, isUntypedExpressionStatementNode(isUntypedReferenceNode("f")));
}
@Test
public void canParseBlockReturn() {
var source = "f";
var node = parseString(source, Parser::parseFunctionStatement);
assertThat(node, isUntypedBlockReturnNode(isUntypedReferenceNode("f")));
}
@Test
public void canParseTuple() {
var source = "#(a)";
var node = parseString(source, Parser::parseFunctionStatement);
assertThat(node, isUntypedBlockReturnNode(isUntypedTupleNode(
isSequence(isUntypedReferenceNode("a"))
)));
}
@Test
public void ifWithAllBranchesAsExpressionStatementsIsExpressionStatement() {
var source = "if (a) { b; } else { c; }";
var node = parseString(source, Parser::parseFunctionStatement);
assertThat(node, isUntypedExpressionStatementNode(
isUntypedIfNode(
isSequence(
isUntypedConditionalBranchNode(
isUntypedReferenceNode("a"),
isUntypedBlockNode(isSequence(
isUntypedExpressionStatementNode(isUntypedReferenceNode("b"))
))
)
),
isUntypedBlockNode(isSequence(
isUntypedExpressionStatementNode(isUntypedReferenceNode("c"))
))
)
));
}
@Test
public void ifWithAllBranchesAsBlockReturnsIsBlockReturn() {
var source = "if (a) { b } else { c }";
var node = parseString(source, Parser::parseFunctionStatement);
assertThat(node, isUntypedBlockReturnNode(
isUntypedIfNode(
isSequence(
isUntypedConditionalBranchNode(
isUntypedReferenceNode("a"),
isUntypedBlockNode(isSequence(
isUntypedBlockReturnNode(isUntypedReferenceNode("b"))
))
)
),
isUntypedBlockNode(isSequence(
isUntypedBlockReturnNode(isUntypedReferenceNode("c"))
))
)
));
}
@Nested
public class BranchBehaviourTests {
@Test
public void whenAllBranchesContinueThenExpressionStatementContinues() {
var blocks = List.of(
blockWithBehaviour(UntypedFunctionStatementBehaviour.CONTINUE),
blockWithBehaviour(UntypedFunctionStatementBehaviour.CONTINUE)
);
var result = Parser.expressionStatementBehaviour(blocks);
assertThat(result, equalTo(UntypedFunctionStatementBehaviour.CONTINUE));
}
@Test
public void whenAllBranchesBlockThenExpressionStatementBlockReturns() {
var blocks = List.of(
blockWithBehaviour(UntypedFunctionStatementBehaviour.BLOCK_RETURN),
blockWithBehaviour(UntypedFunctionStatementBehaviour.BLOCK_RETURN)
);
var result = Parser.expressionStatementBehaviour(blocks);
assertThat(result, equalTo(UntypedFunctionStatementBehaviour.BLOCK_RETURN));
}
@Test
public void whenAllBranchesTerminateThenExpressionStatementTerminates() {
var blocks = List.of(
blockWithBehaviour(UntypedFunctionStatementBehaviour.TERMINATE),
blockWithBehaviour(UntypedFunctionStatementBehaviour.TERMINATE)
);
var result = Parser.expressionStatementBehaviour(blocks);
assertThat(result, equalTo(UntypedFunctionStatementBehaviour.TERMINATE));
}
@Test
public void whenBranchesTerminateOrContinueThenExpressionStatementContinues() {
var blocks = List.of(
blockWithBehaviour(UntypedFunctionStatementBehaviour.TERMINATE),
blockWithBehaviour(UntypedFunctionStatementBehaviour.CONTINUE),
blockWithBehaviour(UntypedFunctionStatementBehaviour.TERMINATE)
);
var result = Parser.expressionStatementBehaviour(blocks);
assertThat(result, equalTo(UntypedFunctionStatementBehaviour.CONTINUE));
}
@Test
public void whenBranchesTerminateOrBlockReturnThenExpressionStatementBlockReturns() {
var blocks = List.of(
blockWithBehaviour(UntypedFunctionStatementBehaviour.TERMINATE),
blockWithBehaviour(UntypedFunctionStatementBehaviour.BLOCK_RETURN),
blockWithBehaviour(UntypedFunctionStatementBehaviour.TERMINATE)
);
var result = Parser.expressionStatementBehaviour(blocks);
assertThat(result, equalTo(UntypedFunctionStatementBehaviour.BLOCK_RETURN));
}
@Test
public void whenBranchesContinueOrBlockReturnThenErrorIsThrown() {
var blocks = List.of(
blockWithBehaviour(UntypedFunctionStatementBehaviour.CONTINUE),
blockWithBehaviour(UntypedFunctionStatementBehaviour.BLOCK_RETURN)
);
assertThrows(
InconsistentBlockBehaviourError.class,
() -> Parser.expressionStatementBehaviour(blocks)
);
}
@Test
public void whenThereAreNoBranchesThenExpressionStatementTerminates() {
var blocks = List.<UntypedBlockNode>of();
var result = Parser.expressionStatementBehaviour(blocks);
assertThat(result, equalTo(UntypedFunctionStatementBehaviour.TERMINATE));
}
private UntypedBlockNode blockWithBehaviour(UntypedFunctionStatementBehaviour behaviour) {
var statement = switch (behaviour) {
case TERMINATE ->
Untyped.functionReturn(Untyped.intLiteral(0));
case BLOCK_RETURN ->
Untyped.blockReturn(Untyped.intLiteral(0));
case CONTINUE ->
Untyped.expressionStatement(Untyped.intLiteral(0));
};
var block = Untyped.block(List.of(statement));
assertThat(block.behaviour(), equalTo(behaviour));
return block;
}
}
}
|