Notes on syntax
Red is case insensitive, but there are few exceptions, the most relevant is that a program must begin with Red (not REd or red).
new-line
characters are mostly ignored by Red interpreter. A relevant exception is anew-line
inside a string.Red is a functional language, meaning that it evaluates a result. The evaluation order is not usual and you may be interested in skipping to chapter Evaluation.
(the following topics may prove to be inacurate, but so far they have explained Red behavior pretty well)
A Red program is a long chain of "words". These words may be either "data" or "actions".
"words" are separated by one or more whitespaces .
Red keeps a dictionary with system's words and user-created words.
"words" may be grouped into "blocks" by enclosing them with brackets. "Blocks" are not necessarily routines, they are just a group of words that may, or may not, be evaluated by an "action".
all the program data is inside the program itself. If external data is required, it is added to the program's chain of "words".
every word must have a value while evaluated. This value may come from:
- the word itself, if it is data;
- evaluation, if the word is an action;
- another word or block. This is achieved by adding a colon after the word, with no spaces, followed by the data or block we want to associate it with (e.g.
myRoom: 33
).
I find that in Red, you may say that the variable is assigned to the data, and not the other way around.
Copying variables in Red may be tricky.
Simply assigning a variable's value to another variable just copies the "pointer" of the original variable. If the value of the first changes, the value of the second variable also changes.When you want truly independent copies , you must use the wordcopy
to . See Copying chapter.As with copying, clearing a series (notice that all strings are series) is also tricky. Simply assigning "" (empty string) or zero to it may not produce the expected results. Red's logic makes it seem to "remember" things in unexpected ways. So to clear a series you should use the command
clear
.every word has a datatype. Red has a remarkably large number of datatypes. They are listed in the Data types chapter.
When a word is first created, it has the datatype word! that is then used as follows:
Notation | Meaning |
---|---|
word | Get thenatural valueof the word. (If the value is a function, evaluate it, otherwise return it.) |
word: | Sets the word (like assignment) to a value. |
:word | Gets the word's value without evaluating it. (Useful for getting the value of a function.) |
'word | Treat word as a value (a word symbol). Does not evaluate it. |
/word | Treat the word as a refinement. Used mainly for optional arguments. |
Somewhat simplified view of Red's flow:
Note: The function that picks data from before it (the third from left to right) refers to infix operators like "+", "-" , "*" , "/" etc.
Refinements
Many actions in Red allow "refinements". A refinement is declared adding "/<refinement>" to the command and it modifies the behavior of the command.