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
|
package org.shedlang.compiler.shid;
import org.junit.jupiter.api.Test;
import org.shedlang.compiler.ast.typed.Typed;
import java.util.List;
import static org.shedlang.compiler.shid.instructions.ShidMatchers.*;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.isSequence;
public class ShidCodeGenAssignTests {
@Test
public void targetIgnoreIsCompiledToDiscard() {
var node = Typed.assign(
Typed.targetIgnore(),
Typed.reference("x")
);
var context = ShidCodeGen.Context.EMPTY
.addShedVal("x");
var result = ShidCodeGen.compileFunctionStatement(node, context);
assertThat(result.instructions(), isSequence(
isShidLocalLoad("x__0"),
isShidDiscard()
));
}
@Test
public void whenTargetIsReassignThenExpressionIsEvaluatedAndStoredAsVar() {
var node = Typed.assign(
Typed.targetReassign("x"),
Typed.stringLiteral("hello")
);
var context = ShidCodeGen.Context.EMPTY
.addShedVal("x");
var result = ShidCodeGen.compileFunctionStatement(node, context);
assertThat(result.instructions(), isSequence(
isShidPushString("hello"),
isShidVarStore("x__0")
));
}
@Test
public void whenTargetIsTupleThenExpressionIsDeconstructed() {
var node = Typed.assign(
Typed.targetTuple(List.of(
Typed.targetVal("a"),
Typed.targetVal("b"),
Typed.targetVal("c")
)),
Typed.reference("x")
);
var context = ShidCodeGen.Context.EMPTY
.addShedVal("x")
.addShedVal("a")
.addShedVal("b")
.addShedVal("c");
var result = ShidCodeGen.compileFunctionStatement(node, context);
assertThat(result.instructions(), isSequence(
isShidLocalLoad("x__0"),
isShidDuplicate(),
isShidTupleGet(0),
isShidLocalStore("a__0"),
isShidDuplicate(),
isShidTupleGet(1),
isShidLocalStore("b__0"),
isShidTupleGet(2),
isShidLocalStore("c__0")
));
}
@Test
public void deconstructingEmptyTupleDiscardsExpression() {
var node = Typed.assign(
Typed.targetTuple(List.of()),
Typed.reference("x")
);
var context = ShidCodeGen.Context.EMPTY
.addShedVal("x");
var result = ShidCodeGen.compileFunctionStatement(node, context);
assertThat(result.instructions(), isSequence(
isShidLocalLoad("x__0"),
isShidDiscard()
));
}
}
|