Skip to content

Commit 8645f2b

Browse files
committed
- Coded comparators a little differently
- Fixed bool operators breaking the entire bool type
1 parent 9c95d98 commit 8645f2b

10 files changed

Lines changed: 56 additions & 25 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ public static CompileError ambiguousType(List<TypeID> candidates) {
2424
return new CompileError(CompileExceptionCode.INFERENCE_AMBIGUOUS, "Type inference ambiguity, possible types: " + possibleTypes);
2525
}
2626

27+
public static CompileError ambiguousComparison(TypeID a, TypeID b) {
28+
return new CompileError(CompileExceptionCode.AMBIGUOUS_COMPARISON, "Ambiguous comparison, not sure to compare as " + a + " or " + b);
29+
}
30+
2731
public static CompileError noMemberInType(TypeID type, String name) {
2832
return new CompileError(CompileExceptionCode.NO_SUCH_MEMBER, "No member " + name + " in type " + type);
2933
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.openzen.zenscript.codemodel.identifiers.instances.IteratorInstance;
1111
import org.openzen.zenscript.codemodel.type.TypeID;
1212

13+
import java.util.List;
1314
import java.util.Optional;
1415

1516
public interface ResolvedType {
@@ -66,7 +67,7 @@ default boolean canCastImplicitlyTo(TypeID target) {
6667

6768
Optional<SwitchMember> findSwitchMember(String name);
6869

69-
Optional<Comparator> compare(TypeID typeId);
70+
List<Comparator> comparators();
7071

7172
Optional<IteratorInstance> findIterator(int variables);
7273

@@ -100,7 +101,7 @@ interface StaticField {
100101

101102
@FunctionalInterface
102103
interface Comparator {
103-
Expression compare(
104+
CastedExpression compare(
104105
ExpressionCompiler compiler,
105106
CodePosition position,
106107
Expression left,

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import org.openzen.zenscript.codemodel.FunctionParameter;
55
import org.openzen.zenscript.codemodel.OperatorType;
66
import org.openzen.zenscript.codemodel.compilation.CastedEval;
7+
import org.openzen.zenscript.codemodel.compilation.CastedExpression;
78
import org.openzen.zenscript.codemodel.expression.CompareExpression;
9+
import org.openzen.zenscript.codemodel.expression.Expression;
810
import org.openzen.zenscript.codemodel.identifiers.MethodID;
911
import org.openzen.zenscript.codemodel.identifiers.instances.FieldInstance;
1012
import org.openzen.zenscript.codemodel.identifiers.instances.MethodInstance;
@@ -583,12 +585,19 @@ private static void setup(MemberSet.Builder builder, BasicTypeID type) {
583585

584586
private static void comparator(MemberSet.Builder builder, BuiltinMethodSymbol method, TypeID ofType) {
585587
MethodInstance comparator = new MethodInstance(method);
586-
builder.comparator(ofType, ((compiler, position, left, right, type) -> new CompareExpression(
587-
position,
588-
left,
589-
right.cast(CastedEval.implicit(compiler, position, ofType)).value,
590-
comparator,
591-
type)));
588+
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);
600+
}));
592601
}
593602

594603
private static MethodInstance[] getWideningMethodInstances(BuiltinMethodSymbol method) {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
import static org.openzen.zenscript.codemodel.type.BasicTypeID.*;
2222

2323
public enum BuiltinMethodSymbol implements MethodSymbol {
24-
BOOL_NOT(BOOL, NOT, BOOL, BOOL),
25-
BOOL_AND(BOOL, AND, BOOL, BOOL, BOOL),
26-
BOOL_OR(BOOL, OR, BOOL, BOOL, BOOL),
27-
BOOL_XOR(BOOL, XOR, BOOL, BOOL, BOOL),
24+
BOOL_NOT(BOOL, NOT, BOOL),
25+
BOOL_AND(BOOL, AND, false, BOOL, BOOL),
26+
BOOL_OR(BOOL, OR, false, BOOL, BOOL),
27+
BOOL_XOR(BOOL, XOR, false, BOOL, BOOL),
2828
BOOL_ADD_STRING(BOOL, ADD, false, STRING, STRING),
2929
BOOL_CAT_STRING(BOOL, CAT, STRING, STRING),
3030
BOOL_EQUALS(BOOL, EQUALS, BOOL, BOOL, BOOL),

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(TypeID typeId) {
186-
return base.compare(typeId);
185+
public List<Comparator> comparators() {
186+
return base.comparators();
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 final Map<TypeID, Comparator> comparators = new HashMap<>();
29+
private final List<Comparator> comparators = new ArrayList<>();
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(TypeID typeId) {
129-
return Optional.ofNullable(comparators.get(typeId));
128+
public List<Comparator> comparators() {
129+
return comparators;
130130
}
131131

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

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

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.openzen.zenscript.codemodel.identifiers.instances.IteratorInstance;
1111
import org.openzen.zenscript.codemodel.type.TypeID;
1212

13+
import java.util.Collections;
1314
import java.util.List;
1415
import java.util.Optional;
1516
import java.util.stream.Collectors;
@@ -117,8 +118,8 @@ public Optional<SwitchMember> findSwitchMember(String name) {
117118
}
118119

119120
@Override
120-
public Optional<Comparator> compare(TypeID typeId) {
121-
return Optional.empty();
121+
public List<Comparator> comparators() {
122+
return Collections.emptyList();
122123
}
123124

124125
@Override

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.openzen.zenscript.parser.expression;
22

3+
import org.openzen.zencode.shared.CompileError;
34
import org.openzen.zenscript.codemodel.compilation.expression.AbstractCompilingExpression;
45
import org.openzen.zencode.shared.CodePosition;
56
import org.openzen.zenscript.codemodel.CompareType;
@@ -65,11 +66,25 @@ public Expression eval() {
6566
return notEquals.get().call(compiler, position, left, TypeID.NONE, right);
6667
}
6768
}
68-
return resolved.compare(right.eval().type)
69+
CastedExpression result = resolved.comparators()
70+
.stream()
6971
.map(comparator -> comparator.compare(compiler, position, left, right, this.type))
70-
.orElseGet(() -> compiler.at(position).invalid(
72+
.reduce((a, b) -> {
73+
if (a.isFailed()) return b;
74+
if (b.isFailed()) return a;
75+
76+
if (a.level.compareTo(b.level) == 0) {
77+
return new CastedExpression(a.level, compiler.at(position).invalid(CompileErrors.ambiguousComparison(a.value.type, b.value.type)));
78+
} else if (a.level.compareTo(b.level) > 0) {
79+
return a;
80+
} else {
81+
return b;
82+
}
83+
})
84+
.orElseGet(() -> CastedExpression.invalid(compiler.at(position).invalid(
7185
CompileErrors.noOperatorInType(left.type, OperatorType.COMPARE), //TODO Make error message more descriptive and include target type.
72-
BasicTypeID.BOOL));
86+
BasicTypeID.BOOL)));
87+
return result.value;
7388
}
7489

7590
@Override

Shared/src/main/java/org/openzen/zencode/shared/CompileExceptionCode.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public enum CompileExceptionCode {
7474
INVALID_TYPE_ARGUMENTS,
7575
INVALID_ARRAY_TYPE,
7676
INFERENCE_AMBIGUOUS,
77+
AMBIGUOUS_COMPARISON,
7778
NOT_AN_EXPRESSION,
7879
INCOMPLETE_HEADER,
7980
INCOMPLETE_IMPLEMENTATION,

StdLibs

0 commit comments

Comments
 (0)