Python_CodeGen.jrag 7.96 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
71
72
73
74
aspect Python_CodeGenEnv {

  // Environment wrapper for Python code generation
  // handles qualid nesting, indentation, file writing and
  // prefix propagation

  public class Python_env {

    final private class Python_printer {
      
      private boolean newline = true;
      private PrintStream out;

      public Python_printer(PrintStream out) {
        this.out = out;
      }

      public void print(Python_env env, String s) {
        if (newline) {
          newline = false;
          for (int i = 0 ; i < env.indent ; i++) {
            out.print("    ");
          }
        }
        out.print(s);
      }

      public void println(Python_env env, String s) {
        print(env, s);
        out.println();
        newline = true;
      }

      public void println(Python_env env) {
        out.println();
        newline = true;
      }

    }

    private int indent;
    private Python_printer printer;

    public Python_env(PrintStream out) {
      this.indent = 0;
      this.printer = new Python_printer(out);
    }

    public void indent() {
      indent++;
    }

    public void unindent() {
      indent--;
    }

    public void print(String s) {
      printer.print(this, s);
    }

    public void println(String s) {
      printer.println(this, s);
    }

    public void println() {
      printer.println(this);
    }

  }

}

aspect Python_CodeGen {

75
  public void Specification.Python_gen(PrintStream out, String baseName, int version) {
Anders Nilsson's avatar
Anders Nilsson committed
76
77
78
79
    Python_env env = new Python_env(out);
    env.println("#!/usr/bin/python");
    env.println("# Auto generated " + baseName);
    env.println();
80
    env.println("import labcomm2014");
Anders Nilsson's avatar
Anders Nilsson committed
81
82
    env.println();
    Python_genTypes(env);
Anders Blomdell's avatar
Anders Blomdell committed
83
84
85
86
87
88
89
    env.println("typedef = tuple([");
    env.indent();
    for (int i = 0 ; i < getNumDecl() ; i++) {
      getDecl(i).Python_genTypedefListEntry(env);
    }
    env.unindent();
    env.println("])");
90
    env.println("sample = tuple([");
91
    env.indent();
Anders Blomdell's avatar
Anders Blomdell committed
92
    for (int i = 0 ; i < getNumDecl() ; i++) {
93
      getDecl(i).Python_genSampleListEntry(env);
Anders Blomdell's avatar
Anders Blomdell committed
94
    }
95
    env.unindent();
96
    env.println("])");
Anders Nilsson's avatar
Anders Nilsson committed
97
98
99
100
101
102
  }

}

