diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2019-08-08 21:51:33 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2019-08-08 21:51:33 -0300 |
| commit | 3c68786af3f2a53b735cb6a088f8dd67fc1a70c1 (patch) | |
| tree | da626ccc6b4eaf087da6e789b5a1540b386fd1e3 /clformat | |
| parent | 1bd9d9803c7a8056831af51d14bad7f5105c3ad3 (diff) | |
Implement compilation of GotoDirective
Diffstat (limited to 'clformat')
| -rw-r--r-- | clformat/src/main/java/bjc/utils/ioutils/format/directives/GotoDirective.java | 85 |
1 files changed, 65 insertions, 20 deletions
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<Object> 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<Object> 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); + } + + } } |
