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

Added compare_to to True/False #3317

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
- [General improved Vector performance and new `Vector.each_with_index`,
`Vector.fold_with_index` and `Vector.take` methods.][3236]
- [Implemented new `Text.insert` method][3311]
- [Implemented `Bool.compare_to` method][3317]

[debug-shortcuts]:
https://github.com/enso-org/enso/blob/develop/app/gui/docs/product/shortcuts.md#debug
Expand Down Expand Up @@ -99,6 +100,7 @@
[3310]: https://github.com/enso-org/enso/pull/3310
[3236]: https://github.com/enso-org/enso/pull/3236
[3311]: https://github.com/enso-org/enso/pull/3311
[3317]: https://github.com/enso-org/enso/pull/3317

#### Enso Compiler

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package org.enso.interpreter.node.expression.builtin.bool;

import com.oracle.truffle.api.CompilerDirectives;
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.Context;
import org.enso.interpreter.runtime.builtin.Ordering;
import org.enso.interpreter.runtime.callable.atom.Atom;
import org.enso.interpreter.runtime.error.PanicException;

@BuiltinMethod(
type = "Boolean",
name = "compare_to",
description = "Comparison for Booleans.")
public abstract class CompareToNode extends Node {
static CompareToNode build() { return CompareToNodeGen.create(); }

abstract Atom execute(Boolean _this, Object that);

@Specialization
Atom doBoolean(
Boolean _this,
Boolean that) {
Ordering ordering = Context.get(this).getBuiltins().ordering();
if (_this == that) {
return ordering.newEqual();
} else if (_this) {
return ordering.newGreater();
} else {
return ordering.newLess();
}
}

@Specialization
Atom doOther(
Boolean _this, Object that) {
CompilerDirectives.transferToInterpreter();
var bool = Context.get(this).getBuiltins().bool().getBool().newInstance();
var typeError = Context.get(this).getBuiltins().error().makeTypeError(that, bool, "that");
throw new PanicException(typeError, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public Bool(Language language, ModuleScope scope) {
scope.registerMethod(bool, "if_then_else", IfThenElseMethodGen.makeFunction(language));
scope.registerMethod(bool, "if_then", IfThenMethodGen.makeFunction(language));
scope.registerMethod(bool, "to_text", ToTextMethodGen.makeFunction(language));
scope.registerMethod(bool, "compare_to", CompareToMethodGen.makeFunction(language));
scope.registerMethod(bool, "&&", AndMethodGen.makeFunction(language));
scope.registerMethod(bool, "||", OrMethodGen.makeFunction(language));
scope.registerMethod(bool, "==", EqualsMethodGen.makeFunction(language));
Expand Down
18 changes: 18 additions & 0 deletions test/Tests/src/Data/Bool_Spec.enso
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from Standard.Base import all

import Standard.Test
from Standard.Base.Data.Ordering import Equal, Less, Greater

spec =
Test.group "Booleans" <|
Test.specify "should allow converting Bools to Text values" <|
True.to_text . should_equal "True"
False.to_text . should_equal "False"

Test.specify "should allow for comparing Bools" <|
hubertp marked this conversation as resolved.
Show resolved Hide resolved
True.compare_to True . should_equal Equal
False.compare_to False . should_equal Equal
True.compare_to False . should_equal Greater
False.compare_to True . should_equal Less

main = Test.Suite.run_main here.spec