From 3c68786af3f2a53b735cb6a088f8dd67fc1a70c1 Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Thu, 8 Aug 2019 21:51:33 -0300 Subject: Implement compilation of GotoDirective --- .../ioutils/format/directives/GotoDirective.java | 85 +++++++++++++++++----- 1 file changed, 65 insertions(+), 20 deletions(-) (limited to 'clformat/src/main/java') diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/GotoDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/GotoDirective.java index 22196fa..a52b577 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/GotoDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/GotoDirective.java @@ -1,5 +1,7 @@ package bjc.utils.ioutils.format.directives; +import java.io.*; + import bjc.utils.esodata.*; import bjc.utils.ioutils.format.*; @@ -10,43 +12,86 @@ import bjc.utils.ioutils.format.*; * */ public class GotoDirective implements Directive { + @Override + public void format(FormatParameters dirParams) throws IOException { + Edict edt = compile(dirParams.toCompileCTX()); + + edt.format(dirParams.toFormatCTX()); + } @Override - public void format(FormatParameters dirParams) { - Tape itemTape = dirParams.tParams; + public Edict compile(CompileContext compCTX) { + CLParameters params = compCTX.decr.parameters; + CLModifiers mods = compCTX.decr.modifiers; - CLParameters params = dirParams.getParams(); - CLModifiers mods = dirParams.getMods(); + CLValue numVal = CLValue.nil(); + GotoEdict.Mode mode; if (mods.colonMod) { - int num = 1; + mode = GotoEdict.Mode.BACKWARD; + if (params.length() >= 1) { params.mapIndices("numargs"); - - num = params.getInt(itemTape, "numargs", "number of arguments backward", "*", 1); + numVal = params.resolveKey("numargs"); } - - dirParams.tParams.left(num); } else if (mods.atMod) { - int num = 0; + mode = GotoEdict.Mode.INDEX; + if (params.length() >= 1) { params.mapIndices("argidx"); - - num = params.getInt(itemTape, "argidx", "argument index", "*", 0); + numVal = params.resolveKey("argidx"); } - - dirParams.tParams.first(); - dirParams.tParams.right(num); } else { - int num = 1; + mode = GotoEdict.Mode.FORWARD; + if (params.length() >= 1) { params.mapIndices("numargs"); - - num = params.getInt(itemTape, "numargs", "number of arguments forward", "*", 1); + numVal = params.resolveKey("numargs"); } - - dirParams.tParams.right(num); } + + return new GotoEdict(mode, numVal); } +} + +class GotoEdict implements Edict { + public static enum Mode { + FORWARD, + BACKWARD, + INDEX + } + + private Mode mode; + private CLValue numVal; + + public GotoEdict(Mode mode, CLValue numVal) { + this.mode = mode; + + this.numVal = numVal; + } + + @Override + public void format(FormatContext formCTX) { + Tape items = formCTX.items; + + int num; + switch(mode) { + case FORWARD: + num = numVal.asInt(items, "number of arguments forward", "*", 1); + items.right(num); + break; + case BACKWARD: + num = numVal.asInt(items, "number of arguments backward", "*", 1); + items.left(num); + break; + case INDEX: + num = numVal.asInt(items, "argument index", "*", 0); + items.seekTo(num); + break; + default: + throw new IllegalArgumentException("Unsupported goto mode " + mode); + } + + } } -- cgit v1.2.3