Red Foundations

From Programming in RED

(WORK IN PROGRESS)

Human languages use words and symbols. Each word may have a different meaning, depending on the context it has been used or from the context given by the user of the word. Let's take as an example the word PLAY: it can be used to play a song, or play a game, it could represent the round of a game or it can be a command to a human. Rebol and Red language have the ability to give the same word different meanings. So, in a phrase you can have 3 subsequent words which are totally different in their meaning: PLAY %shine.MP3 PLAY %Frontier-Elite.exe or print PLAY

As a programmer, you have the power to create a sequence of action to instruct the word to do something. Or you can also assign a number to the word. You have the power, you are the creator!

In this case you create a sequence of action and play the sone whose name appears after it

PLAY %shine.MP3

This happens because the word %PLAY has internally a link to the associated action or value. (Change)

The action to perform is stored in a container called Table where the sequence is stored

Example: Table A


|PLAY | [execute-player %shine.mp3]| +-----+----------------------------+ | | |


Table A


|PLAY | [run %Frontier-Elite.exe] | +-----+----------------------------+ | | |


Or the word can be associated with a value:

Table B


|PLAY | 1 | +-----+----------------------------+ | | |


So, when you start your program and the computer finds

PLAY ..... it reads the internal link to the table as

PLAY(B) and then reads the instruction in the table B for the word PLAY and excutes them, thus starting the game Frontier-Elite

So in the following sequence

PLAY %shine.MP3 PLAY %Frontier-Elite.exe or print PLAY

The word PLAY really does different things even if it seems to be the same on the 3 instances.

Now lets take this:

All Redbol elements are contained in a block.

So,

PLAY %shine.MP3 PLAY %Frontier-Elite.exe print PLAY

is really

[PLAY %shine.MP3 PLAY %Frontier-Elite.exe or print PLAY]

When RUN, the interpreter really reads

[PLAY(A) %shine.MP3 PLAY(B) %Frontier-Elite.exe or print PLAY(C)]

Getting the content of play from the corresponding connected tables.

///

You can make a long discourse reusing the same words, so being concise and expressive like human languages are.

Red can manage words but also numbers, dates.... But WORD is the ony one that can have an entry in the meaning table. So in the right part can appear every element managed by red, in the left part, just words!


But also in red you can do everything you want, changing the basic workings, so you could change its logic and create you or sequences manager so that writing

%shine.mp3 a play

Does the same as before but just in a different order



(Let's introduced manipulating block first, using RED Elements)


A Red program is just a set of elements in a container called a block. The code you create in your text editor, once loaded, it is inserted inside a block whose square braces are not visible.

Here is a simple program:

[Red [Tittle: {My Program}] a: 22 Probe a]

DO [Red [Tittle: {My Program}] a: 22 Probe a] 22 == 22 >>

Let's assign to the word my-program the above block of code

my-program: [Red [Tittle: {My Program}] a: 22 Probe a]

And run it

DO my-program 22 == 22 >>

The result is the same.

A block is composed of elements recognizable by Red interpreter. The above block is made of 6 elements:

probe length? my-program == 6 >>

Let's print the first element

probe pick my-program/1 == Red >>

Now, let's print the last element:

probe pick my-program/6 == a >>

Or use a shortcut:

probe last my-program == a >>

We can modify the program to print 33 instead of 22:

change at my-program 4 33

Read it as: change (at my-program 4) 33

Run the code on the console:

>> change (at my-program 4) 33 == [Probe a] >> probe my-program [Red [Tittle: "My Program"] a: 33 Probe a] == [Red [Tittle: "My Program"] a: 33 Probe a] >>

And run the code:

>> do my-program 33 == 33 >>

Now the program has been changed and it outputs 33


You have the following bricks of Red Element in blocks:

set-words: [a: a: a:]

numbers: [22 33 55]

istructions: [probe probe probe]

blocks: [[] [] []]

code-to-execute: []

...


They can be in any order when stored but to be executed the order should follow some simple rules.


When not interpreted, elements can be in any order, when you evaluate the block, a set of rules are applied to interpret the block elements a data takes life in a program.