/
setting
549 lines (475 loc) · 16.8 KB
/
setting
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
# vim: ft=perl6
# XXX fakes out STD.pm6 into doing more or less the right thing
# need to understand this better
my class CORE { ... }
# We have to do this directly due to the circularity saw. Same
# reason forces uncontainerized .NET values.
#
# class ClassHOW {
# has DynMetaObject $.meta-object;
# }
#
# Due to concerns of screwing up composition, adding new superclasses, roles,
# or attributes in an augment is not allowed at this time. Methods only.
#
# to create a class:
# BEGIN my $Foo_HOW ::= ClassHOW.new("Foo");
# BEGIN my $Foo;
# BEGIN {
# $Foo_HOW.add-super(...);
# $Foo_HOW.add-role(...);
# $Foo_HOW.add-method("bar", anon method bar { ... });
# $Foo_HOW.compose;
# $Foo ::= $Foo_HOW.create-protoobject;
# }
#
# to augment:
# BEGIN { $Foo_HOW.add-method("baz", anon method baz { ... }); }
my class ClassHOW { ... }
PRE-INIT {
# ClassHOW.new($name) --> meta class instance
sub new { Q:CgOp {
(prog
[lextypes $mo DynMetaObject $self DynObject]
[l $mo (rawnew DynMetaObject (unwrap String (@ (pos 1))))]
[l $self (rawnew DynObject (getfield klass
(cast DynObject (@ (pos 0)))))]
[setfield how (l $mo) (l $self)]
[setindex meta-object (getfield slots (l $self))
(nsw (rawnew CLRImportObject (l $mo)))]
[ns (l $self)])
} }
# $how.add-super($how)
sub add-super { Q:CgOp { (prog
[rawcall
(getfield superclasses (unwrap DynMetaObject
(getattr meta-object (@ (pos 0))))) Add
(unwrap DynMetaObject (getattr meta-object (@ (pos 1))))]
[null Variable])
} }
# $how.add-method($name, $sub)
sub add-method { Q:CgOp {
(prog
[setindex (unwrap String (@ (pos 1)))
(getfield local (unwrap DynMetaObject (getattr meta-object
(@ (pos 0)))))
(@ (pos 2))]
[null Variable])
} }
# $how.create-protoobject()
sub create-protoobject { Q:CgOp {
(prog
[lextypes $p DynObject $mo DynMetaObject]
[l $mo (unwrap DynMetaObject (getattr meta-object (@ (pos 0))))]
[l $p (rawnew DynObject (l $mo))]
[rawcall (l $mo) BuildC3MRO]
[setfield slots (l $p) (null Dictionary<string,object>)]
[setfield typeObject (l $mo) (l $p)]
[newscalar (l $p)])
} }
sub compose { Q:CgOp { (null Variable) } }
Q:CgOp {
(prog
[lextypes $chmo DynMetaObject $chch Variable]
[l $chmo (rawnew DynMetaObject (clr_string "ClassHOW"))]
[l $chch (ns (rawnew DynObject (l $chmo)))]
[setfield how (l $chmo) (@ (l $chch))]
[setindex meta-object (getfield slots (cast DynObject (@ (l $chch))))
(w (l $chmo))]
[sink (subcall (@ (l &add-method)) (l $chch)
(wrap (clr_string "new")) (l &new))]
[sink (subcall (@ (l &add-method)) (l $chch)
(wrap (clr_string "add-method")) (l &add-method))]
[sink (subcall (@ (l &add-method)) (l $chch)
(wrap (clr_string "add-super")) (l &add-super))]
[sink (subcall (@ (l &add-method)) (l $chch)
(wrap (clr_string "compose")) (l &compose))]
[sink (subcall (@ (l &add-method)) (l $chch)
(wrap (clr_string "create-protoobject")) (l &create-protoobject))]
[l ClassHOW (subcall (@ (l &create-protoobject)) (l $chch))]
[l ClassHOW!HOW (l $chch)]
[null Variable])
}
}
# cannot be a normal class - it has no parents
my class Mu { ... }
my class Any { ... }
my class Cool { ... }
# these are really defined in the kernel
my class Scalar { ... }
my class Sub { ... }
PRE-INIT {
# (DynMetaObject $dmo, ClassHOW $super --> ClassHOW)
sub wrap-dpmo { Q:CgOp {
(prog
[lextypes $ch Variable $dm DynMetaObject]
[l $ch (methodcall (l ClassHOW) new (w (clr_string "")))]
[l $dm (unwrap DynMetaObject (@ (pos 0)))]
[assign (varattr meta-object (@ (l $ch))) (w (l $dm))]
[setfield how (l $dm) (@ (l $ch))]
[sink (methodcall (l $ch) add-super (pos 1))]
[l $ch])
} }
Q:CgOp {
(prog
[lextypes !plist List<DynMetaObject>]
[l Mu!HOW (methodcall (l ClassHOW) new (w (clr_string Mu)))]
[l Mu (methodcall (l Mu!HOW) create-protoobject)]
[l Any!HOW (methodcall (l ClassHOW) new (w (clr_string Any)))]
[sink (methodcall (l Any!HOW) add-super (l Mu!HOW))]
[l Any (methodcall (l Any!HOW) create-protoobject)]
[l Cool!HOW (methodcall (l ClassHOW) new (w (clr_string Cool)))]
[sink (methodcall (l Cool!HOW) add-super (l Any!HOW))]
[l Cool (methodcall (l Cool!HOW) create-protoobject)]
[sink (methodcall (l ClassHOW!HOW) add-super (l Any!HOW))]
[rawcall (getfield klass (cast DynObject (@ (l ClassHOW)))) BuildC3MRO]
[l Sub!HOW (subcall (@ (l &wrap-dpmo))
(w (rawsget Kernel.SubMO)) (l Any!HOW))]
[l Sub (methodcall (l Sub!HOW) create-protoobject)]
[l Scalar!HOW (subcall (@ (l &wrap-dpmo))
(w (rawsget Kernel.ScalarMO)) (l Any!HOW))]
[l Scalar (methodcall (l Scalar!HOW) create-protoobject)]
[null Variable])
} }
my class Junction is Mu { }
my class Num {
method Str () { Q:CgOp {
(box Str (rawcall (unbox Double (fetch (scopedlex self))) ToString))
} }
method Bool() { Q:CgOp {
(box Bool (compare != (double 0)
(unbox Double (fetch (scopedlex self)))))
} }
method ACCEPTS($t) { self == $t }
}
my class Str {
method Str () { self }
method ACCEPTS($t) { self eq $t }
method chars() { Q:CgOp {
(box Num (cast Double (getfield Length (unbox String (@ (l self))))))
} }
method substr($from, $len) { Q:CgOp {
(box Str (rawcall [unbox String (@ (l self))] Substring
[cast Int32 (unbox Double (@ (l $from)))]
[cast Int32 (unbox Double (@ (l $len)))]))
} }
}
my class Blob { }
my class Char { }
my class CharLingua { }
my class AnyChar { }
my class Codepoint { }
my class Grapheme { }
my class StrPos { }
my class StrLen { }
my class Rat { }
my class FatRat { }
my class Complex { }
my class Int { }
my class UInt { }
my class Bit { }
my class Instant { }
my class Duration { }
my class Enum { }
my class Bool is Enum {
method Str() { if self { "Bool::True" } else { "Bool::False" } }
method Bool() { self }
method ACCEPTS($) { self }
}
# ought to take a slurpy
sub infix:<~>($l,$r) { Q:CgOp {
(box Str (rawscall String.Concat
(unbox String (fetch (methodcall (scopedlex $l) Str)))
(unbox String (fetch (methodcall (scopedlex $r) Str)))))
} }
sub infix:<+>($l,$r) { Q:CgOp {
(box Num (arith + (unbox Double (fetch (scopedlex $l)))
(unbox Double (fetch (scopedlex $r)))))
} }
sub infix:<->($l,$r) { Q:CgOp {
(box Num (arith - (unbox Double (fetch (scopedlex $l)))
(unbox Double (fetch (scopedlex $r)))))
} }
sub infix:<*>($l,$r) { Q:CgOp {
(box Num (arith * (unbox Double (fetch (scopedlex $l)))
(unbox Double (fetch (scopedlex $r)))))
} }
sub infix:</>($l,$r) { Q:CgOp {
(box Num (arith / (unbox Double (fetch (scopedlex $l)))
(unbox Double (fetch (scopedlex $r)))))
} }
sub infix:<< < >>($l,$r) { Q:CgOp {
(box Bool (compare < (unbox Double (fetch (scopedlex $l)))
(unbox Double (fetch (scopedlex $r)))))
} }
sub infix:<< > >>($l,$r) { Q:CgOp {
(box Bool (compare > (unbox Double (fetch (scopedlex $l)))
(unbox Double (fetch (scopedlex $r)))))
} }
sub infix:<< <= >>($l,$r) { Q:CgOp {
(box Bool (compare <= (unbox Double (fetch (scopedlex $l)))
(unbox Double (fetch (scopedlex $r)))))
} }
sub infix:<< >= >>($l,$r) { Q:CgOp {
(box Bool (compare >= (unbox Double (fetch (scopedlex $l)))
(unbox Double (fetch (scopedlex $r)))))
} }
sub infix:<< == >>($l,$r) { Q:CgOp {
(box Bool (compare == (unbox Double (fetch (scopedlex $l)))
(unbox Double (fetch (scopedlex $r)))))
} }
sub infix:<< != >>($l,$r) { Q:CgOp {
(box Bool (compare != (unbox Double (fetch (scopedlex $l)))
(unbox Double (fetch (scopedlex $r)))))
} }
sub warn($str) { Q:CgOp {
(prog [rawscall Console.Error.WriteLine
(unbox String (fetch (methodcall (scopedlex $str) Str))])
[box Bool (bool 1)]
)
} }
sub say($str) { Q:CgOp {
(prog [rawscall Console.WriteLine
(unbox String (fetch (methodcall (scopedlex $str) Str))])
[box Bool (bool 1)]
)
} }
sub exit() { Q:CgOp {
(prog [rawscall Environment.Exit (int 0) ]
[null Variable]
)
} }
sub infix:<=> { Q:CgOp { (prog [assign (pos 0) (pos 1)] (pos 0)) } }
# Buglet in STD: standard infix operators look custom inside the setting, and
# forget their precedence.
sub prefix:<-->($v) { $v = ($v - 1); $v }
sub prefix:<++>($v) { $v = ($v + 1); $v }
sub postfix:<-->($v) { my $old = $v; $v = ($v - 1); $old }
sub postfix:<++>($v) { my $old = $v; $v = ($v + 1); $old }
# actually a macro
# sub infix:<:=>($l,$r) { ... }
sub prefix:<~>($v) { $v.Str } # should be Stringy
sub prefix:<?>($v) { $v.Bool }
sub prefix:<!>($v) { if $v { ?0 } else { ?1 } }
sub infix:<eq>($l,$r) { Q:CgOp {
(box Bool (compare ==
(unbox String (fetch (methodcall (scopedlex $l) Str)))
(unbox String (fetch (methodcall (scopedlex $r) Str)))))
} }
sub infix:<ne>($l,$r) { Q:CgOp {
(box Bool (compare !=
(unbox String (fetch (methodcall (scopedlex $l) Str)))
(unbox String (fetch (methodcall (scopedlex $r) Str)))))
} }
# this one is horribly wrong and only handles the ref eq case.
sub infix:<===>($l,$r) { Q:CgOp {
(box Bool (compare == (fetch (scopedlex $l)) (fetch (scopedlex $r))))
} }
sub infix:<~~>($t,$m) {
if $m.defined {
$m.ACCEPTS($t)
} else {
$t.^does($m)
}
}
# XXX: We can't use augment syntax because we don't have use working, so
# no MONKEY_TYPING.
PRE-INIT {
Mu.HOW.add-method(Q:CgOp { (wrap (clr_string "defined")) },
anon method defined() {
Q:CgOp { (box Bool (!= (null Dictionary<string,object>)
(getfield slots (cast DynObject (@ (l self)))))) }
});
Mu.HOW.add-method(Q:CgOp { (w (clr_string "Bool")) },
anon method Bool() { self.defined });
Mu.HOW.add-method(Q:CgOp { (w (clr_string "Str")) },
anon method Str() {
my $tn := Q:CgOp {
(box Str (getfield name (getfield klass
(cast DynObject (fetch (scopedlex self))))))
};
if self.defined {
$tn ~ "()<instance>"
} else {
$tn ~ "()"
}
});
Mu.HOW.add-method(Q:CgOp { (w (clr_string "notdef")) },
anon method notdef() { ! self.defined });
Mu.HOW.add-method(Q:CgOp { (w (clr_string "so")) },
anon method notdef() { self.Bool });
Mu.HOW.add-method(Q:CgOp { (w (clr_string "not")) },
anon method notdef() { ! self.Bool });
Mu.HOW.add-method(Q:CgOp { (w (clr_string "RAWCREATE")) },
anon method RAWCREATE(*@fields) {
my $obj = Q:CgOp {
(ns (rawnew DynObject (getfield klass
(cast DynObject (@ (l self))))))
};
while @fields {
my $name = @fields.shift;
my $value = @fields.shift;
Q:CgOp {
(prog
[setindex (unbox String (@ (l $name)))
(getfield slots (cast DynObject (@ (l $obj))))
(l $value)]
[null Variable])
};
}
$obj;
});
Any.HOW.add-method(Q:CgOp { (w (clr_string "ACCEPTS")) },
anon method ACCEPTS($t) { self === $t });
# Should be for Block, not Sub
Sub.HOW.add-method(Q:CgOp { (w (clr_string "ACCEPTS")) },
anon method ACCEPTS($t) { (self)($t) });
ClassHOW.HOW.add-method(Q:CgOp { (w (clr_string "isa")) },
anon method isa($obj, $type) { Q:CgOp {
(box Bool (rawcall (getfield klass (cast DynObject (@ (l $obj))))
HasMRO (getfield klass (cast DynObject (@ (l $type))))))
} });
ClassHOW.HOW.add-method(Q:CgOp { (w (clr_string "does")) },
anon method does($obj, $role) { self.isa($obj, $role) }); #no roles yet
}
# boxes a List<Variable>. SCHLIEMEL WAS HERE
my class LLArray {
method push($x) { Q:CgOp { (prog
[rawcall (unbox List<Variable> (@ (l self))) Add (l $x)] [l self]) } }
method shift() { Q:CgOp { (prog
[lextypes $f Variable $lv List<Variable>]
[l $lv (unbox List<Variable> (@ (l self)))]
[l $f (getindex (int 0) (l $lv))]
[rawcall (l $lv) RemoveAt (int 0)]
[l $f]) } }
method unshift($x) { Q:CgOp { (prog
[rawcall (unbox List<Variable> (@ (l self))) Insert (int 0) (l $x)]
[l self]) } }
method first-flattens() { Q:CgOp {
(box Bool (getfield islist (getfield lv (getindex (int 0)
(unbox List<Variable> (@ (l self))))))) } }
method elems() { Q:CgOp {
(box Num (cast Double (getfield Count (unbox List<Variable>
(@ (l self)))))) } }
method Bool() { ?( self.elems ) }
method new(*@v) { Q:CgOp { (varattr rest (@ (l @v))) } }
method at-pos($i) {
Q:CgOp { (getindex (cast Int32 (unbox Double (@ (l $i))))
(unbox List<Variable> (@ (l self)))) }
}
}
my class Iterator {
# has $.valid
# has $.value
# has $.next
# subclasses must provide .validate
method valid() { $!valid }
method value() { $!value }
method next() { $!next }
}
my class EMPTY { }
my class Parcel {
# $!ll
method iterator() {
my $ll = $!ll;
my $it = Iterator.RAWCREATE("valid", 1, "value", Any, "next", Any);
$it.value = EMPTY;
my $ix = ($ll.elems - 1);
while $ix >= 0 {
my $nit = Iterator.RAWCREATE("valid", 1, "next", Any);
$nit.next = $it;
$it = $nit;
Q:CgOp {
(prog
[setindex value (getfield slots
(cast DynObject (@ (l $it))))
(methodcall (l $ll) at-pos (l $ix))]
[null Variable])
};
$ix--;
}
$it;
}
}
sub infix:<,>(*@v) {
my @r := Parcel.RAWCREATE("ll", Q:CgOp { (varattr rest (@ (l @v))) });
@r;
}
# Maybe this should be a constant, but constants are currently forced to
# scalar-nature (TODO)
sub Nil {
state @n;
START {
@n := Parcel.RAWCREATE("ll", LLArray.new);
}
@n;
}
my class List is Cool {
# low level, so not declared yet
# @!items @!rest $!flat
method fill($nr) {
my $i = $!items;
my $r = $!rest;
while $i.elems < $nr && $r {
if $!flat && $r.first-flattens {
$r.unshift($r.shift.iterator);
} else {
my $v := $r.shift;
if $v.^isa(EMPTY) {
# Discard summarily
} elsif $v.^isa(Iterator) {
if ! $v.valid {
$v.validate;
}
# we can't push anything onto items because it might be
# EMPTY
if ! $v.value.^isa(EMPTY) {
$r.unshift($v.next);
$r.unshift($v.value);
}
} else {
$i.push($v);
}
}
}
$i.elems >= $nr;
}
method Bool() { self.fill(1) }
method shift() {
self.fill(1);
$!items.shift;
}
method elems() { self.fill(1000_000_000); $!items.elems; }
method at-pos($i) {
self.fill($i + 1);
$!items.at-pos($i);
}
# XXX this is wrong, it holds on to the entire list -> memory leaks
my class ListIterator is Iterator {
# $!valid $!value $!next $!list $!index
method validate() {
$!valid = 1;
# needed to copy flatness bit if not $!flat ?
my $l = $!list;
my $i = $!index;
if $l.fill($i+1) {
Q:CgOp {
(prog
[setindex value (getfield slots
(cast DynObject (@ (l self))))
(methodcall (l $l) at-pos (l $i))]
[null Variable])
};
} else {
$!value = EMPTY;
}
$!next = ListIterator.RAWCREATE("valid", 0, "next", Any,
"value", Any, "list", $!list, "index", $i+1);
}
}
method iterator() {
ListIterator.RAWCREATE("valid", 0, "next", Any, "value", Any,
"list", self, "index", 0);
}
}
{YOU_ARE_HERE}