use of kalang.ast.ExprNode in project kalang by kasonyang.
the class SemanticAnalyzer method validateBinaryExpr.
public boolean validateBinaryExpr(BinaryExpr node) {
ExprNode expr1 = node.getExpr1();
ExprNode expr2 = node.getExpr2();
Type t1 = expr1.getType();
Type t2 = expr2.getType();
String op = node.getOperation();
switch(op) {
case "==":
case "!=":
if (Types.isNumber(t1)) {
return this.requireNumber(expr2, t2);
} else {
return true;
// TODO pass anything.may be Object needed?
}
case "+":
case "-":
case "*":
case "/":
case "%":
case ">=":
case "<=":
case ">":
case "<":
case "&":
case "|":
case "^":
case BinaryExpr.OP_SHIFT_LEFT:
case BinaryExpr.OP_SHIFT_RIGHT:
return this.requireNumber(expr1, t1) && this.requireNumber(expr2, t2);
case "&&":
case "||":
return requireBoolean(expr1, t1) && requireBoolean(expr2, t2);
default:
diagnosisReporter.report(Diagnosis.Kind.ERROR, "unsupport operation:" + op, node.offset);
return false;
}
}
use of kalang.ast.ExprNode in project kalang by kasonyang.
the class Ast2Class method visitFieldExpr.
@Override
public Object visitFieldExpr(FieldExpr node) {
int opc;
String owner = internalName(node.getField().getFieldNode().getClassNode());
if (node instanceof ObjectFieldExpr) {
ExprNode target = ((ObjectFieldExpr) node).getTarget();
visit(target);
opc = GETFIELD;
} else if (node instanceof StaticFieldExpr) {
opc = GETSTATIC;
} else {
throw new UnsupportedOperationException("unsupported field type:" + node);
}
md.visitFieldInsn(opc, owner, node.getField().getName(), getTypeDescriptor(node.getType()));
return null;
}
use of kalang.ast.ExprNode in project kalang by kasonyang.
the class Ast2Class method visitBinaryExpr.
@Override
public Object visitBinaryExpr(BinaryExpr node) {
ExprNode e1 = node.getExpr1();
ExprNode e2 = node.getExpr2();
int op;
org.objectweb.asm.Type at = asmType(node.getExpr1().getType());
switch(node.getOperation()) {
case "+":
op = IADD;
break;
case "-":
op = ISUB;
break;
case "*":
op = IMUL;
break;
case "/":
op = IDIV;
break;
case "%":
op = IREM;
break;
// bitwise
case BinaryExpr.OP_AND:
op = IAND;
break;
case BinaryExpr.OP_OR:
op = IOR;
break;
case BinaryExpr.OP_XOR:
op = IXOR;
break;
case BinaryExpr.OP_SHIFT_LEFT:
op = ISHL;
break;
case BinaryExpr.OP_SHIFT_RIGHT:
op = ISHR;
break;
default:
// logic expression
Label trueLabel = new Label();
Label stopLabel = new Label();
ifExpr(true, node, trueLabel);
constFalse();
md.visitJumpInsn(GOTO, stopLabel);
md.visitLabel(trueLabel);
constTrue();
md.visitLabel(stopLabel);
return null;
}
visit(e1);
visit(e2);
md.visitInsn(at.getOpcode(op));
return null;
}
use of kalang.ast.ExprNode in project kalang by kasonyang.
the class Ast2Class method visitInvocationExpr.
@Override
public Object visitInvocationExpr(InvocationExpr node) {
int opc;
ExecutableDescriptor method = node.getMethod();
// = internalName(node.getMethod().classNode);
String ownerClass;
if (node instanceof StaticInvokeExpr) {
opc = INVOKESTATIC;
ownerClass = internalName(((StaticInvokeExpr) node).getInvokeClass().getReferencedClassNode());
} else if (node instanceof ObjectInvokeExpr) {
ObjectInvokeExpr oie = (ObjectInvokeExpr) node;
ObjectType targetType = (ObjectType) oie.getInvokeTarget().getType();
ownerClass = internalName(targetType);
ExprNode target = oie.getInvokeTarget();
visit(target);
if (Modifier.isPrivate(method.getModifier()) || (target instanceof SuperExpr) || method.getName().equals("<init>")) {
opc = INVOKESPECIAL;
} else {
opc = ModifierUtil.isInterface(targetType.getClassNode().modifier) ? INVOKEINTERFACE : INVOKEVIRTUAL;
}
} else {
throw Exceptions.unsupportedTypeException(node);
}
visitAll(node.getArguments());
md.visitMethodInsn(opc, ownerClass, method.getName(), getMethodDescriptor(method.getMethodNode()));
return null;
}
use of kalang.ast.ExprNode in project kalang by kasonyang.
the class Ast2Class method visit.
@Override
public Object visit(AstNode node) {
int lineNum = node.offset.startLine;
if (lineNum > 0 && (node instanceof Statement || node instanceof ExprNode) && !lineLabels.containsKey(lineNum)) {
Label lb = new Label();
md.visitLabel(lb);
md.visitLineNumber(lineNum, lb);
lineLabels.put(lineNum, lb);
}
return super.visit(node);
}
Aggregations