How to Create a REBOL Dialect

Starting to Build Business Rules with REBOL Programming

How to Create a REBOL Dialect - Mark Alexander Bain
How to Create a REBOL Dialect - Mark Alexander Bain
The REBOL programming language contains an important concept. That of Dialects. With a dialect the programmer can start to write business rules.

The most exciting thing about REBOL is its dialects. This does not mean that the programming language changes according the part of the world in which it is used.

Instead it means that the programmer can produce a project or problem specific set of grammar rules that can handle blocks of data. REBOL (see A Brief Introduction to REBOL Programming) can then parse the data through the grammar rules. This makes REBOL the perfect language for creating business rules.

Creating a Very Simple REBOL Dialect

A REBOL dialect consists of:

  • a block of statements
  • a block of grammar rules
  • the parse method which applies the grammar rules to the block of statements

Fortunately the programmer can carry out each of these steps very quickly. That is, of course, assuming that they have a good understanding of the rules to be modelled.

Creating a REBOL Block of Data

A REBOL block of data is simple a statement defining the data to be used. For example, consider the block:

daughters: [
[name "Mary-Jane" age 18 got-home 22:30:00]
[name "Joleene" age 21 got-home 24:30:00]
]

Here the business rule is simple: whoever gets home on time is allowed out again; whoever gets home late is grounded. The next step is, therefore, to create the grammar rules.

Creating REBOL Grammar Rules

First any required variables need to be set:

home-time-rule: [
some [
['name [set who string!]]
|['got-home [set actual time!]]
|['age opt [set age integer!]]
]

There are a few points to take note of in this code:

  • the "some" keyword implies that one or more matches are to be made. As well as "some", the following can also be used:
    • none - no matches to be made
    • any - none or more matches to be made
  • specific words are matched by using the single quotation mark at the start of the word. For example, 'name will match the word "name", 'got-home will match the words "got-home". And this raises an important distinction:
    • "got - home" means variable "home" subtracted from variable "got"
    • "got-home" is a single variable name
  • data types are matched by using the exclamation mark at the end of the word. For example "string!" will match to any string types, "time!" with match to a time", etc
  • alternative matches are added by using |
  • optional matches are defined by using the opt key word

Next the business rule for the grammar block can be created:

(either actual <= 22:30:00
[(print [who "is allowed out again"])]
[(print [who "is grounded"])]
)
]

REBOL does not have an if..then..else statement. Instead it uses either in the form:

(either
[true statement]
[false statement]
)

Now the dialect is ready for use.

REBOL Parsing

The REBOL dialect is accessed by using the parse key word. The technique consists of stepping through the data block and applying the grammar rules to each sub-block:

foreach daughter daughters [parse daughter home-time-rule]

And the end result is:

Mary-Jane is allowed out again
Joleene is grounded
Of course, this a very simple business rule (after all Joleene will probably complain about having to come in at the same time as her younger sister) however it does show how easy it is to create a REBOL dialect. In a very short time a programmer can build extensive and complicated business rules which can be applied to any relevant data.

Mark Alexander Bain - Mark Alexander Bain is a writer, Mo Bro and consultant for all aspects of software development at dsquared. He has also written regularly ...

rss
Advertisement

Comments

Jul 9, 2009 2:40 PM
Guest :
Nice Tutorial on Rebol. Hope that will evangelize this underated language.
This article is featured on http://reboltutorial.com
Dec 9, 2009 9:02 AM
Guest :
Very nice to see a new article about dialects! More needs to be written about this topic.

- Nick (http://re-bol.com)
2 Comments
Advertisement
Advertisement