Search in sources :

Example 1 with InstanceOfExpr

use of kalang.ast.InstanceOfExpr in project kalang by kasonyang.

the class AstBuilder method visitInstanceofExpr.

@Override
public Object visitInstanceofExpr(KalangParser.InstanceofExprContext ctx) {
    ExprNode expr = visitExpression(ctx.expression());
    Token ts = ctx.Identifier().getSymbol();
    AstNode tnode = getNodeById(ts.getText(), ts);
    if (tnode instanceof ClassReference) {
        InstanceOfExpr ie = new InstanceOfExpr(expr, (ClassReference) tnode);
        mapAst(ie, ctx);
        return ie;
    } else {
        AstBuilder.this.handleSyntaxError("unsupported type", ts);
        return null;
    }
}
Also used : ExprNode(kalang.ast.ExprNode) Token(org.antlr.v4.runtime.Token) ClassReference(kalang.ast.ClassReference) InstanceOfExpr(kalang.ast.InstanceOfExpr) AstNode(kalang.ast.AstNode)

Example 2 with InstanceOfExpr

use of kalang.ast.InstanceOfExpr in project kalang by kasonyang.

the class AstBuilder method onIf.

protected void onIf(ExprNode expr, boolean onTrue) {
    if (expr instanceof InstanceOfExpr && onTrue) {
        InstanceOfExpr ie = (InstanceOfExpr) expr;
        changeTypeTemporarilyIfCould(ie.getExpr(), Types.getClassType(ie.getTarget().getReferencedClassNode()));
    }
    if (expr instanceof CompareExpr) {
        CompareExpr ce = (CompareExpr) expr;
        ExprNode e1 = ce.getExpr1();
        ExprNode e2 = ce.getExpr2();
        boolean isEQ = ce.getOperation().equals(CompareExpr.OP_EQ);
        if (e1.getType().equals(Types.NULL_TYPE)) {
            onNull(e2, onTrue, isEQ);
        } else if (e2.getType().equals(Types.NULL_TYPE)) {
            onNull(e1, onTrue, isEQ);
        }
    }
    if (expr instanceof UnaryExpr) {
        onIf(((UnaryExpr) expr).getExpr(), !onTrue);
    }
    if (expr instanceof LogicExpr) {
        LogicExpr le = (LogicExpr) expr;
        if (le.getOperation().equals(LogicExpr.OP_LOGIC_AND)) {
            if (onTrue) {
                onIf(le.getExpr1(), true);
                onIf(le.getExpr2(), true);
            }
        } else if (le.getOperation().equals(LogicExpr.OP_LOGIC_OR)) {
            if (!onTrue) {
                onIf(le.getExpr1(), false);
                onIf(le.getExpr2(), false);
            }
        }
    }
}
Also used : ExprNode(kalang.ast.ExprNode) CompareExpr(kalang.ast.CompareExpr) LogicExpr(kalang.ast.LogicExpr) InstanceOfExpr(kalang.ast.InstanceOfExpr) UnaryExpr(kalang.ast.UnaryExpr)

Aggregations

ExprNode (kalang.ast.ExprNode)2 InstanceOfExpr (kalang.ast.InstanceOfExpr)2 AstNode (kalang.ast.AstNode)1 ClassReference (kalang.ast.ClassReference)1 CompareExpr (kalang.ast.CompareExpr)1 LogicExpr (kalang.ast.LogicExpr)1 UnaryExpr (kalang.ast.UnaryExpr)1 Token (org.antlr.v4.runtime.Token)1