/
pair.reds
579 lines (531 loc) · 12.3 KB
/
pair.reds
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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
Red/System [
Title: "Pair! datatype runtime functions"
Author: "Nenad Rakocevic"
File: %pair.reds
Tabs: 4
Rights: "Copyright (C) 2011-2018 Red Foundation. All rights reserved."
License: {
Distributed under the Boost Software License, Version 1.0.
See https://github.com/red/red/blob/master/BSL-License.txt
}
]
pair: context [
verbose: 0
get-named-index: func [
w [red-word!]
ref [red-value!]
return: [integer!]
/local
axis [integer!]
][
axis: symbol/resolve w/symbol
if all [axis <> words/x axis <> words/y][
either TYPE_OF(ref) = TYPE_PAIR [
fire [TO_ERROR(script cannot-use) w ref]
][
fire [TO_ERROR(script invalid-path) ref w]
]
]
either axis = words/x [1][2]
]
do-math: func [
op [integer!]
return: [red-pair!]
/local
left [red-pair!]
right [red-pair!]
int [red-integer!]
fl [red-float!]
p [red-point2D!]
x y [integer!]
f n [float!]
][
left: as red-pair! stack/arguments
right: left + 1
assert TYPE_OF(left) = TYPE_PAIR
switch TYPE_OF(right) [
TYPE_PAIR [
x: right/x
y: right/y
]
TYPE_INTEGER [
int: as red-integer! right
x: int/value
y: x
]
TYPE_POINT2D [
promote-left
return as red-pair! point2D/do-math op
]
TYPE_FLOAT TYPE_PERCENT [
fl: as red-float! right
f: fl/value
switch op [
OP_MUL [
if float/special? f [fire [TO_ERROR(script invalid-arg) right]]
left/x: as-integer (as-float left/x) * f
left/y: as-integer (as-float left/y) * f
return left
]
OP_DIV [
if float/NaN? f [fire [TO_ERROR(script invalid-arg) right]]
left/x: as-integer (as-float left/x) / f
left/y: as-integer (as-float left/y) / f
return left
]
default [
if float/special? f [fire [TO_ERROR(script invalid-arg) right]]
x: as-integer f
y: x
]
]
]
default [
fire [TO_ERROR(script invalid-type) datatype/push TYPE_OF(right)]
]
]
left/x: integer/do-math-op left/x x op null
left/y: integer/do-math-op left/y y op null
left
]
promote-left: func [
/local
p [red-pair!]
pt [red-point2D!]
][
p: as red-pair! stack/arguments
pt: as red-point2D! p
pt/header: TYPE_POINT2D
pt/x: as-float32 p/x
pt/y: as-float32 p/y
]
make-at: func [
slot [red-value!]
x [integer!]
y [integer!]
return: [red-pair!]
/local
pair [red-pair!]
][
#if debug? = yes [if verbose > 0 [print-line "pair/make-at"]]
pair: as red-pair! slot
set-type slot TYPE_PAIR
pair/x: x
pair/y: y
pair
]
make-in: func [
parent [red-block!]
x [integer!]
y [integer!]
return: [red-pair!]
][
#if debug? = yes [if verbose > 0 [print-line "pair/make-in"]]
make-at ALLOC_TAIL(parent) x y
]
push: func [
x [integer!]
y [integer!]
return: [red-pair!]
][
#if debug? = yes [if verbose > 0 [print-line "pair/push"]]
make-at stack/push* x y
]
get-value-int: func [
int [red-integer!]
return: [integer!]
/local
fl [red-float!]
][
either TYPE_OF(int) = TYPE_FLOAT [
fl: as red-float! int
as-integer fl/value
][
int/value
]
]
;-- Actions --
make: func [
proto [red-value!]
spec [red-value!]
type [integer!]
return: [red-pair!]
/local
int [red-integer!]
int2 [red-integer!]
fl [red-float!]
p [red-point2D!]
x [integer!]
y [integer!]
val [red-value!]
][
#if debug? = yes [if verbose > 0 [print-line "pair/make"]]
switch TYPE_OF(spec) [
TYPE_INTEGER [
int: as red-integer! spec
push int/value int/value
]
TYPE_FLOAT [
fl: as red-float! spec
x: as-integer fl/value
push x x
]
TYPE_BLOCK [
int: as red-integer! block/rs-head as red-block! spec
int2: int + 1
if any [
2 > block/rs-length? as red-block! spec
all [TYPE_OF(int) <> TYPE_INTEGER TYPE_OF(int) <> TYPE_FLOAT]
all [TYPE_OF(int2) <> TYPE_INTEGER TYPE_OF(int2) <> TYPE_FLOAT]
][
fire [TO_ERROR(syntax malconstruct) spec]
]
x: get-value-int int
y: get-value-int int2
push x y
]
TYPE_POINT2D [
p: as red-point2D! spec
if any [
float/special? as-float p/x
float/special? as-float p/y
][
fire [TO_ERROR(script invalid-arg) spec]
]
push as-integer p/x as-integer p/y
]
TYPE_STRING [
y: 0
val: as red-value! :y
copy-cell spec val ;-- save spec, load-value will change it
proto: load-value as red-string! spec
if TYPE_OF(proto) <> TYPE_PAIR [
fire [TO_ERROR(script bad-to-arg) datatype/push TYPE_PAIR val]
]
proto
]
TYPE_PAIR [as red-pair! spec]
default [
fire [TO_ERROR(script bad-to-arg) datatype/push TYPE_PAIR spec]
null
]
]
]
random: func [
pair [red-pair!]
seed? [logic!]
secure? [logic!]
only? [logic!]
return: [red-value!]
/local
n [integer!]
][
#if debug? = yes [if verbose > 0 [print-line "pair/random"]]
either seed? [
_random/srand pair/x xor pair/y
pair/header: TYPE_UNSET
][
unless zero? pair/x [
pair/x: _random/int-uniform-distr secure? pair/x
]
unless zero? pair/y [
pair/y: _random/int-uniform-distr secure? pair/y
]
]
as red-value! pair
]
form: func [
pair [red-pair!]
buffer [red-string!]
arg [red-value!]
part [integer!]
return: [integer!]
/local
formed [c-string!]
][
#if debug? = yes [if verbose > 0 [print-line "pair/form"]]
formed: integer/form-signed pair/x
string/concatenate-literal buffer formed
part: part - length? formed ;@@ optimize by removing length?
string/append-char GET_BUFFER(buffer) as-integer #"x"
formed: integer/form-signed pair/y
string/concatenate-literal buffer formed
part - 1 - length? formed ;@@ optimize by removing length?
]
mold: func [
pair [red-pair!]
buffer [red-string!]
only? [logic!]
all? [logic!]
flat? [logic!]
arg [red-value!]
part [integer!]
indent [integer!]
return: [integer!]
][
#if debug? = yes [if verbose > 0 [print-line "pair/mold"]]
form pair buffer arg part
]
eval-path: func [
parent [red-pair!] ;-- implicit type casting
element [red-value!]
value [red-value!]
path [red-value!]
gparent [red-value!]
p-item [red-value!]
index [integer!]
case? [logic!]
get? [logic!]
tail? [logic!]
evt? [logic!]
return: [red-value!]
/local
old [red-value!]
int [red-integer!]
axis [integer!]
type [integer!]
][
#if debug? = yes [if verbose > 0 [print-line "pair/eval-path"]]
switch TYPE_OF(element) [
TYPE_INTEGER [
int: as red-integer! element
axis: int/value
if all [axis <> 1 axis <> 2][
fire [TO_ERROR(script invalid-path) path element]
]
]
TYPE_WORD [axis: get-named-index as red-word! element path]
default [fire [TO_ERROR(script invalid-path) path element]]
]
either value <> null [
type: TYPE_OF(value)
if type <> TYPE_INTEGER [
fire [TO_ERROR(script invalid-type) datatype/push type]
]
if evt? [old: stack/push as red-value! parent]
int: as red-integer! stack/arguments
int/header: TYPE_INTEGER
either axis = 1 [parent/x: int/value][parent/y: int/value]
if evt? [
either TYPE_OF(gparent) = TYPE_OBJECT [
object/fire-on-set as red-object! gparent as red-word! p-item old as red-value! parent
][
ownership/check as red-value! gparent words/_set-path value axis 1
]
stack/pop 1 ;-- avoid moving stack top
]
as red-value! int
][
int: integer/push either axis = 1 [parent/x][parent/y]
stack/pop 1 ;-- avoid moving stack top
int
]
]
compare: func [
left [red-pair!] ;-- first operand
right [red-pair!] ;-- second operand
op [integer!] ;-- type of comparison
return: [integer!]
/local
diff [integer!]
tmp [red-value!]
][
#if debug? = yes [if verbose > 0 [print-line "pair/compare"]]
if TYPE_OF(right) = TYPE_POINT2D [ ;-- promote left to point2D in such case
promote-left
return point2D/compare as red-point2D! left as red-point2D! right op
]
if TYPE_OF(right) <> TYPE_PAIR [RETURN_COMPARE_OTHER]
diff: left/x - right/x
if zero? diff [diff: left/y - right/y]
SIGN_COMPARE_RESULT(diff 0)
]
round: func [
pair [red-pair!]
scale [red-integer!]
_even? [logic!]
down? [logic!]
half-down? [logic!]
floor? [logic!]
ceil? [logic!]
half-ceil? [logic!]
return: [red-value!]
/local
int [red-integer!]
value [red-value!]
p [red-pair!]
scalexy?[logic!]
y [integer!]
][
if TYPE_OF(scale) = TYPE_MONEY [
fire [TO_ERROR(script not-related) stack/get-call datatype/push TYPE_MONEY]
]
scalexy?: all [OPTION?(scale) TYPE_OF(scale) = TYPE_PAIR]
if scalexy? [
p: as red-pair! scale
y: p/y
scale/header: TYPE_INTEGER
scale/value: p/x
]
int: integer/push pair/x
value: integer/round as red-value! int scale _even? down? half-down? floor? ceil? half-ceil?
pair/x: get-value-int as red-integer! value
if scalexy? [scale/value: y]
int/value: pair/y
value: integer/round as red-value! int scale _even? down? half-down? floor? ceil? half-ceil?
pair/y: get-value-int as red-integer! value
as red-value! pair
]
remainder: func [return: [red-value!]][
#if debug? = yes [if verbose > 0 [print-line "pair/remainder"]]
as red-value! do-math OP_REM
]
absolute: func [
return: [red-pair!]
/local
pair [red-pair!]
][
#if debug? = yes [if verbose > 0 [print-line "pair/absolute"]]
pair: as red-pair! stack/arguments
pair/x: integer/abs pair/x
pair/y: integer/abs pair/y
pair
]
add: func [return: [red-value!]][
#if debug? = yes [if verbose > 0 [print-line "pair/add"]]
as red-value! do-math OP_ADD
]
divide: func [return: [red-value!]][
#if debug? = yes [if verbose > 0 [print-line "pair/divide"]]
as red-value! do-math OP_DIV
]
multiply: func [return: [red-value!]][
#if debug? = yes [if verbose > 0 [print-line "pair/multiply"]]
as red-value! do-math OP_MUL
]
subtract: func [return: [red-value!]][
#if debug? = yes [if verbose > 0 [print-line "pair/subtract"]]
as red-value! do-math OP_SUB
]
and~: func [return: [red-value!]][
#if debug? = yes [if verbose > 0 [print-line "pair/and~"]]
as red-value! do-math OP_AND
]
or~: func [return: [red-value!]][
#if debug? = yes [if verbose > 0 [print-line "pair/or~"]]
as red-value! do-math OP_OR
]
xor~: func [return: [red-value!]][
#if debug? = yes [if verbose > 0 [print-line "pair/xor~"]]
as red-value! do-math OP_XOR
]
negate: func [
return: [red-pair!]
/local
pair [red-pair!]
][
pair: as red-pair! stack/arguments
pair/x: 0 - pair/x
pair/y: 0 - pair/y
pair
]
pick: func [
pair [red-pair!]
index [integer!]
boxed [red-value!]
return: [red-value!]
][
#if debug? = yes [if verbose > 0 [print-line "pair/pick"]]
if TYPE_OF(boxed) = TYPE_WORD [index: get-named-index as red-word! boxed as red-value! pair]
if all [index <> 1 index <> 2][fire [TO_ERROR(script out-of-range) boxed]]
as red-value! integer/push either index = 1 [pair/x][pair/y]
]
reverse: func [
pair [red-pair!]
part [red-value!]
skip [red-value!]
return: [red-value!]
/local
tmp [integer!]
][
#if debug? = yes [if verbose > 0 [print-line "pair/reverse"]]
tmp: pair/x
pair/x: pair/y
pair/y: tmp
as red-value! pair
]
init: does [
datatype/register [
TYPE_PAIR
TYPE_VALUE
"pair!"
;-- General actions --
:make
:random
null ;reflect
:make ;to
:form
:mold
:eval-path
null ;set-path
:compare
;-- Scalar actions --
:absolute
:add
:divide
:multiply
:negate
null ;power
:remainder
:round
:subtract
null ;even?
null ;odd?
;-- Bitwise actions --
:and~
null ;complement
:or~
:xor~
;-- Series actions --
null ;append
null ;at
null ;back
null ;change
null ;clear
null ;copy
null ;find
null ;head
null ;head?
null ;index?
null ;insert
null ;length?
null ;move
null ;next
:pick
null ;poke
null ;put
null ;remove
:reverse
null ;select
null ;sort
null ;skip
null ;swap
null ;tail
null ;tail?
null ;take
null ;trim
;-- I/O actions --
null ;create
null ;close
null ;delete
null ;modify
null ;open
null ;open?
null ;query
null ;read
null ;rename
null ;update
null ;write
]
]
]