Skip to content

Commit

Permalink
Handle Type_Error
Browse files Browse the repository at this point in the history
  • Loading branch information
Akirathan committed Sep 7, 2023
1 parent 627dd60 commit 6adb207
Show file tree
Hide file tree
Showing 20 changed files with 67 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.enso.interpreter.node.expression.builtin.number.integer;

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.dsl.BuiltinMethod;
Expand Down Expand Up @@ -35,4 +36,9 @@ Object doLongOverflow(long self) {
Object doBigInt(EnsoBigInteger self) {
return toEnsoNumberNode.execute(BigIntegerOps.abs(self.getValue()));
}

@Fallback
Object doOther(Object self) {
return IntegerUtils.throwTypeErrorIfNotInt(self, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "+", description = "Addition of numbers.")
Expand Down Expand Up @@ -60,8 +57,6 @@ Object doBigIntegerLong(EnsoBigInteger self, long that) {

@Fallback
Object doOther(Object self, Object that) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
var number = builtins.number().getInteger();
throw new PanicException(builtins.error().makeTypeError(number, that, "that"), this);
return IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "bit_and", description = "Bitwise and.")
Expand Down Expand Up @@ -43,8 +40,6 @@ Object doBigIntBigInt(EnsoBigInteger self, EnsoBigInteger that) {

@Fallback
Object doOther(Object self, Object that) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
var integer = builtins.number().getInteger();
throw new PanicException(builtins.error().makeTypeError(integer, that, "that"), this);
return IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "bit_not", description = "Bitwise negation.")
Expand All @@ -31,8 +28,6 @@ EnsoBigInteger doBigInteger(EnsoBigInteger self) {

@Fallback
Object doOther(Object self) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
var integer = builtins.number().getInteger();
throw new PanicException(builtins.error().makeTypeError(integer, self, "self"), this);
return IntegerUtils.throwTypeErrorIfNotInt(self, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "bit_or", description = "Bitwise or.")
Expand Down Expand Up @@ -43,8 +40,6 @@ Object doBigInteger(EnsoBigInteger self, EnsoBigInteger that) {

@Fallback
Object doOther(Object self, Object that) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
var integer = builtins.number().getInteger();
throw new PanicException(builtins.error().makeTypeError(integer, that, "that"), this);
return IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.error.DataflowError;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@ImportStatic(BigIntegerOps.class)
Expand Down Expand Up @@ -128,9 +126,7 @@ Object doBigIntThat(EnsoBigInteger self, EnsoBigInteger that) {

@Fallback
Object doOther(Object self, Object that) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
var integer = builtins.number().getInteger();
throw new PanicException(builtins.error().makeTypeError(integer, that, "that"), this);
return IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
}

boolean hasFreeBitsLeftShift(long number, long shift) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "bit_shift_r", description = "Bitwise right-shift.")
Expand Down Expand Up @@ -52,8 +49,6 @@ Object doBigInteger(

@Fallback
Object doOther(Object self, Object that) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
var integer = builtins.number().getInteger();
throw new PanicException(builtins.error().makeTypeError(integer, that, "that"), this);
return IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "bit_xor", description = "Bitwise exclusive or.")
Expand Down Expand Up @@ -43,8 +40,6 @@ Object doBigInteger(EnsoBigInteger self, EnsoBigInteger that) {

@Fallback
Object doOther(Object self, Object that) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
var integer = builtins.number().getInteger();
throw new PanicException(builtins.error().makeTypeError(integer, that, "that"), this);
return IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "ceil", description = "Small integer ceiling.")
Expand All @@ -29,8 +26,6 @@ EnsoBigInteger doBigInt(EnsoBigInteger self) {

@Fallback
Object doOther(Object self) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
var integer = builtins.number().getInteger();
throw new PanicException(builtins.error().makeTypeError(integer, self, "self"), this);
return IntegerUtils.throwTypeErrorIfNotInt(self, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.error.DataflowError;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "div", description = "Division of numbers.")
Expand Down Expand Up @@ -57,8 +55,6 @@ Object doBigInteger(EnsoBigInteger self, EnsoBigInteger that) {

@Fallback
Object doOther(Object self, Object that) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
var integer = builtins.number().getInteger();
throw new PanicException(builtins.error().makeTypeError(integer, self, "self"), this);
return IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "/", description = "Division of numbers.")
Expand Down Expand Up @@ -50,8 +47,6 @@ static DivideNode build() {

@Fallback
double doOther(Object self, Object that) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
var number = builtins.number().getNumber();
throw new PanicException(builtins.error().makeTypeError(number, self, "self"), this);
throw IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "floor", description = "Small integer floor.")
Expand All @@ -30,8 +27,6 @@ EnsoBigInteger doBigInt(EnsoBigInteger self) {

@Fallback
Object doOther(Object self) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
var integer = builtins.number().getInteger();
throw new PanicException(builtins.error().makeTypeError(integer, self, "self"), this);
return IntegerUtils.throwTypeErrorIfNotInt(self, this);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package org.enso.interpreter.node.expression.builtin.number.integer;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.library.dispatch.TypesLibrary;

public final class IntegerUtils {
private IntegerUtils() {}

@TruffleBoundary
static PanicException throwTypeErrorIfNotInt(Object self, Object that, Node node) {
var builtins = EnsoContext.get(node).getBuiltins();
var intType = builtins.number().getInteger();
var selfType = TypesLibrary.getUncached().getType(self);
if (selfType != intType) {
return new PanicException(builtins.error().makeTypeError(intType, self, "self"), node);
} else {
return new PanicException(builtins.error().makeTypeError(intType, that, "that"), node);
}
}

@TruffleBoundary
static PanicException throwTypeErrorIfNotInt(Object self, Node node) {
var builtins = EnsoContext.get(node).getBuiltins();
var intType = builtins.number().getInteger();
return new PanicException(builtins.error().makeTypeError(intType, self, "self"), node);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.error.DataflowError;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "%", description = "Modulo division of numbers.")
Expand Down Expand Up @@ -70,8 +68,6 @@ Object doBigInteger(EnsoBigInteger self, EnsoBigInteger that) {

@Fallback
Object doOther(Object self, Object that) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
var number = builtins.number().getNumber();
throw new PanicException(builtins.error().makeTypeError(number, that, "that"), this);
return IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "*", description = "Multiplication of numbers.")
Expand Down Expand Up @@ -58,8 +55,6 @@ Object doBigInteger(EnsoBigInteger self, EnsoBigInteger that) {

@Fallback
Object doOther(Object self, Object that) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
var number = builtins.number().getNumber();
throw new PanicException(builtins.error().makeTypeError(number, that, "that"), this);
return IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.enso.interpreter.node.expression.builtin.number.integer;

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.dsl.BuiltinMethod;
Expand Down Expand Up @@ -31,4 +32,9 @@ Object doBigInt(EnsoBigInteger self) {
Object doOverflow(long self) {
return toEnsoNumberNode.execute(BigIntegerOps.negate(self));
}

@Fallback
Object doOther(Object self) {
return IntegerUtils.throwTypeErrorIfNotInt(self, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "^", description = "Exponentiation of numbers.")
Expand Down Expand Up @@ -91,8 +88,6 @@ private static EnsoBigInteger toBigInteger(long self) {

@Fallback
Object doOther(Object self, Object that) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
var number = builtins.number().getNumber();
throw new PanicException(builtins.error().makeTypeError(number, that, "that"), this);
return IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
import org.enso.interpreter.dsl.BuiltinMethod;
import org.enso.interpreter.node.expression.builtin.number.utils.BigIntegerOps;
import org.enso.interpreter.node.expression.builtin.number.utils.ToEnsoNumberNode;
import org.enso.interpreter.runtime.EnsoContext;
import org.enso.interpreter.runtime.builtin.Builtins;
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Integer", name = "-", description = "Subtraction of numbers.")
Expand Down Expand Up @@ -58,8 +55,6 @@ Object doBigInteger(EnsoBigInteger self, EnsoBigInteger that) {

@Fallback
Object doOther(Object self, Object that) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
var number = builtins.number().getNumber();
throw new PanicException(builtins.error().makeTypeError(number, that, "that"), this);
return IntegerUtils.throwTypeErrorIfNotInt(self, that, this);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.enso.interpreter.node.expression.builtin.number.integer;

import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
import org.enso.interpreter.dsl.BuiltinMethod;
Expand All @@ -26,4 +27,9 @@ public static ToDecimalNode build() {
double doBigInt(EnsoBigInteger self) {
return BigIntegerOps.toDouble(self.getValue());
}

@Fallback
Object doOther(Object self) {
return IntegerUtils.throwTypeErrorIfNotInt(self, this);
}
}
Loading

0 comments on commit 6adb207

Please sign in to comment.