summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorstudent <student@localhost>2018-02-12 13:56:22 -0500
committerstudent <student@localhost>2018-02-12 13:56:22 -0500
commit5115f1d2a7eab41436debc696870953e18a1b236 (patch)
treedbb2453580038b9f8102bb5bc53c60d2410f0f00
parent4c8639f361983d97a7b46282b12528b550fff946 (diff)
General update
-rw-r--r--base/src/bjc/dicelang/CLIArgsParser.java14
-rw-r--r--base/src/bjc/dicelang/CompilerTweaker.java1
-rw-r--r--base/src/bjc/dicelang/Define.java7
-rw-r--r--base/src/bjc/dicelang/DiceLangConsole.java5
-rw-r--r--base/src/bjc/dicelang/DiceLangEngine.java23
-rw-r--r--base/src/bjc/dicelang/DiceToken.java43
-rw-r--r--base/src/bjc/dicelang/Errors.java7
-rw-r--r--base/src/bjc/dicelang/Token.java99
-rw-r--r--base/src/bjc/dicelang/Tokenizer.java20
-rw-r--r--base/src/bjc/dicelang/expr/ExprREPL.java96
-rw-r--r--base/src/bjc/dicelang/expr/Lexer.java9
-rw-r--r--base/src/bjc/dicelang/expr/Parser.java94
-rw-r--r--base/src/bjc/dicelang/expr/Shunter.java14
-rw-r--r--base/src/bjc/dicelang/expr/Token.java1
-rw-r--r--base/src/bjc/dicelang/expr/TokenType.java4
-rw-r--r--base/src/bjc/dicelang/expr/Tokens.java29
-rw-r--r--base/src/bjc/dicelang/scl/StreamControlEngine.java1
-rw-r--r--dice/src/main/java/bjc/dicelang/App.java13
18 files changed, 278 insertions, 202 deletions
diff --git a/base/src/bjc/dicelang/CLIArgsParser.java b/base/src/bjc/dicelang/CLIArgsParser.java
index 7e417ea..1cec55d 100644
--- a/base/src/bjc/dicelang/CLIArgsParser.java
+++ b/base/src/bjc/dicelang/CLIArgsParser.java
@@ -1,11 +1,5 @@
package bjc.dicelang;
-import static bjc.dicelang.Errors.ErrorKey.EK_CLI_INVDFNTYPE;
-import static bjc.dicelang.Errors.ErrorKey.EK_CLI_MISARG;
-import static bjc.dicelang.Errors.ErrorKey.EK_CLI_UNARG;
-import static bjc.dicelang.Errors.ErrorKey.EK_MISC_IOEX;
-import static bjc.dicelang.Errors.ErrorKey.EK_MISC_NOFILE;
-
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
@@ -14,6 +8,8 @@ import java.util.Scanner;
import bjc.dicelang.util.ResourceLoader;
+import static bjc.dicelang.Errors.ErrorKey.*;
+
/**
* Parse CLI arguments.
*
@@ -48,8 +44,10 @@ public class CLIArgsParser {
final String arg = args[i];
/*
- * @TODO 10/08/17 Ben Culkin :CLIArgRefactor Use whatever library gets added to
- * BJC-Utils for this, and extend these to do more things.
+ * @TODO 10/08/17 Ben Culkin :CLIArgRefactor
+ *
+ * Use whatever library gets added to BJC-Utils for this, and extend these to do
+ * more things.
*/
switch (arg) {
case "-d":
diff --git a/base/src/bjc/dicelang/CompilerTweaker.java b/base/src/bjc/dicelang/CompilerTweaker.java
index 219705f..95926f0 100644
--- a/base/src/bjc/dicelang/CompilerTweaker.java
+++ b/base/src/bjc/dicelang/CompilerTweaker.java
@@ -4,6 +4,7 @@ import bjc.utils.parserutils.splitter.ConfigurableTokenSplitter;
/*
* @TODO 10/09/17 Ben Culkin :CompilerTweaking
+ *
* Expand this to allow tweaking more things about the compiler.
*/
/**
diff --git a/base/src/bjc/dicelang/Define.java b/base/src/bjc/dicelang/Define.java
index 7b7bda6..1051957 100644
--- a/base/src/bjc/dicelang/Define.java
+++ b/base/src/bjc/dicelang/Define.java
@@ -1,9 +1,5 @@
package bjc.dicelang;
-import static bjc.dicelang.Errors.ErrorKey.EK_DFN_PREDSYN;
-import static bjc.dicelang.Errors.ErrorKey.EK_DFN_RECUR;
-import static bjc.dicelang.Errors.ErrorKey.EK_DFN_SRCSYN;
-
import java.util.Iterator;
import java.util.function.UnaryOperator;
import java.util.regex.Matcher;
@@ -12,8 +8,11 @@ import java.util.regex.PatternSyntaxException;
import bjc.utils.data.CircularIterator;
+import static bjc.dicelang.Errors.ErrorKey.*;
+
/*
* @TODO 10/09/17 Ben Culkin :DefineRefactor
+ *
* Consider replacing this with the defines package from BJC-Utils.
*/
/**
diff --git a/base/src/bjc/dicelang/DiceLangConsole.java b/base/src/bjc/dicelang/DiceLangConsole.java
index 71310da..d2c214d 100644
--- a/base/src/bjc/dicelang/DiceLangConsole.java
+++ b/base/src/bjc/dicelang/DiceLangConsole.java
@@ -1,8 +1,5 @@
package bjc.dicelang;
-import static bjc.dicelang.Errors.ErrorKey.EK_CONS_INVDEFINE;
-import static bjc.dicelang.Errors.ErrorKey.EK_CONS_INVPRAG;
-
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
@@ -12,6 +9,8 @@ import java.util.regex.Pattern;
import jline.ConsoleReader;
import jline.Terminal;
+import static bjc.dicelang.Errors.ErrorKey.*;
+
/**
* CLI interface to DiceLang
*
diff --git a/base/src/bjc/dicelang/DiceLangEngine.java b/base/src/bjc/dicelang/DiceLangEngine.java
index faa8a90..128e2a6 100644
--- a/base/src/bjc/dicelang/DiceLangEngine.java
+++ b/base/src/bjc/dicelang/DiceLangEngine.java
@@ -1,14 +1,5 @@
package bjc.dicelang;
-import static bjc.dicelang.Errors.ErrorKey.EK_ENG_NOCLOSING;
-import static bjc.dicelang.Errors.ErrorKey.EK_ENG_NOOPENING;
-import static bjc.dicelang.Token.Type.CBRACE;
-import static bjc.dicelang.Token.Type.CBRACKET;
-import static bjc.dicelang.Token.Type.CPAREN;
-import static bjc.dicelang.Token.Type.OBRACE;
-import static bjc.dicelang.Token.Type.OBRACKET;
-import static bjc.dicelang.Token.Type.OPAREN;
-
import java.util.Deque;
import java.util.Iterator;
import java.util.LinkedList;
@@ -28,6 +19,9 @@ import bjc.utils.funcutils.ListUtils;
import bjc.utils.parserutils.TokenUtils;
import bjc.utils.parserutils.splitter.ConfigurableTokenSplitter;
+import static bjc.dicelang.Errors.ErrorKey.*;
+import static bjc.dicelang.Token.Type.*;
+
/**
* Implements the orchestration necessary for processing DiceLang commands.
*
@@ -224,8 +218,9 @@ public class DiceLangEngine {
public boolean runCommand(final String command) {
/* Preprocess the command into tokens */
/*
- * @NOTE Instead of strings, this should maybe use a RawToken class or
- * something.
+ * @NOTE
+ *
+ * Instead of strings, this should maybe use a RawToken class or something.
*/
final IList<String> preprocessedTokens = preprocessCommand(command);
@@ -664,8 +659,10 @@ public class DiceLangEngine {
/* Add a string literal to the string literal table. */
/*
- * @NOTE The string literal table should be abstracted into some kind of
- * auto-numbered map thing.
+ * @NOTE
+ *
+ * The string literal table should be abstracted into some kind of auto-numbered
+ * map thing.
*/
void addStringLiteral(final int key, final String val) {
stringLits.put(key, val);
diff --git a/base/src/bjc/dicelang/DiceToken.java b/base/src/bjc/dicelang/DiceToken.java
new file mode 100644
index 0000000..fcb8a0c
--- /dev/null
+++ b/base/src/bjc/dicelang/DiceToken.java
@@ -0,0 +1,43 @@
+package bjc.dicelang;
+
+import bjc.dicelang.dice.DiceExpression;
+
+public class DiceToken extends Token {
+ public DiceExpression diceValue;
+
+ public DiceToken(DiceExpression val) {
+ super(Type.DICE_LIT);
+
+ diceValue = val;
+ }
+
+ @Override
+ public String toString() {
+ return super.toString() + "(" + diceValue + ")";
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((diceValue == null) ? 0 : diceValue.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (!super.equals(obj))
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ DiceToken other = (DiceToken) obj;
+ if (diceValue == null) {
+ if (other.diceValue != null)
+ return false;
+ } else if (!diceValue.equals(other.diceValue))
+ return false;
+ return true;
+ }
+}
diff --git a/base/src/bjc/dicelang/Errors.java b/base/src/bjc/dicelang/Errors.java
index b404415..2263532 100644
--- a/base/src/bjc/dicelang/Errors.java
+++ b/base/src/bjc/dicelang/Errors.java
@@ -3,9 +3,10 @@ package bjc.dicelang;
/**
* Repository for error messages.
*
- * @TODO 10/08/17 Ben Culkin :ErrorRefactor This way of handling error messages
- * is not easy to deal with. Something else needs to be done, but I'm not
- * sure what at the moment.
+ * @TODO 10/08/17 Ben Culkin :ErrorRefactor
+ *
+ * This way of handling error messages is not easy to deal with. Something
+ * else needs to be done, but I'm not sure what at the moment.
*
* @author EVE
*
diff --git a/base/src/bjc/dicelang/Token.java b/base/src/bjc/dicelang/Token.java
index f1bca6c..50e5b31 100644
--- a/base/src/bjc/dicelang/Token.java
+++ b/base/src/bjc/dicelang/Token.java
@@ -1,10 +1,11 @@
package bjc.dicelang;
-import bjc.dicelang.dice.DieExpression;
+import bjc.dicelang.dice.DiceExpression;
import bjc.utils.funcdata.IList;
/*
* @TODO 10/09/17 Ben Culkin :TokenReorg
+ *
* Split the class into subclasses based off of type.
*/
/**
@@ -18,35 +19,81 @@ public class Token {
* Possible token types
*/
public static enum Type {
- // Natural tokens
- // These are produced from lexemes
- ADD, SUBTRACT, MULTIPLY, DIVIDE, IDIVIDE, INT_LIT, FLOAT_LIT, STRING_LIT, VREF, DICE_LIT, DICESCALAR, DICEFUDGE, DICEGROUP, DICECONCAT, DICELIST, LET, BIND, COERCE, STRCAT, STRREP, OPAREN, CPAREN, OBRACKET, CBRACKET, OBRACE, CBRACE,
-
- // Synthetic tokens
- // These are produced when needed
+ /*
+ * Natural tokens
+ *
+ * These are produced from lexemes.
+ */
+ /*
+ * Math tokens.
+ */
+ ADD, SUBTRACT, MULTIPLY, DIVIDE, IDIVIDE,
+
+ /*
+ * Literal tokens.
+ */
+ INT_LIT, FLOAT_LIT, STRING_LIT, VREF, DICE_LIT,
+
+ /*
+ * Dice operators.
+ */
+ DICESCALAR, DICEFUDGE, DICEGROUP, DICECONCAT, DICELIST,
+
+ /*
+ * Expression operators.
+ */
+ LET, BIND, COERCE,
+
+ /*
+ * String operators.
+ */
+ STRCAT, STRREP,
+
+ /*
+ * Grouping operators.
+ */
+ OPAREN, CPAREN, OBRACKET, CBRACKET, OBRACE, CBRACE,
+
+ /*
+ * Synthetic tokens
+ *
+ * These are produced when needed.
+ */
NIL, GROUPSEP, TOKGROUP, TAGOP, TAGOPR
}
public final Type type;
- // This is used for the following token types
- // INT_LIT (int value)
- // STRING_LIT (index into string table)
- // VREF (index into sym table)
- // O* and C* (sym-count of current token)
+ /*
+ * This is used for the following token types
+ *
+ * - INT_LIT (int value)
+ *
+ * - STRING_LIT (index into string table)
+ *
+ * - VREF (index into sym table)
+ *
+ * - O* and C* (sym-count of current token)
+ *
+ */
public long intValue;
- // This is used for the following token types
- // FLOAT_LIT (float value)
+ /*
+ * This is used for the following token types
+ *
+ * - FLOAT_LIT (float value)
+ *
+ */
public double floatValue;
- // This is used for the following token types
- // DICE_LIT (dice value)
- public DieExpression diceValue;
-
- // This is used for the following token types
- // TOKGROUP (the tokens in the group)
- // TAG* (the tagged construct)
+ /*
+ * This is used for the following token types
+ *
+ * - TOKGROUP (the tokens in the group)
+ *
+ * - TAG* (the tagged construct)
+ *
+ */
public IList<Token> tokenValues;
public Token(final Type typ) {
@@ -65,12 +112,6 @@ public class Token {
floatValue = val;
}
- public Token(final Type typ, final DieExpression val) {
- this(typ);
-
- diceValue = val;
- }
-
public Token(final Type typ, final IList<Token> tkVals) {
this(typ);
@@ -94,9 +135,6 @@ public class Token {
case FLOAT_LIT:
return type.toString() + "(" + floatValue + ")";
- case DICE_LIT:
- return type.toString() + "(" + diceValue + ")";
-
case TAGOP:
case TAGOPR:
case TOKGROUP:
@@ -123,7 +161,6 @@ public class Token {
case OBRACE:
case OBRACKET:
return intValue == otk.intValue;
-
default:
return true;
}
diff --git a/base/src/bjc/dicelang/Tokenizer.java b/base/src/bjc/dicelang/Tokenizer.java
index 0de8c53..b59c4be 100644
--- a/base/src/bjc/dicelang/Tokenizer.java
+++ b/base/src/bjc/dicelang/Tokenizer.java
@@ -1,10 +1,5 @@
package bjc.dicelang;
-import static bjc.dicelang.Errors.ErrorKey.EK_TOK_INVBASE;
-import static bjc.dicelang.Errors.ErrorKey.EK_TOK_INVFLEX;
-import static bjc.dicelang.Errors.ErrorKey.EK_TOK_UNGROUP;
-import static bjc.dicelang.Token.Type.*;
-
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -14,20 +9,21 @@ import bjc.utils.funcdata.IMap;
import bjc.utils.funcutils.StringUtils;
import bjc.utils.parserutils.TokenUtils;
+import static bjc.dicelang.Errors.ErrorKey.*;
+import static bjc.dicelang.Token.Type.*;
+
/**
* Converts strings into tokens.
*/
public class Tokenizer {
/* Literal tokens for tokenization */
- private final IMap<String, Token.Type> litTokens;
+ private static final IMap<String, Token.Type> litTokens;
private final DiceLangEngine eng;
private int nextSym = 0;
- public Tokenizer(final DiceLangEngine engine) {
- eng = engine;
-
+ static {
litTokens = new FunctionalMap<>();
litTokens.put("+", ADD);
@@ -48,6 +44,10 @@ public class Tokenizer {
litTokens.put("crc", COERCE);
}
+ public Tokenizer(final DiceLangEngine engine) {
+ eng = engine;
+ }
+
public Token lexToken(final String token, final IMap<String, String> stringLts) {
if (token.equals("")) {
return null;
@@ -149,7 +149,7 @@ public class Tokenizer {
} else if (TokenUtils.isDouble(token)) {
tk = new Token(FLOAT_LIT, Double.parseDouble(token));
} else if (DiceBox.isValidExpression(token)) {
- tk = new Token(DICE_LIT, DiceBox.parseExpression(token));
+ tk = new DiceToken(DiceBox.parseExpression(token));
} else {
final Matcher stringLit = stringLitMatcher.matcher(token);
diff --git a/base/src/bjc/dicelang/expr/ExprREPL.java b/base/src/bjc/dicelang/expr/ExprREPL.java
new file mode 100644
index 0000000..84149a0
--- /dev/null
+++ b/base/src/bjc/dicelang/expr/ExprREPL.java
@@ -0,0 +1,96 @@
+package bjc.dicelang.expr;
+
+import java.util.Scanner;
+
+import bjc.utils.data.ITree;
+import bjc.utils.funcdata.IList;
+import bjc.utils.parserutils.TreeConstructor;
+
+public class ExprREPL {
+
+ /*
+ * @TODO 10/08/17 Ben Culkin :MainSeperation
+ *
+ * This main method should be moved to its own class.
+ */
+ /**
+ * Main method.
+ *
+ * @param args
+ * Unused CLI args.
+ */
+ public static void main(final String[] args) {
+ /* Create our objects. */
+ final Tokens toks = new Tokens();
+ final Lexer lex = new Lexer();
+
+ /* Prepare our input source. */
+ final Scanner scan = new Scanner(System.in);
+
+ /* Read initial command. */
+ System.out.print("Enter a math expression (blank line to quit): ");
+ String ln = scan.nextLine().trim();
+
+ /* Enter REPL loop. */
+ while (!ln.equals("")) {
+ /* Print raw command. */
+ System.out.println("Raw command: " + ln);
+ System.out.println();
+
+ /* Lex command to infix tokens. */
+ final Token[] infixTokens = lex.lexString(ln, toks);
+ System.out.println("Lexed tokens: ");
+ for (final Token tok : infixTokens) {
+ System.out.println("\t" + tok);
+ }
+
+ /* Print out infix expression. */
+ System.out.print("Lexed expression: ");
+ for (final Token tok : infixTokens) {
+ System.out.print(tok.toExpr() + " ");
+ }
+
+ /* Space stages. */
+ System.out.println();
+ System.out.println();
+
+ /* Shunt infix tokens to postfix tokens. */
+ final IList<Token> postfixTokens = Shunter.shuntTokens(infixTokens);
+ System.out.println("Lexed tokens: ");
+ for (final Token tok : postfixTokens) {
+ System.out.println("\t" + tok);
+ }
+
+ /* Print out postfix tokens. */
+ System.out.print("Shunted expression: ");
+ for (final Token tok : postfixTokens) {
+ System.out.print(tok.toExpr() + " ");
+ }
+
+ /* Space stages. */
+ System.out.println();
+ System.out.println();
+
+ /* Construct a tree from the list of postfixed tokens. */
+ final ITree<Token> ast = TreeConstructor.constructTree(postfixTokens, tok -> tok.typ.isOperator);
+
+ /* Print the tree, then the canonical expression for it. */
+ System.out.println("Parsed tree");
+ System.out.println(ast.toString());
+ System.out.println("\nCanonical expr: " + Parser.toCanonicalExpr(ast));
+
+ /* Space stages. */
+ System.out.println();
+ System.out.println();
+
+ /* Prompt for a new expression. */
+ System.out.print("Enter a math expression (blank line to quit): ");
+ /* Read it. */
+ ln = scan.nextLine().trim();
+ }
+
+ /* Cleanup after ourselves. */
+ scan.close();
+ }
+
+}
diff --git a/base/src/bjc/dicelang/expr/Lexer.java b/base/src/bjc/dicelang/expr/Lexer.java
index 0b95cb8..7530ba5 100644
--- a/base/src/bjc/dicelang/expr/Lexer.java
+++ b/base/src/bjc/dicelang/expr/Lexer.java
@@ -26,7 +26,9 @@ public class Lexer {
split = new ConfigurableTokenSplitter(true);
split.addSimpleDelimiters("(", ")");
- split.addSimpleDelimiters("+", "-", "*", "/");
+
+ split.addSimpleDelimiters("+", "-");
+ split.addSimpleDelimiters("*", "/");
}
/**
@@ -50,8 +52,11 @@ public class Lexer {
for (final String spacedToken : spacedTokens) {
/* Split on operators. */
final IList<String> splitTokens = split.split(spacedToken);
+
/* Convert strings to tokens. */
- final IList<Token> rawTokens = splitTokens.map(tok -> tks.lexToken(tok, spacedToken));
+ final IList<Token> rawTokens = splitTokens.map(tok -> {
+ return tks.lexToken(tok, spacedToken);
+ });
/* Add tokens to results. */
rawTokens.forEach(tokens::add);
diff --git a/base/src/bjc/dicelang/expr/Parser.java b/base/src/bjc/dicelang/expr/Parser.java
index 72f7bbe..1156334 100644
--- a/base/src/bjc/dicelang/expr/Parser.java
+++ b/base/src/bjc/dicelang/expr/Parser.java
@@ -1,11 +1,6 @@
package bjc.dicelang.expr;
-import java.util.Arrays;
-import java.util.Scanner;
-
import bjc.utils.data.ITree;
-import bjc.utils.funcdata.FunctionalList;
-import bjc.utils.parserutils.TreeConstructor;
/**
* Parser for simple math expressions.
@@ -14,97 +9,10 @@ import bjc.utils.parserutils.TreeConstructor;
*/
public class Parser {
/*
- * @TODO 10/08/17 Ben Culkin :MainSeperation This main method should be moved to
- * its own class.
- */
- /**
- * Main method.
- *
- * @param args
- * Unused CLI args.
- */
- public static void main(final String[] args) {
- /* Create our objects. */
- final Tokens toks = new Tokens();
- final Lexer lex = new Lexer();
-
- /* Prepare our input source. */
- final Scanner scan = new Scanner(System.in);
-
- /* Read initial command. */
- System.out.print("Enter a math expression (blank line to quit): ");
- String ln = scan.nextLine().trim();
-
- /* Enter REPL loop. */
- while (!ln.equals("")) {
- /* Print raw command. */
- System.out.println("Raw command: " + ln);
- System.out.println();
-
- /* Lex command to infix tokens. */
- final Token[] infixTokens = lex.lexString(ln, toks);
- System.out.println("Lexed tokens: ");
- for (final Token tok : infixTokens) {
- System.out.println("\t" + tok);
- }
-
- /* Print out infix expression. */
- System.out.print("Lexed expression: ");
- for (final Token tok : infixTokens) {
- System.out.print(tok.toExpr() + " ");
- }
-
- /* Space stages. */
- System.out.println();
- System.out.println();
-
- /* Shunt infix tokens to postfix tokens. */
- final Token[] postfixTokens = Shunter.shuntTokens(infixTokens);
- System.out.println("Lexed tokens: ");
- for (final Token tok : postfixTokens) {
- System.out.println("\t" + tok);
- }
-
- /* Print out postfix tokens. */
- System.out.print("Shunted expression: ");
- for (final Token tok : postfixTokens) {
- System.out.print(tok.toExpr() + " ");
- }
-
- /* Space stages. */
- System.out.println();
- System.out.println();
-
- /* Construct a list from the array of tokens. */
- final FunctionalList<Token> tokList = new FunctionalList<>(Arrays.asList(postfixTokens));
-
- /* Construct a tree from the list of postfixed tokens. */
- final ITree<Token> ast = TreeConstructor.constructTree(tokList, tok -> tok.typ.isOperator);
-
- /* Print the tree, then the canonical expression for it. */
- System.out.println("Parsed tree");
- System.out.println(ast.toString());
- System.out.println("\nCanonical expr: " + toCanonicalExpr(ast));
-
- /* Space stages. */
- System.out.println();
- System.out.println();
-
- /* Prompt for a new expression. */
- System.out.print("Enter a math expression (blank line to quit): ");
- /* Read it. */
- ln = scan.nextLine().trim();
- }
-
- /* Cleanup after ourselves. */
- scan.close();
- }
-
- /*
* Convert an expression to one that uses the smallest necessary amount of
* parens.
*/
- private static String toCanonicalExpr(final ITree<Token> ast) {
+ public static String toCanonicalExpr(final ITree<Token> ast) {
final Token data = ast.getHead();
if (ast.getChildrenCount() == 0) {
diff --git a/base/src/bjc/dicelang/expr/Shunter.java b/base/src/bjc/dicelang/expr/Shunter.java
index e4add67..a1b24cb 100644
--- a/base/src/bjc/dicelang/expr/Shunter.java
+++ b/base/src/bjc/dicelang/expr/Shunter.java
@@ -1,9 +1,10 @@
package bjc.dicelang.expr;
-import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
-import java.util.List;
+
+import bjc.utils.funcdata.FunctionalList;
+import bjc.utils.funcdata.IList;
/**
* Converts a infix series of tokens into a prefix series of tokens.
@@ -11,9 +12,6 @@ import java.util.List;
* @author Ben Culkin
*/
public class Shunter {
- /*
- * @NOTE Why does this method return an array, and not the list of tokens?
- */
/**
* Convert a infix series of tokens to a postfix series of tokens.
*
@@ -22,9 +20,9 @@ public class Shunter {
*
* @return The tokens in postfix order.
*/
- public static Token[] shuntTokens(final Token[] infixTokens) {
+ public static IList<Token> shuntTokens(final Token[] infixTokens) {
/* The returned tokens. */
- final List<Token> postfixTokens = new ArrayList<>(infixTokens.length);
+ final IList<Token> postfixTokens = new FunctionalList<>();
/* The current stack of operators. */
final Deque<Token> opStack = new LinkedList<>();
@@ -98,6 +96,6 @@ public class Shunter {
postfixTokens.add(opStack.pop());
}
- return postfixTokens.toArray(new Token[0]);
+ return postfixTokens;
}
}
diff --git a/base/src/bjc/dicelang/expr/Token.java b/base/src/bjc/dicelang/expr/Token.java
index be980ff..3c6b1da 100644
--- a/base/src/bjc/dicelang/expr/Token.java
+++ b/base/src/bjc/dicelang/expr/Token.java
@@ -2,6 +2,7 @@ package bjc.dicelang.expr;
/*
* @TODO 10/08/17 :TokenReorg
+ *
* I am not a fan of this 'having a bunch of subclasses' in one thing I
* seem to have been doing around this project. This should be multiple
* subclasses, one for each value for TokenType.
diff --git a/base/src/bjc/dicelang/expr/TokenType.java b/base/src/bjc/dicelang/expr/TokenType.java
index 9462139..71dd597 100644
--- a/base/src/bjc/dicelang/expr/TokenType.java
+++ b/base/src/bjc/dicelang/expr/TokenType.java
@@ -7,7 +7,9 @@ package bjc.dicelang.expr;
*/
public enum TokenType {
/*
- * @NOTE Do we want to switch to auto-numbering the tokens? They were manually
+ * @NOTE
+ *
+ * Do we want to switch to auto-numbering the tokens? They were manually
* numbered because this was an assignment for PoPL and that was what Dr. Naz
* wanted.
*/
diff --git a/base/src/bjc/dicelang/expr/Tokens.java b/base/src/bjc/dicelang/expr/Tokens.java
index 4cf5378..92416d1 100644
--- a/base/src/bjc/dicelang/expr/Tokens.java
+++ b/base/src/bjc/dicelang/expr/Tokens.java
@@ -23,26 +23,16 @@ public class Tokens {
private int nextSym;
/* Mapping from literal tokens to token types. */
- private final Map<String, TokenType> litTokens;
-
- /** Create a new set of tokens. */
- public Tokens() {
- /* Create tables. */
- symTab = new HashMap<>();
- revSymTab = new HashMap<>();
-
- /* Init public view. */
- symbolTable = Collections.unmodifiableMap(symTab);
-
- /* Set sym ID. */
- nextSym = 0;
+ private static final Map<String, TokenType> litTokens;
+ static {
/*
* Setup literal mappings.
*
* @NOTE Should this be a static member?
*/
litTokens = new HashMap<>();
+
litTokens.put("+", TokenType.ADD);
litTokens.put("-", TokenType.SUBTRACT);
litTokens.put("*", TokenType.MULTIPLY);
@@ -51,6 +41,19 @@ public class Tokens {
litTokens.put(")", TokenType.CPAREN);
}
+ /** Create a new set of tokens. */
+ public Tokens() {
+ /* Create tables. */
+ symTab = new HashMap<>();
+ revSymTab = new HashMap<>();
+
+ /* Init public view. */
+ symbolTable = Collections.unmodifiableMap(symTab);
+
+ /* Set sym ID. */
+ nextSym = 0;
+ }
+
/**
* Convert the string representation of a token into a token.
*
diff --git a/base/src/bjc/dicelang/scl/StreamControlEngine.java b/base/src/bjc/dicelang/scl/StreamControlEngine.java
index 077d9f3..e05c983 100644
--- a/base/src/bjc/dicelang/scl/StreamControlEngine.java
+++ b/base/src/bjc/dicelang/scl/StreamControlEngine.java
@@ -16,6 +16,7 @@ import static bjc.dicelang.scl.WordSCLToken.Word.*;
/*
* @TODO 10/08/17 Ben Culkin :SCLReorg
+ *
* This is a large enough class that it should maybe be split into
* subclasses.
*/
diff --git a/dice/src/main/java/bjc/dicelang/App.java b/dice/src/main/java/bjc/dicelang/App.java
deleted file mode 100644
index 694bd75..0000000
--- a/dice/src/main/java/bjc/dicelang/App.java
+++ /dev/null
@@ -1,13 +0,0 @@
-package bjc.dicelang;
-
-/**
- * Hello world!
- *
- */
-public class App
-{
- public static void main( String[] args )
- {
- System.out.println( "Hello World!" );
- }
-}