summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/v2/Parser.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2017-02-19 08:30:45 -0500
committerbculkin2442 <bjculkin@mix.wvu.edu>2017-02-19 08:30:45 -0500
commite03b3f477bcc160b72af4ab09cd8d12081017d2c (patch)
treef4e1779c5cf3aa893bd7bdc6c826c64f15648ed9 /dice-lang/src/bjc/dicelang/v2/Parser.java
parent03e40ec669ee51c697de64178af6bb662161a8ae (diff)
Lots of things, but mostly evaluation
Diffstat (limited to 'dice-lang/src/bjc/dicelang/v2/Parser.java')
-rw-r--r--dice-lang/src/bjc/dicelang/v2/Parser.java147
1 files changed, 147 insertions, 0 deletions
diff --git a/dice-lang/src/bjc/dicelang/v2/Parser.java b/dice-lang/src/bjc/dicelang/v2/Parser.java
new file mode 100644
index 0000000..0e778b4
--- /dev/null
+++ b/dice-lang/src/bjc/dicelang/v2/Parser.java
@@ -0,0 +1,147 @@
+package bjc.dicelang.v2;
+
+import java.util.Deque;
+import java.util.LinkedList;
+
+import bjc.utils.data.ITree;
+import bjc.utils.data.Tree;
+import bjc.utils.funcdata.IList;
+
+import static bjc.dicelang.v2.Node.Type.*;
+import static bjc.dicelang.v2.Token.Type.*;
+
+public class Parser {
+ public Parser() {
+
+ }
+
+ public boolean parseTokens(IList<Token> tokens,
+ IList<ITree<Node>> results) {
+ Deque<ITree<Node>> working = new LinkedList<>();
+
+ for(Token tk : tokens) {
+ switch(tk.type) {
+ case OBRACKET:
+ case OBRACE:
+ working.push(new Tree<>(new Node(OGROUP, tk)));
+ break;
+ case CBRACKET:
+ case CBRACE:
+ if(working.size() == 0) {
+ System.out.println("\tERROR: Group closing with no possible group opener");
+ return false;
+ }
+
+ ITree<Node> groupNode = null;
+ switch(tk.type) {
+ case CBRACE:
+ groupNode = new Tree<>(new Node(GROUP, Node.GroupType.CODE));
+ break;
+ case CBRACKET:
+ groupNode = new Tree<>(new Node(GROUP, Node.GroupType.ARRAY));
+ break;
+ default:
+ break;
+ }
+ Token matching = null;
+
+ if(tk.type == CBRACKET) {
+ matching = new Token(Token.Type.OBRACKET, tk.intValue);
+ } else if(tk.type == CBRACE) {
+ matching = new Token(Token.Type.OBRACE, tk.intValue);
+ }
+
+ ITree<Node> matchNode = new Tree<>(new Node(OGROUP, matching));
+
+ if(!working.contains(matchNode)) {
+ System.out.println("\tERROR: Found group closing without group opener: (closing was " + tk + ", matcher was "
+ + matchNode + ")");
+
+ System.out.println("\tCurrent forest is: ");
+
+ int treeNo = 1;
+ for(ITree<Node> ast : working) {
+ System.out.println("Tree " + treeNo++ + ": " + ast.toString());
+ }
+ return false;
+ } else {
+ Deque<ITree<Node>> childs = new LinkedList<>();
+
+ while(!working.peek().equals(matchNode)) {
+ childs.push(working.pop());
+ }
+
+ // Discard opener
+ working.pop();
+
+ for(ITree<Node> child : childs) {
+ groupNode.addChild(child);
+ }
+
+ working.push(groupNode);
+ }
+ break;
+ case LET:
+ case BIND:
+ if(working.size() < 2) {
+ System.out.println("\tERROR: Let and bind require at least two operands");
+ } else {
+ ITree<Node> right = working.pop();
+ ITree<Node> left = working.pop();
+
+ ITree<Node> opNode = new Tree<>(new Node(BINOP, tk.type));
+
+ working.push(opNode);
+ }
+ break;
+ case ADD:
+ case SUBTRACT:
+ case MULTIPLY:
+ case DIVIDE:
+ case IDIVIDE:
+ case DICEGROUP:
+ case DICECONCAT:
+ case DICELIST:
+ if(working.size() == 0) {
+ System.out.println("\tERROR: Binary operator " + tk.type + " requires more operands than provided.");
+ return false;
+ } else if(working.size() == 1) {
+ ITree<Node> operand = working.pop();
+
+ ITree<Node> opNode = new Tree<>(new Node(UNARYOP, tk.type));
+
+ opNode.addChild(operand);
+
+ working.push(opNode);
+ } else {
+ ITree<Node> right = working.pop();
+ ITree<Node> left = working.pop();
+
+ ITree<Node> opNode = new Tree<>(new Node(BINOP, tk.type));
+
+ opNode.addChild(left);
+ opNode.addChild(right);
+
+ working.push(opNode);
+ }
+ break;
+ case INT_LIT:
+ case FLOAT_LIT:
+ case STRING_LIT:
+ case VREF:
+ case DICE_LIT:
+ working.push(new Tree<>(new Node(TOKREF, tk)));
+ break;
+ default:
+ System.out.println("\tERROR: Unrecognized token type in parsing: " + tk.type);
+ return false;
+ }
+ }
+
+ for(ITree<Node> ast : working) {
+ results.add(ast);
+ }
+
+ return true;
+ }
+}