private Expression parsePrimaryExpression() { TokenType type = myLexer.nextToken(); if (type == TokenType.LPAREN) { Expression expression = parseExpression(); tokenExpected(TokenType.RPAREN); return expression; } if (type == TokenType.IDENTIFIER) { String name = myLexer.getToken(); if (myLexer.nextToken() == TokenType.LPAREN) { List<Expression> arguments = parseExpressionList(); tokenExpected(TokenType.RPAREN); return new FunctionCallExpression(name, arguments); } myLexer.pushBack(); return new VariableExpression(name); } if (type == TokenType.INT) { try { int value = Integer.parseInt(myLexer.getToken()); return new IntegerLiteralExpression(value); } catch (NumberFormatException e) { throw new ParsingException(myLexer.getToken() + " is not an integer"); } } throw new ParsingException( "'(' or number or identifier expected but " + myLexer.getToken() + " found"); }
private void tokenExpected(TokenType type, String text) { myLexer.nextToken(); if (type != myLexer.getTokenType()) { throw new ParsingException(type.getName() + " expected but " + myLexer.getToken() + " found"); } if (text != null && !text.equals(myLexer.getToken())) { throw new ParsingException(text + " expected but " + myLexer.getToken() + " found"); } }
private BooleanExpression parseBooleanExpression() { Expression left = parseExpression(); TokenType type = myLexer.nextToken(); ComparisonOperation operation = BOOLEAN_OPERATIONS.get(type); if (operation == null) { throw new ParsingException( "'<', '>', '<=', '>=', '!=' or '==' expected but " + myLexer.getToken() + " found"); } Expression right = parseExpression(); return new ComparisonExpression(operation, left, right); }
private Statement parseIf() { tokenExpected(TokenType.LPAREN); BooleanExpression condition = parseBooleanExpression(); tokenExpected(TokenType.RPAREN); Statement thenClause = parseStatement(); Statement elseClause = null; if (myLexer.nextToken() == TokenType.IDENTIFIER && myLexer.getToken().equals("else")) { elseClause = parseStatement(); } else { myLexer.pushBack(); } return new IfStatement(condition, thenClause, elseClause); }
private Statement parseStatement() { TokenType type = myLexer.nextToken(); if (type == TokenType.IDENTIFIER) { String token = myLexer.getToken(); if (token.equals("print")) { Expression expression = parseExpression(); tokenExpected(TokenType.SEMICOLON); return new PrintStatement(expression); } else if (token.equals("return")) { Expression expression = parseExpression(); tokenExpected(TokenType.SEMICOLON); return new ReturnStatement(expression); } else if (token.equals("fun")) { return parseFunctionDeclaration(); } else if (token.equals("if")) { return parseIf(); } else if (token.equals("while")) { return parseWhile(); } if (myLexer.nextToken() == TokenType.ASSIGN) { Expression expression = parseExpression(); tokenExpected(TokenType.SEMICOLON); return new AssignmentStatement(token, expression); } myLexer.pushBack(); myLexer.pushBack(token, TokenType.IDENTIFIER); } else if (type == TokenType.LBRACE) { StatementsNode statement = parseStatements(true); tokenExpected(TokenType.RBRACE); return statement; } else { myLexer.pushBack(); } Expression expression = parseExpression(); tokenExpected(TokenType.SEMICOLON); return new ExpressionStatement(expression); }
private Statement parseFunctionDeclaration() { tokenExpected(TokenType.IDENTIFIER); String name = myLexer.getToken(); tokenExpected(TokenType.LPAREN); List<String> parameters = new ArrayList<String>(); do { tokenExpected(TokenType.IDENTIFIER); parameters.add(myLexer.getToken()); } while (myLexer.nextToken() == TokenType.COMMA); myLexer.pushBack(); tokenExpected(TokenType.RPAREN); Statement body = parseStatement(); return new FunctionDeclaration(name, parameters, body); }