PrettyPrint.jrag 2.63 KB
Newer Older
Anders Nilsson's avatar
Anders Nilsson committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.io.PrintStream;

aspect PPIndentation {

  inh String Exp.pp_indent();
  inh String Field.pp_indent();
  inh String StructType.pp_indent();
  eq StructType.getField(int index).pp_indent() = pp_indent() + "  ";
  eq Program.getDecl(int index).pp_indent() = "";
  
}

aspect PrettyPrint {
  
  public void ASTNode.pp(PrintStream out) {
    throw new Error(this.getClass().getName() + 
		    ".pp(PrintStream out)" + 
		    " not declared");
  }

  public void Program.pp(PrintStream out) {
    for(int i = 0; i < getNumDecl(); i++) {
    	getDecl(i).pp(out);
    }
  }

   // Pretty print declarations
  public void TypeDecl.pp(PrintStream out) {
    out.print("typedef ");
    getType().ppIdentifier(out, getName());
    out.println(";");
  }

  public void SampleDecl.pp(PrintStream out) {
    out.print("sample ");
    getType().ppIdentifier(out, getName());
    out.println(";");
  }

  public void Field.pp(PrintStream out) {
    out.print(pp_indent());
    getType().ppIdentifier(out, getName());
    out.println(";");
  }

  // Pretty print variable of a given type 
  public void Type.ppIdentifier(PrintStream out, String id) { 
    ppPrefix(out);
    out.print(" ");
    out.print(id);
  }

  public void ArrayType.ppIdentifier(PrintStream out, String id) {
    ppPrefix(out);
    out.print(" ");
    out.print(id);
    ppSuffix(out);
  }

  // PrettyPrint prefix type info
  public void Type.ppPrefix(PrintStream out) {
    throw new Error(this.getClass().getName() + 
		    ".ppPrefix(PrintStream out)" + 
		    " not declared");
  }

  public void VoidType.ppPrefix(PrintStream out) { 
    out.print("void");
  }

71
72
73
74
  public void SampleRefType.ppPrefix(PrintStream out) { 
    out.print("sample");
  }

Anders Nilsson's avatar
Anders Nilsson committed
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  public void PrimType.ppPrefix(PrintStream out) { 
    out.print(getName());
  }

  public void UserType.ppPrefix(PrintStream out) { 
    out.print(getName());
  }

  public void ArrayType.ppPrefix(PrintStream out) {
    getType().ppPrefix(out);
  }

  public void StructType.ppPrefix(PrintStream out) {
    out.println("struct {");
    for (int i = 0 ; i < getNumField() ; i++) {
      getField(i).pp(out);
    }
    out.print(pp_indent());
    out.print("}");
  }

  // PrettyPrint suffix type info (array dimensions)
  public void Type.ppSuffix(PrintStream out) { }

  public void ArrayType.ppSuffix(PrintStream out) { 
    out.print("[");
    for (int i = 0 ; i < getNumExp() ; i++) {
      if (i > 0) { out.print(", "); }
      getExp(i).pp(out);
    }
    out.print("]");
    getType().ppSuffix(out);
  }

  public void IntegerLiteral.pp(PrintStream out) {
    out.print(getValue());
  }

  public void VariableSize.pp(PrintStream out) {
    out.print("_");
  }

}