aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/shedlang/compiler/stackir
diff options
context:
space:
mode:
authorMichael Williamson <mike@zwobble.org>2023-09-16 16:35:38 +0100
committerMichael Williamson <mike@zwobble.org>2023-09-16 16:35:38 +0100
commit02bd64dfdbfa8134cb93165ee606817b2e6737b5 (patch)
tree9a2e463a79150fc90a1481b768c89b2158481dfc /src/main/java/org/shedlang/compiler/stackir
parent904f02af5e2965f73b037e4b3c5d97f57af81987 (diff)
Combine assign, val and var statements
Diffstat (limited to 'src/main/java/org/shedlang/compiler/stackir')
-rw-r--r--src/main/java/org/shedlang/compiler/stackir/StackCompiler.java63
1 files changed, 30 insertions, 33 deletions
diff --git a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
index 1621a50c..2c9d6065 100644
--- a/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
+++ b/src/main/java/org/shedlang/compiler/stackir/StackCompiler.java
@@ -3,12 +3,10 @@ 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() {
@@ -16,7 +14,7 @@ public class StackCompiler {
private static Context compileAssign(TypedAssignNode node, Context context) {
context = compileExpression(node.expression(), context);
- context = compileTarget(node.target(), VarStoreInstruction::new, context);
+ context = compileTarget(node.target(), context);
return context;
}
@@ -239,12 +237,6 @@ public class StackCompiler {
case TypedFunctionReturnNode functionReturn ->
compileFunctionReturn(functionReturn, context);
- case TypedValNode val ->
- compileVal(val, context);
-
- case TypedVarNode var ->
- compileVar(var, context);
-
case TypedWhileNode while_ ->
compileWhile(while_, context);
};
@@ -340,11 +332,11 @@ public class StackCompiler {
public static Context compileNamespaceStatement(TypedNamespaceStatementNode node, Context context) {
return switch (node) {
+ case TypedAssignNode assign ->
+ compileAssign(assign, context);
+
case TypedFunctionDefinitionNode functionDefinition ->
compileFunctionDefinition(functionDefinition, context);
-
- case TypedValNode val ->
- compileVal(val, context);
};
}
@@ -374,40 +366,45 @@ public class StackCompiler {
private static Context compileTarget(
TypedTargetNode target,
- BiFunction<String, Source, Instruction> store,
Context context
) {
return switch (target) {
- case TypedTargetNode.Variable variable -> {
+ case TypedTargetNode.Reassign reassign -> {
context = context.addInstruction(
- store.apply(
- context.shedVariableToLocal(variable.name()),
- variable.source()
+ new VarStoreInstruction(
+ context.shedVariableToLocal(reassign.name()),
+ reassign.source()
)
);
yield context;
}
- };
- }
- private static Context compileVal(TypedValNode val, Context context) {
- context = compileExpression(val.expression(), context);
- context = compileTarget(val.target(), LocalStoreInstruction::new, context);
- return context;
- }
+ case TypedTargetNode.Val val -> {
+ context = context.addInstruction(
+ new LocalStoreInstruction(
+ context.shedVariableToLocal(val.name()),
+ val.source()
+ )
+ );
- private static Context compileVar(TypedVarNode var, Context context) {
- context = compileExpression(var.expression(), context);
- context = compileTarget(var.target(), VarStoreInstruction::new, context);
+ yield 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());
- }
+ case TypedTargetNode.Var var -> {
+ context = context.addInstruction(
+ new VarStoreInstruction(
+ context.shedVariableToLocal(var.name()),
+ var.source()
+ )
+ );
- return context;
+ // TODO: better to store this information in the block node with the bound variables?
+ context = context.markShedVariableAsVar(var.name());
+
+ yield context;
+ }
+ };
}
private static Context compileWhile(TypedWhileNode node, Context context) {