b162f72863e31d67c036498b0f4518000751eddb
[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     int indent = 0;
9     java.io.PrintWriter output;
10     Stack symboltables = new Stack();
11
12     public StandardCodeWriter(java.io.PrintWriter output) { this.output = output; }
13
14     public void startblock() {
15         indent();
16         outputline("{");
17     }
18
19     public void endblock() {
20         outputline("}");
21         unindent();
22     }
23     
24     public void indent() { 
25         indent++; 
26     }
27     
28     public void unindent() { 
29         indent--; 
30         assert indent >= 0; 
31     }
32     
33     private void doindent() {
34         for (int i = 0; i < indent; i++) { 
35             output.print("  ");
36         }
37         linestarted = true;
38     }
39     
40     public void outputline(String s) {
41         if (!linestarted) {
42             doindent();
43         }
44         output.println(s);
45         linestarted = false;
46         output.flush();
47     }                 
48     
49     public void output(String s) {
50         if (!linestarted) {
51             doindent();
52         }
53         output.print(s);
54         output.flush(); 
55     }
56
57     public void pushSymbolTable(SymbolTable st) {
58         symboltables.push(st);
59     }
60
61     public SymbolTable popSymbolTable() {
62         return (SymbolTable) symboltables.pop();
63     }
64
65     public SymbolTable getSymbolTable() { 
66         if (symboltables.empty()) {
67             throw new IRException(); 
68         }
69         return (SymbolTable) symboltables.peek();
70     }
71     
72 }