REBOL 3 Docs Guide Concepts Functions Datatypes Errors
  TOC < Back Next >   Updated: 20-Nov-2013 Edit History  

REBOL 3 Guide: Code: Basic syntax

To create REBOL scripts, you need to understand its basic syntax.

Contents

What makes a language?

Languages are built on lexicon, grammar, and semantics.

lexiconare the written symbols of the language - its literal syntax. The lexicon is what defines that 123 is an integer and "ok" is a string.
grammarprovides rules that control the order of symbols used to form expressions. It is what makes a line like "print 1 + 2" have meaning as a sequence of symbols, just as sentences do in natural languages.
semanticsassociates meaning to symbols and values, and in an interpreter such as REBOL, also performs computation.

REBOL's lexicon is advanced, because it allows many datatypes to be directly expressed. However, its basic grammar is very simple - mostly functions with arguments, but it does allow specialized grammars, dialects of the language. This gives the language some real power to build a wide range of expressions. And, REBOL's semantics are quite different - based on rules of context and association.

That's enough about theory for now. Let's get into some details.

Elements of the language

Both code and data are combinations of elements called

valuesthe datatypes of the language

Important values are

wordsnotated in a few ways.
blockscombinations of values.

There are also

commentstext that is ignored by the language

Special characters

White-space is used in general for delimiting (for separating values).

This is especially important because words may contain characters such as + and -.

For example, this line is an expression consisting of 5 words:

a + b - c

, while this line is actually a single word a+b-c

a+b-c

Other special characters are

[ ]mark the start and end of a block
( )mark the start and end of a paren
/used to "glue together" paths
{ }mark the start and end of a string
" "mark the start and end of a short string fitting into one line of text
;mark the start of a comment to the end of line

Words

Words are used for symbols and variable names.

They are formed from a non-numeric character followed by alphabetic characters:

print show next image
on off true false one none good1

They can include hyphens and other special characters:

+ - `  * ! ~ & ? |

So, you can make words like:

number?  time?  date!
image-files  l'image
++ -- == +-
***** *new-line*

Words are not case sensitive, but casing is preserved:

state = State = STATE

The end of a word is marked by a white-space character, end of text or one of the characters:

] ) /

More on words can be found in the next section, code: words and variables.

Values

Values are the raw data of REBOL.

A wide variety of values can be written directly. No special constructor is required. You simply write the values as is.

The language strives to use human-friendly forms for values. This makes scripts more readable. In addition, it accepts more than one format for some values to allow for international variations.

Numeric data:

1234 -432 5'678 ; integer

3.1415 1.23E12 0,01 1,2E12 ; decimal

1% 100% 12.34% 1234% ; percent

$12.34 ; money (arbitrary precision)

3.1.5           ; version numbers
255.255.0.100   ; colors
199.4.80.7      ; network addresses

5x10 100x100 -50x-25 ; x/y coordinates/sizes

Times and dates:

12:34 20:05:32 -0:25.34

20-Apr-2008
20-4-1998
20-Apr-1998/12:32        ; date and time
20-Apr-1998/12:32-8:00   ; with timezone

Strings and bytes:

"Here is a string"
{Here is another way to write
a string that spans many lines
and contains "quoted" strings.}

#{0A6B14728C4DBEF5}      ; hex encoded
64#{45kLKJOIU8439LKJklj} ; base-64 encoded

Special strings, such as files, tags, URLs, and email forms:

%data.txt
%images/photo.jpg
%../scripts/*.r

<title> </body>
<font size="2" color="blue">

http://www.rebol.com
ftp://ftp.rebol.com/sendmail.r

info@rebol.com
bill-smity@example.com

More details are found in expressions: values.

Blocks

Words and values are grouped into blocks, enclosed in square brackets [].

Within a block, values and words can be organized in any order and can span any number of lines.

Example blocks:

[1 2 3 4.0 5% $6.00]

[white red green blue yellow orange black]

["Spielberg" "Back to the Future" 1:56:20 MCA]

[
    "Bill"  billg@ms.com  #315-555-1234
    "Steve" jobs@apl.com  #408-555-4321
    "Ted"   ted@gw2.com   #213-555-1010
]

[
    "John"  6894  0:55:68
    "Dion"  68861 0:61:35
    "Floyd" 46001 0:50:12
]

if time > 10:30 [send jim news]

loop 100 [print/only "*"]

sites: [
    http://www.rebol.com [save %reb.html data]
    http://www.cnn.com   [print data]
    ftp://www.amiga.com  [send cs@org.foo data]
]

foreach [site action] sites [
    data: read site
    do action
]

You can see that both code and data use blocks.

Why brackets, not braces?

REBOL uses square brackets because they are un-shifted on the US keyboard. Basically, they are easier to type. (Of course, that's not true on non-US keyboards, where it's probably best to setup a keyboard macro for [ and ].)

Also, they seemed easier to read than braces (because they render as a cleaner glyph.)

Also, REBOL's design was influenced just a small amount by Logo, the language from MIT created to help teach programming in schools.

Comments

A single-line comment is made with a semicolon. Everything following the semicolon to the end of the line is part of the comment:

; Settings follow below:
quality: 100   ; set to the highest quality

Another form of comments also exists. See scripts: comments for more.


  TOC < Back Next > REBOL.com - WIP Wiki Feedback Admin