/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.mcmaster.cas.matlab2smt;

import java.util.Arrays;
import java.util.Collection;

/**
 *
 * @author matthew
 */
public enum MatlabBinaryOperation {

    Addition,
    Minus,
    Multiplication,
    Division,

    GreaterThen,
    GreaterThenEqual,
    LessThen,
    LessThenEqual,
    Equals,

    BooleanAnd,
    BooleanOr;

    static MatlabBinaryOperation getOpForSymbol(String text) {
        if (text.equals("+")) {
            return Addition;
        } else if (text.equals("-")) {
            return Minus;
        } else if (text.equals("*")) {
            return Multiplication;
        } else if (text.equals("/")) {
            return Division;
        } else if (text.equals(">")) {
            return GreaterThen;
        } else if (text.equals(">=")) {
            return GreaterThenEqual;
        } else if (text.equals("<")) {
            return LessThen;
        } else if (text.equals("<=")) {
            return LessThenEqual;
        } else if (text.equals("==")) {
            return Equals;
        } else if (text.equals("&&")) {
            return BooleanAnd;
        } else if (text.equals("||")) {
            return BooleanOr;
        } else {
            throw new IllegalArgumentException("No such binary matlab operation defined (" + text + ")!");
        }
    }

    Collection<VariableTypeMarker> getInputTypeForOutput(VariableTypeMarker outputType) {
        // Boolean inputs only work with a boolean input!
        if (this == BooleanAnd || this == BooleanOr || this == GreaterThen || this == GreaterThenEqual ||
                this == LessThen || this == LessThenEqual || this == Equals) {
            if (!outputType.equals(new BooleanVariableType())) {
                throw new IllegalArgumentException("Boolean operators and comparisons require a boolean output type!");
            }
        }

        // Comparisions produce boolean outputs!
        switch(this) {
            case GreaterThen:
            case GreaterThenEqual:
            case LessThen:
            case LessThenEqual:
            case Equals:
                return Arrays.asList(new VariableTypeMarker[]{new RealVariableType(), new FixedPointVariableType.Marker()});
        }
        return Arrays.asList(new VariableTypeMarker[]{outputType});
    }

    public VariableType getOutputForInput(VariableType inputType) {
        // Boolean inputs only work with a boolean input!
        if (this == BooleanAnd || this == BooleanOr) {
            if (!inputType.equals(new BooleanVariableType())) {
                throw new IllegalArgumentException("Boolean operators require a boolean input type!");
            }
        }

        // Comparisions produce boolean outputs!
        switch(this) {
            case GreaterThen:
            case GreaterThenEqual:
            case LessThen:
            case LessThenEqual:
            case Equals:
                return new BooleanVariableType();
        }
        return inputType;
    }
}
