Wiki

Clone wiki

MindStream / Articles in English / Script engine organisation / Introduction

Caching. Introduction.

The “previous” series was here (in Russian)

Now, I’d like to talk about functions values caching.

In order for you to understand the idea expressed hereafter, I would recommend you to have a look at the article.

All our elements, i.e. the “models” as well as the “script engine dictionaries”, are as follows:

#!delphi
: OuterElement1
 CONST Const1 Value1
 ...
 CONST ConstN ValueN

 VAR Var1
 ...
 VAR VarN

 : InnerElement1
  InnerElementCode1
 ; // InnerElement1
 ...
 : InnerElementN
  InnerElementCodeN
 ; // InnerElementN

 OuterElementCode1
; // OuterElement1
...
: OuterElementN
 CONST Const1 Value1
 ...
 CONST ConstN ValueN

 VAR Var1
 ...
 VAR VarN

 : InnerElement1
  InnerElementCode1
 ; // InnerElement1
 ...
 : InnerElementN
  InnerElementCodeN
 ; // InnerElementN

 OuterElementCodeN
; // OuterElementN

What is it about?

Let us agree this way: the “element of the dictionary” and the “word” are SYNONYMOUS unless otherwise stated.

: - specifies the new element of the dictionary (that can pop as well as push as many values in stack as needed).

CONST specifies the constant in the element of the dictionary.

VAR specifies the variable in the element of the dictionary.

[XXX]ElementCode[YYY] is the “code” of the word that is executed when the word is “called”.

; ends the definition of the word.

Let’s move on.

To begin with, let us be clear about the “code of the word”.

This is the example:

#!delphi
: A
 1
; // A

A Print

What does it do?

It calls the “code of the word” A which is “1” in this case.

What do we get?

The integer value “1” is printed in the console.

How?

First, the construct ": A" specifies the word A in the dictionary.

Then, the construct "1" specifies the “code of the word” A that can push the value “1” in the stack.

The construct "A Print" then calls the word A (that uses the value “1” in the stack) and after that the word Print is called (that takes the value “1” of the stack and prints it in the console).

Now, let us consider the constants.

Our constants are the named fixed values as in any other programming languages (that I know).

We rewrite our example:

#!delphi
: A
 CONST One 1
 One
; // A

A Print

What is going on?

Generally speaking, this code prints the integer value “1” - as in the previous example.

What has changed?

Instead of direct access to the value “1”, the constant “One” has been introduced to represent the value “1”.

Thus, the “code of the word” has changed from “1” to “One”.

The word “One” pushes the value “1” in the stack similarly to the word “1”.

This means that the “One” constant is kind of an alias to the value “1”.

Obviously, we can write in this way:

#!delphi
: A
 CONST One 1
 CONST OneAlias One
 OneAlias
; // A

A Print

Although, actually, it is not that obvious.

I hope you get the idea.

Let’s move further to variables.

Our variables, as in any programming language I know, are the “named memory cells” that can store computed values.

These “memory cells” can change while executing the program.

How can we use them?

We rewrite the example:

#!delphi
: A
 VAR One
 // - the variable One introduced
 1 >>> One
 // - the value "1" assigned to the variable One
 One
 // - the value of the variable One pushed in the stack
; // A

A Print

What does this code do?

Well, just the same as the previous one did: prints the integer value “1” to the console.

But, now it uses variables instead of constants for it.

I hope it is clear, too.

Let’s move on and mix variables and constants.

We rewrite the example:

#!delphi
: A
 VAR One
 // - the variable One introduced
 1 >>> One
 // - the value "1" assigned to the variable One
 CONST Two 2
 // - the constant Two introduced
 One
 // - the value of the variable One pushed in the stack
 // - the value of the constant Two pushed in the stack
 +
 // - values pushed in the stack and the result pushed back to the stack
; // A

A Print

What is going on here?

The integer value “3” computed from the One + Two is printed.

One is a variable and Two is a constant.

That’s it for the introduction.

What have we done?

The “constants”, “variables” and “words” have been specified.

We have also demonstrated how these terms are used.

Updated