Context and word
When we enter a piece of code, how does the REBOL interpreter know how to execute this code?
In fact, when the REBOL interpreter is started, the Lib is prepared in advance.
Context. Context is a table that corresponds to a single word.
To its definition. These REBOL
The interpreter pre-defines a single word in the Lib context. I call it a REBOL built-in word,
Are common words. The definition of a single word may be a program (function) or a general value (such as an integer ).
When the REBOL interpreter sees a single word, It will query it in the Lib context and learn how to handle it. Take power
For example,
The REBOL will find its definition in this context, know that this function must calculate the power (and how to calculate the power), and
Two parameters are required: base number and index.
Some single words are defined as programs. Such programs are called functions. Some are not programs, but static values, such
Pi (circumference rate) is defined as 3.14159265358979. Some single words are completely the same, such as Q
And
Quit. When executing a program, some words do not affect other values (that is, no parameters are required), such as what-Dir, ls,
Pi. Some single words will affect the following values (that is, parameters are required), such as do and print. Some single words will affect
A value (that is, a parameter must be placed before and after the parameter), for example, +
And /.
If we want to know how many words are defined in Lib, we can use length? Lib
. If you want to know the Lib
Which words are defined in context and can be input in an interactive environment? Lib
Command line to see its internal definition. Take a closer look,
There is a word called red, which is defined as 255.0.0. If there are too many words, we cannot see all the definition of words,
We can consider using echo described in Chapter 1.
Function, output the result to a file, and then study it slowly.
If you only want to see which words you have and do not want to know the definition of the word, you can use the words-of function to obtain the Lib
All words in the context, but still can only see a part of them because the returned data volume is too large. In this case, we can use
In the foreach loop mode, the single words are printed one by one. If you do not understand foreach usage, do not worry.
The three sections will be detailed.
What if we change the definition of a single word in the Lib context? Red
The original value is 255.0.0 (bright red ).
Set red
Set it to 200.0.0 (a little dark red ). Observe lib again, but find that my settings are ineffective. Why
Will this happen? Is my settings not recorded. I enter red in the command line to obtain a new value.
I suspect that another context exists. All definition changes will only affect this mysterious context, but will not affect lib
Context.
Next, perform the experiment. First, confirm that Lib does not contain my-name, set my-name to "Jerry", and then go to Lib.
And cannot see the existence of my-name.
After the above two experiments, I figured out that there should be another context specifically used to record the single-word settings in the user code execution process.
Meaning change.
Yes, there is indeed such a context. It is called the user context!
User context and Lib
The division of labor between contexts is clear:
• Lib
Context provides a variety of frequently-used single-word definitions, so that we can use them when writing code.
• The user context is used to record changes that occur during user code execution.
Use self
You can access the user context. Pass first? Self
Let's look at the words in the user context, and
The value of a single word. We found that the previously modified red
And the newly added my-name are all here.
What if I change the definition of a REBOL built-in word (for example, print? Connect
After experiment, we found that we modified the version in the user context, rather than the version in Lib.
After the experiment and observation above, there is no single word in Lib in the user context, and only one word is used. Each time
Enter a line of code to add a new word to the table.
All in all, before the code is executed, the system will find all the words in the Code and add them to the user table. For
The word that already exists in the user is not affected. The newly added word will try to get the definition from LIB and then copy it.
To the user context. In this way, we only need to use the user context when executing the code. However
The REBOL interpreter will automatically help us with everything we have mentioned here, so we don't have to worry about these troubles.
In general, after the code is cut, before the code is executed
The interpreter has some preparation work to do.
1. Add the new word used in the Code to the user
In context, the definitions of these new words are special unset values,
Indicates that it has not been set.
2. To lib
Search for the definitions of these new words in the context, and copy these definitions to the corresponding words in the user context.
After completing the above preparations, you can start to execute the code. Through the above preparations, the execution of the time code only requires Parameters
Test the user context without referring to the Lib context.
This article is excerpted from "programming ing: everyone can learn programming ".
Dr. Cai
Published by Electronic Industry Publishing House
Book details: http://blog.csdn.net/broadview2006/article/details/7768124