-
Notifications
You must be signed in to change notification settings - Fork 2
/
NQPSetting.pm
210 lines (188 loc) · 4.27 KB
/
NQPSetting.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
knowhow NQPStr is repr('P6str') {
method new() {
nqp::instance_of(self.WHAT);
}
method Bool() {
nqp::logical_not_int(nqp::equal_strs(self, "")) &&
nqp::logical_not_int(nqp::equal_strs(self, "0"))
}
method Numeric() {
nqp::coerce_str_to_num(self, NQPNum)
}
method Str() {
self
}
method Stringy() {
self
}
}
knowhow NQPInt is repr('P6int') {
method new() {
nqp::instance_of(self.WHAT);
}
method Bool() {
nqp::logical_not_int(nqp::equal_ints(self, 0))
}
method Int() {
self
}
method Num() {
nqp::coerce_int_to_num(self, NQPNum)
}
method Numeric() {
self
}
method Str() {
nqp::coerce_int_to_str(self, NQPStr)
}
method Stringy() {
nqp::coerce_int_to_str(self, NQPStr)
}
}
knowhow NQPNum is repr('P6num') {
method new() {
nqp::instance_of(self.WHAT);
}
method Bool() {
nqp::logical_not_int(nqp::equal_nums(self, 0.0))
}
method Int() {
nqp::coerce_num_to_int(self, NQPStr)
}
method Num() {
self
}
method Numeric() {
self
}
method Str() {
nqp::coerce_num_to_str(self, NQPStr)
}
method Stringy() {
nqp::coerce_num_to_str(self, NQPStr)
}
}
knowhow NQPList is repr('P6list') {
method new() {
nqp::instance_of(self.WHAT)
}
method elems() {
nqp::lllist_elems(self)
}
method Numeric() {
self.elems
}
method at_pos($idx) {
nqp::lllist_get_at_pos(self, $idx.Int)
}
}
knowhow NQPArray is repr('P6list') {
method new() {
nqp::instance_of(self.WHAT)
}
method Numeric() {
self.elems
}
method elems() {
nqp::lllist_elems(self)
}
method at_pos($idx) {
nqp::lllist_get_at_pos(self, $idx.Int)
}
}
# This is a little bit of a cheat. We only really need to keep
# hold of a name for the most basic attribute class, so we just
# use the string representation.
knowhow KnowHOWAttribute is repr('P6str') {
method new(:$name) {
nqp::box_str(nqp::unbox_str($name), KnowHOWAttribute)
}
method name() {
nqp::box_str(nqp::unbox_str(self), NQPStr)
}
}
## XXX Need coercive Any fallbacks too.
proto sub &infix:<==>($x, $y) {
nqp::multi_dispatch_over_lexical_candidates("&infix:<==>");
}
multi sub &infix:<==>(NQPInt $x, NQPInt $y) {
nqp::equal_ints($x, $y)
}
multi sub &infix:<==>(NQPNum $x, NQPNum $y) {
nqp::equal_nums($x, $y)
}
proto sub &infix:<!=>($x, $y) {
nqp::multi_dispatch_over_lexical_candidates("&infix:<!=>");
}
multi sub &infix:<!=>(NQPInt $x, NQPInt $y) {
nqp::logical_not_int(nqp::equal_ints($x, $y))
}
multi sub &infix:<!=>(NQPNum $x, NQPNum $y) {
nqp::logical_not_int(nqp::equal_nums($x, $y))
}
sub &infix:<eq>($x, $y) {
nqp::equal_strs($x.Str, $y.Str)
}
sub &infix:<ne>($x, $y) {
!nqp::equal_strs($x.Str, $y.Str)
}
sub &infix:<=:=>($x, $y) {
nqp::equal_refs($x, $y)
}
sub &prefix:<!>($x) {
nqp::logical_not_int($x.Bool)
}
sub &prefix:<?>($x) {
$x.Bool
}
sub &prefix:<~>($x) {
$x.Stringy
}
sub &prefix:<+>($x) {
$x.Numeric
}
proto sub &infix:<+>($x, $y) {
nqp::multi_dispatch_over_lexical_candidates("&infix:<+>");
}
multi sub &infix:<+>(NQPInt $x, NQPInt $y) {
nqp::add_int($x, $y);
}
proto sub &infix:<->($x, $y) {
nqp::multi_dispatch_over_lexical_candidates("&infix:<->");
}
multi sub &infix:<->(NQPInt $x, NQPInt $y) {
nqp::sub_int($x, $y);
}
proto sub &infix:<*>($x, $y) {
nqp::multi_dispatch_over_lexical_candidates("&infix:<*>");
}
multi sub &infix:<*>(NQPInt $x, NQPInt $y) {
nqp::mul_int($x, $y);
}
proto sub &infix:</>($x, $y) {
nqp::multi_dispatch_over_lexical_candidates("&infix:</>");
}
multi sub &infix:</>(NQPInt $x, NQPInt $y) {
nqp::div_int($x, $y);
}
proto sub &infix:<%>($x, $y) {
nqp::multi_dispatch_over_lexical_candidates("&infix:<%>");
}
multi sub &infix:<%>(NQPInt $x, NQPInt $y) {
nqp::mod_int($x, $y);
}
sub &infix:<~>($x, $y) {
nqp::concat($x.Str, $y.Str);
}
# For tests.
my $count := NQPInt.new();
sub plan($n) {
print("1..");
say($n);
}
sub ok($check, $diag) {
$count := $count + 1;
unless $check { print("not ") }
print("ok ");
say($count);
}