/
Int.pm
99 lines (78 loc) · 2.12 KB
/
Int.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# XXX Should be my...
our sub upgrade_to_num_if_needed($test) {
Q:PIR {
.local num test
$P0 = find_lex '$test'
test = $P0
if test > 2147483647.0 goto upgrade
if test < -2147483648.0 goto upgrade
$I0 = test
.return ($I0)
upgrade:
.return (test)
}
}
augment class Int does Real {
method Bridge() {
self.Num;
}
our Int method Int() { self; }
our Int method Rat(Real $epsilon = 1.0e-6) { self / 1; }
our Num method Num() {
pir::box__PN(pir::set__NP(self));
}
method sign(Int $x:) {
$x.notdef ?? Mu !! $x <=> 0;
}
}
multi sub infix:«cmp»(Int $a, Int $b) {
pir::cmp__III($a, $b);
}
multi sub infix:«<=>»(Int $a, Int $b) {
pir::cmp__III($a, $b);
}
multi sub infix:«==»(Int $a, Int $b) {
pir::iseq__III( $a, $b) ?? True !! False
}
multi sub infix:«!=»(Int $a, Int $b) {
pir::isne__III( $a, $b) ?? True !! False
}
multi sub infix:«<»(Int $a, Int $b) {
pir::islt__III( $a, $b) ?? True !! False
}
multi sub infix:«<=»(Int $a, Int $b) {
pir::isle__III( $a, $b) ?? True !! False
}
multi sub infix:«>»(Int $a, Int $b) {
pir::isgt__III( $a, $b) ?? True !! False
}
multi sub infix:«>=»(Int $a, Int $b) {
pir::isge__III( $a, $b) ?? True !! False
}
# Should pull along the other Int comparison operators at some point,
# but this is a great start.
our multi sub prefix:<->(Int $a) {
upgrade_to_num_if_needed(pir::neg__NN($a))
}
our multi sub infix:<+>(Int $a, Int $b) {
upgrade_to_num_if_needed(pir::add__NNN($a, $b))
}
our multi sub infix:<->(Int $a, Int $b) {
upgrade_to_num_if_needed(pir::sub__NNN($a, $b))
}
our multi sub infix:<*>(Int $a, Int $b) {
upgrade_to_num_if_needed(pir::mul__NNN($a, $b))
}
our multi sub infix:<div>(Int $a, Int $b) {
upgrade_to_num_if_needed(pir::fdiv__NNN($a, $b));
}
our multi sub infix:<%>(Int $a, Int $b) {
upgrade_to_num_if_needed(pir::mod__NNN($a, $b))
}
our multi sub infix:<**>(Int $a, Int $b) {
if $b >= 0 {
upgrade_to_num_if_needed(pir::pow__NNN($a, $b))
} else {
pir::pow__NNN($a, $b)
}
}