/
logic.reds
408 lines (369 loc) · 8.03 KB
/
logic.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
Red/System [
Title: "Logic! datatype runtime functions"
Author: "Nenad Rakocevic"
File: %logic.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
}
]
true-value: declare red-logic! ;-- preallocate TRUE value
false-value: declare red-logic! ;-- preallocate FALSE value
logic: context [
verbose: 0
get: func [ ;-- unboxing integer value
value [red-value!]
return: [logic!]
/local
cell [red-logic!]
][
assert TYPE_OF(value) = TYPE_LOGIC
cell: as red-logic! value
cell/value
]
box: func [
value [logic!]
return: [red-logic!]
/local
cell [red-logic!]
][
cell: as red-logic! stack/arguments
cell/header: TYPE_LOGIC
cell/value: value
cell
]
rs-true?: func [
value [red-value!]
strict? [logic!] ;-- Yes: error on unset! value
ref [red-word!]
return: [logic!]
/local
arg [red-logic!]
type [integer!]
][
if all [strict? TYPE_OF(value) = TYPE_UNSET][fire [TO_ERROR(script no-value) ref]]
arg: as red-logic! value
type: TYPE_OF(arg)
not any [ ;-- true if not none or false
type = TYPE_NONE
all [type = TYPE_LOGIC not arg/value]
]
]
top-true?: func [
return: [logic!]
][
not top-false? ;-- true if not none or false
]
top-false?: func [
return: [logic!]
/local
arg [red-logic!]
type [integer!]
][
arg: as red-logic! either stack/top = stack/arguments [stack/top][stack/get-top]
type: TYPE_OF(arg)
any [
type = TYPE_NONE
all [type = TYPE_LOGIC not arg/value]
]
]
true?: func [
return: [logic!]
/local
arg [red-logic!]
type [integer!]
][
#if debug? = yes [if verbose > 0 [print-line "logic/true?"]]
arg: as red-logic! stack/arguments
type: TYPE_OF(arg)
if type = TYPE_UNSET [fire [TO_ERROR(script no-return)]]
not any [ ;-- true if not none or false
type = TYPE_NONE
all [type = TYPE_LOGIC not arg/value]
]
]
false?: func [
return: [logic!]
/local
arg [red-logic!]
type [integer!]
][
#if debug? = yes [if verbose > 0 [print-line "logic/false?"]]
arg: as red-logic! stack/arguments
type: TYPE_OF(arg)
any [
type = TYPE_NONE
all [type = TYPE_LOGIC not arg/value]
]
]
make-in: func [
parent [red-block!]
value [logic!]
return: [red-logic!]
/local
cell [red-logic!]
][
#if debug? = yes [if verbose > 0 [print-line "logic/make-in"]]
cell: as red-logic! ALLOC_TAIL(parent)
cell/header: TYPE_LOGIC ;-- implicit reset of all header flags
cell/value: value
cell
]
push: func [
value [logic!]
return: [red-logic!]
/local
cell [red-logic!]
][
#if debug? = yes [if verbose > 0 [print-line "logic/push"]]
cell: as red-logic! stack/push*
cell/header: TYPE_LOGIC ;-- implicit reset of all header flags
cell/value: value
cell
]
;-- Actions --
make: func [
proto [red-value!]
spec [red-value!]
type [integer!]
return: [red-logic!] ;-- return cell pointer
/local
bool [red-logic!]
int [red-integer!]
fl [red-float!]
][
#if debug? = yes [if verbose > 0 [print-line "logic/make"]]
switch TYPE_OF(spec) [
TYPE_INTEGER [
int: as red-integer! spec
bool: as red-logic! proto
bool/header: TYPE_LOGIC ;-- implicit reset of all header flags
bool/value: as-logic int/value
bool
]
TYPE_FLOAT
TYPE_PERCENT [
fl: as red-float! spec
bool: as red-logic! proto
bool/header: TYPE_LOGIC ;-- implicit reset of all header flags
bool/value: fl/value <> 0.0
bool
]
default [to proto spec type]
]
]
random: func [
logic [red-logic!]
seed? [logic!]
secure? [logic!]
only? [logic!]
return: [red-logic!]
/local
value [integer!]
][
#if debug? = yes [if verbose > 0 [print-line "logic/random"]]
either seed? [
_random/srand as-integer logic/value
logic/header: TYPE_UNSET
][
value: either secure? [_random/rand-secure] [_random/rand]
logic/value: value > 3FFFFFFFh
]
logic
]
to: func [
proto [red-value!] ;-- overwrite this slot with result
spec [red-value!]
type [integer!]
return: [red-logic!]
][
#if debug? = yes [if verbose > 0 [print-line "logic/to"]]
switch TYPE_OF(spec) [
TYPE_LOGIC [as red-logic! spec]
TYPE_NONE [false-value]
default [true-value]
]
]
form: func [
boolean [red-logic!]
buffer [red-string!]
arg [red-value!]
part [integer!]
return: [integer!]
/local
res [c-string!]
len [integer!]
][
#if debug? = yes [if verbose > 0 [print-line "logic/form"]]
res: either boolean/value [len: 4 "true"][len: 5 "false"]
string/concatenate-literal buffer res
part - len
]
mold: func [
boolean [red-logic!]
buffer [red-string!]
only? [logic!]
all? [logic!]
flat? [logic!]
arg [red-value!]
part [integer!]
indent [integer!]
return: [integer!]
/local
res [c-string!]
len [integer!]
][
#if debug? = yes [if verbose > 0 [print-line "logic/mold"]]
either all? [
res: either boolean/value [len: 7 "#(true)"][len: 8 "#(false)"]
string/concatenate-literal buffer res
part - len
] [
form boolean buffer arg part
]
]
compare: func [
arg1 [red-logic!] ;-- first operand
arg2 [red-logic!] ;-- second operand
op [integer!] ;-- type of comparison
return: [integer!]
/local
type [integer!]
res [integer!]
][
#if debug? = yes [if verbose > 0 [print-line "logic/compare"]]
type: TYPE_OF(arg2)
if type <> TYPE_LOGIC [RETURN_COMPARE_OTHER]
switch op [
COMP_EQUAL
COMP_FIND
COMP_SAME
COMP_STRICT_EQUAL
COMP_NOT_EQUAL
COMP_SORT
COMP_CASE_SORT [res: (as-integer arg1/value) - (as-integer arg2/value)]
default [
res: -2
]
]
res
]
complement: func [
bool [red-logic!]
return: [red-value!]
][
bool/value: not bool/value
as red-value! bool
]
do-bitwise: func [
type [integer!]
return: [red-logic!]
/local
left [red-logic!]
right [red-logic!]
][
left: as red-logic! stack/arguments
right: left + 1
if TYPE_OF(right) <> TYPE_LOGIC [
ERR_EXPECT_ARGUMENT((TYPE_OF(right)) 1)
]
left/value: switch type [
OP_AND [left/value and right/value]
OP_OR [left/value or right/value]
OP_XOR [left/value xor right/value]
]
left
]
and~: func [return: [red-value!]][
#if debug? = yes [if verbose > 0 [print-line "logic/and~"]]
as red-value! do-bitwise OP_AND
]
or~: func [return: [red-value!]][
#if debug? = yes [if verbose > 0 [print-line "logic/or~"]]
as red-value! do-bitwise OP_OR
]
xor~: func [return: [red-value!]][
#if debug? = yes [if verbose > 0 [print-line "logic/xor~"]]
as red-value! do-bitwise OP_XOR
]
init: does [
true-value/header: TYPE_LOGIC
true-value/value: yes
false-value/header: TYPE_LOGIC
false-value/value: no
datatype/register [
TYPE_LOGIC
TYPE_VALUE
"logic!"
;-- General actions --
:make
:random
null ;reflect
:to
:form
:mold
null ;eval-path
null ;set-path
:compare
;-- Scalar actions --
null ;absolute
null ;add
null ;divide
null ;multiply
null ;negate
null ;power
null ;remainder
null ;round
null ;subtract
null ;even?
null ;odd?
;-- Bitwise actions --
:and~
: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
null ;pick
null ;poke
null ;put
null ;remove
null ;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
]
]
]