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;
}
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;
}
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);
}
}
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;
}
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);
}
}
}
}
Aggregations