use of kalang.ast.LogicExpr 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);
}
}
use of kalang.ast.LogicExpr 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);
}
}
}
}
use of kalang.ast.LogicExpr in project kalang by kasonyang.
the class AstBuilder method createBinaryExpr.
private BinaryExpr createBinaryExpr(ExprNode expr1, ExprNode expr2, String op) {
BinaryExpr binExpr;
switch(op) {
case "==":
case "!=":
case ">":
case ">=":
case "<":
case "<=":
binExpr = new CompareExpr(expr1, expr2, op);
break;
case "&&":
case "||":
binExpr = new LogicExpr(expr1, expr2, op);
break;
default:
binExpr = new MathExpr(expr1, expr2, op);
break;
}
semanticAnalyzer.validateBinaryExpr(binExpr);
return binExpr;
}
Aggregations