From 02bd64dfdbfa8134cb93165ee606817b2e6737b5 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sat, 16 Sep 2023 16:35:38 +0100 Subject: Combine assign, val and var statements --- .../shedlang/compiler/stackir/StackCompiler.java | 63 +++++++++++----------- 1 file changed, 30 insertions(+), 33 deletions(-) (limited to 'src/main/java/org/shedlang/compiler/stackir') 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 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) { -- cgit v1.2.3