Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Small_Integer and Big_Integer types #7636

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.enso.interpreter.node.expression.builtin.number.smallInteger;
package org.enso.interpreter.node.expression.builtin.number.integer;

import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.Specialization;
import com.oracle.truffle.api.nodes.Node;
Expand All @@ -11,11 +12,11 @@
import org.enso.interpreter.runtime.error.PanicException;
import org.enso.interpreter.runtime.number.EnsoBigInteger;

@BuiltinMethod(type = "Small_Integer", name = "+", description = "Addition of numbers.")
@BuiltinMethod(type = "Integer", name = "+", description = "Addition of numbers.")
public abstract class AddNode extends Node {
private @Child ToEnsoNumberNode toEnsoNumberNode = ToEnsoNumberNode.create();

abstract Object execute(long self, Object that);
abstract Object execute(Object self, Object that);

static AddNode build() {
return AddNodeGen.create();
Expand All @@ -26,25 +27,36 @@ long doLong(long self, long that) {
return Math.addExact(self, that);
}

@Specialization
@Specialization(replaces = "doLong")
Object doOverflow(long self, long that) {
return toEnsoNumberNode.execute(BigIntegerOps.add(self, that));
}

@Specialization
double doDouble(long self, double that) {
Object doDouble(long self, double that) {
return self + that;
}

@TruffleBoundary
@Specialization
Object doBigIntegers(EnsoBigInteger self, EnsoBigInteger that) {
return self.asBigInteger().add(that.asBigInteger());
}

@Specialization
Object doBigInteger(long self, EnsoBigInteger that) {
Object doLongBigInteger(long self, EnsoBigInteger that) {
return toEnsoNumberNode.execute(BigIntegerOps.add(that.getValue(), self));
}

@Specialization
Object doBigIntegerLong(EnsoBigInteger self, long that) {
return toEnsoNumberNode.execute(BigIntegerOps.add(self.getValue(), that));
}

@Fallback
Object doOther(long self, Object that) {
Object doOther(Object self, Object that) {
Builtins builtins = EnsoContext.get(this).getBuiltins();
var number = builtins.number().getNumber();
var number = builtins.number().getInteger();
throw new PanicException(builtins.error().makeTypeError(number, that, "that"), this);
}
}
7 changes: 7 additions & 0 deletions test/Tests/src/Data/Numbers_Spec.enso
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,13 @@ spec =
Integer.parse "-101021010" 2 . should_fail_with Number_Parse_Error
Integer.parse "123" 128 . should_fail_with Number_Parse_Error

Test.specify "should be able to invoke methods on Integer via static method call" <|
Integer.+ 1 2 . should_equal 3
Integer.+ 1 2.5 . should_equal 3.5
Test.expect_panic_with (Integer.+ 1.5 1) Type_Error
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This results in Type_Error because self is 1.5 which is Decimal, and not Integer

Test.expect_panic_with (Integer.+ 1.5 2.5) Type_Error
Test.expect_panic_with (Integer.+ 1 "hello") Type_Error

Test.group "Decimals" <|

Test.specify "should exist and expose basic arithmetic operations" <|
Expand Down
Loading