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

Introducing $*RAT-UPGRADE-CLASS #4299

Merged
merged 5 commits into from Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/core.c/Exception.pm6
Expand Up @@ -91,6 +91,10 @@ my class Exception {

method is-compile-time(--> False) { }
method message() { ... }

method UPGRADE-RAT(Int $nu, Int $de) {
die "Upgrading of Rat $nu / $de not allowed"
}
}

my class X::SecurityPolicy is Exception {}
Expand Down Expand Up @@ -398,6 +402,11 @@ my class CX::Take does X::Control {
}
my class CX::Warn does X::Control {
has $.message;

method UPGRADE-RAT(Int $nu, Int $de) is raw {
warn "Downgrading Rat $nu / $de to Num";
nqp::p6box_n(nqp::div_In($nu,$de))
}
}
my class CX::Succeed does X::Control {
method message() { "<succeed control exception>" }
Expand Down
3 changes: 3 additions & 0 deletions src/core.c/Failure.pm6
Expand Up @@ -130,6 +130,9 @@ my class Failure is Nil {
method STORE(Failure:D: *@) {
self!throw()
}
method UPGRADE-RAT(Int $nu, Int $de) {
Failure.new("Upgrading of Rat $nu / $de not allowed")
}
}

proto sub fail(|) {*};
Expand Down
4 changes: 4 additions & 0 deletions src/core.c/Num.pm6
Expand Up @@ -283,6 +283,10 @@ my class Num does Real { # declared in BOOTSTRAP
?? $i
!! self
}

method UPGRADE-RAT(\nu, \de) is raw {
nqp::p6box_n(nqp::div_In(nu,de)) # downgrade to float
}
}

my constant tau = 6.28318_53071_79586_476e0;
Expand Down
11 changes: 11 additions & 0 deletions src/core.c/Rat.pm6
Expand Up @@ -40,6 +40,13 @@ my class FatRat is Cool does Rational[Int, Int] {
multi method raku(FatRat:D: --> Str:D) {
"FatRat.new($!numerator, $!denominator)";
}

method UPGRADE-RAT(\nu, \de) is raw {
nqp::p6bindattrinvres(
nqp::p6bindattrinvres(nqp::create(FatRat),FatRat,'$!numerator',nu),
FatRat,'$!denominator',de
)
}
}

# NORMALIZE two integer values and create a Rat/FatRat/float from them.
Expand Down Expand Up @@ -72,6 +79,9 @@ sub DIVIDE_NUMBERS(
)
}

# Initialize the $*RAT-UPGRADE-POLICY dynamic var so that it can be used
PROCESS::<$RAT-UPGRADE-POLICY> = Num;
lizmat marked this conversation as resolved.
Show resolved Hide resolved

# ALL RATIONALS MUST BE NORMALIZED, however in some operations we cannot
# ever get a non-normalized Rational, if we start with a normalized Rational.
# For such cases, we can use this routine, to bypass normalization step,
Expand All @@ -83,6 +93,7 @@ multi sub CREATE_RATIONAL_FROM_INTS(Int:D $nu, Int:D $de, Any, Any) is raw {
nqp::p6bindattrinvres(nqp::create(Rat),Rat,'$!numerator',$nu),
Rat,'$!denominator',$de
)
!! $*RAT-UPGRADE-POLICY.UPGRADE-RAT(nu, de)
lizmat marked this conversation as resolved.
Show resolved Hide resolved
!! nqp::p6box_n(nqp::div_In($nu,$de)) # downgrade to float
Copy link
Contributor

Choose a reason for hiding this comment

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

Syntax error here, looks like you meant to replace the nqp::p6box_n(nqp::div_In($nu,$de)) # downgrade to float with $*RAT-UPGRADE-POLICY.UPGRADE-RAT(nu, de), but accidentally just copied it in.

}

Expand Down
1 change: 1 addition & 0 deletions t/08-performance/05-processkeys.t
Expand Up @@ -13,6 +13,7 @@ my $allowed = (
Q{$PID},
Q{$RAKU},
Q{$RAKUDO_MODULE_DEBUG},
Q{$RAT-UPGRADE-POLICY},
Q{$REPO},
Q{$SCHEDULER},
Q{$SPEC},
Expand Down