Rev 2 | Blame | Compare with Previous | Last modification | View Log | RSS feed
<!-- Crown Copyright (c) 1998 -->
<HTML>
<HEAD>
<TITLE>Example PL_TDF programs</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000FF" VLINK="#400080" ALINK="#FF0000">
<A NAME=S41>
<H1>PL_TDF Definition</H1>
<H3>January 1998</H3>
<A HREF="pl6.html"><IMG SRC="../images/next.gif" ALT="next section">
</A> <A HREF="pl4.html">
<IMG SRC="../images/prev.gif" ALT="previous section"></A>
<A HREF="pl1.html"><IMG SRC="../images/top.gif" ALT="current document"></A>
<A HREF="../index.html"><IMG SRC="../images/home.gif" ALT="TenDRA home page">
</A>
<IMG SRC="../images/no_index.gif" ALT="document index"><P>
<HR>
<DL>
<DT><A HREF="#S42"><B>4.1 </B> - Sieve of Erastothenes</A><DD>
<DT><A HREF="#S43"><B>4.2 </B> - Example with structures</A><DD>
<DT><A HREF="#S44"><B>4.3 </B> - Test for case</A><DD>
<DT><A HREF="#S45"><B>4.4 </B> - Example of use of high-order TOKENs</A><DD>
<DT><A HREF="#S46"><B>4.5 </B> - A test for long jumps</A><DD>
</DL>
<HR>
<H1>4 <A NAME=0>Example PL_TDF programs</H1>
<A NAME=S42>
<HR><H2>4.1. Sieve of Erastothenes</H2>
<P>
<PRE>
/* Print out the primes less than 10000 */
String s1 = "%d\t"; /* good strings for printf */
String s2 = "\n";
Var n: nof(10000, Char); /* will contain1 for prime; 0 for composite */
Tokdef N = [ind:EXP]EXP n *+. (Sizeof(Char) .* ind);
/* Token delivering pointer to element of n */
Iddec printf : proc; /* definition provided by ansi library */
Proc main = top ()
Var i:Int
Var j:Int
{ Rep (i = 2(Int))
{ /* set i-th element of n to 1 */
N[* i] = 1(Char);
i = (* i + 1(Int));
?(* i >= 10000(Int)) /* NB assertion fails to continue loop */
}
Rep (i = 2(Int) )
{
?{ ?( *(Char)N[* i] == 1(Char));
/* if its a prime ... */
Rep ( j = (* i + * i) )
{ /*... wipe out composites */
N[* j] = 0(Char);
j = (* j + * i);
?(* j >= 10000(Int))
}
| make_top
};
i = (* i + 1(Int));
?(* i >= 100(Int))
};
Rep (i = 2(Int); j = 0(Int) )
{ ?{ ?( *(Char)N[* i] == 1(Char));
/* if it's a prime, print it */
printf[top](s1, * i);
j = (* j + 1(Int));
?{ ?( * j == 5(Int));
/* print new line */
printf[top](s2);
j = 0(Int)
| make_top
}
| make_top
};
i = (* i + 1(Int));
?(* i >= 10000(Int))
};
return(make_top)
};
Keep (main) /* main will be an external name; so will printf since it is not defined */
</PRE>
<A NAME=S43>
<HR><H2>4.2. Example with structures</H2>
<PRE>
Struct C (re:Double, im:Double);
/* define TOKENs : C as a SHAPE for complex, with field offsets .re and .im
and selectors re and im */
Iddec printf:proc;
Proc addC = C (lv:C, rv:C) /* add two complex numbers */
Let l = * lv
Let r = * rv
{ return( Cons[shape_offset(C)] ( .re: re[l] F+ re[r], .im: im[l] F+ im[r]) ) } ;
String s1 = "Ans = (%g, %g)\n";
Proc main = top()
Let x = Cons[shape_offset(C)] (.re: 1.0(Double), .im:2.0(Double))
Let y = Cons[shape_offset(C)] (.re: 3.0(Double), .im:4.0(Double))
Let z = addC[C](x,y)
{ printf[top](s1, re[z], im[z]);
/* prints out "Ans = (4, 6)" */
return(make_top)
};
Keep(main)
</PRE>
<A NAME=S44>
<HR><H2>4.3. Test for case</H2>
<PRE>
Iddec printf:proc;
String s1 = "%d is not in [%d,%d]\n";
String s2 = "%d OK\n";
Proc test = top(i:Int, l:Int, u:Int) /* report whether l<=i<=u */
?{ ?(* i >= * l); ?(* i <= * u);
printf[top](s2, * i);
return(make_top)
| printf[top](s1, * i, * l, * u);
return(make_top)
};
String s3 = "ERROR with %d\n";
Proc main = top() /* check to see that case is working */
Var i:Int = 0(Int)
Rep {
Labelled {
Case * i (0 -> l0, 1 -> l1, 2:3 -> l2, 4:10000 -> l3)
| :l0: test[top](* i, 0(Int), 0(Int))
| :l1: test[top](* i, 1(Int), 1(Int))
| :l2: test[top](* i, 2(Int), 3(Int))
| :l3: printf[top](s3, * i)
};
i = (* i + 1(Int));
?(* i > 3(Int));
return(make_top)
};
Keep (main, test)
</PRE>
<A NAME=S45>
<HR><H2>4.4. Example of use of high-order TOKENs</H2>
<PRE>
Tokdef IF = [ boolexp:TOKEN[LABEL]EXP, thenpt:EXP, elsept:EXP] EXP
?{ boolexp[lab]; thenpt | :lab: elsept };
/* IF is a TOKEN which can be used to mirror a standard if ... then ... else
construction; the boolexp is a formal TOKEN with a LABEL parameter
which is jumped to if the boolean is false */
Iddec printf: proc;
String cs = "Correct\n";
String ws = "Wrong\n";
Proc main = top()
Var i:Int = 0(Int)
{
IF[ Use [l:LABEL]EXP ?(* i == 0(Int) | l), printf[top](cs), printf[top](ws) ];
/* in other words if (i==0) printf("Correct") else printf("Wrong") */
IF[ Use [l:LABEL]EXP ?(* i != 0(Int) | l), printf[top](ws), printf[top](cs) ];
i = IF[ Use [l:LABEL]EXP ?(* i != 0(Int) | l), 2(Int), 3(Int)];
IF[ Use [l:LABEL]EXP ?(* i == 3(Int) | l), printf[top](cs), printf[top](ws) ];
return(make_top)
};
Keep (main)
</PRE>
<A NAME=S46>
<HR><H2>4.5. A test for long jumps</H2>
<PRE>
Iddec printf:proc;
Proc f = bottom(env:pointer(frame_alignment), lab:pointer(code_alignment) )
{
long_jump(* env, * lab)
};
String s1 = "Should not reach here\n";
String s2 = "long-jump OK\n";
Proc main = top()
Labelled{
f[bottom](current_env, make_local_lv(l));
printf[top](s1); /* should never reach here */
return(make_top)
| :l:
printf[top](s2);
return(make_top)
};
Keep (main)
</PRE>
<HR>
<P><I>Part of the <A HREF="../index.html">TenDRA Web</A>.<BR>Crown
Copyright © 1998.</I></P>
</BODY>
</HTML>