Package asteroid

Class Statements


  • public final class Statements
    extends Object
    This class hides the different implementations to create expressions through the Groovy api to provide a unified an easier way to create statements when coding an AST transformation.
    Since:
    0.1.0
    • Constructor Detail

      • Statements

        public Statements()
    • Method Detail

      • stmt

        public static Statement stmt​(Expression expression)
        Returns an instance of Statement. When declaring a MethodNode the body is always whether a ReturnStatement if it returns something or just a Statement if it's void.

        AST
        
         // body of a void method
         stmt(callThisX("println", constX("1")));
         
        Result
        println "1"
        Parameters:
        expression -
        Returns:
        an instance of Statement
        Since:
        0.1.0
      • ctorSuperS

        public static Statement ctorSuperS​(Expression... args)
        Returns an instance of Statement. This normally will be used to call to a super constructor from the current class:

        AST
        
         Statement callSuper = A.STMT
             .ctorSuperS(
                 A.EXPR.constX(1),
                 A.EXPR.constX(2));
         
        Result
        super(1,2)
        Parameters:
        args -
        Returns:
        an instance of type Statement
        Since:
        0.1.0
      • blockSFromString

        public static BlockStatement blockSFromString​(String code)
        Returns an instance of BlockStatement. The code contained in the string passed as argument will be parsed and converted to a BlockStatement
        Parameters:
        code - the string representation of the code
        Returns:
        an instance of BlockStatement
        Since:
        0.1.0
      • assertS

        public static AssertStatement assertS​(BooleanExpression booleanExpr,
                                              String errorMessage)
        Returns an instance of AssertStatement

        AST
        assertS(booleanExpr, "checking something important")
        Result
        assert 1 == 1 : "checking something important"
        Parameters:
        booleanExpr - the expression we want to check
        errorMessage - the error message in case assertion fails
        Returns:
        an instance of AssertStatement
        Since:
        0.1.5
      • ifS

        public static IfStatement ifS​(BooleanExpression booleanExpr,
                                      Statement ifStmt)
        Builds a if statement, without the else part. It receives a boolean expression and the code executed in case the boolean expression evaluates to true

        AST
        ifS(boolX('a', Types.COMPARE_GREATER_THAN, 'b'), codeStmt)
        Result
        if (a > b) {
          // content of the codeStmt statement
         }
        Parameters:
        booleanExpr - the boolean expression used to decide whether the next statement will be executed or not
        ifStmt - code that will be executed if the boolean expression evaluates to true
        Since:
        0.2.4
      • doWhileStatement

        @Deprecated
        public static DoWhileStatement doWhileStatement​(BooleanExpression booleanExpr,
                                                        Statement loopBlock)
        Deprecated.
        As of release 2.4.7, replaced by doWhileS(BooleanExpression, Statement)
        Represents a do-while statement. A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block
        Parameters:
        booleanExpr - boolean condition
        loopBlock - the block that could be repeated
        Returns:
        an instance of type DoWhileStatement
        Since:
        0.2.6
      • doWhileS

        public static DoWhileStatement doWhileS​(BooleanExpression booleanExpr,
                                                Statement loopBlock)
        Represents a do-while statement. A do while loop is a control flow statement that executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block
        Parameters:
        booleanExpr - boolean condition
        loopBlock - the block that could be repeated
        Returns:
        an instance of type DoWhileStatement
        Since:
        0.2.7
      • whileS

        public static WhileStatement whileS​(BooleanExpression booleanExpr,
                                            Statement loopBlock)
        Represents a while statement. A while loop is a control flow statement that executes maybe repeatedly a code block, depending on a given boolean condition.
        Parameters:
        booleanExpr - boolean condition
        loopBlock - the block that could be repeated
        Returns:
        an instance of type DoWhileStatement
        Since:
        0.2.7
      • emptyStatement

        public static Statement emptyStatement()
        Deprecated.
        As of release 2.4.7, replaced by emptyS()
        Represents an empty statement. It could be used when building an IfStatement with an empty else part.
        Returns:
        an empty Statement
        Since:
        0.2.4
      • emptyS

        public static Statement emptyS()
        Represents an empty statement. It could be used when building an IfStatement with an empty else part.
        Returns:
        an empty Statement
        Since:
        0.2.7
      • ifElseS

        public static IfStatement ifElseS​(BooleanExpression booleanExpr,
                                          Statement ifStmt,
                                          Statement elseStmt)
        Builds a if-else statement. It receives a boolean expression and two statements corresponding to the both if and else parts

        AST
        ifElseS(boolX('a', Types.COMPARE_GREATER_THAN, 'b'), ifStmt, elseStmt)
        Result
        if (a > b) {
          // ifStmt code
         } else {
          // elseStmt
         }
        Parameters:
        booleanExpr - the boolean expression used to decide whether the next statement will be executed or not
        ifStmt - code that will be executed if the boolean expression evaluates to true
        Since:
        0.2.4
      • throwS

        public static ThrowStatement throwS​(Expression expression)
        Represents how to throw an exception AST
        throwS(newX(IllegalStateException, constX('wrong value')))
        Result
        throw new IllegalStateException('wrong value')
        Parameters:
        expression - it will be normally a representation of a new instance of a given Throwable type
        Returns:
        an instance of ThrowStatement
        Since:
        0.2.4