Rz language
(This compiler is still a work in progress. For an older system
(generate code for S2 processor), please visit the Rz
compiler tools kit page )
Rz is aimed to be a teaching language for system programming and computer
architecture subjects, which emphasises a small language that can be used to
illustrate all the "inner" working parts of a computer system (compilation,
code generation, ISA simulation), in other words it allows students to
"play" with the system. In a way, Rz "looks like" C (without type).
Short description
The language is a small subset of C look-alike language. It has no type (or
having only one type which is "int"). Global variables must be
declared but local variables are automatic. A variable can be either a
scalar or an array. There is no user defined data type. An array
is one dimension. Rz language can be summarised as follows:
- It has only integer as primitive data. (natural size depends on
implementation)
- Global variables must be declared before their use. Local
variables are automatic (not required to be declared).
- Global variables can be array. The size of array must be known
at compile time. An array has only one dimension. Local
variables can be only scalar.
- Reserved words are: if, else, while, return, print.
- Operators are: +, -, *,
/, ==, !=, <, <=, >, >=, !, &&, ||, *
(dereference), & (address).
For C programmer, please note, no: for, break, do, missing many operators
especially ++, --
In version 3.5, it has a new syntax, using indentation instead of {
} and newline instead of
;
Examples
It is easier just to look at an example to know most of the syntax.
Here is an example of Rz
// find max
in an array
a[10], N
init()
i = 0
while (
i < N )
a[i] = i
i = i + 1
main()
N = 10
init()
max =
a[0]
i = 1
while(
i < N )
if( max < a[i] ) max = a[i]
i = i + 1
print(
"the max value is ", max )
The variables a[], N are globals, max, i are locals. For an array, the
size must be known at compile time. (A note of C user, there is no ++,
--, and no "break", "print" is not "printf"). "print" knows only integer and
string. The size of basic unit (integer) depends on the target
machine.
increment(x)
*x = *x + 1
main()
a = 1
increment(&a)
print(a)
The call by reference can be achieved using the * and & operators just
like in C. In short, you can think of Rz syntax as C without type
declaration.
History
Rz is a descendant of R1, a concurrent language for small control
applications. (Have a look at full report and implementation from my research work web
page). R1 is a concurrent language. Rz simplifies
that by eliminating all the real-time concurrency language features and
retains only the most basic language constructs.
Current state of implementation
Rz (version 3) is a work in progress. This is a new
implementation of Rz language. The new parser generator is
written. The grammar of Rz language is simplified. The compiler
is modified to output a parse-tree. The parse-tree is an intermediate
and explicit representation. A separate code generator can be written
for any desired target processor. (The original Rz is compiled into its own
byte-code. The original system also included a byte-code interpreter.
For more information about the original system please visit old Rz-language
)
rz3 included:
comp: the whole compiler
lexgen: lexical analyser generator
pgen-c: parser generator, the input is
"rz-grammar9.txt"
rz3 version 1.0 include its "meta-interpreter".
readme
rz3 version 2.0 is an excercise in code generation. It outputs a linear-code (assembly-like) to illustrate how
to traverse and transform Rz parse-tree. See example
of output.
rz3 version 3.3 outputs s-code (version som-v2). The output object
code is run under Som-v2 virtual machine.
rz3 version 3.5 with pre-processing of input to use indentation as {
} and \n as ";".
The output is s-code (som-v2).
A session in Rz (version 3.5)
It has a new syntax, using indentation instead of {
} and newline instead of
;
Here is an example of the new form:
fac(n)
if( n == 0 ) return 1
else return n * fac(n-1)
main()
print(fac(6))
Try to compile "fac.txt" shown here:
Here is the command line and the output at the screen:
D:\prabhas\bag\rz35\test>rz35 fac.txt
fac
main
(fun main (print (call fac 6 )))
(fun fac (else (== #1 0 )(return 1
)(return (* #1 (call fac (- #1 1 ))))))
:main
fun.1
lit.6
call.fac
sys.1
ret.1
:fac
fun.1
get.1
lit.0
. . .
You will get the file "fac.obj" as an output file. It is an object
file that is executable under Som VM (a kind of virtual machine similar to
JVM). You can "run" it under somv2x.
D:\prabhas\bag\rz\rz33\test>somv2x fac.obj
720
That's it. Enjoy!
Code Release
12 Nov 2010 rz3.zip
16 Nov 2010 rz31.zip readme
28 Nov 2010 rz32.zip
28 Aug 2011 rz33.zip rz33-1.zip
(bug fix)
1 Sept 2011 rz35.zip
with
a
new look see example bubble.txt
26 Sept 2011 rz35-1.zip
(bug fix and include "syscall") readme
13 July 2013 rz35-2.zip
(refresh, no executable)
last update 13 July 2013