Skip to content

Commit

Permalink
Minor code simplification / cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
stanhebben committed Feb 7, 2025
1 parent 4b64cfb commit 3746fcf
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,15 @@ public CastedExpression of(Expression value) {

Optional<Expression> implicitCast = resolvedValueType.tryCastImplicit(type, compiler, position, value, optional);
if (implicitCast.isPresent())
return new CastedExpression(CastedExpression.Level.IMPLICIT, implicitCast.get());
return CastedExpression.implicit(implicitCast.get());

if (value.type.canCastImplicitTo(type))
return new CastedExpression(CastedExpression.Level.IMPLICIT, value.type.castImplicitTo(position, value, type));
if (type.canCastImplicitFrom(value.type))
return CastedExpression.implicit(type.castImplicitFrom(position, value));
Optional<Expression> castedImplicitlyTo = value.type.castImplicitTo(position, value, type);
if (castedImplicitlyTo.isPresent())
return CastedExpression.implicit(castedImplicitlyTo.get());

Optional<Expression> castedImplicitlyFrom = type.castImplicitFrom(position, value);
if (castedImplicitlyFrom.isPresent())
return CastedExpression.implicit(castedImplicitlyFrom.get());

if (value.type == BasicTypeID.NULL && type.isOptional())
return CastedExpression.exact(new NullExpression(position, type));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,36 +94,12 @@ default InvalidTypeID asInvalid() {
return new InvalidTypeID(CodePosition.UNKNOWN, CompileErrors.invalidType());
}

default boolean canCastImplicitTo(TypeID other) {
return false;
}

default boolean canCastExplicitTo(TypeID other) {
return false;
}

default boolean canCastImplicitFrom(TypeID other) {
return false;
}

default boolean canCastExplicitFrom(TypeID other) {
return false;
}

default Expression castImplicitTo(CodePosition position, Expression value, TypeID toType) {
return null;
}

default Expression castExplicitTo(CodePosition position, Expression value, TypeID toOther) {
return null;
}

default Expression castImplicitFrom(CodePosition position, Expression value) {
return null;
default Optional<Expression> castImplicitTo(CodePosition position, Expression value, TypeID toType) {
return Optional.empty();
}

default Expression castExplicitFrom(CodePosition position, Expression value) {
return null;
default Optional<Expression> castImplicitFrom(CodePosition position, Expression value) {
return Optional.empty();
}

default Optional<OptionalTypeID> asOptional() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.openzen.zenscript.javashared.expressions.JavaFunctionInterfaceCastExpression;

import java.lang.reflect.Method;
import java.util.Optional;

public class JavaFunctionalInterfaceTypeID extends FunctionTypeID {
public final Method functionalInterfaceMethod;
Expand All @@ -28,32 +29,23 @@ public TypeID instance(GenericMapper mapper) {
}

@Override
public boolean canCastImplicitTo(TypeID other) {
return other instanceof FunctionTypeID && ((FunctionTypeID) other).header.isEquivalentTo(header);
}

@Override
public boolean canCastImplicitFrom(TypeID other) {
return other instanceof FunctionTypeID && ((FunctionTypeID) other).header.isEquivalentTo(header);
}

@Override
public Expression castImplicitTo(CodePosition position, Expression value, TypeID other) {
public Optional<Expression> castImplicitTo(CodePosition position, Expression value, TypeID other) {
if (other instanceof FunctionTypeID) {
FunctionTypeID otherType = (FunctionTypeID) other;
if (header.isEquivalentTo(otherType.header))
return new JavaFunctionInterfaceCastExpression(position, otherType, value);
return Optional.of(new JavaFunctionInterfaceCastExpression(position, otherType, value));
}
return null;

return Optional.empty();
}

@Override
public Expression castImplicitFrom(CodePosition position, Expression value) {
public Optional<Expression> castImplicitFrom(CodePosition position, Expression value) {
if (value.type instanceof FunctionTypeID) {
FunctionTypeID otherType = (FunctionTypeID) value.type;
if (header.isEquivalentTo(otherType.header))
return new JavaFunctionInterfaceCastExpression(position, this, value);
return Optional.of(new JavaFunctionInterfaceCastExpression(position, this, value));
}
return null;
return Optional.empty();
}
}

0 comments on commit 3746fcf

Please sign in to comment.