Quick start Rz
This short article will help you to learn Rz language quickly.
Here are the list of distinct features of Rz (version 3.3).
1 Rz is quite like C with only integer type.
2 Global variables must be declared. Local variables are
automatic.
3 An array must be global.
4 Pass reference by &v.
A session in Rz
Try to compile "fac.txt" shown here:
// factorial
fac(n){
if( n == 0 ) return
1;
else return n *
fac(n-1);
}
main(){
print(fac(6));
}
Here is the command line and the output at the screen:
D:\prabhas\bag\rz\rz33\test>rz33 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!
Examples
Ex1. To add one to ten.
main(){
i = 1;
s = 0;
while( i <= 10 ){
s = s +
i;
i = i +
1;
}
print(s);
}
Ex2. Find a maximum value in an array.
ax[10]; // declare an array size
10
// max of array a size n
max(a, n){
m = a[0];
i = 1;
while( i < n ){
if( m
< a[i] ) m = a[i];
i = i +
1;
}
return m;
}
// initialise array
init(a, n){
i = 0;
while( i < n ){
a[i] = i
+ 10;
i = i +
1;
}
}
main(){
init(&ax,10);
// pass ref of ax
print(max(&ax,10));
}
Ex3. Recursive function
// factorial
fac(n){
if( n == 0 ) return
1;
else return n *
fac(n-1);
}
main(){
print(fac(6));
}
Ex4. Making and printing a linked list of integer
cell[1000]; freecell;
// cell storage
// access functions
head(a){ return cell[a];}
tail(a){ return cell[a+1];}
sethead(a,i){ cell[a] = i;}
settail(a,i){ cell[a+1] =
i;}
newnode(i){
// get a new node, with i
p = freecell;
freecell = freecell +
2;
if( freecell >
10000 ) print("no more cell");
sethead(p,i);
settail(p,0);
return p;
}
// link node a to list m
insert(m,a){
settail(a,tail(m));
settail(m,a);
}
// print item in list
recursively
prlist2(m){
if( m != 0 ){
print(head(m)," ");
prlist2(tail(m));
}
}
// main print list, skip
header node
prlist(m){ prlist2(tail(m));
}
main(){
h =
newnode(0); // make header
insert(h,newnode(1));
insert(h,newnode(2));
insert(h,newnode(3));
prlist(h);
}
... (to be continue)
last update 8 June 2012