aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/org/shedlang/compiler/shid/ShidCodeGenReferenceTests.java
blob: 2cad5cb5a901f633b5d0216a72ad089bb3166541 (plain)
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
package org.shedlang.compiler.shid;

import org.junit.jupiter.api.Test;
import org.shedlang.compiler.ast.typed.BoundVariable;
import org.shedlang.compiler.ast.typed.Typed;

import static org.shedlang.compiler.shid.instructions.ShidMatchers.*;
import static org.zwobble.precisely.AssertThat.assertThat;
import static org.zwobble.precisely.Matchers.isSequence;

public class ShidCodeGenReferenceTests {
    @Test
    public void referenceIsCompiledToLocalLoad() {
        var node = Typed.reference("x");
        var context = ShidCodeGen.Context.EMPTY
            .addShedVariable(BoundVariable.val("x"));

        var result = ShidCodeGen.compileExpression(node, context);

        assertThat(result.instructions(), isSequence(
            isShidLocalLoad("x__0")
        ));
    }

    @Test
    public void whenLocalIsVarThenReferenceIsCompiledToVarLoad() {
        var node = Typed.reference("x");
        var context = ShidCodeGen.Context.EMPTY
            .addShedVariable(BoundVariable.var("x"));
        context = ShidCodeGen.compileFunctionStatement(
            Typed.var("x", Typed.intLiteral(0)),
            context
        );

        var result = ShidCodeGen.compileExpression(node, context);

        assertThat(result.instructions(), isSequence(
            isShidPushInt(0),
            isShidVarStore("x__0"),
            isShidVarLoad("x__0")
        ));
    }
}