|
5 | 5 | import org.openzen.zenscript.codemodel.OperatorType; |
6 | 6 | import org.openzen.zenscript.codemodel.compilation.CastedEval; |
7 | 7 | import org.openzen.zenscript.codemodel.compilation.CastedExpression; |
| 8 | +import org.openzen.zenscript.codemodel.compilation.CompileErrors; |
8 | 9 | import org.openzen.zenscript.codemodel.expression.CompareExpression; |
9 | 10 | import org.openzen.zenscript.codemodel.expression.Expression; |
10 | 11 | import org.openzen.zenscript.codemodel.identifiers.MethodID; |
|
14 | 15 | import org.openzen.zenscript.codemodel.type.TypeID; |
15 | 16 | import org.openzen.zenscript.codemodel.type.member.MemberSet; |
16 | 17 |
|
| 18 | +import java.util.Optional; |
| 19 | + |
17 | 20 | public class BasicTypeMembers { |
18 | 21 | private static final MemberSet NO_MEMBERS = new MemberSet(); |
19 | 22 | private static final MethodID CONSTRUCTOR = MethodID.operator(OperatorType.CONSTRUCTOR); |
@@ -549,7 +552,7 @@ private static MemberSet getString() { |
549 | 552 | private static void setup(MemberSet.Builder builder, BasicTypeID type) { |
550 | 553 | for (BuiltinMethodSymbol method : BuiltinMethodSymbol.values()) { |
551 | 554 | if (method.getDefiningType().equals(type) && method.getID().equals(COMPARE)) { |
552 | | - comparator(builder, method, method.getHeader().getParameterType(false, 0)); |
| 555 | + comparator(builder); |
553 | 556 | }/* else if (method.getID().equals(CONSTRUCTOR)) { |
554 | 557 | builder.constructor(new MethodInstance(method)); |
555 | 558 | } else if (method.getDefiningType() == type) { |
@@ -583,23 +586,46 @@ private static void setup(MemberSet.Builder builder, BasicTypeID type) { |
583 | 586 | } |
584 | 587 | } |
585 | 588 |
|
586 | | - private static void comparator(MemberSet.Builder builder, BuiltinMethodSymbol method, TypeID ofType) { |
587 | | - MethodInstance comparator = new MethodInstance(method); |
| 589 | + private static void comparator(MemberSet.Builder builder) { |
588 | 590 | builder.comparator(((compiler, position, left, right, type) -> { |
589 | | - CastedExpression casted = right.cast(CastedEval.implicit(compiler, position, ofType)); |
590 | | - if (casted.isFailed()) |
591 | | - return casted; |
592 | | - |
593 | | - Expression value = new CompareExpression( |
594 | | - position, |
595 | | - left, |
596 | | - casted.value, |
597 | | - comparator, |
598 | | - type); |
599 | | - return new CastedExpression(casted.level, value); |
| 591 | + Expression rightCompiled = right.eval(); |
| 592 | + Optional<TypeID> union = compiler.union(left.type, rightCompiled.type); |
| 593 | + return union.map(typeID -> { |
| 594 | + CastedEval cast = new CastedEval(compiler, left.position, typeID, false, false); |
| 595 | + Expression castedLeft = cast.of(left).value; |
| 596 | + Expression castedRight = cast.of(rightCompiled).value; |
| 597 | + Expression value = new CompareExpression( |
| 598 | + position, |
| 599 | + castedLeft, |
| 600 | + castedRight, |
| 601 | + new MethodInstance(getComparator((BasicTypeID) typeID)), |
| 602 | + type); |
| 603 | + return new CastedExpression(CastedExpression.Level.EXACT, value); |
| 604 | + }).orElseGet(() -> { |
| 605 | + return new CastedExpression(CastedExpression.Level.INVALID, compiler.at(position).invalid(CompileErrors.cannotCompare(left.type, rightCompiled.type))); |
| 606 | + }); |
600 | 607 | })); |
601 | 608 | } |
602 | 609 |
|
| 610 | + private static BuiltinMethodSymbol getComparator(BasicTypeID type) { |
| 611 | + switch (type) { |
| 612 | + case BYTE: return BuiltinMethodSymbol.BYTE_COMPARE; |
| 613 | + case SBYTE: return BuiltinMethodSymbol.SBYTE_COMPARE; |
| 614 | + case SHORT: return BuiltinMethodSymbol.SHORT_COMPARE; |
| 615 | + case USHORT: return BuiltinMethodSymbol.USHORT_COMPARE; |
| 616 | + case INT: return BuiltinMethodSymbol.INT_COMPARE; |
| 617 | + case UINT: return BuiltinMethodSymbol.UINT_COMPARE; |
| 618 | + case LONG: return BuiltinMethodSymbol.LONG_COMPARE; |
| 619 | + case ULONG: return BuiltinMethodSymbol.ULONG_COMPARE; |
| 620 | + case USIZE: return BuiltinMethodSymbol.USIZE_COMPARE; |
| 621 | + case FLOAT: return BuiltinMethodSymbol.FLOAT_COMPARE; |
| 622 | + case DOUBLE: return BuiltinMethodSymbol.DOUBLE_COMPARE; |
| 623 | + case CHAR: return BuiltinMethodSymbol.CHAR_COMPARE; |
| 624 | + case STRING: return BuiltinMethodSymbol.STRING_COMPARE; |
| 625 | + default: throw new IllegalArgumentException("No comparator for " + type); |
| 626 | + } |
| 627 | + } |
| 628 | + |
603 | 629 | private static MethodInstance[] getWideningMethodInstances(BuiltinMethodSymbol method) { |
604 | 630 | FunctionHeader original = method.getHeader(); |
605 | 631 | if (original.parameters.length != 1) |
|
0 commit comments