From 829ff6db80143208fd6f1a09f831aa1da72fd2d7 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 30 Jul 2023 18:06:05 +0100 Subject: Support vars in stack compiler --- .../shedlang/compiler/stackir/StackCompiler.java | 102 ++++++++++++++++----- .../org/shedlang/compiler/ast/typed/Typed.java | 14 +++ .../compiler/stackir/StackCompilerAssignTests.java | 28 ++++++ .../stackir/StackCompilerReferenceTests.java | 21 ++++- .../compiler/stackir/StackCompilerVarTests.java | 27 ++++++ .../stackir/instructions/InstructionMatchers.java | 14 +++ 6 files changed, 181 insertions(+), 25 deletions(-) create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerAssignTests.java create mode 100644 src/test/java/org/shedlang/compiler/stackir/StackCompilerVarTests.java (limited to 'src') diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java index 8863253c..5b58d975 100644 --- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java +++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java @@ -3,15 +3,23 @@ package org.shedlang.compiler.stackir; import org.pcollections.*; import org.shedlang.compiler.ShedBuiltins; import org.shedlang.compiler.ast.typed.*; +import org.shedlang.compiler.sources.Source; import org.shedlang.compiler.stackir.instructions.*; import org.shedlang.compiler.util.P; import java.util.List; +import java.util.function.BiFunction; public class StackCompiler { private StackCompiler() { } + private static Context compileAssign(TypedAssignNode node, Context context) { + context = compileExpression(node.expression(), context); + context = compileTarget(node.target(), VarStoreInstruction::new, context); + return context; + } + private static Context compileBinaryOperation( TypedBinaryOperationNode node, Context context @@ -202,7 +210,7 @@ public class StackCompiler { ) { return switch (node) { case TypedAssignNode assign -> - throw new UnsupportedOperationException(); + compileAssign(assign, context); case TypedBlockReturnNode blockReturn -> compileBlockReturn(blockReturn, context); @@ -217,7 +225,7 @@ public class StackCompiler { compileVal(val, context); case TypedVarNode var -> - throw new UnsupportedOperationException(); + compileVar(var, context); }; } @@ -319,9 +327,19 @@ public class StackCompiler { } private static Context compileReference(TypedReferenceNode node, Context context) { - var localName = context.shedVariableToLocal(node.name()); + var shedVariable = context.shedVariable(node.name()); - return context.addInstruction(new LocalLoadInstruction(localName, node.source())); + if (shedVariable.isVar) { + return context.addInstruction(new VarLoadInstruction( + shedVariable.stackIrLocalName, + node.source() + )); + } else { + return context.addInstruction(new LocalLoadInstruction( + shedVariable.stackIrLocalName, + node.source() + )); + } } private static Context compileStringLiteral( @@ -332,11 +350,15 @@ public class StackCompiler { return context.addInstruction(instruction); } - private static Context compileTarget(TypedTargetNode target, Context context) { + private static Context compileTarget( + TypedTargetNode target, + BiFunction store, + Context context + ) { return switch (target) { case TypedTargetNode.Variable variable -> { context = context.addInstruction( - new LocalStoreInstruction( + store.apply( context.shedVariableToLocal(variable.name()), variable.source() ) @@ -349,7 +371,20 @@ public class StackCompiler { private static Context compileVal(TypedValNode val, Context context) { context = compileExpression(val.expression(), context); - context = compileTarget(val.target(), context); + context = compileTarget(val.target(), LocalStoreInstruction::new, context); + return context; + } + + private static Context compileVar(TypedVarNode var, Context context) { + context = compileExpression(var.expression(), context); + context = compileTarget(var.target(), VarStoreInstruction::new, context); + + // TODO: better to store this information in the block node with the bound variables? + switch (var.target()) { + case TypedTargetNode.Variable variable -> + context = context.markShedVariableAsVar(variable.name()); + } + return context; } @@ -365,20 +400,21 @@ public class StackCompiler { private final PStack> instructionsStack; private final PVector instructions; private final PSet localNames; - private final PStack> shedVariableNameToLocalNameStack; - private final PMap shedVariableNameToLocalName; + private final PStack> shedVariablesByNameStack; + private final PMap shedVariablesByName; private Context( PStack> instructionsStack, PVector instructions, PSet localNames, - PStack> shedVariableNameToLocalNameStack, PMap shedVariableNameToLocalName + PStack> shedVariablesByNameStack, + PMap shedVariablesByName ) { this.instructionsStack = instructionsStack; this.instructions = instructions; this.localNames = localNames; - this.shedVariableNameToLocalNameStack = shedVariableNameToLocalNameStack; - this.shedVariableNameToLocalName = shedVariableNameToLocalName; + this.shedVariablesByNameStack = shedVariablesByNameStack; + this.shedVariablesByName = shedVariablesByName; } public Context enterBlock(List shedVariableNames) { @@ -386,8 +422,8 @@ public class StackCompiler { this.instructionsStack.plus(this.instructions), P.vector(), this.localNames, - this.shedVariableNameToLocalNameStack.plus(this.shedVariableNameToLocalName), - this.shedVariableNameToLocalName + this.shedVariablesByNameStack.plus(this.shedVariablesByName), + this.shedVariablesByName ); for (var shedVariableName : shedVariableNames) { @@ -402,8 +438,8 @@ public class StackCompiler { this.instructionsStack.minus(0), this.instructionsStack.get(0), this.localNames, - this.shedVariableNameToLocalNameStack.minus(0), - this.shedVariableNameToLocalNameStack.get(0) + this.shedVariablesByNameStack.minus(0), + this.shedVariablesByNameStack.get(0) ); } @@ -412,19 +448,23 @@ public class StackCompiler { this.instructionsStack, this.instructions.plus(instruction), this.localNames, - this.shedVariableNameToLocalNameStack, - this.shedVariableNameToLocalName + this.shedVariablesByNameStack, + this.shedVariablesByName ); } public String shedVariableToLocal(String variableName) { - var localName = this.shedVariableNameToLocalName.get(variableName); + return shedVariable(variableName).stackIrLocalName; + } - if (localName == null) { - throw new RuntimeException("No local name for Shed variable: " + variableName); + public ShedVariable shedVariable(String variableName) { + var shedVariable = this.shedVariablesByName.get(variableName); + + if (shedVariable == null) { + throw new RuntimeException("Unknown Shed variable: " + variableName); } - return localName; + return shedVariable; } public Context addShedVariable(String variableName) { @@ -434,8 +474,20 @@ public class StackCompiler { this.instructionsStack, this.instructions, this.localNames.plus(localName), - this.shedVariableNameToLocalNameStack, - this.shedVariableNameToLocalName.plus(variableName, localName) + this.shedVariablesByNameStack, + this.shedVariablesByName.plus(variableName, new ShedVariable(localName, false)) + ); + } + + public Context markShedVariableAsVar(String variableName) { + var localName = shedVariableToLocal(variableName); + + return new Context( + this.instructionsStack, + this.instructions, + this.localNames, + this.shedVariablesByNameStack, + this.shedVariablesByName.plus(variableName, new ShedVariable(localName, true)) ); } @@ -455,4 +507,6 @@ public class StackCompiler { return this.instructions; } } + + private record ShedVariable(String stackIrLocalName, boolean isVar) {} } diff --git a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java index 75a67f13..c73e77a0 100644 --- a/src/test/java/org/shedlang/compiler/ast/typed/Typed.java +++ b/src/test/java/org/shedlang/compiler/ast/typed/Typed.java @@ -13,6 +13,13 @@ public class Typed { private Typed() { } + public static TypedAssignNode assign( + TypedTargetNode target, + TypedExpressionNode expression + ) { + return new TypedAssignNode(target, expression, Sources.NULL); + } + public static TypedBlockNode block(List statements) { return new TypedBlockNode(statements, List.of(), Sources.NULL); } @@ -195,4 +202,11 @@ public class Typed { ) { return new TypedValNode(target, expression, Sources.NULL); } + + public static TypedVarNode var( + TypedTargetNode target, + TypedExpressionNode expression + ) { + return new TypedVarNode(target, expression, Sources.NULL); + } } diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerAssignTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerAssignTests.java new file mode 100644 index 00000000..5b138e7d --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerAssignTests.java @@ -0,0 +1,28 @@ +package org.shedlang.compiler.stackir; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.ast.typed.Typed; + +import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isPushStringInstruction; +import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isVarStoreInstruction; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class StackCompilerAssignTests { + @Test + public void expressionIsEvaluatedAndStoredAsVar() { + var node = Typed.assign( + Typed.targetVariable("x"), + Typed.stringLiteral("hello") + ); + var context = StackCompiler.Context.EMPTY + .addShedVariable("x"); + + var result = StackCompiler.compileFunctionStatement(node, context); + + assertThat(result.instructions(), isSequence( + isPushStringInstruction("hello"), + isVarStoreInstruction("x__0") + )); + } +} diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerReferenceTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerReferenceTests.java index 96907222..1e98c35e 100644 --- a/src/test/java/org/shedlang/compiler/stackir/StackCompilerReferenceTests.java +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerReferenceTests.java @@ -3,7 +3,7 @@ package org.shedlang.compiler.stackir; import org.junit.jupiter.api.Test; import org.shedlang.compiler.ast.typed.Typed; -import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.isLocalLoadInstruction; +import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; import static org.zwobble.precisely.AssertThat.assertThat; import static org.zwobble.precisely.Matchers.isSequence; @@ -20,4 +20,23 @@ public class StackCompilerReferenceTests { isLocalLoadInstruction("x__0") )); } + + @Test + public void whenLocalIsVarThenReferenceIsCompiledToVarLoad() { + var node = Typed.reference("x"); + var context = StackCompiler.Context.EMPTY + .addShedVariable("x"); + context = StackCompiler.compileFunctionStatement( + Typed.var(Typed.targetVariable("x"), Typed.intLiteral(0)), + context + ); + + var result = StackCompiler.compileExpression(node, context); + + assertThat(result.instructions(), isSequence( + isPushIntInstruction(0), + isVarStoreInstruction("x__0"), + isVarLoadInstruction("x__0") + )); + } } diff --git a/src/test/java/org/shedlang/compiler/stackir/StackCompilerVarTests.java b/src/test/java/org/shedlang/compiler/stackir/StackCompilerVarTests.java new file mode 100644 index 00000000..414f20d9 --- /dev/null +++ b/src/test/java/org/shedlang/compiler/stackir/StackCompilerVarTests.java @@ -0,0 +1,27 @@ +package org.shedlang.compiler.stackir; + +import org.junit.jupiter.api.Test; +import org.shedlang.compiler.ast.typed.Typed; + +import static org.shedlang.compiler.stackir.instructions.InstructionMatchers.*; +import static org.zwobble.precisely.AssertThat.assertThat; +import static org.zwobble.precisely.Matchers.isSequence; + +public class StackCompilerVarTests { + @Test + public void expressionIsEvaluatedAndStoredAsVar() { + var node = Typed.var( + Typed.targetVariable("x"), + Typed.stringLiteral("hello") + ); + var context = StackCompiler.Context.EMPTY + .addShedVariable("x"); + + var result = StackCompiler.compileFunctionStatement(node, context); + + assertThat(result.instructions(), isSequence( + isPushStringInstruction("hello"), + isVarStoreInstruction("x__0") + )); + } +} diff --git a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java index 4be747b7..a95680d0 100644 --- a/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java +++ b/src/test/java/org/shedlang/compiler/stackir/instructions/InstructionMatchers.java @@ -118,4 +118,18 @@ public class InstructionMatchers { public static Matcher isStringAddInstruction() { return instanceOf(StringAddInstruction.class); } + + public static Matcher isVarLoadInstruction(String name) { + return instanceOf( + VarLoadInstruction.class, + has("name", x -> x.name(), equalTo(name)) + ); + } + + public static Matcher isVarStoreInstruction(String name) { + return instanceOf( + VarStoreInstruction.class, + has("name", x -> x.name(), equalTo(name)) + ); + } } -- cgit v1.2.3