diff options
| author | Michael Williamson <mike@zwobble.org> | 2023-12-13 22:44:55 +0000 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2023-12-13 22:45:23 +0000 |
| commit | 86b4a6cdb0e2c311bdcbdcb109e77d04b094a86f (patch) | |
| tree | 72ee2d698d1410ed319dfdb526ed4eb5c0e36af8 /src/main/java | |
| parent | 06653d434794e612672027ecf44e9fc40c1758f0 (diff) | |
Store bound locals in ShidBlock
Diffstat (limited to 'src/main/java')
3 files changed, 18 insertions, 4 deletions
diff --git a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java index 2888446d..a16ae895 100644 --- a/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java +++ b/src/main/java/org/shedlang/compiler/shid/ShidCodeGen.java @@ -734,8 +734,9 @@ public class ShidCodeGen { context = compileExpression(node.condition(), context); context = context.addInstruction(new ShidIf( 0, - new ShidBlock(List.of(), node.source()), + new ShidBlock(List.of(), List.of(), node.source()), new ShidBlock( + List.of(), List.of( new ShidLoopBreak(node.source()) ), @@ -761,31 +762,37 @@ public class ShidCodeGen { public static final BlockContext EMPTY = new BlockContext( P.vector(), P.map(), + P.vector(), Sources.NULL ); private final PVector<ShidInstruction> instructions; private final PMap<String, ShedVariable> shedVariablesByName; + private final PVector<String> boundLocals; private final Source source; public BlockContext( PVector<ShidInstruction> instructions, PMap<String, ShedVariable> shedVariablesByName, + PVector<String> boundLocals, Source source ) { this.instructions = instructions; this.shedVariablesByName = shedVariablesByName; + this.boundLocals = boundLocals; this.source = source; } public ShidBlock toBlock() { - return new ShidBlock(this.instructions, this.source); + // TODO: test collection of bound locals + return new ShidBlock(this.boundLocals, this.instructions, this.source); } public BlockContext addInstruction(ShidInstruction instruction) { return new BlockContext( this.instructions.plus(instruction), this.shedVariablesByName, + this.boundLocals, this.source ); } @@ -804,6 +811,7 @@ public class ShidCodeGen { return new BlockContext( this.instructions, this.shedVariablesByName.plus(name, variable), + this.boundLocals.plus(variable.shidLocalName), this.source ); } @@ -834,7 +842,12 @@ public class ShidCodeGen { public Context enterBlock(List<BoundVariable> shedVariables, Source source) { var context = new Context( this.blockStack.plus(this.block), - new BlockContext(P.vector(), this.block.shedVariablesByName, source), + new BlockContext( + P.vector(), + this.block.shedVariablesByName, + P.vector(), + source + ), this.localNames ); diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidBlock.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidBlock.java index 427c2a88..9bf17b3f 100644 --- a/src/main/java/org/shedlang/compiler/shid/instructions/ShidBlock.java +++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidBlock.java @@ -5,6 +5,7 @@ import org.shedlang.compiler.sources.Source; import java.util.List; public record ShidBlock( + List<String> boundLocals, List<ShidInstruction> instructions, Source source ) implements ShidInstruction { diff --git a/src/main/java/org/shedlang/compiler/shid/instructions/ShidDefineFunction.java b/src/main/java/org/shedlang/compiler/shid/instructions/ShidDefineFunction.java index 9377a69b..5ae8bf7a 100644 --- a/src/main/java/org/shedlang/compiler/shid/instructions/ShidDefineFunction.java +++ b/src/main/java/org/shedlang/compiler/shid/instructions/ShidDefineFunction.java @@ -55,7 +55,7 @@ public record ShidDefineFunction( positionalParams, namedParams, freeVariables.build(), - new ShidBlock(body, Sources.NULL), + new ShidBlock(List.of(), body, Sources.NULL), variant, Sources.NULL ); |
