From 2d64cff0fb9eb913bce44dc61d3274ee9de22682 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 28 Jul 2019 15:01:51 -0400 Subject: Implement compilation for AestheticDirective --- .../java/bjc/utils/ioutils/format/CLValue.java | 36 +++++++++- .../format/directives/AestheticDirective.java | 77 ++++++++++++++++------ 2 files changed, 92 insertions(+), 21 deletions(-) diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/CLValue.java b/clformat/src/main/java/bjc/utils/ioutils/format/CLValue.java index 595eee5..bc78f3f 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/CLValue.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/CLValue.java @@ -20,6 +20,8 @@ public interface CLValue { return new VValue(); } + if (val == null) return new NullValue(); + switch (val) { case "V": case "v": @@ -46,7 +48,7 @@ public interface CLValue { public default int asInt(Tape params, String paramName, String directive, int def) { String param = getValue(params); - if (!param.equals("")) { + if (param != null && !param.equals("")) { try { return Integer.parseInt(param); } catch(NumberFormatException nfex) { @@ -61,6 +63,38 @@ public interface CLValue { return def; } + + public static CLValue nil() { + return new NullValue(); + } + + public default char asChar(Tape params, String paramName, String directive, char def) { + String param = getValue(params); + + if (param != null && !param.equals("")) { + if (param.length() == 1) { + // Punt in the case we have a slightly malformed + // character + return param.charAt(0); + } + + if(!param.startsWith("'")) { + throw new IllegalArgumentException( + String.format(MSG_FMT, paramName, param, directive)); + } + + return param.charAt(1); + } + + return def; + } + +} + +class NullValue implements CLValue { + public String getValue(Tape params) { + return null; + } } class PercValue implements CLValue { diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java index 35119e5..3368174 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java @@ -15,20 +15,22 @@ public class AestheticDirective implements Directive { @Override public void format(FormatParameters dirParams) throws IOException { - Tape itemTape = dirParams.tParams; + Edict edt = compile(dirParams.toCompileCTX()); - // Check that we have an item - CLFormatter.checkItem(dirParams.item, 'A'); + edt.format(dirParams.toFormatCTX()); + } - // Parameter values - int mincol = 0; - int colinc = 1; - int minpad = 0; + @Override + public Edict compile(CompileContext compCTX) { + CLParameters params = compCTX.decr.parameters; + CLModifiers mods = compCTX.decr.modifiers; - char padchar = ' '; + // Parameter values + CLValue mincol = CLValue.nil(); + CLValue colinc = CLValue.nil(); + CLValue minpad = CLValue.nil(); - CLParameters params = dirParams.getParams(); - CLModifiers mods = dirParams.getMods(); + CLValue padchar = CLValue.nil(); // We take 0, 1 or 4 parameters switch (params.length()) { @@ -38,26 +40,61 @@ public class AestheticDirective implements Directive { case 1: params.mapIndices("mincol"); - mincol = params.getInt(itemTape, "mincol", "minimum column count", "A", 0); + mincol = params.resolveKey("mincol"); break; case 4: params.mapIndices("mincol", "colinc", "minpad", "padchar"); - mincol = params.getInt(itemTape, "mincol", "minimum column count", "A", 0); - colinc = params.getInt(itemTape, "colinc", "padding increment", "A", 1); - minpad = params.getInt(itemTape, "minpad", "minimum amount of padding", "A", 0); + mincol = params.resolveKey("mincol"); + colinc = params.resolveKey("colinc"); + minpad = params.resolveKey("minpad"); - padchar = params.getChar(itemTape, "padchar", "padding character", "A", ' '); + padchar = params.resolveKey("padchar"); break; default: throw new IllegalArgumentException("Must provide either zero, one or four arguments to A directive"); } - String tmp = dirParams.item.toString(); + return new AestheticEdict(mods.atMod, padchar, mincol, colinc, minpad); + } +} + +class AestheticEdict implements Edict { + private boolean padBefore; + + private CLValue padcharPar; + private CLValue mincolPar; + private CLValue colincPar; + private CLValue minpadPar; + + public AestheticEdict(boolean padBefore, CLValue padPar, CLValue minPar, + CLValue colPar, CLValue mpadPar) { + this.padBefore = padBefore; + + this.padcharPar = padPar; + this.mincolPar = minPar; + this.colincPar = colPar; + this.minpadPar = mpadPar; + } + + @Override + public void format(FormatContext formCTX) throws IOException { + Tape itemTape = formCTX.items; + + // Check that we have an item + CLFormatter.checkItem(itemTape.item(), 'A'); + + String tmp = itemTape.item().toString(); StringBuilder work = new StringBuilder(); - if (mods.atMod) { + char padchar = padcharPar.asChar(itemTape, "padding character", "A", ' '); + + int mincol = mincolPar.asInt(itemTape, "minimum column count", "A", 0); + int colinc = colincPar.asInt(itemTape, "padding increment", "A", 1); + int minpad = minpadPar.asInt(itemTape, "minumum amount of padding", "A", 0); + + if (padBefore) { for (int i = 0; i < minpad; i++) { work.append(padchar); } @@ -71,7 +108,7 @@ public class AestheticDirective implements Directive { work.append(tmp); - if (!mods.atMod) { + if (!padBefore) { for (int i = 0; i < minpad; i++) { work.append(padchar); } @@ -83,8 +120,8 @@ public class AestheticDirective implements Directive { } } - dirParams.rw.write(work.toString()); + formCTX.writer.write(work.toString()); - dirParams.tParams.right(); + itemTape.right(); } } -- cgit v1.2.3