diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/AnnotatableType.java b/AlgebraicDataflowArchitectureModel/src/code/ast/AnnotatableType.java new file mode 100644 index 0000000..6e87c83 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/AnnotatableType.java @@ -0,0 +1,26 @@ +package code.ast; + +import java.util.ArrayList; +import java.util.List; + +/** + * Represents an abstract base class for AST types that can have annotations. + * This class extends the functionality of {@link Type} by introducing support + * for attaching annotations to the type. + *

+ * Subclasses of this class represent specific kinds of types in the AST that + * allow annotations, such as primitive types or other specialized types. + * + * @author s-yamagiwa + */ +public abstract class AnnotatableType extends Type { + private final List annotations = new ArrayList<>(); + + public void addAnnotation(Annotation annotation) { + annotations.add(annotation); + } + + public List getAnnotations() { + return annotations; + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/Expression.java b/AlgebraicDataflowArchitectureModel/src/code/ast/Expression.java new file mode 100644 index 0000000..d492947 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/Expression.java @@ -0,0 +1,9 @@ +package code.ast; + +/** + * Abstract base class for all expression nodes in the abstract syntax tree (AST). + * + * @author s-yamagiwa + */ +public abstract class Expression extends ASTNode { +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/ParameterizedType.java b/AlgebraicDataflowArchitectureModel/src/code/ast/ParameterizedType.java new file mode 100644 index 0000000..dbb16e2 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/ParameterizedType.java @@ -0,0 +1,92 @@ +package code.ast; + +import java.util.List; + +/** + * Represents a parameterized type in an abstract syntax tree (AST). + * A parameterized type consists of a base type and a set of type arguments. + *

+ * For example, List<String> is a parameterized type where + * List is the base type and String is the type argument. + * + * @author s-yamagiwa + */ +public class ParameterizedType extends Type { + /** + * The base type of the parameterized type. + *

+ * For example, List in List<String> is a base type. + */ + private Type type; + + /** + * The type arguments of the parameterized type. + * Defaults to an empty list. + *

+ * For example, String in List<String> is a type argument. + */ + private List typeArguments; + + public ParameterizedType(Type type, List typeArguments) { + this.type = type; + this.typeArguments = typeArguments; + } + + public ParameterizedType(Type type) { + this(type, List.of()); + } + + /** + * Adds a type argument to the parameterized type. + * + * @param type The type argument to add. Only {@link SimpleType}s are allowed for now. + */ + public void addTypeArgument(Type type) { + typeArguments.add(type); + } + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } + + /** + * Returns the type arguments of the parameterized type. + * Only {@link SimpleType}s are allowed for now. + *

+ * It can be empty if it is the type of created instances like in the following example. + * new List<>() or new HashMap<>() + * + * @return The type arguments of the parameterized type. + */ + public List getTypeArguments() { + return typeArguments; + } + + public void setTypeArguments(List typeArguments) { + this.typeArguments = typeArguments; + } + + @Override + public String toString() { + if (typeArguments.isEmpty()) { + return type.toString(); + } + + StringBuilder builder = new StringBuilder(); + builder.append(type.toString()); + builder.append("<"); + for (int i = 0; i < typeArguments.size(); i++) { + builder.append(typeArguments.get(i).toString()); + if (i < typeArguments.size() - 1) { + builder.append(", "); + } + } + builder.append(">"); + + return builder.toString(); + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/PrimitiveType.java b/AlgebraicDataflowArchitectureModel/src/code/ast/PrimitiveType.java new file mode 100644 index 0000000..9a80598 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/PrimitiveType.java @@ -0,0 +1,42 @@ +package code.ast; + +/** + * Represents a primitive type in the abstract syntax tree (AST). + * This class extends the {@code AnnotatableType}, allowing annotations + * to be attached to primitive types. + *

+ * Primitive types are: {@code boolean}, {@code byte}, {@code char}, {@code short}, {@code int}, {@code long}, {@code float}, {@code double} and {@code void} + * + * @author s-yamagiwa + */ +public class PrimitiveType extends AnnotatableType { + private SimpleName name; + + public PrimitiveType(SimpleName name) { + this.name = name; + } + + public SimpleName getName() { + return name; + } + + public void setName(SimpleName name) { + this.name = name; + } + + @Override + public String toString() { + if (getAnnotations().isEmpty()) { + return name.getIdentifier(); + } + + StringBuilder builder = new StringBuilder(); + for (Annotation annotation : getAnnotations()) { + builder.append(annotation.toString()); + builder.append(" "); + } + builder.append(name.getIdentifier()); + + return builder.toString(); + } +} \ No newline at end of file diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/SimpleName.java b/AlgebraicDataflowArchitectureModel/src/code/ast/SimpleName.java new file mode 100644 index 0000000..e8de874 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/SimpleName.java @@ -0,0 +1,45 @@ +package code.ast; + +/** + * AST Node for identifiers such as types, variables, and methods. + * + * @author s-yamagiwa + */ +public class SimpleName extends Expression { + private String identifier; + + /** + * Indicates whether this name represents a variable or not, default to false. + */ + private boolean isVar = false; + + public SimpleName(String identifier) { + this.identifier = identifier; + } + + public SimpleName(String identifier, boolean isVar) { + this(identifier); + this.isVar = isVar; + } + + public String getIdentifier() { + return identifier; + } + + public void setIdentifier(String identifier) { + this.identifier = identifier; + } + + public boolean isVar() { + return isVar; + } + + public void setVar(boolean isVar) { + this.isVar = isVar; + } + + @Override + public String toString() { + return identifier; + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/SimpleType.java b/AlgebraicDataflowArchitectureModel/src/code/ast/SimpleType.java new file mode 100644 index 0000000..7e66ae4 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/SimpleType.java @@ -0,0 +1,39 @@ +package code.ast; + +/** + * Represents a simple type in the AST, which is a type with a single identifier. + * For example, String, List, Map or MyClass. + * + * @author s-yamagiwa + */ +public class SimpleType extends AnnotatableType { + private SimpleName name; + + public SimpleType(SimpleName name) { + this.name = name; + } + + public SimpleName getName() { + return name; + } + + public void setName(SimpleName name) { + this.name = name; + } + + @Override + public String toString() { + if (getAnnotations().isEmpty()) { + return name.getIdentifier(); + } + + StringBuilder builder = new StringBuilder(); + for (Annotation annotation : getAnnotations()) { + builder.append(annotation.toString()); + builder.append(" "); + } + builder.append(name.getIdentifier()); + + return builder.toString(); + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/Type.java b/AlgebraicDataflowArchitectureModel/src/code/ast/Type.java new file mode 100644 index 0000000..bdf304b --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/Type.java @@ -0,0 +1,44 @@ +package code.ast; + +/** + * Represents an abstract base class for all types in an abstract syntax tree (AST). + * A type can represent various forms such as primitive types, simple types, + * parameterized types, or others as defined by subclasses. + * + * @author s-yamagiwa + */ +public abstract class Type extends ASTNode { + /** + * Returns whether this type is a primitive type or not. + * + * @return true if this type is a primitive type, false otherwise. + */ + public final boolean isPrimitiveType() { + return (this instanceof PrimitiveType); + } + + /** + * Returns whether this type is a simple type or not. + * + * @return true if this type is a simple type, false otherwise. + */ + public final boolean isSimpleType() { + return (this instanceof SimpleType); + } + + /** + * Returns whether this type is a parameterized type or not. + * + * @return true if this type is a parameterized type, false otherwise. + */ + public final boolean isParameterizedType() { + return (this instanceof ParameterizedType); + } + + /** + * Returns whether this type is a var or not. + */ + public boolean isVar() { + return false; + } +} \ No newline at end of file