From e6614156f4b86d531720ff3b8ff55ca19ee7b978 Mon Sep 17 00:00:00 2001 From: "Benjamin J. Culkin" Date: Wed, 2 Oct 2019 17:35:17 -0300 Subject: More compilation work --- .../java/bjc/utils/ioutils/format/CLFormatter.java | 89 ++++++++++++++++++++++ .../java/bjc/utils/ioutils/format/CLString.java | 42 ++++++++++ .../ioutils/format/directives/StringEdict.java | 27 +++++++ 3 files changed, 158 insertions(+) create mode 100644 clformat/src/main/java/bjc/utils/ioutils/format/CLString.java create mode 100644 clformat/src/main/java/bjc/utils/ioutils/format/directives/StringEdict.java (limited to 'clformat/src/main/java/bjc/utils/ioutils/format') diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/CLFormatter.java b/clformat/src/main/java/bjc/utils/ioutils/format/CLFormatter.java index 91ff30c..86ffd54 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/CLFormatter.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/CLFormatter.java @@ -370,4 +370,93 @@ public class CLFormatter { doTail = false; } } + + public void compile(Iterator cltok) { + List result = new ArrayList<>(); + + while (cltok.hasNext()) { + Decree decr = cltok.next(); + String nam = decr.name; + + CompileContext compCTX = new CompileContext(cltok, this, decr); + + if (decr.isLiteral) { + result.add(new StringEdict(decr.name)); + + continue; + } + + if(decr.isUserCall) { + /* + * @TODO implement user-called functions. + */ + throw new IllegalArgumentException("User-called functions have not yet been implemented"); + } + + if(extraDirectives.containsKey(nam)) { + Edict edt = extraDirectives.get(nam).compile(compCTX); + + result.add(edt); + + continue; + } else if(builtinDirectives.containsKey(nam)) { + Edict edt = builtinDirectives.get(nam).compile(compCTX); + + result.add(edt); + + continue; + } + + if (nam == null) nam = ""; + + switch(nam) { + case "]": + throw new IllegalArgumentException("Found conditional-end outside of conditional."); + case ";": + throw new IllegalArgumentException( + "Found seperator outside of block."); + case "}": + throw new IllegalArgumentException("Found iteration-end outside of iteration"); + case ")": + throw new IllegalArgumentException("Case-conversion end outside of case conversion"); + case "`]": + throw new IllegalArgumentException("Inflection-end outside of inflection"); + case "<": + case ">": + throw new IllegalArgumentException("Inflection marker outside of inflection"); + case "`<": + case "`>": + throw new IllegalArgumentException("Layout-control directives aren't implemented yet."); + case "F": + case "E": + case "G": + case "$": + /* @TODO + * + * implement floating point directives. + */ + throw new IllegalArgumentException("Floating-point directives aren't implemented yet."); + case "W": + /* + * @TODO + * + * figure out if we want to + * implement someting for these + * directives instead of + * punting. + */ + throw new IllegalArgumentException("S and W aren't implemented. Use A instead"); + case "P": + throw new IllegalArgumentException("These directives aren't implemented yet"); + case "\n": + /* + * Ignored newline. + */ + break; + default: + String msg = String.format("Unknown format directive '%s'", nam); + throw new IllegalArgumentException(msg); + } + } + } } diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/CLString.java b/clformat/src/main/java/bjc/utils/ioutils/format/CLString.java new file mode 100644 index 0000000..74e4960 --- /dev/null +++ b/clformat/src/main/java/bjc/utils/ioutils/format/CLString.java @@ -0,0 +1,42 @@ +package bjc.utils.ioutils.format; + +import java.io.*; +import java.util.*; + +import bjc.utils.esodata.*; +import bjc.utils.ioutils.*; +import bjc.utils.ioutils.format.directives.*; + +/** + * Represents a compiled format string. + * + * @author Ben Culkin + */ +public class CLString { + private List edicts; + + /** + * Create a new compiled format string. + * + * @param edts + * The compiled directives that make up the format. + */ + public CLString(List edts) { + edicts = edts; + } + + public String format(Object... parms) throws IOException { + StringWriter sw = new StringWriter(); + ReportWriter rw = new ReportWriter(sw); + + Tape itms = new SingleTape<>(parms); + + FormatContext formCTX = new FormatContext(rw, itms); + + for (Edict edt : edicts) { + edt.format(formCTX); + } + + return rw.toString(); + } +} diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/StringEdict.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/StringEdict.java new file mode 100644 index 0000000..8267145 --- /dev/null +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/StringEdict.java @@ -0,0 +1,27 @@ +package bjc.utils.ioutils.format.directives; + +import java.io.*; + +/** + * Edict that prints a provided string. + * + * @author Ben Culkin + */ +public class StringEdict implements Edict { + private String val; + + /** + * Create a new string edict for a given string. + * + * @param vl + * The string to print. + */ + public StringEdict(String vl) { + this.val = vl; + } + + @Override + public void format(FormatContext formCTX) throws IOException { + formCTX.writer.write(val); + } +} -- cgit v1.2.3