Skip to content

Commit 473f6f2

Browse files
committed
Improve comparisons, although they need some help
Add some extra tests, although their outputs are indetermined
1 parent 2ca88eb commit 473f6f2

9 files changed

Lines changed: 43 additions & 15 deletions

File tree

CodeModel/src/main/java/org/openzen/zenscript/codemodel/compilation/ResolvedType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ default boolean canCastImplicitlyTo(TypeID target) {
6666

6767
Optional<SwitchMember> findSwitchMember(String name);
6868

69-
Optional<Comparator> compare();
69+
Optional<Comparator> compare(TypeID typeId);
7070

7171
Optional<IteratorInstance> findIterator(int variables);
7272

CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/builtin/BasicTypeMembers.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ private static MemberSet getString() {
547547
private static void setup(MemberSet.Builder builder, BasicTypeID type) {
548548
for (BuiltinMethodSymbol method : BuiltinMethodSymbol.values()) {
549549
if (method.getDefiningType().equals(type) && method.getID().equals(COMPARE)) {
550-
comparator(builder, method, type);
550+
comparator(builder, method, method.getHeader().getParameterType(false, 0));
551551
}/* else if (method.getID().equals(CONSTRUCTOR)) {
552552
builder.constructor(new MethodInstance(method));
553553
} else if (method.getDefiningType() == type) {
@@ -581,9 +581,9 @@ private static void setup(MemberSet.Builder builder, BasicTypeID type) {
581581
}
582582
}
583583

584-
private static void comparator(MemberSet.Builder builder, BuiltinMethodSymbol method, BasicTypeID ofType) {
584+
private static void comparator(MemberSet.Builder builder, BuiltinMethodSymbol method, TypeID ofType) {
585585
MethodInstance comparator = new MethodInstance(method);
586-
builder.comparator(((compiler, position, left, right, type) -> new CompareExpression(
586+
builder.comparator(ofType, ((compiler, position, left, right, type) -> new CompareExpression(
587587
position,
588588
left,
589589
right.cast(CastedEval.implicit(compiler, position, ofType)).value,

CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/ExpandedResolvedType.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ public Optional<SwitchMember> findSwitchMember(String name) {
182182
}
183183

184184
@Override
185-
public Optional<Comparator> compare() {
186-
return base.compare();
185+
public Optional<Comparator> compare(TypeID typeId) {
186+
return base.compare(typeId);
187187
}
188188

189189
@Override

CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/MemberSet.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public static Builder create() {
2626
private final Map<String, SwitchMember> switchMembers = new HashMap<>();
2727
private final Map<String, TypeSymbol> innerTypes = new HashMap<>();
2828
private final List<IteratorInstance> iterators = new ArrayList<>();
29-
private Comparator comparator;
29+
private final Map<TypeID, Comparator> comparators = new HashMap<>();
3030

3131
@Override
3232
public StaticCallable getConstructor() {
@@ -125,8 +125,8 @@ public Optional<SwitchMember> findSwitchMember(String name) {
125125
}
126126

127127
@Override
128-
public Optional<Comparator> compare() {
129-
return Optional.ofNullable(comparator);
128+
public Optional<Comparator> compare(TypeID typeId) {
129+
return Optional.ofNullable(comparators.get(typeId));
130130
}
131131

132132
@Override
@@ -200,8 +200,8 @@ public Builder inner(TypeSymbol type) {
200200
return this;
201201
}
202202

203-
public Builder comparator(Comparator comparator) {
204-
target.comparator = comparator;
203+
public Builder comparator(TypeID typeId, Comparator comparator) {
204+
target.comparators.put(typeId, comparator);
205205
return this;
206206
}
207207

JavaIntegration/src/main/java/org/openzen/zencode/java/module/JavaNativeTypeMembers.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public Optional<SwitchMember> findSwitchMember(String name) {
117117
}
118118

119119
@Override
120-
public Optional<Comparator> compare() {
120+
public Optional<Comparator> compare(TypeID typeId) {
121121
return Optional.empty();
122122
}
123123

Parser/src/main/java/org/openzen/zenscript/parser/expression/ParsedExpressionCompare.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,10 @@ public Expression eval() {
6565
return notEquals.get().call(compiler, position, left, TypeID.NONE, right);
6666
}
6767
}
68-
69-
return resolved.compare()
68+
return resolved.compare(right.eval().type)
7069
.map(comparator -> comparator.compare(compiler, position, left, right, this.type))
7170
.orElseGet(() -> compiler.at(position).invalid(
72-
CompileErrors.noOperatorInType(left.type, OperatorType.COMPARE),
71+
CompileErrors.noOperatorInType(left.type, OperatorType.COMPARE), //TODO Make error message more descriptive and include target type.
7372
BasicTypeID.BOOL));
7473
}
7574

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#output: Two is int
2+
3+
alias Foo as int;
4+
5+
val two : Foo = 2;
6+
7+
if (two is Foo) {
8+
print("Two is Foo");
9+
}
10+
11+
if (two is int) {
12+
print("Two is int");
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#output: true
2+
#output: true
3+
#output: true
4+
#output: true
5+
#output: false
6+
7+
println("" + ((3 < 5) ? "true" : "false"));
8+
println("" + ((3 < (5 as usize)) ? "true" : "false"));
9+
println("" + ((3 < 5.0) ? "true" : "false"));
10+
println("" + ((3 < 5.0f) ? "true" : "false"));
11+
println("" + (3 < 0x40 ? "true" : "false"));
12+
13+
println("" + ((-5 < (3 as usize)) ? "true" : "false")); //What does this return?
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#output: true
2+
3+
println("" + true);

0 commit comments

Comments
 (0)