Search in sources :

Example 1 with ExprNode

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;
    }
}
Also used : ExprNode(kalang.ast.ExprNode) ArrayType(kalang.core.ArrayType) Type(kalang.core.Type) ObjectType(kalang.core.ObjectType) MathType(kalang.util.MathType) PrimitiveType(kalang.core.PrimitiveType)

Example 2 with ExprNode

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;
}
Also used : ExprNode(kalang.ast.ExprNode) ObjectFieldExpr(kalang.ast.ObjectFieldExpr) StaticFieldExpr(kalang.ast.StaticFieldExpr)

Example 3 with ExprNode

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;
}
Also used : ExprNode(kalang.ast.ExprNode) Label(org.objectweb.asm.Label)

Example 4 with ExprNode

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;
}
Also used : StaticInvokeExpr(kalang.ast.StaticInvokeExpr) ExprNode(kalang.ast.ExprNode) ObjectType(kalang.core.ObjectType) SuperExpr(kalang.ast.SuperExpr) ObjectInvokeExpr(kalang.ast.ObjectInvokeExpr) ExecutableDescriptor(kalang.core.ExecutableDescriptor)

Example 5 with ExprNode

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);
}
Also used : ExprNode(kalang.ast.ExprNode) Statement(kalang.ast.Statement) Label(org.objectweb.asm.Label)

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