package org.yinwang.pysonar.ast;

import org.jetbrains.annotations.NotNull;
import org.yinwang.pysonar.State;
import org.yinwang.pysonar.types.Type;
import org.yinwang.pysonar.types.UnionType;


public class BinOp extends Node {

    @NotNull
    public Node left;
    @NotNull
    public Node right;
    @NotNull
    public Op op;


    public BinOp(@NotNull Op op, @NotNull Node left, @NotNull Node right, String file, int start, int end) {
        super(file, start, end);
        this.left = left;
        this.right = right;
        this.op = op;
        addChildren(left, right);
    }


    @NotNull
    @Override
    public Type transform(State s) {
        Type ltype = transformExpr(left, s);
        Type rtype = transformExpr(right, s);

        // If either non-null operand is a string, assume the result is a string.
        if (ltype == Type.STR || rtype == Type.STR) {
            return Type.STR;
        }
        // If either non-null operand is a number, assume the result is a number.
        if (ltype == Type.INT || rtype == Type.INT) {
            return Type.INT;
        }

        if (Op.isBoolean(op)) {
            return Type.BOOL;
        } else {
            return UnionType.union(ltype, rtype);
        }
    }


    @NotNull
    @Override
    public String toString() {
        return "(" + left + " " + op + " " + right + ")";
    }

}

@@ -747,359 +747,8 @@
 );%0A%0A
-        // If either non-null operand is a string, assume the result is a string.%0A        if (ltype == Type.STR %7C%7C rtype == Type.STR) %7B%0A            return Type.STR;%0A        %7D%0A        // If either non-null operand is a number, assume the result is a number.%0A        if (ltype == Type.INT %7C%7C rtype == Type.INT) %7B%0A            return Type.INT;%0A        %7D%0A%0A
     

fix boolean operators