forked from red/red
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.r
4459 lines (4049 loc) · 107 KB
/
compiler.r
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
REBOL [
Title: "Red compiler"
Author: "Nenad Rakocevic"
File: %compiler.r
Tabs: 4
Rights: "Copyright (C) 2011-2015 Nenad Rakocevic. All rights reserved."
License: "BSD-3 - https://github.com/red/red/blob/master/BSD-3-License.txt"
]
do-cache %system/compiler.r
red: context [
verbose: 0 ;-- logs verbosity level
job: none ;-- reference the current job object
script-name: none
script-path: none
script-file: none ;-- #system metadata for R/S loader
main-path: none
runtime-path: %runtime/
include-stk: make block! 3
included-list: make block! 20
script-stk: make block! 10
needed: make block! 4
symbols: make hash! 1000
globals: make hash! 1000 ;-- words defined in global context
aliases: make hash! 100
contexts: make hash! 100 ;-- storage for statically compiled contexts
ctx-stack: make block! 8 ;-- contexts access path
shadow-funcs: make block! 1000 ;-- shadow functions contexts [symbol object! ctx...]
objects: make block! 600 ;-- shadow objects contexts [name object! ctx...]
obj-stack: to path! 'objects ;-- current object access path
container-obj?: none ;-- closest wrapping object
func-objs: none ;-- points to 'objects first in-function object
paths-stack: make block! 4 ;-- stack of generated code for handling dual codepaths for paths
native-ts: make block! 200 ;-- prepared native! typesets: [name [<ts-list>] ...]
rebol-gctx: bind? 'rebol
expr-stack: make block! 8
current-call: none
lexer: do bind load-cache %lexer.r 'self
extracts: do bind load-cache %utils/extractor.r 'self
redbin: do bind load-cache %utils/redbin.r 'self
sys-global: make block! 1
lit-vars: reduce [
'block make hash! 1000
'string make hash! 1000
'context make hash! 1000
]
pc: none
locals: none
locals-stack: make block! 32
output: make block! 100
sym-table: make block! 1000
literals: make block! 1000
declarations: make block! 1000
bodies: make block! 1000
ssa-names: make block! 10 ;-- unique names lookup table (SSA form)
types-cache: make hash! 100 ;-- store compiled typesets [types array name...]
last-type: none
return-def: to-set-word 'return ;-- return: keyword
s-counter: 0 ;-- series suffix counter
depth: 0 ;-- expression nesting level counter
max-depth: 0
booting?: none ;-- YES: compiling boot script
no-global?: no ;-- YES: put global code in a function
nl: newline
set 'float! 'float ;-- type name not defined in Rebol
unboxed-set: [integer! char! float! float32! logic!]
block-set: [block! paren! path! set-path! lit-path!] ;@@ missing get-path!
string-set: [string! binary!]
series-set: union block-set string-set
actions: make block! 100
op-actions: make block! 20
keywords: make block! 10
actions-prefix: to path! 'actions
natives-prefix: to path! 'natives
intrinsics: [
if unless either any all while until loop repeat
forever foreach forall func function does has
exit return switch case routine set get reduce
context object construct try break continue
remove-each
]
logic-words: [true false yes no on off]
word-iterators: [repeat foreach forall remove-each] ;-- only the ones using word(s) as counter(s)
iterators: [loop until while repeat foreach forall forever remove-each]
standard-modules: [
View %modules/view/view.red
]
func-constructors: [
'func | 'function | 'does | 'has | 'routine | 'make 'function!
]
functions: make hash! [
;---name--type--arity----------spec----------------------------refs--
make [action! 2 [type [datatype! word!] spec [any-type!]] #[none]] ;-- must be pre-defined
]
make-keywords: does [
foreach [name spec] functions [
if spec/1 = 'intrinsic! [
repend keywords [name reduce [to word! join "comp-" name]]
]
]
bind keywords self
]
set-last-none: does [copy [stack/reset none/push-last]] ;-- copy required for R/S line counting injection
--not-implemented--: does [print "Feature not yet implemented!" halt]
quit-on-error: does [
clean-up
if system/options/args [quit/return 1]
halt
]
throw-error: func [err [word! string! block!] /near code [block!]][
print [
"*** Compilation Error:"
either word? err [
join uppercase/part mold err 1 " error"
][reform err]
"^/*** in file:" mold script-name
;either locals [join "^/*** in function: " func-name][""]
]
if pc [
print [
;"*** at line:" calc-line lf
"*** near:" mold any [code copy/part pc 8]
]
]
quit-on-error
]
dispatch-ctx-keywords: func [original [any-word! none!] /with alt-value][
if path? alt-value [alt-value: alt-value/1]
switch/default any [alt-value pc/1][
func [comp-func]
function [comp-function]
has [comp-has]
does [comp-does]
routine [comp-routine]
construct [comp-construct]
object
context [
either obj: is-object? pc/2 [
comp-context/with/extend original obj
][
comp-context/with original
]
]
][no]
]
relative-path?: func [file [file!]][
not find "/~" first file
]
process-include-paths: func [code [block!] /local rule file][
parse code rule: [
some [
#include file: (
script-path: any [script-path main-path]
if all [script-path relative-path? file/1][
file/1: clean-path join script-path file/1
]
unless empty? script-stk [
insert next file reduce [#script last script-stk]
]
)
| into rule
| skip
]
]
]
process-calls: func [code [block!] /global /local rule pos mark][
parse code rule: [
some [
#call pos: (
mark: tail output
process-call-directive pos/1 to logic! global
change/part back pos mark 2
clear mark
)
| #get pos: (process-get-directive pos/1 back pos)
| into rule
| skip
]
]
]
process-routine-calls: func [code [block!] ctx [word!] ignore [block!] obj [object!] /local rule name][
parse code rule: [
some [
name: word! (
if all [in obj name/1 not find ignore name/1][
name/1: decorate-obj-member name/1 ctx
]
)
| path! | set-path! | lit-path!
| into rule
| skip
]
]
]
preprocess-strings: func [code [block!] /local rule s][ ;-- re-encode strings for Red/System
parse code rule: [
any [
s: string! (lexer/decode-UTF8-string s/1)
| into rule
| skip
]
]
]
convert-to-block: func [mark [block!]][
change/part/only mark copy/deep mark tail mark ;-- put code between [...]
clear next mark ;-- remove code at "upper" level
]
any-function?: func [value [word!]][
find [native! action! op! function! routine!] value
]
scalar?: func [expr][
find [
unset!
none!
logic!
datatype!
char!
integer!
decimal!
refinement!
issue!
lit-word!
word!
get-word!
set-word!
pair!
time!
] type?/word :expr
]
local-bound?: func [original [any-word!] /local obj][
all [
not empty? locals-stack
rebol-gctx <> obj: bind? original
find shadow-funcs obj
]
]
local-word?: func [name [word!]][
all [not empty? locals-stack find last locals-stack name]
]
unicode-char?: func [value][value/1 = #"'"]
float-special?: func [value][value/1 = #"."]
tuple-value?: func [value][value/1 = #"~"]
percent-value?: func [value][#"%" = last value]
map-value?: func [value][all [block? value value/1 = #!map!]]
insert-lf: func [pos][
new-line skip tail output pos yes
]
emit: func [value][
either block? value [append output value][append/only output value]
]
emit-src-comment: func [pos [block! paren! none!] /with cmt [string!]][
unless cmt [
cmt: trim/lines mold/only/flat clean-lf-deep copy/deep/part pos offset? pos pc
]
if 50 < length? cmt [cmt: append copy/part cmt 50 "..."]
emit reduce [
'------------| (cmt)
]
]
find-ssa: func [name [word!]][find/skip ssa-names name 2]
select-ssa: func [name [word!] /local pos][
all [pos: find/skip ssa-names name 2 pos/2]
]
parent-object?: func [obj [object!]][
all [not empty? locals-stack (next first obj) = container-obj?]
]
find-binding: func [original [any-word!] /local ctx idx obj][
all [
ctx: all [
rebol-gctx <> obj: bind? original
any [select objects obj select shadow-funcs obj]
]
attempt [idx: get-word-index/with to word! original ctx]
reduce [ctx idx]
]
]
select-object: func [ctx [word!] /local pos][
pos: find objects ctx
either object? pos/2 [pos/2][pos/-1]
]
bind-function: func [body [block!] shadow [object!] /local self* rule pos][
bind body shadow
if 1 < length? obj-stack [
self*: in do obj-stack 'self ;-- rebing SELF to the wrapping object
parse body rule: [
any [pos: 'self (pos/1: self*) | into rule | skip]
]
]
]
get-word-index: func [name [word!] /with c [word!] /local ctx pos list][
if with [
ctx: select contexts c
return either pos: find ctx name [(index? pos) - 1][none]
]
list: tail ctx-stack
until [ ;-- search backward in parent contexts
list: back list
ctx: select contexts list/1
if pos: find ctx name [
return (index? pos) - 1 ;-- 0-based access in context table
]
head? list
]
throw-error ["Should not happen: not found context for word: " mold name]
]
emit-push-from: func [
name [any-word!] original [any-word!] type [word!] actions [block!]
/local ctx obj idx
][
either all [
ctx: all [
rebol-gctx <> obj: bind? original
select objects obj
]
attempt [idx: get-word-index/with name ctx]
][
emit append to path! type actions/1
emit either parent-object? obj ['octx][ctx] ;-- optional parametrized context reference (octx)
emit idx
insert-lf -3
][
emit append to path! type actions/2
emit prefix-exec name
insert-lf -2
]
]
emit-push-word: func [name [any-word!] original [any-word!] /local type ctx obj][
type: to word! form type? name
name: to word! :name
either all [
rebol-gctx <> obj: bind? original
ctx: select shadow-funcs obj
][
emit append to path! type 'push-local
emit ctx
emit get-word-index name ;@@ replace that
insert-lf -3
][
emit-push-from name original type [push-local push]
]
]
emit-get-word: func [name [word!] original [any-word!] /any? /literal /local new obj ctx][
either all [
rebol-gctx <> obj: bind? original
ctx: select shadow-funcs obj
][
either all [not empty? ctx-stack ctx <> last ctx-stack][
emit 'word/get-local
emit ctx
emit get-word-index name ;-- word from another function context
insert-lf -3
exit
][
emit 'stack/push ;-- local word
]
emit decorate-symbol/no-alias name
][
if new: select-ssa name [name: new] ;@@ add a check for function! type
emit case [ ;-- global word
all [
literal
obj = rebol-gctx
][
'get-word/get
]
any? ['word/get-any]
'else [
emit-push-from name name 'word [get-local get]
exit
]
]
emit decorate-symbol name
]
insert-lf -2
]
get-path-word: func [
original [any-word!] blk [block!] get? [logic!]
/local name new obj ctx idx
][
name: to word! original
either all [
rebol-gctx <> obj: bind? original
find shadow-funcs obj
][
either get? [
append blk decorate-symbol/no-alias name ;-- local word, point to value slot
][
append blk [as cell!]
append/only blk prefix-exec name ;-- force global word
]
][
if new: select-ssa name [name: new] ;@@ add a check for function! type
either get? [
either all [
rebol-gctx <> obj
ctx: select objects obj
attempt [idx: get-word-index/with name ctx]
][
repend blk [
'word/get-in
either parent-object? obj ['octx][ctx] ;-- optional parametrized context reference (octx)
idx
]
][
append/only blk '_context/get
append/only blk prefix-exec name
]
][
append blk [as cell!]
append/only blk prefix-exec name
]
]
blk
]
emit-open-frame: func [name [word!] /local symbol type][
symbol: either name = 'try-all ['try][name]
unless find symbols symbol [add-symbol symbol]
emit case [
'function! = all [
type: find functions name
first first next type
]['stack/mark-func]
find iterators name ['stack/mark-loop]
name = 'try ['stack/mark-try]
name = 'try-all ['stack/mark-try-all]
name = 'catch ['stack/mark-catch]
'else ['stack/mark-native]
]
emit prefix-exec symbol
insert-lf -2
]
emit-close-frame: func [/last][
emit pick [stack/unwind-last stack/unwind] to logic! last
insert-lf -1
]
emit-stack-reset: does [
emit 'stack/reset
insert-lf -1
]
emit-dyn-check: does [
;emit 'stack/check-call
;insert-lf -1
]
build-exception-handler: has [body][
body: make block! 8
append body [
0 [0]
]
unless find expr-stack 'while-cond [
either empty? intersect iterators expr-stack [
append body [
RED_THROWN_BREAK
RED_THROWN_CONTINUE [re-throw]
]
][
append body [
RED_THROWN_BREAK [break]
RED_THROWN_CONTINUE [continue]
]
]
]
append body [
RED_THROWN_RETURN RED_THROWN_EXIT
]
append/only body pick [
[re-throw]
[ctx/values: saved system/thrown: 0 exit]
] empty? locals-stack
append body [
default [re-throw]
]
reduce [body]
]
emit-function: func [name [word!] /with ctx-name [word!]][
emit decorate-func name
insert-lf either with [emit ctx-name -2][-1]
]
emit-action: func [name [word!] /with options [block!]][
emit join actions-prefix to word! join name #"*"
insert-lf either with [
emit options
-1 - length? options
][
-1
]
]
emit-native: func [name [word!] /with options [block!] /local wrap? pos body][
if wrap?: to logic! find [parse do] name [emit 'switch]
emit join natives-prefix to word! join name #"*"
emit 'true ;-- request run-time type-checking
pos: either with [
emit options
-2 - length? options
][
-2
]
insert-lf pos - pick [1 0] wrap?
if wrap? [emit build-exception-handler]
]
emit-exit-function: does [
emit [
stack/unroll stack/FRAME_FUNCTION
ctx/values: saved
exit
]
insert-lf -5
]
emit-deep-check: func [path [series!] fpath [path!] /local obj-stk list check check2 obj top? parent-ctx][
check: [
'object/unchanged?
prefix-exec path/1 ;-- word (object! value)
third obj: find objects do obj-stk ;-- class id (integer!)
]
check2: [
'object/unchanged2?
parent-ctx ;-- ctx (node!)
get-word-index/with path/1 parent-ctx ;-- object slot in parent's ctx
third obj: find objects do obj-stk ;-- class id
]
obj-stk: copy/part fpath (index? find fpath path/1) - 1
obj-stk/1: either find-contexts path/1 ['func-objs]['objects]
either 2 = length? path [
append obj-stk path/1
reduce check
][
list: make block! 3 * length? path
while [not tail? next path][
append obj-stk path/1
repend list get pick [check check2] head? path
parent-ctx: obj/2
path: next path
]
new-line list on
new-line skip list 3 on
new-line/all/skip skip list 3 on 4
reduce ['all list]
]
]
get-RS-type-ID: func [name [word! datatype!] /local type][ ;-- Red type name to R/S type ID
name: head remove back tail form name ;-- remove ending #"!"
replace/all name #"-" #"_"
type: to word! uppercase head insert name "TYPE_"
select extracts/definitions type
]
make-typeset: func [
spec [block!] option [block! none!] f-spec [block!] native? [logic!]
/local bs ts word bit idx name
][
spec: sort spec ;-- sort types to reduce cache misses
either bs: find/only/skip types-cache spec 4 [
ts: bs/2
name: bs/3
][
ts: copy [0 0 0]
foreach type spec [
unless block? type [
type: either word: in extracts/scalars type [get word][reduce [type]]
foreach word type [
bit: get-RS-type-ID name: word
unless bit [throw-error/near ["invalid datatype name:" name] f-spec]
idx: (bit / 32) + 1
poke ts idx ts/:idx or shift/logical -2147483648 bit and 255
]
]
]
forall ts [ts/1: to integer! to-bin32 ts/1] ;-- convert to little-endian values
idx: redbin/emit-typeset/root ts/1 ts/2 ts/3
redirect-to literals [
name: decorate-series-var 'ts
emit compose [(to set-word! name) as red-typeset! get-root (idx)]
insert-lf -5
]
append types-cache reduce [spec ts name idx]
]
spec: either option [
option: to word! join "~" clean-lf-flag option/1
reduce ['type-check-alt option name]
][
reduce ['type-check name]
]
if native? [
clear back tail spec
append spec compose [as red-typeset! get-root (any [idx bs/4])]
]
spec
]
emit-type-checking: func [name [word!] spec [block!] /native /local pos type][
unless native [name: to word! next form name] ;-- remove prefix decoration
either pos: any [
find spec name
find spec to lit-word! name
][
type: case [
block? pos/2 [pos/2]
all [string? pos/3 block? pos/3][pos/3]
'else [[default!]]
]
make-typeset type find/reverse pos refinement! spec to logic! native
][
none
]
]
get-counter: does [s-counter: s-counter + 1]
clean-lf-deep: func [blk [block! paren!] /local pos][
blk: copy/deep blk
parse blk rule: [
pos: (new-line/all pos off)
into rule | skip
]
blk
]
clean-lf-flag: func [name [word! lit-word! set-word! get-word! refinement!]][
mold/flat to word! name
]
prefix-func: func [word [word!] /with path][
if 1 < length? obj-stack [
path: any [obj-func-call? word next any [path obj-stack]]
word: decorate-obj-member word path
]
word
]
prefix-exec: func [word [word!]][
either any [empty? locals-stack not find-contexts word][
decorate-symbol word
][
decorate-exec-ctx decorate-symbol word ;-- 'exec prefix to access the word! and not the local value
]
]
generate-anon-name: has [name][
add-symbol name: to word! rejoin ["<anon" get-counter #">"]
name
]
decorate-obj-member: func [word [word!] path /local value][
parse value: mold path [some [p: #"/" (p/1: #"~") | skip]]
to word! rejoin [value #"~" word]
]
decorate-type: func [type [word!]][
to word! join "red-" mold/flat type
]
decorate-exec-ctx: func [name [word!]][
append to path! 'exec name
]
decorate-symbol: func [name [word!] /no-alias /local pos][
if all [not no-alias pos: find/case/skip aliases name 2][name: pos/2]
to word! join "~" clean-lf-flag name
]
decorate-func: func [name [word!] /strict /local new][
if all [not strict new: select-ssa name][name: new]
to word! join "f_" clean-lf-flag name
]
decorate-series-var: func [name [word!] /local new list][
new: to word! join name get-counter
list: select lit-vars select [blk block str string ctx context] name
if all [list not find list new][append list new]
new
]
declare-variable: func [name [string! word!] /init value /local var set-var][
set-var: to set-word! var: to word! name
unless find declarations set-var [
repend declarations [set-var any [value 0]] ;-- declare variable at root level
new-line skip tail declarations -2 yes
]
reduce [var set-var]
]
add-symbol: func [name [word!] /with original /local sym id alias][
unless find/case symbols name [
if find symbols name [
if find/case/skip aliases name 2 [exit]
alias: decorate-series-var name
repend aliases [name alias]
]
sym: decorate-symbol name
id: 1 + ((length? symbols) / 2)
repend symbols [name reduce [sym id]]
repend sym-table [
to set-word! sym 'word/load mold any [original name]
]
new-line skip tail sym-table -3 on
]
]
get-symbol-id: func [name [word!]][
second select symbols name
]
add-global: func [name [word!]][
unless any [
local-word? name
find globals name
][
repend globals [name 'unset!]
]
]
push-call: func [name [word! tag!]][
append expr-stack name
]
pop-call: does [
remove back tail expr-stack
]
add-context: func [ctx [block!] /local name][
append contexts name: decorate-series-var 'ctx
append/only contexts ctx
name
]
push-context: func [ctx [block!] /local name][
append ctx-stack name: add-context ctx
name
]
pop-context: does [
clear back tail ctx-stack
]
find-contexts: func [name [word!]][
ctx: tail ctx-stack
while [not head? ctx][
ctx: back ctx
if find select contexts ctx/1 name [return ctx/1]
]
none
]
to-context-spec: func [spec [block!]][
spec: copy spec
forall spec [spec/1: to set-word! spec/1]
append spec none
make object! spec
]
iterator-pending?: does [
not empty? intersect expr-stack iterators
]
get-obj-base: func [name [any-word!]][
either local-word? name [func-objs][objects]
]
get-obj-base-word: func [name [any-word!]][
either local-word? name ['func-objs]['objects]
]
find-object: func [spec [word! object!] /by-name][
case [
by-name [find/skip objects spec 6]
'else [none]
]
]
find-proto: func [obj [block!] fun [word!] /local proto o multi?][
if proto: obj/4 [
all [
multi?: 2 = length? proto ;-- multiple inheritance case
in proto/1 fun
in proto/2 fun
return obj/1 ;-- method redefined in spec
]
if in proto/1 fun [return obj/1] ;-- check <spec> prototype
if o: find-proto find objects proto/1 fun [return o] ;-- recurse into previous prototypes
unless proto/2 [return none] ;-- finish if simple inheritance case
if in proto/2 fun [return proto/2] ;-- check <base> prototype
if o: find-proto find objects proto/2 fun [return o] ;-- recurse into previous prototypes
]
none
]
search-obj: func [path [path!] /local search base fpath found?][
search: [
fpath: head insert copy path base
until [ ;-- evaluate nested paths from longer to shorter
remove back tail fpath
any [
tail? next fpath
object? found?: attempt [do fpath] ;-- path evaluates to an object: found!
]
]
]
base: get-obj-base-word path/1
do search ;-- check if path is an absolute object path
if all [not found? 1 < length? obj-stack][
base: obj-stack
do search ;-- check if path is a relative object path
unless all [
found?
find fpath path/1 ;-- check if the start of path is in the found path (avoids false positive)
][
return none ;-- not an object access path
]
]
reduce [found? fpath base]
]
object-access?: func [path [series!] /local res][
either path/1 = 'self [
bind? path/1
][
all [
1 < length? obj-stack
in do obj-stack path/1
insert path next obj-stack ;-- insert prefix into object path
]
search-obj to path! path
]
]
is-object?: func [expr /local pos][
unless find [word! get-word! path!] type?/word expr [return none]
any [
attempt [do join obj-stack expr]
all [
find [object! word!] type?/word expr
pos: find-object/by-name expr
pos/2
]
]
]
obj-func-call?: func [name [any-word!] /local obj][
if any [rebol-gctx = obj: bind? name find shadow-funcs obj][return no]
select objects obj
]
obj-func-path?: func [path [path!] /local fpath base symbol found? fun origin name obj][
either path/1 = 'self [
found?: bind? path/1
path: copy path
path/1: pick find objects found? -1
fun: head insert copy path 'objects
fpath: head clear next copy path
][
set [found? fpath base] search-obj to path! path
unless found? [return none]
fun: append copy fpath either base = obj-stack [ ;-- extract function access path without refinements
pick path 1 + (length? fpath) - (length? obj-stack)
][
pick path length? fpath
]
unless function! = attempt [do fun][return none] ;-- not a function call
remove fpath ;-- remove 'objects prefix
]
obj: find objects found?
origin: find-proto obj last fun
name: either origin [select objects origin][obj/2]
symbol: decorate-obj-member first find/tail fun fpath name
either find functions symbol [
fpath: next find path last fpath ;-- point to function name
reduce [
either 1 = length? fpath [fpath/1][copy fpath]
symbol
obj/2 ;-- object instance ctx name
]
][
none
]
]
system-words-path?: func [path [path! set-path!]][
if all [
2 < length? path
find/match path 'system/words
][
remove/part path 2
either 1 = length? path [
switch type?/word pc/1: load mold path [
set-word! [comp-set-word]
word! [comp-word]
get-word! [comp-word/literal]
]
path: none
][
bind path/1 'rebol ;-- force binding to global context
]
]
path
]
push-locals: func [symbols [block!]][
append/only locals-stack symbols
]
pop-locals: does [
also
last locals-stack
remove back tail locals-stack
]
literal-first-arg?: func [spec [block!]][
parse spec [
any [
word! (return no)
| lit-word! (return yes)
| /local (return no)
| skip
]
]
no
]
infix?: func [pos [block! paren!] /local specs][
all [
not tail? pos
word? pos/1
specs: select functions pos/1
'op! = specs/1
not all [ ;-- check if a literal argument is not expected
word? pos/-1