/**
 * This file is part of JICI, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2015-2015 Aleksi Sapon <http://sapon.ca/jici/>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package ca.sapon.jici.evaluator;

public enum ValueKind {
    BOOLEAN,
    BYTE,
    SHORT,
    CHAR,
    INT,
    LONG,
    FLOAT,
    DOUBLE,
    OBJECT;

    public static boolean canNarrowTo(ValueKind kind, int value) {
        switch (kind) {
            case BYTE:
                return (value + 128 & ~0xFF) == 0;
            case SHORT:
                return (value + 32768 & ~0xFFFF) == 0;
            case CHAR:
                return (value & ~0xFFFF) == 0;
            default:
                throw new IllegalArgumentException("Not a narrowing conversion: " + kind + " to INT");
        }
    }
}

@@ -1821,11 +1821,787 @@
  %7D%0A    %7D
+%0A%0A    public static ValueKind unaryWidensTo(ValueKind inner) %7B%0A        // BYTE, SHORT CHAR to INT%0A        if ((1 %3C%3C inner.ordinal() & 0b1110) != 0) %7B%0A            return INT;%0A        %7D%0A        // anything else to itself%0A        return inner;%0A    %7D%0A%0A    public static ValueKind binaryWidensTo(ValueKind left, ValueKind right) %7B%0A        final int mask = 1 %3C%3C left.ordinal() %7C 1 %3C%3C right.ordinal();%0A        // either DOUBLE to DOUBLE%0A        if ((mask & 0b10000000) != 0) %7B%0A            return DOUBLE;%0A        %7D%0A        // either FLOAT to FLOAT%0A        if ((mask & 0b1000000) != 0) %7B%0A            return FLOAT;%0A        %7D%0A        // either LONG to LONG%0A        if ((mask & 0b100000) != 0) %7B%0A            return LONG;%0A        %7D%0A        // anything else to INT%0A        return INT;%0A    %7D
 %0A%7D%0A

Add widening utilities to ValueKind