public Rexpr(int[] loopStart) throws ParserException { expr1 = new Expr(); switch (Lexer.nextToken) { case Token.EQ_OP: Op = "if_icmpne"; Lexer.lex(); break; case Token.NOT_EQ: Op = "if_icmpeq"; Lexer.lex(); break; case Token.LESSER_OP: Op = "if_icmpge"; Lexer.lex(); break; case Token.GREATER_OP: Op = "if_icmple"; Lexer.lex(); break; default: throw new ParserException(Lexer.nextChar); } expr2 = new Expr(); loopStart[0] = Code.codeptr; Code.gen(Op, 3); }
public Assign() throws ParserException { int idnum; if (Code.variables.containsKey(Lexer.ident)) { idnum = Code.variables.get(Lexer.ident); } else throw new ParserException(Lexer.nextChar); Lexer.lex(); if (Lexer.nextToken == Token.ASSIGN_OP) { Lexer.lex(); } else throw new ParserException(Lexer.nextChar); expr = new Expr(); if (Lexer.nextToken == Token.SEMICOLON) { Lexer.lex(); } else if (Lexer.nextToken == Token.RIGHT_PAREN) { Lexer.lex(); } else throw new ParserException(Lexer.nextChar); if (!Loop.inc) { if (idnum < 3) { Code.gen("istore_" + (idnum + 1)); } else { Code.gen("istore_" + (idnum + 1), 2); } } else { Loop.storeValue[Loop.incpt] = "istore_" + (idnum + 1); Loop.incpt++; Loop.inc = false; } }
public Factor() throws ParserException { switch (Lexer.nextToken) { case Token.INT_LIT: i = Lexer.intValue; Lexer.lex(); if (!Loop.inc) { if (i >= 0 && i <= 5) { Code.gen("iconst_" + i); } else if (i <= 127 && i >= -128) { Code.gen("bipush " + i, 2); } else if (i <= 32767 && i >= -32768) { Code.gen("sipush " + i, 3); } } else { if (i >= 0 && i <= 5) { Loop.storeValue[Loop.incpt] = "iconst_" + i; Loop.incpt++; } else if (i <= 127 && i >= -128) { Loop.storeValue[Loop.incpt] = "bipush " + i; Loop.incpt++; } else if (i <= 32767 && i >= -32768) { Loop.storeValue[Loop.incpt] = "sipush " + i; Loop.incpt++; } } break; case Token.ID: int idnum; if (Code.variables.containsKey(Lexer.ident)) { idnum = Code.variables.get(Lexer.ident); } else throw new ParserException(Lexer.nextChar); if (!Loop.inc) { if (idnum < 3) { Code.gen("iload_" + (idnum + 1)); } else { Code.gen("iload_" + (idnum + 1), 2); } } else { Loop.storeValue[Loop.incpt] = "iload_" + (idnum + 1); Loop.incpt++; } Lexer.lex(); break; case Token.LEFT_PAREN: Lexer.lex(); expr = new Expr(); Lexer.lex(); break; default: throw new ParserException(Lexer.nextChar); } }
public Idlist() { while (Lexer.nextToken != Token.ID && Lexer.nextToken != Token.SEMICOLON) { Lexer.lex(); } if (Lexer.nextToken == Token.ID) { Code.variables.put(Lexer.ident, ptr++); Lexer.lex(); if (Lexer.nextToken == Token.COMMA) idlist = new Idlist(); if (Lexer.nextToken == Token.SEMICOLON) Lexer.lex(); } }
private void testSingleValue(String statement, String expectedData, TokenType expectedTokenType) { ArrayList<Token> tokens = Lexer.lex(statement); Assert.assertEquals(1, tokens.size()); Token actualToken = tokens.get(0); Assert.assertEquals(expectedData, actualToken.getData()); Assert.assertEquals(expectedTokenType, actualToken.getType()); }
public Term() throws ParserException { factor = new Factor(); if (Lexer.nextToken == Token.MULT_OP || Lexer.nextToken == Token.DIV_OP) { Op = Lexer.nextChar; Lexer.lex(); term = new Term(); if (!Loop.inc) Code.gen(Code.opcode(Op)); else { Loop.storeValue[Loop.incpt] = Code.opcode(Op); Loop.incpt++; } } }
public Expr() throws ParserException { term = new Term(); if (Lexer.nextToken == Token.ADD_OP || Lexer.nextToken == Token.SUB_OP) { Op = Lexer.nextChar; Lexer.lex(); expr = new Expr(); if (!Loop.inc) Code.gen(Code.opcode(Op)); else { Loop.storeValue[Loop.incpt] = Code.opcode(Op); Loop.incpt++; } } }
public SerializablePredicate<HString> parse(String query) throws ParseException { Parser parser = new Parser(grammar, lexer.lex(query)); List<SerializablePredicate<HString>> predicates = new ArrayList<>(); while (parser.hasNext()) { Expression expression = parser.next(); predicates.add(generate(expression)); } if (predicates.isEmpty()) { return d -> true; } SerializablePredicate<HString> finalPredicate = predicates.get(0); for (int i = 1; i < predicates.size(); i++) { finalPredicate = defaultOperator == Operator.AND ? and(finalPredicate, predicates.get(i)) : or(finalPredicate, predicates.get(i)); } return finalPredicate; }
public Cond() throws ParserException { if (Lexer.nextToken == Token.KEY_IF) { Lexer.lex(); } if (Lexer.nextToken == Token.LEFT_PAREN) { Lexer.lex(); } else throw new ParserException(Lexer.nextChar); rexpr = new Rexpr(condStart); if (Lexer.nextToken == Token.RIGHT_PAREN) { Lexer.lex(); } else throw new ParserException(Lexer.nextChar); if (Lexer.nextToken == Token.LEFT_BRACE) { smt1 = new Stmt(); } else if (Lexer.nextToken == Token.KEY_IF) { smt3 = new Stmt(); } else if (Lexer.nextToken == Token.ID) { smt4 = new Stmt(); } else throw new ParserException(Lexer.nextChar); if (Lexer.nextToken == Token.RIGHT_BRACE) { Lexer.lex(); } condEnd = Code.codeptr; if (Lexer.nextToken == Token.KEY_ELSE) { int elseGotoCodePtr = Code.codeptr; Code.gen("goto", 3); condEnd = Code.codeptr; Lexer.lex(); if (Lexer.nextToken == Token.LEFT_BRACE || Lexer.nextToken == Token.ID || Lexer.nextToken == Token.KEY_IF) { smt2 = new Stmt(); } if (Lexer.nextToken == Token.RIGHT_BRACE) { Lexer.lex(); } elseEnd = Code.codeptr; Code.code[elseGotoCodePtr] = Code.code[elseGotoCodePtr] + " " + elseEnd; } Code.code[condStart[0]] = Code.code[condStart[0]] + " " + condEnd; }
private Node parse() { Lexer lex = new Lexer(operators, in); stack.push(new Section(' ')); Token token; while ((token = lex.lex()) != null) { Operator operator = token.operator; String data = token.getData(); char ch = data.charAt(0); if (operator != null) { addOperator(operator); if (operator == TermOp.BRACES) stack.push(new Section('{')); } else if (ch == '(' || ch == '[' || ch == '{') stack.push(new Section(ch)); else if (ch == ')' || ch == ']' || ch == '}') { Section section = stack.pop(); if (section.kind == '(' && ch == ')' // || section.kind == '[' && ch == ']' // || section.kind == '{' && ch == '}') { Node node = section.unwind(null).getRight(); if (ch == ']') node = Tree.of(TermOp.TUPLE_, Atom.of("["), node); add(node); } else throw new RuntimeException("Cannot parse " + in); } else if (ch == '`') if (stack.peek().kind == ch) { Node node = stack.pop().unwind(null).getRight(); node = Tree.of(TermOp.TUPLE_, Atom.of("`"), node); add(node); } else stack.push(new Section(ch)); else if (Util.isNotBlank(data)) add(terminalParser.parseTerminal(data)); } if (stack.size() == 1) return stack.pop().unwind(null).getRight(); else throw new RuntimeException("Cannot parse " + in); }
public Cmpd() throws ParserException { Lexer.lex(); stmts = new Stmts(); }
public static void main(String[] args) { System.out.println("Enter program and terminate with 'end'!\n"); Lexer.lex(); new Program(); Code.output(); }
public Decls() throws ParserException { Lexer.lex(); idlist = new Idlist(); }
public Loop() throws ParserException { if (Lexer.nextToken == Token.KEY_FOR) { Lexer.lex(); } if (Lexer.nextToken == Token.LEFT_PAREN) { Lexer.lex(); } else throw new ParserException(Lexer.nextChar); loopStart[0] = Code.codeptr; if (Lexer.nextToken != Token.SEMICOLON && Lexer.nextToken == Token.ID) { assign = new Assign(); } else if (Lexer.nextToken == Token.SEMICOLON) { Lexer.lex(); } loopExprstart = Code.codeptr; if (Lexer.nextToken != Token.SEMICOLON && Lexer.nextToken == Token.ID) { rexpr = new Rexpr(loopStart); Lexer.lex(); } if (Lexer.nextToken != Token.SEMICOLON && Lexer.nextToken == Token.ID) { inc = true; toPrint = true; assign1 = new Assign(); } else if (Lexer.nextToken == Token.RIGHT_PAREN) { Lexer.lex(); } else if (Lexer.nextToken == Token.LEFT_BRACE) { Lexer.lex(); } else throw new ParserException(Lexer.nextChar); if (Lexer.nextToken == Token.LEFT_BRACE) { stmt3 = new Stmt(); } // else // toPrint=false; if (Lexer.nextToken == Token.ID) { stmt1 = new Stmt(); } if (Lexer.nextToken == Token.KEY_IF) { stmt4 = new Stmt(); } if (Lexer.nextToken == Token.KEY_FOR) { toPrint = false; stmt2 = new Stmt(); if (Lexer.nextToken == Token.KEY_END && Loop.storeValue[0] != null && Loop.storeValue[4] != null) { if (!Loop.storeValue[4].equals("0")) { String[] brk = Loop.storeValue[4].split("_"); int id = Integer.parseInt(brk[1]); if (id <= 3) { Code.gen("iload_" + (id)); } else { Code.gen("iload_" + (id), 2); } brk = Loop.storeValue[5].split("_"); id = Integer.parseInt(brk[1]); if (id >= 0 && id <= 5) { Code.gen("iconst_" + id); } else if (id <= 127 && id >= -128) { Code.gen("bipush " + id, 2); } else if (id <= 32767 && id >= -32768) { Code.gen("sipush " + id, 3); } Code.gen(Loop.storeValue[6]); brk = Loop.storeValue[7].split("_"); id = Integer.parseInt(brk[1]); if (id <= 3) { Code.gen("istore_" + (id)); } else { Code.gen("istore_" + (id), 2); } Loop.storeValue[4] = "0"; } } } if (Lexer.nextToken == Token.KEY_END && toPrint) { String[] brk = Loop.storeValue[0].split("_"); int id = Integer.parseInt(brk[1]); if (id <= 3) { Code.gen("iload_" + (id)); } else { Code.gen("iload_" + (id), 2); } brk = Loop.storeValue[1].split("_"); id = Integer.parseInt(brk[1]); if (id >= 0 && id <= 5) { Code.gen("iconst_" + id); } else if (id <= 127 && id >= -128) { Code.gen("bipush " + id, 2); } else if (id <= 32767 && id >= -32768) { Code.gen("sipush " + id, 3); } Code.gen(Loop.storeValue[2]); brk = Loop.storeValue[3].split("_"); id = Integer.parseInt(brk[1]); if (id <= 3) { Code.gen("istore_" + (id)); } else { Code.gen("istore_" + (id), 2); } } Code.gen("goto " + loopExprstart, 3); loopEnd = Code.codeptr; Code.code[loopStart[0]] = Code.code[loopStart[0]] + " " + loopEnd; toPrint = false; }