From 57f9a3bfdad20bead5b35ee540e8790e80a6b9a4 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Tue, 11 Apr 2017 21:48:50 -0400 Subject: Cleanup --- dice-lang/src/bjc/dicelang/Parser.java | 124 +++++++++++++++++---------------- 1 file changed, 64 insertions(+), 60 deletions(-) (limited to 'dice-lang/src/bjc/dicelang/Parser.java') diff --git a/dice-lang/src/bjc/dicelang/Parser.java b/dice-lang/src/bjc/dicelang/Parser.java index 7907137..1029943 100644 --- a/dice-lang/src/bjc/dicelang/Parser.java +++ b/dice-lang/src/bjc/dicelang/Parser.java @@ -1,16 +1,24 @@ package bjc.dicelang; -import bjc.utils.data.ITree; -import bjc.utils.data.Tree; -import bjc.utils.funcdata.IList; +import static bjc.dicelang.Errors.ErrorKey.EK_PARSE_BINARY; +import static bjc.dicelang.Errors.ErrorKey.EK_PARSE_INVTOKEN; +import static bjc.dicelang.Errors.ErrorKey.EK_PARSE_NOCLOSE; +import static bjc.dicelang.Errors.ErrorKey.EK_PARSE_UNCLOSE; +import static bjc.dicelang.Errors.ErrorKey.EK_PARSE_UNOPERAND; +import static bjc.dicelang.Node.Type.BINOP; +import static bjc.dicelang.Node.Type.GROUP; +import static bjc.dicelang.Node.Type.OGROUP; +import static bjc.dicelang.Node.Type.TOKREF; +import static bjc.dicelang.Node.Type.UNARYOP; +import static bjc.dicelang.Token.Type.CBRACE; +import static bjc.dicelang.Token.Type.CBRACKET; import java.util.Deque; import java.util.LinkedList; -import static bjc.dicelang.Errors.ErrorKey.*; -import static bjc.dicelang.Node.Type.*; -import static bjc.dicelang.Token.Type.CBRACE; -import static bjc.dicelang.Token.Type.CBRACKET; +import bjc.utils.data.ITree; +import bjc.utils.data.Tree; +import bjc.utils.funcdata.IList; /** * Parse a series of tree into tokens. @@ -37,19 +45,19 @@ public class Parser { * * @return Whether or not the parse was successful. */ - public boolean parseTokens(IList tokens, IList> results) { - Deque> working = new LinkedList<>(); + public static boolean parseTokens(final IList tokens, final IList> results) { + final Deque> working = new LinkedList<>(); - for(Token tk : tokens) { - switch(tk.type) { + for (final Token tk : tokens) { + switch (tk.type) { case OBRACKET: case OBRACE: working.push(new Tree<>(new Node(OGROUP, tk))); break; case CBRACKET: case CBRACE: - boolean sc = parseClosingGrouper(working, tk); - if(!sc) return false; + final boolean sc = parseClosingGrouper(working, tk); + if (!sc) return false; break; case MULTIPLY: case DIVIDE: @@ -61,54 +69,38 @@ public class Parser { case STRREP: case LET: case BIND: - if(working.size() < 2) { + if (working.size() < 2) { Errors.inst.printError(EK_PARSE_BINARY); return false; - } else { - ITree right = working.pop(); - ITree left = working.pop(); - - ITree opNode = new Tree<>(new Node(BINOP, tk.type)); - - opNode.addChild(left); - opNode.addChild(right); - - working.push(opNode); } + + handleBinaryNode(working, tk); break; case ADD: case SUBTRACT: - if(working.size() == 0) { + if (working.size() == 0) { Errors.inst.printError(EK_PARSE_UNOPERAND, tk.toString()); return false; - } else if(working.size() == 1) { - ITree operand = working.pop(); + } else if (working.size() == 1) { + final ITree operand = working.pop(); - ITree opNode = new Tree<>(new Node(UNARYOP, tk.type)); + final ITree opNode = new Tree<>(new Node(UNARYOP, tk.type)); opNode.addChild(operand); working.push(opNode); } else { - ITree right = working.pop(); - ITree left = working.pop(); - - ITree opNode = new Tree<>(new Node(BINOP, tk.type)); - - opNode.addChild(left); - opNode.addChild(right); - - working.push(opNode); + handleBinaryNode(working, tk); } break; case COERCE: case DICESCALAR: case DICEFUDGE: - if(working.size() == 0) { + if (working.size() == 0) { Errors.inst.printError(EK_PARSE_UNOPERAND, tk.toString()); } else { - ITree operand = working.pop(); - ITree opNode = new Tree<>(new Node(UNARYOP, tk.type)); + final ITree operand = working.pop(); + final ITree opNode = new Tree<>(new Node(UNARYOP, tk.type)); opNode.addChild(operand); @@ -128,21 +120,33 @@ public class Parser { } } - for(ITree ast : working) { + for (final ITree ast : working) { results.add(ast); } return true; } - private boolean parseClosingGrouper(Deque> working, Token tk) { - if(working.size() == 0) { + private static void handleBinaryNode(final Deque> working, final Token tk) { + final ITree right = working.pop(); + final ITree left = working.pop(); + + final ITree opNode = new Tree<>(new Node(BINOP, tk.type)); + + opNode.addChild(left); + opNode.addChild(right); + + working.push(opNode); + } + + private static boolean parseClosingGrouper(final Deque> working, final Token tk) { + if (working.size() == 0) { Errors.inst.printError(EK_PARSE_NOCLOSE); return false; } ITree groupNode = null; - switch(tk.type) { + switch (tk.type) { case CBRACE: groupNode = new Tree<>(new Node(GROUP, Node.GroupType.CODE)); break; @@ -155,41 +159,41 @@ public class Parser { } Token matching = null; - if(tk.type == CBRACKET) { + if (tk.type == CBRACKET) { matching = new Token(Token.Type.OBRACKET, tk.intValue); - } else if(tk.type == CBRACE) { + } else if (tk.type == CBRACE) { matching = new Token(Token.Type.OBRACE, tk.intValue); } - ITree matchNode = new Tree<>(new Node(OGROUP, matching)); - if(!working.contains(matchNode)) { + final ITree matchNode = new Tree<>(new Node(OGROUP, matching)); + if (!working.contains(matchNode)) { Errors.inst.printError(EK_PARSE_UNCLOSE, tk.toString(), matchNode.toString()); System.out.println("\tCurrent forest is: "); int treeNo = 1; - for(ITree ast : working) { + for (final ITree ast : working) { System.out.println("Tree " + treeNo++ + ": " + ast.toString()); } return false; - } else { - Deque> childs = new LinkedList<>(); + } - while(!working.peek().equals(matchNode)) { - childs.push(working.pop()); - } + final Deque> childs = new LinkedList<>(); - // Discard opener - working.pop(); + while (!working.peek().equals(matchNode)) { + childs.push(working.pop()); + } - for(ITree child : childs) { - groupNode.addChild(child); - } + // Discard opener + working.pop(); - working.push(groupNode); + for (final ITree child : childs) { + groupNode.addChild(child); } + working.push(groupNode); + return true; } } -- cgit v1.2.3