8a649b55ffbe63fbae1c47433a43416a9c66f933
[repair.git] / Repair / RepairCompiler / MCC / IR / PrintWrapper.java
1 package MCC.IR;
2 import java.util.*;
3
4 public class PrintWrapper {
5   java.io.PrintWriter output;
6   String buffered="";
7   boolean buffer=false;
8   Hashtable vartable=new Hashtable();
9
10   public PrintWrapper(java.io.PrintWriter output) {
11     this.output=output;
12   }
13   void print(String s) {
14     if (buffer)
15       buffered+=s;
16     else
17       output.print(s);
18   }
19   void flush() {
20     if (!buffer)
21       output.flush();
22   }
23   void println(String s) {
24     if (buffer)
25       buffered+=s+"\n";
26     else
27       output.println(s);
28   }
29   void startBuffer() {
30     buffer=true;
31   }
32   void emptyBuffer() {
33     //Print out declarations
34     for(Iterator it=vartable.keySet().iterator();it.hasNext();) {
35       String var=(String)it.next();
36       output.println(((String)vartable.get(var))+" "+var+";");
37     }
38     output.print(buffered);
39     buffered="";
40     vartable=new Hashtable();
41     buffer=false;
42   }
43   void addDeclaration(String type, String varname) {
44     if (buffer) {
45       if (vartable.containsKey(varname)) {
46         String oldtype=(String)vartable.get(varname);
47         if (!oldtype.equals(type)) {
48           throw new Error("Internal error: Inconsistent declarations for:"+varname);
49         }
50       } else {
51         vartable.put(varname,type);
52       }
53     } else
54       output.println(type+" "+varname+";");
55   }
56 }