From 6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6 Mon Sep 17 00:00:00 2001 From: Elizabeth Alexander Hunt Date: Thu, 2 Jul 2026 11:55:17 -0700 Subject: Init --- .../p5-compiler/submit/ast/VarDeclaration.java | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Homework/cs5300/compiler/p5-compiler/submit/ast/VarDeclaration.java (limited to 'Homework/cs5300/compiler/p5-compiler/submit/ast/VarDeclaration.java') diff --git a/Homework/cs5300/compiler/p5-compiler/submit/ast/VarDeclaration.java b/Homework/cs5300/compiler/p5-compiler/submit/ast/VarDeclaration.java new file mode 100644 index 0000000..809518f --- /dev/null +++ b/Homework/cs5300/compiler/p5-compiler/submit/ast/VarDeclaration.java @@ -0,0 +1,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 ids; + private final List arraySizes; + private final boolean isStatic; + + public VarDeclaration(VarType type, List ids, + List 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"); + } +} -- cgit v1.3