Backend.jadd 2.04 KB
Newer Older
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
aspect BackendAspect {
  public String Program.generateAspect() {
    StringBuilder sb = new StringBuilder();
    generateAspect(sb);
    return sb.toString();
  }

  public void Program.generateAspect(StringBuilder sb) {
    sb.append("import java.util.ArrayList;\n");
    sb.append("import java.util.Collections;\n");
    sb.append("import java.time.Instant;\n");
    sb.append("import java.time.Period;\n");
    sb.append("aspect RelAstAPI {\n");

    for (TypeDecl td: getTypeDecls()) {
      if (td.needsConstructor()) {
        td.generateConstructor(sb);
      }
    }
    for (Relation r: getRelations()) {
      r.generateAPI(sb);
    }

    generateLowerBoundCheck(sb);

    sb.append(ind(1) + "public static void ASTNode.assertNotNull(Object obj) {\n");
    sb.append(ind(2) + "if (obj == null) {\n");
    sb.append(ind(3) + "throw new NullPointerException();\n");
    sb.append(ind(2) + "}\n");
    sb.append(ind(1) + "}\n");
    sb.append("}\n");
  }

  public void TypeDecl.generateConstructor(StringBuilder sb) {
    sb.append(ind(1) + "public " + getID() + "." + getID() + "(");
    int i = 0;
    for (Component c: componentsTransitive()) {
      sb.append(c.constructorParameter());
      if (++i < componentsTransitive().size()) {
        sb.append(", ");
      }
    }
    sb.append(") {\n");
    for (Component c: componentsTransitive()) {
      sb.append(ind(2) + c.constructorSetMethod() + "(" + c.getID() + ");\n");
    }
    sb.append(ind(1) + "}\n");
  }
  public String Component.constructorParameter() {
    return getTypeUse() + " " + getID();
  }
  public String ListComponent.constructorParameter() {
    return ASTNode.jastAddListType + "<" + getTypeUse() + "> " + getID();
  }
  public String OptComponent.constructorParameter() {
    return "Opt<" + getTypeUse() + "> " + getID();
  }
  public String Component.constructorSetMethod() {
    return "set" + getID();
  }
  public String ListComponent.constructorSetMethod() {
    return "set" + getID() + "List";
  }
  public String OptComponent.constructorSetMethod() {
    return "set" + getID() + "Opt";
  }
}