Ejemplo n.º 1
0
 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");
 }
Ejemplo n.º 2
0
 private Expression parseMultExpression() {
   Expression expression = parsePrimaryExpression();
   TokenType type = myLexer.nextToken();
   while (type == TokenType.MULT || type == TokenType.DIV) {
     Expression next = parsePrimaryExpression();
     expression =
         new BinaryExpression(
             type == TokenType.MULT ? Operations.MULT : Operations.DIV, expression, next);
     type = myLexer.nextToken();
   }
   myLexer.pushBack();
   return expression;
 }
Ejemplo n.º 3
0
 private Expression parseAddExpression() {
   Expression expression = parseMultExpression();
   TokenType type = myLexer.nextToken();
   while (type == TokenType.PLUS || type == TokenType.MINUS) {
     Expression next = parseMultExpression();
     expression =
         new BinaryExpression(
             type == TokenType.PLUS ? Operations.ADD : Operations.SUB, expression, next);
     type = myLexer.nextToken();
   }
   myLexer.pushBack();
   return expression;
 }
Ejemplo n.º 4
0
 private List<Expression> parseExpressionList() {
   List<Expression> expressions = new ArrayList<Expression>();
   do {
     Expression expression = parseExpression();
     expressions.add(expression);
   } while (myLexer.nextToken() == TokenType.COMMA);
   myLexer.pushBack();
   return expressions;
 }
Ejemplo n.º 5
0
 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");
   }
 }
Ejemplo n.º 6
0
 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);
 }
Ejemplo n.º 7
0
 private StatementsNode parseStatements(boolean insideGroup) {
   List<Statement> statements = new ArrayList<Statement>();
   do {
     Statement statement = parseStatement();
     statements.add(statement);
     TokenType type = myLexer.nextToken();
     myLexer.pushBack();
     if (type == TokenType.EOF || insideGroup && type == TokenType.RBRACE) {
       break;
     }
   } while (true);
   return new StatementsNode(statements);
 }
Ejemplo n.º 8
0
 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);
 }
Ejemplo n.º 9
0
  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);
  }
Ejemplo n.º 10
-1
 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);
 }