initial
[prop.git] / tests / persist2.pcc
blobca9a9d8d17aa76d17d752fd68f09458fdd0944a5
1 #include <AD/persist/pstream.h>
2 #include <AD/memory/strpool.h>
3 #include <fstream.h>
5 datatype Exp :: collectable persistent =
6     NOexp                  => "<none>" 
7   | IDexp  (Id)            => _
8   | LITexp (Literal)       => _
9   | ADDexp (Exp, Exp)      => "(" _ "+" _ ")"
10   | SUBexp (Exp, Exp)      => "(" _ "-" _ ")"
11   | MULexp (Exp, Exp)      => "(" _ "*" _ ")"
12   | DIVexp (Exp, Exp)      => "(" _ "/" _ ")"
13   | IFexp  (Exp, Exp, Exp) => "if" _ "then" { _ } "else" { _ } "endif"
15 and Literal :: collectable persistent
16   = NOlit                    => "<???>"
17   | INTlit (int)             => _
18   | REALlit (double)         => _
19   | STRINGlit (const char *) => "\"" _ "\""
21 where type Id = const char *
22   ;
24 refine persistent Exp      => "Expressions"
25                 | Literal  => "Literals"
26   ;
27    
29 instantiate datatype Exp, Literal;
31 const char * data_file = "persist2.dat";
33 int main()
34 {  GC::get_default_gc().set_initial_heap_size(1024*1024);
35    for (int i = 0; i < 5000; i++) {
36       StringPool pool;
37       Exp e0 = ADDexp(IDexp("x"),LITexp(INTlit(1)));
38       Exp e1 = MULexp(e0,SUBexp(e0,IDexp("y")));
39       e1 = IFexp(e1,e1,e1);
40       e1 = DIVexp(LITexp(STRINGlit("Hello world")),e1);
41       cout << '.' << flush;
42       if (i % 1000 == 0) cout << ": e1 = " << e1 << '\n';
43       {  ofstream F(data_file);
44          Postream P(F);   
45          if (i % 1000 == 0) 
46             cout << "[Writing object to file '" << data_file << "']\n";
47          P << e1;
48          P << e1;
49          F.close();
50       }
51       // Generate garbage
52       {  Exp e0 = ADDexp(IDexp("x"),LITexp(INTlit(1)));
53          Exp e1 = MULexp(e0,SUBexp(e0,IDexp("y")));
54          e1 = IFexp(e1,e1,e1);
55          e1 = DIVexp(LITexp(STRINGlit("Hello world")),e1);
56       }
57       Exp e2 = NOexp;
58       Exp e3 = NOexp;
59       {  ifstream F(data_file);
60          Pistream P(F,pool);     // (F,GC::get_default_gc());
61          if (i % 1000 == 0) 
62             cout << "[Reading object to file '" << data_file << "']\n";
63          e2 = (Exp)read_object(P);
64          e3 = (Exp)read_object(P);
65          F.close();
66       } 
67       if (i % 1000 == 0)
68       {  cout << "e2 = " << e2 << " aka " << (void*)e2 << '\n';
69          cout << "e3 = " << e3 << " aka " << (void*)e3 << '\n';
70       }
71    }
72    cout << "\nOkay\n";
73    return 0;