private ArithmeticExpression parseArithmeticExpression() throws CompileException { Term term = parseTerm(); ASTList<TermBlock> termBlocks = new ASTList<TermBlock>(); while (currentToken.getTokenKind() == TokenKind.TERMOPERATOR) { termBlocks.add(new TermBlock(parsePlusMinusOperator(), parseTerm())); } if (termBlocks.isEmpty()) return new ArithmeticExpression(term); else return new ArithmeticExpression(term, termBlocks); }
private Term parseTerm() throws CompileException { ASTList<FactorBlock> factorBlocks = new ASTList<FactorBlock>(); Factor factor = parseFactor(); while (currentToken.getTokenKind() == TokenKind.FACTOROPERATOR) { factorBlocks.add(new FactorBlock(parseMultiplyDivideOperator(), parseFactor())); } if (factorBlocks.isEmpty()) return new Term(factor); else return new Term(factor, factorBlocks); }
private Program parseProgram() throws CompileException { ASTList<VarDeclaration> varDeclarations = new ASTList<VarDeclaration>(); ASTList<EventCommand> eventCommands = new ASTList<EventCommand>(); while (currentToken.getTokenKind() == TokenKind.TYPE) varDeclarations.add(parseVarDeclaration()); while (currentToken.getTokenKind() == TokenKind.EVENT) eventCommands.add(parseEventCommand()); accept(TokenKind.EOT); if (!eventCommands.isEmpty() && varDeclarations.isEmpty()) // only eventCommands return new Program( eventCommands, true); // the boolean is garbage required as java uses type erasure else if (eventCommands.isEmpty() && !varDeclarations.isEmpty()) // only varDeclarations return new Program(varDeclarations); else // Both return new Program(varDeclarations, eventCommands); }
private IfStatement parseIfStatement() throws CompileException { ASTList<Command> commandIfs = new ASTList<Command>(); ASTList<ElseIfBlock> elseIfBlocks = new ASTList<ElseIfBlock>(); ASTList<Command> commandElses = new ASTList<Command>(); accept(TokenKind.IF); accept(TokenKind.LPAREN); BooleanExpression booleanExpressionIf = parseBooleanExpression(); accept(TokenKind.RPAREN); accept(TokenKind.LCRLPARAN); while (currentToken.getTokenKind() == TokenKind.MOVE || currentToken.getTokenKind() == TokenKind.GOTO || currentToken.getTokenKind() == TokenKind.SET || currentToken.getTokenKind() == TokenKind.IDENTIFIER || currentToken.getTokenKind() == TokenKind.IF) { // Its a command commandIfs.add(parseCommand()); } accept(TokenKind.RCRLPARAN); // We're at the end of the IF clause while (currentToken.getTokenKind() == TokenKind.ELSEIF) { acceptIt(); accept(TokenKind.LPAREN); BooleanExpression orExpression = parseBooleanExpression(); accept(TokenKind.RPAREN); accept(TokenKind.LCRLPARAN); ASTList<Command> commandOrs = new ASTList<Command>(); while (currentToken.getTokenKind() == TokenKind.MOVE || currentToken.getTokenKind() == TokenKind.GOTO || currentToken.getTokenKind() == TokenKind.SET || currentToken.getTokenKind() == TokenKind.IDENTIFIER || currentToken.getTokenKind() == TokenKind.IF) { // Its a command commandOrs.add(parseCommand()); } elseIfBlocks.add(new ElseIfBlock(orExpression, commandOrs)); accept(TokenKind.RCRLPARAN); } if (currentToken.getTokenKind() == TokenKind.ELSE) { acceptIt(); accept(TokenKind.LCRLPARAN); while (currentToken.getTokenKind() == TokenKind.MOVE || currentToken.getTokenKind() == TokenKind.GOTO || currentToken.getTokenKind() == TokenKind.SET || currentToken.getTokenKind() == TokenKind.IDENTIFIER || currentToken.getTokenKind() == TokenKind.IF) { // Its a command commandElses.add(parseCommand()); } accept(TokenKind.RCRLPARAN); } // Find out what constructor to call if (elseIfBlocks.isEmpty() && commandElses.isEmpty()) { return new IfStatement(booleanExpressionIf, commandIfs); } else if (!elseIfBlocks.isEmpty() && commandElses.isEmpty()) { return new IfStatement( booleanExpressionIf, commandIfs, elseIfBlocks, true); // the boolean is garbage required as java uses type erasure } else if (elseIfBlocks.isEmpty() && !commandElses.isEmpty()) { return new IfStatement(booleanExpressionIf, commandIfs, commandElses); } else { return new IfStatement(booleanExpressionIf, commandIfs, elseIfBlocks, commandElses); } }