summaryrefslogtreecommitdiff
path: root/submit/ast/VarDeclaration.java
blob: 809518f3868dda301f8847f8f40ee21075a236ac (plain)
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
/*
 * Code formatter project
 * CS 4481
 */
package submit.ast;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author edwajohn
 */
public class VarDeclaration extends AbstractNode implements Declaration {

  private final VarType type;
  private final List<String> ids;
  private final List<Integer> arraySizes;
  private final boolean isStatic;

  public VarDeclaration(VarType type, List<String> ids,
                        List<Integer> arraySizes, boolean isStatic) {
    this.type = type;
    this.ids = new ArrayList<>(ids);
    this.arraySizes = arraySizes;
    this.isStatic = isStatic;
  }

  public void toCminus(StringBuilder builder, final String prefix) {
    builder.append(prefix);
    if (isStatic) {
      builder.append("static ");
    }
    builder.append(type).append(" ");
    for (int i = 0; i < ids.size(); ++i) {
      final String id = ids.get(i);
      final int arraySize = arraySizes.get(i);
      if (arraySize >= 0) {
        builder.append(id).append("[").append(arraySize).append("]").append(
            ", ");
      } else {
        builder.append(id).append(", ");
      }
    }
    builder.delete(builder.length() - 2, builder.length());
    builder.append(";\n");
  }
}