Skip to content

Commit

Permalink
- Coded comparators a little differently
Browse files Browse the repository at this point in the history
- Fixed bool operators breaking the entire bool type
  • Loading branch information
stanhebben committed Feb 23, 2024
1 parent 9c95d98 commit 8645f2b
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public static CompileError ambiguousType(List<TypeID> candidates) {
return new CompileError(CompileExceptionCode.INFERENCE_AMBIGUOUS, "Type inference ambiguity, possible types: " + possibleTypes);
}

public static CompileError ambiguousComparison(TypeID a, TypeID b) {
return new CompileError(CompileExceptionCode.AMBIGUOUS_COMPARISON, "Ambiguous comparison, not sure to compare as " + a + " or " + b);
}

public static CompileError noMemberInType(TypeID type, String name) {
return new CompileError(CompileExceptionCode.NO_SUCH_MEMBER, "No member " + name + " in type " + type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.openzen.zenscript.codemodel.identifiers.instances.IteratorInstance;
import org.openzen.zenscript.codemodel.type.TypeID;

import java.util.List;
import java.util.Optional;

public interface ResolvedType {
Expand Down Expand Up @@ -66,7 +67,7 @@ default boolean canCastImplicitlyTo(TypeID target) {

Optional<SwitchMember> findSwitchMember(String name);

Optional<Comparator> compare(TypeID typeId);
List<Comparator> comparators();

Optional<IteratorInstance> findIterator(int variables);

Expand Down Expand Up @@ -100,7 +101,7 @@ interface StaticField {

@FunctionalInterface
interface Comparator {
Expression compare(
CastedExpression compare(
ExpressionCompiler compiler,
CodePosition position,
Expression left,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import org.openzen.zenscript.codemodel.FunctionParameter;
import org.openzen.zenscript.codemodel.OperatorType;
import org.openzen.zenscript.codemodel.compilation.CastedEval;
import org.openzen.zenscript.codemodel.compilation.CastedExpression;
import org.openzen.zenscript.codemodel.expression.CompareExpression;
import org.openzen.zenscript.codemodel.expression.Expression;
import org.openzen.zenscript.codemodel.identifiers.MethodID;
import org.openzen.zenscript.codemodel.identifiers.instances.FieldInstance;
import org.openzen.zenscript.codemodel.identifiers.instances.MethodInstance;
Expand Down Expand Up @@ -583,12 +585,19 @@ private static void setup(MemberSet.Builder builder, BasicTypeID type) {

private static void comparator(MemberSet.Builder builder, BuiltinMethodSymbol method, TypeID ofType) {
MethodInstance comparator = new MethodInstance(method);
builder.comparator(ofType, ((compiler, position, left, right, type) -> new CompareExpression(
position,
left,
right.cast(CastedEval.implicit(compiler, position, ofType)).value,
comparator,
type)));
builder.comparator(((compiler, position, left, right, type) -> {
CastedExpression casted = right.cast(CastedEval.implicit(compiler, position, ofType));
if (casted.isFailed())
return casted;

Expression value = new CompareExpression(
position,
left,
casted.value,
comparator,
type);
return new CastedExpression(casted.level, value);
}));
}

private static MethodInstance[] getWideningMethodInstances(BuiltinMethodSymbol method) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import static org.openzen.zenscript.codemodel.type.BasicTypeID.*;

public enum BuiltinMethodSymbol implements MethodSymbol {
BOOL_NOT(BOOL, NOT, BOOL, BOOL),
BOOL_AND(BOOL, AND, BOOL, BOOL, BOOL),
BOOL_OR(BOOL, OR, BOOL, BOOL, BOOL),
BOOL_XOR(BOOL, XOR, BOOL, BOOL, BOOL),
BOOL_NOT(BOOL, NOT, BOOL),
BOOL_AND(BOOL, AND, false, BOOL, BOOL),
BOOL_OR(BOOL, OR, false, BOOL, BOOL),
BOOL_XOR(BOOL, XOR, false, BOOL, BOOL),
BOOL_ADD_STRING(BOOL, ADD, false, STRING, STRING),
BOOL_CAT_STRING(BOOL, CAT, STRING, STRING),
BOOL_EQUALS(BOOL, EQUALS, BOOL, BOOL, BOOL),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ public Optional<SwitchMember> findSwitchMember(String name) {
}

@Override
public Optional<Comparator> compare(TypeID typeId) {
return base.compare(typeId);
public List<Comparator> comparators() {
return base.comparators();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static Builder create() {
private final Map<String, SwitchMember> switchMembers = new HashMap<>();
private final Map<String, TypeSymbol> innerTypes = new HashMap<>();
private final List<IteratorInstance> iterators = new ArrayList<>();
private final Map<TypeID, Comparator> comparators = new HashMap<>();
private final List<Comparator> comparators = new ArrayList<>();

@Override
public StaticCallable getConstructor() {
Expand Down Expand Up @@ -125,8 +125,8 @@ public Optional<SwitchMember> findSwitchMember(String name) {
}

@Override
public Optional<Comparator> compare(TypeID typeId) {
return Optional.ofNullable(comparators.get(typeId));
public List<Comparator> comparators() {
return comparators;
}

@Override
Expand Down Expand Up @@ -200,8 +200,8 @@ public Builder inner(TypeSymbol type) {
return this;
}

public Builder comparator(TypeID typeId, Comparator comparator) {
target.comparators.put(typeId, comparator);
public Builder comparator(Comparator comparator) {
target.comparators.add(comparator);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.openzen.zenscript.codemodel.identifiers.instances.IteratorInstance;
import org.openzen.zenscript.codemodel.type.TypeID;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -117,8 +118,8 @@ public Optional<SwitchMember> findSwitchMember(String name) {
}

@Override
public Optional<Comparator> compare(TypeID typeId) {
return Optional.empty();
public List<Comparator> comparators() {
return Collections.emptyList();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.openzen.zenscript.parser.expression;

import org.openzen.zencode.shared.CompileError;
import org.openzen.zenscript.codemodel.compilation.expression.AbstractCompilingExpression;
import org.openzen.zencode.shared.CodePosition;
import org.openzen.zenscript.codemodel.CompareType;
Expand Down Expand Up @@ -65,11 +66,25 @@ public Expression eval() {
return notEquals.get().call(compiler, position, left, TypeID.NONE, right);
}
}
return resolved.compare(right.eval().type)
CastedExpression result = resolved.comparators()
.stream()
.map(comparator -> comparator.compare(compiler, position, left, right, this.type))
.orElseGet(() -> compiler.at(position).invalid(
.reduce((a, b) -> {
if (a.isFailed()) return b;
if (b.isFailed()) return a;

if (a.level.compareTo(b.level) == 0) {
return new CastedExpression(a.level, compiler.at(position).invalid(CompileErrors.ambiguousComparison(a.value.type, b.value.type)));
} else if (a.level.compareTo(b.level) > 0) {
return a;
} else {
return b;
}
})
.orElseGet(() -> CastedExpression.invalid(compiler.at(position).invalid(
CompileErrors.noOperatorInType(left.type, OperatorType.COMPARE), //TODO Make error message more descriptive and include target type.
BasicTypeID.BOOL));
BasicTypeID.BOOL)));
return result.value;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ public enum CompileExceptionCode {
INVALID_TYPE_ARGUMENTS,
INVALID_ARRAY_TYPE,
INFERENCE_AMBIGUOUS,
AMBIGUOUS_COMPARISON,
NOT_AN_EXPRESSION,
INCOMPLETE_HEADER,
INCOMPLETE_IMPLEMENTATION,
Expand Down
2 changes: 1 addition & 1 deletion StdLibs

0 comments on commit 8645f2b

Please sign in to comment.