From dda20b14d927ed4aba765ddacf5f38e788427d95 Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Sat, 10 Aug 2019 19:25:34 -0300 Subject: Implement compilation for IndentDirective --- .../ioutils/format/directives/IndentDirective.java | 70 ++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/IndentDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/IndentDirective.java index 9226761..0898cc1 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/IndentDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/IndentDirective.java @@ -13,6 +13,12 @@ import bjc.utils.ioutils.format.*; public class IndentDirective implements Directive { @Override public void format(FormatParameters dirParams) throws IOException { + Edict edt = compile(dirParams.toCompileCTX()); + + edt.format(dirParams.toFormatCTX()); + } + + public void formatF(FormatParameters dirParams) throws IOException { Tape itemTape = dirParams.tParams; CLModifiers mods = dirParams.getMods(); @@ -47,4 +53,68 @@ public class IndentDirective implements Directive { dirParams.rw.setLevel(nIndents); } } + + @Override + public Edict compile(CompileContext compCTX) { + CLParameters params = compCTX.decr.parameters; + CLModifiers mods = compCTX.decr.modifiers; + + if (mods.dollarMod) { + return new IndentConfigureEdict(); + } + + CLValue indentCount = CLValue.nil(); + if(params.length() >= 1) { + params.mapIndices("count"); + + indentCount = params.resolveKey("count"); + } + + return new IndentEdict(indentCount, mods.colonMod); + } +} + +class IndentEdict implements Edict { + private CLValue numIndentsVal; + + private boolean isRelative; + + public IndentEdict(CLValue numIndents, boolean isRelative) { + this.numIndentsVal = numIndents; + + this.isRelative = isRelative; + } + + @Override + public void format(FormatContext formCTX) { + int numIndents = numIndentsVal.asInt(formCTX.items, "indent count", "I", 1); + + boolean dedent = false; + if (numIndents < 0) { + numIndents = -numIndents; + + dedent = true; + } + + if (isRelative) { + if (dedent) { + formCTX.writer.dedent(numIndents); + } else { + formCTX.writer.indent(numIndents); + } + } else { + if (dedent) { + throw new IllegalArgumentException("Cannot have negative indent level"); + } + + formCTX.writer.setLevel(numIndents); + } + } +} + +class IndentConfigureEdict implements Edict { + @Override + public void format(FormatContext formCTX) { + + } } -- cgit v1.2.3