[REBOL] Words, Bindings and Contexts. (5)
From: agem:crosswinds at: 24-Jul-2000 19:39
[
REBOL[
version: 1.0.0.5
file: %/home/volker/edis/screbol-0.1.6.4/public/test.r
date: 24-Jul-2000/19:33:50+1:00
author: "volker"
purpose: {
here is a context-simulator to show what happens with
binding. AFAIK. in a way Elan is right with inheritance,
virtual function tables in c++ are created similar.
on the other hand he is "wrong", because scoping in
rebol can be a bit confusing, as Gabriele showed with
objects. but inheritance with objects is made by 'make
in another way,
I think one should think about binding by replacing
same-named addresses from another context
with functions bindings are summed, and each new call
to 'func binds its body to its argument-list. this
replaces all bindings to global names, if this names
are local variables. so getting "scoping".
for parameters i think Gabrieles push/pop arg-list-values
are right.
in Gabrieles nested-object sample for scoping he should
write [make make make object! [][][] ] to get scoping,
in his sample all stuff is bound to global, then 'object! . it can't
be bound to the static scope (=brackets around it
in sourcecode) because at block-creation no sub-context
exists..
>> obj1: make object! [
[ a: 2
[ obj2: make object! [
[ b: 2
..
Volker
}
]
{we nedd a memory-simulation}
memory: array 10
set-m: func [n][pick memory n]
get-m: func [n v][poke memory n v]
top: 1
new-adr: func [/local res][res: top top: add top 1]
is-unset: 0
{and one for contexts}
;note the need for copy contents! use make-context!
context: [words: make hash! [] addresses: make block! []]
new-context!: func [][make object! context]
get-adr-for: func [context word /local there adr][
there: find context/words word
either there [adr: pick context/addresses index? there] [none]
]
set-c: func [context word value /local adr][
adr: get-adr-for context word
if not adr [
adr: new-adr
append context/words word
append context/addresses adr
]
poke memory adr value
]
get-c: func [context word /local][
pick memory get-adr-for context word
]
{to simulate and dump code-blocks this}
code-entry!:
make object! [
context: none
word: none
adr: none
]
make-code-entry: func [.word /local id .adr res][
make code-entry! [context: 'no-context-yet word: .word adr: is-unset]
]
{and a bind-simulation (not for subblocks yet)}
bind-c: func [.context code /local adr][
foreach entry code [
adr: get-adr-for .context entry/word
if adr [entry/adr: adr entry/context: .context]
]
]
print {^/^/now the testing}
print {^/--- some data^/}
a-context: new-context!
another-context: new-context!
some-code: reduce [
make-code-entry 'a-word
make-code-entry 'b-word
make-code-entry 'c-word
]
? a-context ? another-context print "" ? some-code ? memory
print {^/--- filling a bit in^/}
set-c a-context 'a-word "a-word"
set-c another-context 'b-word "b-word"
set-c a-context 'c-word "c-word-in a-context"
set-c another-context 'c-word "c-word-in another-context"
? a-context ? another-context print "" ? some-code ? memory
print {^/--- binding first to a-context^/}
bind-c a-context some-code
? some-code
print {^/--- binding then to another-context^/}
bind-c another-context some-code
? some-code
print {^/in "original" the binding is performed to all sub-blocks}
]