REBOL Programming/Language Features/Dialects

From Wikibooks, open books for an open world
< REBOL Programming‎ | Language Features
Jump to: navigation, search

A lot of the power of REBOL comes from its concept of dialects. A dialect is a sub-language used for a specific purpose (also called a domain specific language).

They're Everywhere[edit]

It's like buying a Volkswagen Bug. Once you own one, you start to see them everywhere. That's true for dialects as well. And, even better, once you know what they are, you start to add them to your code and use them to gain greater programming leverage in writing applications.

You use dialects a lot in REBOL, even though you may not realize it. For example, when you write:

sum: func [num1 [number!] num2 [number!]] [num1 + num2]

The block that follows the func word is actually a dialect. It is not "normal REBOL code" (also called the Do dialect), it is a special little language that is used for defining the arguments of functions.

Similarly, when you create GUI you often write it in VID, the visual interface dialect. A snip of VID might look like this:

image %nice-graphic.jpg
button "Go" [do something]
button "Stop" [quit]
the-field: area "Enter text here"

You may not realize it, but you use a dialect when you tell the parse function how to parse strings or blocks:

thru <title> copy title to </title>

When you want to control the security for your program, you also use a dialect:

net allow
file [
    ask all
    allow read
    quit execute
shell quit

So, there are many such dialects in REBOL.

Definition of a Dialect[edit]

The definition of a dialect in REBOL is very simple: any loadable expression is a dialect.

That is, for a dialect to be valid, you must be able to use the load function to load it into REBOL. The dialect must be a valid lexical expression of REBOL. If a string cannot be loaded, then it is not a dialect (it might be a valid language of some kind, but it is just not a dialect).

For example, this code shows that a given string is a valid dialect of REBOL because it can be loaded:

>> load {sell 100 shares "aacmee" now}
== [sell 100 shares "aacmee" now]

But, the example below is not a valid dialect. It cannot be loaded.

>> load {move 100-200 draw @line}
** Syntax Error: Invalid date -- 100-200

Nor is this line of C code a valid dialect:

>> load {for (i = 1; i < 10; i++) print(i);}
** Syntax Error: Missing ) at end-of-script

These examples contain character sequences that are not valid in REBOL; therefore, they are not dialects of REBOL.

This loadability factor is part of what makes dialecting so special in REBOL. You don't need to write a lexical string scanner. You get to skip that part and get to the meat of your special language.

Not Just Code, Data Too[edit]

Dialects are not only useful for code expressions, they are equally productive for data storage (and data exchange over the net).

It is important to recognize that a dialect does not require that you write a parser or any other complex method to interpret the meaning of the data. Even a block as simple as this is considered to be a dialect:

 "Bob" 21 $100

This is a dialect because it is a loadable REBOL string. The way you interpret it is up to you, but it can be as simple as using a multivariable assignment:

 data: load {"Bob" 21 $100}
 set [name age email payment] data

Here is a more complex data dialect that is used for holding data about students in a REBOL-based class reunion program. This is a single record, but we've broken it into separate lines so you can see the details:

37 "Bob" "Smith" "1000 Example Dr." "Example" "CA" "90000"
spouse "Linda Smith"
phone "555-1234"
package [all 2 true]
paid $70.00
bio {I went to school for Industrial Instrumentation.}
approve [book-name book-bio]

The words in this dialect (like spouse, phone, package) are used for optional fields of the record.