diff options
author | Lizzy Hunt <elizabeth.hunt@simponic.xyz> | 2023-04-10 09:17:11 -0600 |
---|---|---|
committer | Lizzy Hunt <elizabeth.hunt@simponic.xyz> | 2023-04-10 09:17:11 -0600 |
commit | 5f28f80c4e25a56cd444914c2f0b3da5e7fdb088 (patch) | |
tree | 600ad8b1ec5aad5155baf8c0352281054a8e6366 /submit/ast/VarDeclaration.java | |
download | cminus-5f28f80c4e25a56cd444914c2f0b3da5e7fdb088.tar.gz cminus-5f28f80c4e25a56cd444914c2f0b3da5e7fdb088.zip |
Initial commit - building
Diffstat (limited to 'submit/ast/VarDeclaration.java')
-rw-r--r-- | submit/ast/VarDeclaration.java | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/submit/ast/VarDeclaration.java b/submit/ast/VarDeclaration.java new file mode 100644 index 0000000..d2a5264 --- /dev/null +++ b/submit/ast/VarDeclaration.java @@ -0,0 +1,47 @@ +/* + * 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"); + } + +} |