package me.august.lumen.compile.parser.ast.expr;

import me.august.lumen.common.BytecodeUtil;
import me.august.lumen.compile.codegen.BuildContext;
import me.august.lumen.compile.parser.ast.Typed;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

public class CastExpr extends Typed implements Expression {

    private Expression value;

    public CastExpr(Expression value, String type) {
        super(type);
        this.value = value;
    }

    public Expression getValue() {
        return value;
    }

    @Override
    public Type expressionType() {
        return getResolvedType();
    }

    @Override
    public Expression[] getChildren() {
        return new Expression[]{value};
    }

    @Override
    public void generate(MethodVisitor visitor, BuildContext context) {
        value.generate(visitor, context);

        Type orig   = value.expressionType();
        Type target = getResolvedType();


        // ensure orig and target are either both primitive, or both objects
        if (BytecodeUtil.isPrimitive(orig) != BytecodeUtil.isPrimitive(target)) {
            throw new RuntimeException("Incomptable types");
        }

        // if both are primitives
        if (BytecodeUtil.isPrimitive(orig)) {
            if (orig.getSort() < Type.INT) {
                visitor.visitInsn(BytecodeUtil.castNumberOpcode(Type.INT_TYPE, target));
            } else if (orig.getSort() > Type.INT && target.getSort() < Type.INT) {
                visitor.visitInsn(BytecodeUtil.castNumberOpcode(orig, Type.INT_TYPE));
                visitor.visitInsn(BytecodeUtil.castNumberOpcode(Type.INT_TYPE, target));
            } else if (orig.getSort() >= Type.INT && target.getSort() >= Type.INT) {
                visitor.visitInsn(BytecodeUtil.castNumberOpcode(orig, target));
            }
        } else { // if both are objects, just checkcast
            visitor.visitTypeInsn(Opcodes.CHECKCAST, target.getInternalName());
        }
    }
}

@@ -1296,129 +1296,614 @@
     
-if (orig.getSort() %3C Type.INT) %7B%0A                visitor.visitInsn(BytecodeUtil.castNumberOpcode(Type.INT_TYPE, target));
+// if the original type is shorter than an int, e.g.%0A            // byte -%3E double, we can just interpret it as%0A            // int -%3E double%0A            if (orig.getSort() %3C Type.INT) %7B%0A                visitor.visitInsn(BytecodeUtil.castNumberOpcode(Type.INT_TYPE, target));%0A%0A            // if the original type is wider than an int, and the target type%0A            // is shorter than an int, we first convert the original type to%0A            // an int and then convert that int result to the target type.%0A            // e.g. double -%3E byte is interpreted as:%0A            // double -%3E int, then int -%3E byte
 %0A   
@@ -2149,32 +2149,197 @@
 _TYPE, target));
+%0A%0A            // if both types are wider (or equal) to an int, we can convert%0A            // them directly.%0A            // e.g. double -%3E int produces the D2I opcode
 %0A            %7D e

Add explanation to primitive casting