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") )); } }