Words form the grammar of the Red programming language. For people familiar with more mainstream languages, word!
replaces keywords, variables, and operators. However, unlike other programming languages where those constructs have fixed definitions hard-wired in the compiler, in Red words are just a basic type of data. This makes the structure of a program extremely flexible.
A word!
is made of up of 3 basic parts.
- name - The symbolic representation of the word. It's just the text that you typed to create the word
- context - This determines the meaning of the word. Just as in spoken language, a word can change its meaning depending on the context. In Red, a context is a mapping of words to their meanings.
- value - This is the meaning of the word; it can be any Red value. When a word is evaluated (as code), its name is looked up in the associated context, then if it is a function it will be invoked. Otherwise the corresponding value is returned.
A binding associates a word to the context where it gets its value. You can change the meaning of a word by binding it to a different context.