aspect PythonTypes {
  
103
  public void Specification.Python_genTypes(Python_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
104
    for (int i = 0 ; i < getNumDecl() ; i++) {
105
      getDecl(i).Python_genSignatureAndTypedef(env);
Anders Nilsson's avatar
Anders Nilsson committed
106
107
108
    }
  }

109
  public void Decl.Python_genSignatureAndTypedef(Python_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
110
    throw new Error(this.getClass().getName() + 
111
                    ".Python_genSignatureAndTypedef(Python_env env)" + 
Anders Nilsson's avatar
Anders Nilsson committed
112
113
114
                    " not declared");
  }

115
  public void TypeDecl.Python_genSignatureAndTypedef(Python_env env) {
116
117
    env.println("class " + getName() + "(object):");
    env.indent();
118
    env.println("typedef = labcomm2014.typedef('" + getName() + "',");
Anders Nilsson's avatar
Anders Nilsson committed
119
    env.indent();
120
    getDataType().Python_genTypedef(env);
Anders Nilsson's avatar
Anders Nilsson committed
121
122
    env.unindent();
    env.println(")");
123
124
    env.unindent();
    env.println();
Anders Nilsson's avatar
Anders Nilsson committed
125
126
  }

127
  public void SampleDecl.Python_genSignatureAndTypedef(Python_env env) {
128
129
    env.println("class " + getName() + "(object):");
    env.indent();
130
    env.println("signature = labcomm2014.sample('" + getName() + "', ");
Anders Nilsson's avatar
Anders Nilsson committed
131
    env.indent();
132
    getDataType().Python_genSignature(env);
Anders Nilsson's avatar
Anders Nilsson committed
133
134
    env.unindent();
    env.println(")");
135
136
    env.println("typedef = labcomm2014.sample('" + getName() + "', ");
    env.indent();
137
    getDataType().Python_genTypedef(env);
138
139
    env.unindent();
    env.println(")");
140
141
    env.unindent();
    env.println();
Anders Nilsson's avatar
Anders Nilsson committed
142
143
144
  }

  public void UserType.Python_genSignature(Python_env env) {
145
    lookupType(getName()).getDataType().Python_genSignature(env);
Anders Nilsson's avatar
Anders Nilsson committed
146
147
  }

148
  public void DataType.Python_genSignature(Python_env env) {
Anders Nilsson's avatar
Anders Nilsson committed
149
150
151
152
153
154
155
    throw new Error(this.getClass().getName() + 
                    ".Python_genSignature(Python_env env)" + 
                    " not declared");
  }

  public void PrimType.Python_genSignature(Python_env env) {
    switch (getToken()) {
156
157
158
159
160
161
162
163
164
      case LABCOMM_BOOLEAN: { env.print("labcomm2014.BOOLEAN()"); } break;
      case LABCOMM_BYTE: { env.print("labcomm2014.BYTE()"); } break;
      case LABCOMM_SHORT: { env.print("labcomm2014.SHORT()"); } break;
      case LABCOMM_INT: { env.print("labcomm2014.INTEGER()"); } break;
      case LABCOMM_LONG: { env.print("labcomm2014.LONG()"); } break;
      case LABCOMM_FLOAT: { env.print("labcomm2014.FLOAT()"); } break;
      case LABCOMM_DOUBLE: { env.print("labcomm2014.DOUBLE()"); } break;
      case LABCOMM_STRING: { env.print("labcomm2014.STRING()"); } break;
      case LABCOMM_SAMPLE: { env.print("labcomm2014.SAMPLE()"); } break;
Anders Nilsson's avatar
Anders Nilsson committed
165
166
167
168
    }
  }

  public void ArrayType.Python_genSignature(Python_env env) {
169
    env.print("labcomm2014.array([");
Anders Nilsson's avatar
Anders Nilsson committed
170
171
172
173
174
175
    for (int i = 0 ; i < getNumExp() ; i++) {
      if (i > 0) { env.print(", "); }
      env.print(getExp(i).Python_getValue());
    }
    env.println("],");
    env.indent();
176
    getDataType().Python_genSignature(env);
Anders Nilsson's avatar
Anders Nilsson committed
177
178
179
180
181
    env.print(")");
    env.unindent();
  }

  public void StructType.Python_genSignature(Python_env env) {
182
    env.println("labcomm2014.struct([");
Anders Nilsson's avatar
Anders Nilsson committed
183
184
185
186
187
188
189
190
191
192
    env.indent();
    for (int i = 0 ; i < getNumField() ; i++) {
      if (i > 0) { env.println(","); }
      getField(i).Python_genSignature(env);
    }
    env.print("])");
    env.unindent();
  }

  public void VoidType.Python_genSignature(Python_env env) {
193
    env.println("labcomm2014.struct([])");
Anders Nilsson's avatar
Anders Nilsson committed
194
195
196
197
  }

  public void Field.Python_genSignature(Python_env env) {
    env.print("('" + getName() + "', ");
198
    getDataType().Python_genSignature(env);
Anders Nilsson's avatar
Anders Nilsson committed
199
200
201
    env.print(")");
  }

202
203
204
205
  public void UserType.Python_genTypedef(Python_env env) {
    env.println(getName() + ".typedef");
  }

206
  public void DataType.Python_genTypedef(Python_env env) {
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
    throw new Error(this.getClass().getName() + 
                    ".Python_genTypedef(Python_env env)" + 
                    " not declared");
  }

  public void PrimType.Python_genTypedef(Python_env env) {
    switch (getToken()) {
      case LABCOMM_BOOLEAN: { env.print("labcomm2014.BOOLEAN()"); } break;
      case LABCOMM_BYTE: { env.print("labcomm2014.BYTE()"); } break;
      case LABCOMM_SHORT: { env.print("labcomm2014.SHORT()"); } break;
      case LABCOMM_INT: { env.print("labcomm2014.INTEGER()"); } break;
      case LABCOMM_LONG: { env.print("labcomm2014.LONG()"); } break;
      case LABCOMM_FLOAT: { env.print("labcomm2014.FLOAT()"); } break;
      case LABCOMM_DOUBLE: { env.print("labcomm2014.DOUBLE()"); } break;
      case LABCOMM_STRING: { env.print("labcomm2014.STRING()"); } break;
      case LABCOMM_SAMPLE: { env.print("labcomm2014.SAMPLE()"); } break;
    }
  }

  public void ArrayType.Python_genTypedef(Python_env env) {
    env.print("labcomm2014.array([");
    for (int i = 0 ; i < getNumExp() ; i++) {
      if (i > 0) { env.print(", "); }
      env.print(getExp(i).Python_getValue());
    }
    env.println("],");
    env.indent();
234
    getDataType().Python_genTypedef(env);
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
    env.print(")");
    env.unindent();
  }

  public void StructType.Python_genTypedef(Python_env env) {
    env.println("labcomm2014.struct([");
    env.indent();
    for (int i = 0 ; i < getNumField() ; i++) {
      if (i > 0) { env.println(","); }
      getField(i).Python_genTypedef(env);
    }
    env.print("])");
    env.unindent();
  }

  public void VoidType.Python_genTypedef(Python_env env) {
    env.println("labcomm2014.struct([])");
  }

  public void Field.Python_genTypedef(Python_env env) {
    env.print("('" + getName() + "', ");
256
    getDataType().Python_genTypedef(env);
257
258
259
    env.print(")");
  }

260
261
262
263
  public void Decl.Python_genTypedefListEntry(Python_env env) {
  }

  public void TypeDecl.Python_genTypedefListEntry(Python_env env) {
Anders Blomdell's avatar
Anders Blomdell committed
264
    env.println(getName() + ",");
265
266
267
268
269
270
  }

  public void Decl.Python_genSampleListEntry(Python_env env) {
  }

  public void SampleDecl.Python_genSampleListEntry(Python_env env) {
271
    env.println(getName()+ ",");
272
273
  }

Anders Nilsson's avatar
Anders Nilsson committed
274
275
276
277
278
279
280
281
282
283
284
285
286
287
  public String Exp.Python_getValue() {
   throw new Error(this.getClass().getName() + 
                    ".Python_getValue()" + 
                    " not declared");
  }

  public String IntegerLiteral.Python_getValue() {
    return getValue();
  }

  public String VariableSize.Python_getValue() {
    return "0";
  }

288
}
289