Skip to content

Commit

Permalink
Avoiding duplications when Number & Integer & Float and co.
Browse files Browse the repository at this point in the history
  • Loading branch information
JaroslavTulach committed Jan 4, 2025
1 parent 98ebc39 commit a9f57f0
Showing 1 changed file with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.enso.interpreter.node.typecheck;

import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.ExplodeLoop;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.enso.interpreter.node.ExpressionNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.data.EnsoMultiValue;
import org.enso.interpreter.runtime.data.Type;
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;
Expand Down Expand Up @@ -37,12 +39,26 @@ Object executeCheckOrConversion(VirtualFrame frame, Object value, ExpressionNode
var values = new Object[checks.length];
var valueTypes = new Type[checks.length];
var at = 0;
var integers = 0;
var floats = 0;
for (var n : checks) {
var result = n.executeCheckOrConversion(frame, value, expr);
if (result == null) {
return null;
}
valueTypes[at] = types.getType(result);
var t = types.getType(result);
var ctx = EnsoContext.get(this);
if (ctx.getBuiltins().number().getInteger() == t) {
if (++integers > 1) {
continue;
}
}
if (ctx.getBuiltins().number().getFloat() == t) {
if (++floats > 1) {
continue;
}
}
valueTypes[at] = t;
if (result instanceof EnsoMultiValue emv) {
result =
EnsoMultiValue.CastToNode.getUncached()
Expand All @@ -54,6 +70,17 @@ Object executeCheckOrConversion(VirtualFrame frame, Object value, ExpressionNode
values[at] = result;
at++;
}
if (at != checks.length) {
// request for Number & Integer may yield two integers collision
// request for Number & Float may yield two floats collision
// request for Number & Integer & Float must yield one collision
//
// people shouldn't be doing such things but the code must be
// ready for that - switching to interpreter without optimization
CompilerDirectives.transferToInterpreter();
values = Arrays.copyOf(values, at);
valueTypes = Arrays.copyOf(valueTypes, at);
}
return newNode.newValue(valueTypes, valueTypes.length, values);
}

Expand Down

0 comments on commit a9f57f0

Please sign in to comment.