How to Create a REBOL Dialect

Starting to Build Business Rules with REBOL Programming

© Mark Alexander Bain

Jul 6, 2009
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.

The copyright of the article How to Create a REBOL Dialect in Computer Programming Languages is owned by Mark Alexander Bain. Permission to republish How to Create a REBOL Dialect in print or online must be granted by the author in writing.


How to Create a REBOL Dialect, Mark Alexander Bain
       


Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo

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
1 Comment: