diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2019-10-02 17:35:17 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2019-10-02 17:35:17 -0300 |
| commit | e6614156f4b86d531720ff3b8ff55ca19ee7b978 (patch) | |
| tree | 03b236547643eea5166591ea1c5e0856be915758 /clformat/src/main/java | |
| parent | 9a003b4e9a44d5685554d9f31c6249c1f087d6ad (diff) | |
More compilation work
Diffstat (limited to 'clformat/src/main/java')
3 files changed, 158 insertions, 0 deletions
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<Decree> cltok) { + List<Edict> 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 = "<null>"; + + 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<Edict> edicts; + + /** + * Create a new compiled format string. + * + * @param edts + * The compiled directives that make up the format. + */ + public CLString(List<Edict> edts) { + edicts = edts; + } + + public String format(Object... parms) throws IOException { + StringWriter sw = new StringWriter(); + ReportWriter rw = new ReportWriter(sw); + + Tape<Object> 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); + } +} |
