summaryrefslogtreecommitdiff
path: root/base/src/bjc/dicelang/expr
diff options
context:
space:
mode:
Diffstat (limited to 'base/src/bjc/dicelang/expr')
-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
7 files changed, 130 insertions, 117 deletions
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.
*