Wiki

Clone wiki

MindStream / Articles in English / Script engine organisation / Let us talk about adding the nested elements

Caching. Let us talk about adding the nested elements

The previous article was here.

The organisation of the words in the dictionary was discussed in it.

We have also discussed how the internal elements of the words can be got.

The following question aroused: as we can get internal elements of the word, can we add new elements?

Yes, we can.

Now, I will show how.

We have already considered the words %% and ::. Now, let’s consider the word ->.

It is defined as follows:

#!delphi
OBJECT operator ->^
 ^@ IN aSelf
 ^ IN aName

 STRING VAR l_Name
 aName DO >>> l_Name

 OBJECT VAR l_Self
 aSelf |^@ >>> l_Self

 if ( l_Self pop:object:IsNil ) then
 begin
  nil >>> Result
 end
 else
 begin
  VAR l_NewVar

  l_Name l_Self pop:NewWordDefinitor:CheckWord pop:KeyWord:Word >>> l_NewVar

  if ( l_NewVar pop:object:IsNil ) then
   (
    l_Name false l_Self pop:NewWordDefinitor:CheckVar

     >>> l_NewVar
    Ctx:ClearTypeInfo

    @ VAR l_NewVar pop:Word:SetProducer
   )

  l_NewVar >>> Result
 end // l_Self pop:object:IsNil
; // ->^

^@ operator ->
 ^@ IN aSelf
 ^L IN aName

 aSelf ->^ ( aName |N ) >>> Result
; // ->

Even two words are defined here: ->^ and ->.

The word ->^ will be considered further.

At this point, let’s look at the example of using the word ->:

#!delphi
: A
; // A
A -> X := 2 // adds the variable X to the word A and assigns the integer value “2” to it.
X -> X Print // prints the value of the variable X of the word A.

What is going on here?

First, the word A is declared here.

Then, the variable X is added and the integer value “2” is assigned to it.

After that, we get the reference to the variable X of the word A and print its value.

What do we get?

We find out that our scripts have reflection.

This means we can manage the code from the scripts using the scripts.

Not only we can add the variables to the script words but add or get the metainformation about the words as well.

This is what my next article is about.

Updated