1 #include <AD/persist/pstream.h>
2 #include <AD/memory/strpool.h>
5 datatype Exp :: collectable persistent =
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
18 | REALlit (double) => _
19 | STRINGlit (const char *) => "\"" _ "\""
21 where type Id = const char *
24 refine persistent Exp => "Expressions"
25 | Literal => "Literals"
29 instantiate datatype Exp, Literal;
31 const char * data_file = "persist2.dat";
34 { GC::get_default_gc().set_initial_heap_size(1024*1024);
35 for (int i = 0; i < 5000; i++) {
37 Exp e0 = ADDexp(IDexp("x"),LITexp(INTlit(1)));
38 Exp e1 = MULexp(e0,SUBexp(e0,IDexp("y")));
40 e1 = DIVexp(LITexp(STRINGlit("Hello world")),e1);
42 if (i % 1000 == 0) cout << ": e1 = " << e1 << '\n';
43 { ofstream F(data_file);
46 cout << "[Writing object to file '" << data_file << "']\n";
52 { Exp e0 = ADDexp(IDexp("x"),LITexp(INTlit(1)));
53 Exp e1 = MULexp(e0,SUBexp(e0,IDexp("y")));
55 e1 = DIVexp(LITexp(STRINGlit("Hello world")),e1);
59 { ifstream F(data_file);
60 Pistream P(F,pool); // (F,GC::get_default_gc());
62 cout << "[Reading object to file '" << data_file << "']\n";
63 e2 = (Exp)read_object(P);
64 e3 = (Exp)read_object(P);
68 { cout << "e2 = " << e2 << " aka " << (void*)e2 << '\n';
69 cout << "e3 = " << e3 << " aka " << (void*)e3 << '\n';