use typename
[prop.git] / tests / printer1.pcc
blobd344f10c975a16322128f4511e5553b2130fd0cd
1 /////////////////////////////////////////////////////////////////////////////
2 //
3 //  The qualifier `printable' for a datatype automatic generates a printing
4 //  method suitable for debugging use.
5 //
6 /////////////////////////////////////////////////////////////////////////////
8 #include <iostream.h>
10 /////////////////////////////////////////////////////////////////////////////
12 //  Defines a simple datatype to represent expressions.
13 //  Also specify the pretty printing formats.
15 /////////////////////////////////////////////////////////////////////////////
16 datatype EXP = num (int)           => _
17              | var (const char *)  => _
18              | add (EXP, EXP)      => "(" _ " + " _ ")"
19              | sub (EXP, EXP)      => "(" _ " - " _ ")"
20              | mul (EXP, EXP)      => "(" _ " * " _ ")"
21              | div (EXP, EXP)      => "(" _ " / " _ ")"
22              ;
24 /////////////////////////////////////////////////////////////////////////////
26 //  Instantiate the datatype
28 /////////////////////////////////////////////////////////////////////////////
29 instantiate datatype EXP;
31 /////////////////////////////////////////////////////////////////////////////
33 //  Now test out the printer
35 /////////////////////////////////////////////////////////////////////////////
36 int main()
38    EXP a = var ("a");
39    EXP b = var ("b");
40    EXP c = var ("c");
42    //  a * a + b * b + c * c - 1
43    EXP e1 = sub(add(mul(a,a), add(mul(b,b), mul(c,c))),num(1));
45    cout << e1 << '\n'; 
47    return 0;