correct
[repair.git] / Repair / RepairCompiler / MCC / IR / StandardCodeWriter.java
1 package MCC.IR;
2
3 import java.util.*;
4
5 public class StandardCodeWriter implements CodeWriter {
6
7     boolean linestarted = false;
8     PrintWrapper output;
9     Stack symboltables = new Stack();
10     InvariantValue ivalue;
11
12     public StandardCodeWriter(PrintWrapper output) { this.output = output; }
13     public StandardCodeWriter(java.io.PrintWriter output) { this.output = new PrintWrapper(output);}
14
15     public void startBuffer() {
16         output.startBuffer();
17     }
18     public void emptyBuffer() {
19         output.emptyBuffer();
20     }
21
22     public void startblock() {
23         indent();
24         outputline("{");
25     }
26
27     public void endblock() {
28         outputline("}");
29         unindent();
30     }
31     public void addDeclaration(String type, String varname) {
32         output.addDeclaration(type,varname);
33     }
34     public void addDeclaration(String f) {
35         output.addDeclaration(f);
36     }
37     
38     public void indent() {
39         output.indent++;
40     }
41
42     public void unindent() {
43         output.indent--;
44     }
45
46     private void doindent() {
47         for (int i = 0; i < output.indent; i++) {
48             output.print("  ");
49         }
50         linestarted = true;
51     }
52
53     public void outputline(String s) {
54         if (!linestarted) {
55             doindent();
56         }
57         output.println(s);
58         linestarted = false;
59         output.flush();
60     }
61
62     public void output(String s) {
63         if (!linestarted) {
64             doindent();
65         }
66         output.print(s);
67         output.flush();
68     }
69
70     public void pushSymbolTable(SymbolTable st) {
71         symboltables.push(st);
72     }
73
74     public SymbolTable popSymbolTable() {
75         return (SymbolTable) symboltables.pop();
76     }
77
78     public SymbolTable getSymbolTable() {
79         if (symboltables.empty()) {
80             throw new IRException();
81         }
82         return (SymbolTable) symboltables.peek();
83     }
84
85     public InvariantValue getInvariantValue() {
86         return ivalue;
87     }
88
89     public void setInvariantValue(InvariantValue iv) {
90         ivalue=iv;
91     }
92 }