Exemple #1
0
    // ************************************************
    // * EXPRESSIONS
    // ************************************************

    public Type visit(Binop n) {

        // based on op type, perform corresponding checks

        Type t1 = n.e1.accept(this);
        Type t2 = n.e2.accept(this);

        if (compatible(t1,  t2)) {

            if ((n.op == Binop.ADD) || (n.op == Binop.SUB) ||(n.op == Binop.MUL) ||(n.op == Binop.DIV)) {
                if (compatible(t1,  intType)) {
                    return t1;
                }
            }

            else if ((n.op == Binop.AND) || (n.op == Binop.OR)) {
                if (compatible(t1,  boolType)) {
                    return t1;
                }
            }
            else {
                throw new TypeException("Indeterminate Binop encountered.");
            }
        }

        throw new TypeException("Binop operands' type mismatch: "+ t1.toString()+ n.opName(n.op) + t2.toString());
    }