initial
[prop.git] / app / test_vcg / exp.pcc
blob3d1286cac48f1c7cbdd40cae22d6d307ba2f5364
1 #include <stdlib.h>
2 #include <iostream.h>
3 #include <strstream.h>
4 #include <fstream.h>
5 #include <AD/visualize/vcg.h>
7 //
8 //  Visualize an expression tree 
9 //
11 datatype Exp = INT int
12              | ID Id
13              | BINOP(Id,Exp,Exp)
14              | UNARYOP(Id,Exp)
15 where type Id = const char *
18 instantiate datatype Exp;
20 void f(VCG& V, Exp e)
21 {  if (V.visited(e)) return;
22    match (e)
23    {  INT i:   { V.begin_node(e).label(i).color(VCG::lightyellow).end_node(e); }
24    |  ID id:   { V.begin_node(e).label(id).shape(VCG::box).color(VCG::pink).end_node(e); }
25    |  BINOP(op,a,b): { V.begin_node(e).label(op).end_node(e).
26                          edge(e,a).edge(e,b);
27                        f(V,a); f(V,b);
28                      }
29    |  UNARYOP(op,a): { V.begin_node(e).label(op).end_node(e).edge(e,a);
30                        f(V,a);
31                      }
32    }
35 int main()
37    VCG V;
38    const char * G = "A Sample Graph";
39    Exp e1 = BINOP("*",ID("z"),BINOP("+",ID("x"),ID("y")));
40    Exp e2 = BINOP("*",ID("z"),BINOP("+",ID("x"),ID("y")));
41    Exp e3 = BINOP("/",e1,BINOP("-",e2,INT(123)));
42    Exp e  = UNARYOP("-",BINOP("+",e3,BINOP("+",ID("x"),ID("z"))));
44    V . begin_graph(G) . label(G) . display_edge_labels(true) 
45      . layoutalgorithm(VCG::tree)
46      . xspace(50) . yspace(40) 
47      . xbase(100) 
48      . begin_node_default() 
49         . shape(VCG::ellipse) 
50         . color(VCG::lightgreen) 
51      . end_default()
52    ;
54    //  Traverse expression
55    f(V,e);
57    V . end_graph(G);
59    //  translate it into GDL
60    ostrstream S;
61    V.print_GDL_on(S);
63    ofstream TMP("./exp.vcg");
64    TMP << S.str();
65    TMP.close();
66    system("xvcg ./exp.vcg");
68    return 0;