Search in sources :

Example 31 with ExprNode

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

the class Ast2Class method visitPrimitiveCastExpr.

@Override
public Object visitPrimitiveCastExpr(PrimitiveCastExpr node) {
    ExprNode expr = node.getExpr();
    visit(expr);
    int opc;
    Type ft = expr.getType();
    Type tt = node.getToType();
    opc = getPrimitiveCastOpc(ft, tt);
    if (opc > 0) {
        md.visitInsn(opc);
    }
    return null;
}
Also used : ExprNode(kalang.ast.ExprNode) Type(kalang.core.Type) GenericType(kalang.core.GenericType) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ObjectType(kalang.core.ObjectType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType)

Example 32 with ExprNode

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

the class Ast2Class method visitIfStmt.

@Override
public Object visitIfStmt(IfStmt node) {
    Label stopLabel = new Label();
    Label falseLabel = new Label();
    ExprNode condition = node.getConditionExpr();
    Statement trueBody = node.getTrueBody();
    Statement falseBody = node.getFalseBody();
    ifExpr(false, condition, falseLabel);
    if (trueBody != null) {
        visit(trueBody);
    }
    if (falseBody == null) {
        md.visitLabel(falseLabel);
    } else {
        md.visitJumpInsn(GOTO, stopLabel);
        md.visitLabel(falseLabel);
        visit(falseBody);
    }
    md.visitLabel(stopLabel);
    return null;
}
Also used : ExprNode(kalang.ast.ExprNode) Statement(kalang.ast.Statement) Label(org.objectweb.asm.Label)

Example 33 with ExprNode

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

the class Ast2Class method ifExpr.

private void ifExpr(boolean jumpOnTrue, ExprNode condition, Label label) {
    if (condition instanceof LogicExpr) {
        LogicExpr be = (LogicExpr) condition;
        ExprNode e1 = be.getExpr1();
        ExprNode e2 = be.getExpr2();
        String op = be.getOperation();
        switch(op) {
            case "&&":
                if (jumpOnTrue) {
                    Label stopLabel = new Label();
                    ifExpr(false, e1, stopLabel);
                    ifExpr(false, e2, stopLabel);
                    md.visitJumpInsn(GOTO, label);
                    md.visitLabel(stopLabel);
                } else {
                    ifExpr(false, e1, label);
                    ifExpr(false, e2, label);
                }
                break;
            case "||":
                if (jumpOnTrue) {
                    ifExpr(true, e1, label);
                    ifExpr(true, e2, label);
                } else {
                    Label stopLabel = new Label();
                    ifExpr(true, e1, stopLabel);
                    ifExpr(true, e2, stopLabel);
                    md.visitJumpInsn(GOTO, label);
                    md.visitLabel(stopLabel);
                }
                break;
            default:
                throw new UnsupportedOperationException("Unsupported operation:" + op);
        }
    } else if (condition instanceof CompareExpr) {
        ifCompare(jumpOnTrue, ((CompareExpr) condition).getExpr1(), ((CompareExpr) condition).getExpr2(), ((CompareExpr) condition).getOperation(), label);
    } else if (condition instanceof UnaryExpr && ((UnaryExpr) condition).getOperation().equals("!")) {
        ifExpr(!jumpOnTrue, ((UnaryExpr) condition).getExpr(), label);
    } else {
        visit(condition);
        md.visitJumpInsn(jumpOnTrue ? IFNE : IFEQ, label);
    }
}
Also used : ExprNode(kalang.ast.ExprNode) LogicExpr(kalang.ast.LogicExpr) CompareExpr(kalang.ast.CompareExpr) Label(org.objectweb.asm.Label) UnaryExpr(kalang.ast.UnaryExpr)

Example 34 with ExprNode

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

the class AstBuilder method getObjectFieldLikeExpr.

protected ExprNode getObjectFieldLikeExpr(ExprNode expr, String fieldName, @Nullable ParserRuleContext rule) {
    ExprNode ret;
    Type type = expr.getType();
    if (!(type instanceof ObjectType)) {
        AstBuilder.this.handleSyntaxError("unsupported type", rule == null ? ParserRuleContext.EMPTY : rule);
        return null;
    }
    ObjectType exprType = (ObjectType) type;
    if ((exprType instanceof ArrayType) && fieldName.equals("length")) {
        ret = new ArrayLengthExpr(expr);
    } else {
        try {
            ret = ObjectFieldExpr.create(expr, fieldName, thisClazz);
        } catch (FieldNotFoundException ex) {
            this.diagnosisReporter.report(Diagnosis.Kind.ERROR, "field not found:" + fieldName, rule);
            ret = new UnknownFieldExpr(expr, exprType.getClassNode(), fieldName);
        }
    }
    if (rule != null)
        mapAst(ret, rule);
    return ret;
}
Also used : ExprNode(kalang.ast.ExprNode) ArrayType(kalang.core.ArrayType) ObjectType(kalang.core.ObjectType) WildcardType(kalang.core.WildcardType) ArrayType(kalang.core.ArrayType) ClassType(kalang.core.ClassType) PrimitiveType(kalang.core.PrimitiveType) Type(kalang.core.Type) GenericType(kalang.core.GenericType) ObjectType(kalang.core.ObjectType) FieldNotFoundException(kalang.FieldNotFoundException) UnknownFieldExpr(kalang.ast.UnknownFieldExpr) ArrayLengthExpr(kalang.ast.ArrayLengthExpr)

Example 35 with ExprNode

use of kalang.ast.ExprNode 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)47 ObjectType (kalang.core.ObjectType)23 ArrayType (kalang.core.ArrayType)17 GenericType (kalang.core.GenericType)17 Type (kalang.core.Type)17 ClassType (kalang.core.ClassType)16 PrimitiveType (kalang.core.PrimitiveType)15 WildcardType (kalang.core.WildcardType)14 ExprStmt (kalang.ast.ExprStmt)13 AssignExpr (kalang.ast.AssignExpr)12 BlockStmt (kalang.ast.BlockStmt)10 AmbiguousMethodException (kalang.AmbiguousMethodException)9 MethodNotFoundException (kalang.MethodNotFoundException)9 ClassReference (kalang.ast.ClassReference)9 ThisExpr (kalang.ast.ThisExpr)8 ExpressionContext (kalang.antlr.KalangParser.ExpressionContext)7 Statement (kalang.ast.Statement)7 LocalVarNode (kalang.ast.LocalVarNode)6 ObjectInvokeExpr (kalang.ast.ObjectInvokeExpr)6 VarExpr (kalang.ast.VarExpr)6