Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] REBOL SCOPING / CONTEXT EXPLANATIONS Re:(3)

From: bhandley::zip::com::au at: 28-Jul-2000 1:45

Well this deserved a response, since it's has consumed so much of my time :) I do not claim my response to be elegant, but I hope it is practically accurate. And I want to know if I'll be eligible for that master's degree! A description of Carl's Completely Confusing Complex Contexts. b: function [] [b] [ make object! [ b1: make object! [a: "Time" b: [a]] b2: make b1 [a: "for you"] b3: make b2 [a: "to leave!" b: does [a]] set 'b [b1 b2 b3] get-it: func [b] [get in b 'b] ] b ] 1) A set-word starts evaluation, this involves first evaluating it's parameters. 1.1) The parameter to the set-word is the "function" function - it begins evaluation. 1.1.1) A "b" word is found in the vars parameter to the "function" function and thus becomes a part of the context of this function. 1.1.2) The body block becomes the body of the function. 1.2) The function returns the specified function and this used as a value to define the global word 'b var: [] 2) Another set-word is encountered and process, it results in a block being created as a value which is used to define the global word "var". foreach b b: b bind [append var get-it get b] first b 3) foreach is encountered and a new temporary context is created, and it's first parameter - the word "b" becomes part of that context. 3.1) The second parameter to foreach is evaluated. It is a set-word. The set-word evaluates it's parameter which is the global word "b". This word has the value of the function created in 1). 3.1.1) The function is evaluated. 3.1.1.1) A make is encountered and evaluated. The type parameter to the make is object! so an object definition dialect is used to interpret the spec parameter of make. This dialect applies any set-words (but no "set" expressions) in the spec block to the context of the object being created. In this case there will be four such set-words. 3.1.1.2.1) During interpretation of the object spec block, a set-word is encountered and evaluated. The parameter to this set-word is evaluated and it a make which has parameters that are evaluated. The type is object!. This initiates interpretation of the spec block using an object definition dialect. An object context will apply to the spec parameter. The spec block is evaluated and any set-words in the block apply to context of the object being created. The second set-word in this spec block sets the word "b" to have a value of a block that contains a word "a". 3.1.1.2.2) The next set-word is evaluated in the same manner as the first. The type parameter to the make is evaluated and returns an object. The spec parameter to the make is evaluated and applied (in make+datatype fashion) to the creation of a new object. 3.1.1.2.3) The next set-word is evaluted in the same manner again. During this evaluation, the second set-word in the spec block evaluates the word does . The word "does" evaluates it's parameter and returns a function that will return the word "a" in the context of the object. 3.1.1.2.4) A set expression is encountered it's parameters are therefore evaluated and this results in the word "b" of the function context having a value of a block containing the three words "b1 b2 b3". Note that each of these three words are defined in the context of the object. 3.1.1.2.5) The last set-word of this object is encountered. It results in the word "get-it" defined as having the value of a function. This function takes a parameter, locally known as "b", and applys an "in" expression to it (so "b" better be an object). The in expression finds in the context of the object a word literally named "b". The result of this is passed to the "get" expression in order to look up a value of the returned word. This then will be the result of the function. 3.1.1.2.6) The make object! is concluded and an object is returned from the evalutation. 3.1.1.2) The word "b" is evaluated and returns the value set there in 3.1.1.2.4. This then becomes the result of the function. 3.1.2) The global word "b" is then set with the value returned from the function. Ie a block of three words "b1 b2 b3". This then is returned to become the data block of the foreach. 3.2) The body parameter of foreach is evaluated. 3.2.1) bind is encountered and it's parameters are evaluated. 3.2.1.1) bind evaluates known-word parameter - thus the expression "first b" is evaluated and so this results in returning a word whose definition is in the context of the object created by the function in 3.1.1.2.6. 3.2.1.2) bind binds each word of the data block that it can find in the context of the known-word to that same context. In this case "get-it" is bound to the object context. 3.2.2) bind returns the bound block. 3.3) foreach begins evaluating the body block (bound block) for each element of it's data block - setting the value of it's iterator word as it goes. 3.3.1) On the first iteration the local "b" word is set to the word "b1". 3.3.2) The body block is evaluated. Thus append's parameters are recursively evaluated. So that "get b" evalutes to b1. "get-it b1" calls the function in the object and thus returns the value of the word "b" in the object that is associated with "b1". This value (a string) is then appended to the value (a block) of the global word "var". 3.3.3) The same goes for the other two iterations, except that for "b3" a function of the nested object returns the value of "a" in that context. Fiiiinnnalllllyyyy... print var 4) You can guess. Brett Handley. ----- Original Message ----- From: <[carl--sassenrath--com]> To: <[list--rebol--com]> Sent: Friday, July 21, 2000 3:10 AM Subject: [REBOL] REBOL SCOPING / CONTEXT EXPLANATIONS Re:(2)
> Good explanation. Fun with contexts. I like the "do" not "try" zen yoda
thing. Nice touch. Here's the next one to zen. "Fetch these objects from my hand..."