public int get() { if (lookAheadTokens == null) return get(currentToken); else { Token t; currentToken = t = lookAheadTokens; lookAheadTokens = lookAheadTokens.next; return t.tokenId; } }
private Stmnt parseDo(SymbolTable tbl) throws CompileError { int t = lex.get(); // DO Stmnt body = parseStatement(tbl); if (lex.get() != WHILE || lex.get() != '(') throw new SyntaxError(lex); ASTree expr = parseExpression(tbl); if (lex.get() != ')' || lex.get() != ';') throw new SyntaxError(lex); return new Stmnt(t, expr, body); }
private Declarator parseFormalParam(SymbolTable tbl) throws CompileError { Declarator d = parseFormalType(tbl); if (lex.get() != Identifier) throw new SyntaxError(lex); String name = lex.getString(); d.setVariable(new Symbol(name)); d.addArrayDim(parseArrayDimension()); tbl.append(name, d); return d; }
private Stmnt parseSynchronized(SymbolTable tbl) throws CompileError { int t = lex.get(); // SYNCHRONIZED if (lex.get() != '(') throw new SyntaxError(lex); ASTree expr = parseExpression(tbl); if (lex.get() != ')') throw new SyntaxError(lex); Stmnt body = parseBlock(tbl); return new Stmnt(t, expr, body); }
private Stmnt parseSwitch(SymbolTable tbl) throws CompileError { int t = lex.get(); // SWITCH ASTree expr = parseParExpression(tbl); Stmnt body = parseSwitchBlock(tbl); return new Stmnt(t, expr, body); }
private Stmnt parseWhile(SymbolTable tbl) throws CompileError { int t = lex.get(); // WHILE ASTree expr = parseParExpression(tbl); Stmnt body = parseStatement(tbl); return new Stmnt(t, expr, body); }
private ASTree parseConditionalExpr(SymbolTable tbl) throws CompileError { ASTree cond = parseBinaryExpr(tbl); if (lex.lookAhead() == '?') { lex.get(); ASTree thenExpr = parseExpression(tbl); if (lex.get() != ':') throw new CompileError(": is missing", lex); ASTree elseExpr = parseExpression(tbl); return new CondExpr(cond, thenExpr, elseExpr); } else return cond; }
private Stmnt parseStmntOrCase(SymbolTable tbl) throws CompileError { int t = lex.lookAhead(); if (t != CASE && t != DEFAULT) return parseStatement(tbl); lex.get(); Stmnt s; if (t == CASE) s = new Stmnt(t, parseExpression(tbl)); else s = new Stmnt(DEFAULT); if (lex.get() != ':') throw new CompileError(": is missing", lex); return s; }
private Stmnt parseExprList(SymbolTable tbl) throws CompileError { Stmnt expr = null; for (;;) { Stmnt e = new Stmnt(EXPR, parseExpression(tbl)); expr = (Stmnt)ASTList.concat(expr, new Stmnt(BLOCK, e)); if (lex.lookAhead() == ',') lex.get(); else return expr; } }
public ASTree parseExpression(SymbolTable tbl) throws CompileError { ASTree left = parseConditionalExpr(tbl); if (!isAssignOp(lex.lookAhead())) return left; int t = lex.get(); ASTree right = parseExpression(tbl); return AssignExpr.makeAssign(t, left, right); }
private Stmnt parseIf(SymbolTable tbl) throws CompileError { int t = lex.get(); // IF ASTree expr = parseParExpression(tbl); Stmnt thenp = parseStatement(tbl); Stmnt elsep; if (lex.lookAhead() == ELSE) { lex.get(); elsep = parseStatement(tbl); } else elsep = null; return new Stmnt(t, expr, new ASTList(thenp, new ASTList(elsep))); }
public MethodDecl parseMethod2(SymbolTable tbl, MethodDecl md) throws CompileError { Stmnt body = null; if (lex.lookAhead() == ';') lex.get(); else { body = parseBlock(tbl); if (body == null) body = new Stmnt(BLOCK); } md.sublist(4).setHead(body); return md; }
private Declarator parseFormalType(SymbolTable tbl) throws CompileError { int t = lex.lookAhead(); if (isBuiltinType(t) || t == VOID) { lex.get(); // primitive type int dim = parseArrayDimension(); return new Declarator(t, dim); } else { ASTList name = parseClassType(tbl); int dim = parseArrayDimension(); return new Declarator(name, dim); } }