[ Home ]

rpp Quick Reference

Contents

  1. Simple Variables
  2. Lists
  3. Control Structures
  4. Functions
  5. Rules and Matching
    1. Patterns
    2. Rules
    3. Matcher
  6. The 'input' Variable
  7. The 'system.rpp' File
This is a brief overview of the current functionality of the rpp language.

Simple variables

The typical syntax for a variable definition is:
var [variable] : [type] = [value]
Basic types include int, string, and bool. Some examples:
var _Intr : int = 3
var _Str : string = "test"
var _Bool : bool = true
A string is always written in quotes, so therefore "Test" describes a constant string with value Test. An integer is always written simply as a number, as in -1 or 500. A boolean value is written either as true or false

Lists

Defining a list is also straightforward. nil is used to represent an empty list. A non-empty list is represented using the :: operator, and is terminated by a nil. An example of a list is 1::2::3::nil.

There is a shorthand notation that can be used when defining a list (and only with a list of simple datatypes). An example of this shorthand is '(1,2,3)

An example of defining list variables:

var _IntLst : int list = '{1,2,3,4}
var _NullLst : string list = nil
You can also construct a list like this:
var _List : string list
_List = "One" :: "Two" :: "Three" :: nil
print_str_list(_List)
A list is always denoted as either nil, which is an untyped empty list, or as a cons-delimited list.

Operations on a list

The head element of a list can be extracted using the head, or hd, function:
var hd : 'a list -> 'a
hd(_List)
which in the case above would return "Three"

The rest of the list can be extracted using the tail, or tl function.

var tail : 'a list -> 'a list
tl(_List)
Which in the example above would return "Two"::"One"::nil.

Control Structures

Control structures in rpp are very like those in other languages.

If..else..

if (true) 
	println("True")
else 
	println("False")
or
if (i < 3) 
	println("almost finished...")

for

the for statement is also just like C's:
var lst : int list
var i : int
for (i = 0 ; i < 10 ; i = i + 1) {
	lst = (i :: lst)
	print_int_list(lst)
}

dolist

The dolist requires a little extra description.
dolist ( [var] : [type] ; [conditional] ; [list]) 
{
	[block]
}
Where:
dolist (_S : string ; true ; x)
{
	print(_S ^ "::")
}

Functions

Since the system print() function doesn't print a new line at the end, here is an example of how a println() function can be written (actually, this function is included in the system.rpp libs).
fun println(_S: 'a) : unit {
	print(_S)
	print("\n")
}
The last line of the block is returned, so an example of a function that triples its parameter woudl be:
fun triple(x : int) : int {
  3 * x
}
Here is an example of a function that returns a function.
fun mymult (x : int) : int -> int 
  (lambda (y :int) : int = y * x)
 

Rules and Matching

Patterns

A pattern is a type of variable that is described by a regular expression. Below you'll see a pattern that will match any string with one or more letters or spaces.
var DESC : pattern = `[a-zA-Z ]+`

Rules

A rule has two parts. The first part is a series of constants and patterns which are used to match a series of text. Each constant or pattern can also be assigned a variable. In the example below, there are three items on which the rule will match, two constants and a pattern. The first item is the constant "$begin", which will then be assigned to the variable a. The second item is the pattern DESC (described above), which will then be assigned to the variable proto.

The variables are used in the second part of the rule, which describe what to do when the pattern is found. In the below example, when the pattern is found a phrase is emit()ed (more about emit() in a bit).

rule statement [a: "$begin", proto: DESC, e:"$end"]
{
	emit(proto)
}

Matcher

The matcher command runs a rule on a string (or file), and everytime the rule is matched, the rule's code block is executed with the appropriate parameters.
var str : string  = matcher[statement] "$beginfoo$end bar"
The above rule uses the emit() function, which instead of printing emits the code back to the string that gets returned. The value of str after the above matcher command is run would be "foo bar".

If the emit() in the rule was replaced with a print, then the value of str would be " bar", but the word "foo" would have been printed to the screen when the matcher was run.

The 'input' Variable

There is an easy way for a user to feed input into an rpp program. The input variable is automatically defined and reserved for user input.

It can be set in two different ways. Either by using the '-i <file>' parameter when running the 'rpp' script, or by piping output into the program.

rpp -i inputfile cat.rpp
cat inputfile | rpp cat.rpp
An example of a cat.rpp use in the above example:
println("Input file:")
println(input)

The 'system.rpp' file

The system.rpp file contains the function prototypes of the system files. It is installed by default into the share/rpp directory. If you want to define any global functions, you can place the code into the this file.
[ Home ]