diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/Assignment.java b/AlgebraicDataflowArchitectureModel/src/code/ast/Assignment.java new file mode 100644 index 0000000..a7a2fc9 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/Assignment.java @@ -0,0 +1,37 @@ +package code.ast; + +/** + * Represents an assignment expression node in AST (Abstract Syntax Tree) + * + * @author s-yamagiwa + */ +public class Assignment extends Expression { + private Expression left; + private Expression right; + + public Assignment(Expression left, Expression right) { + this.left = left; + this.right = right; + } + + public Expression getLeft() { + return left; + } + + public void setLeft(Expression left) { + this.left = left; + } + + public Expression getRight() { + return right; + } + + public void setRight(Expression right) { + this.right = right; + } + + @Override + public String toString() { + return left.toString() + " " + "+" + " " + right.toString(); + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/ClassInstanceCreation.java b/AlgebraicDataflowArchitectureModel/src/code/ast/ClassInstanceCreation.java new file mode 100644 index 0000000..fe20607 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/ClassInstanceCreation.java @@ -0,0 +1,59 @@ +package code.ast; + +import java.util.List; + +public class ClassInstanceCreation extends Expression { + private Type type; + + private List arguments; + + public ClassInstanceCreation(Type type) { + this(type, List.of()); + } + + public ClassInstanceCreation(Type type, List arguments) { + this.type = type; + this.arguments = arguments; + } + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } + + public List getArguments() { + return arguments; + } + + public void setArguments(List arguments) { + this.arguments = arguments; + } + + public void addArgument(Expression expression) { + arguments.add(expression); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + + builder.append("new "); + builder.append(type.toString()); + builder.append("("); + for (int i = 0; i < arguments.size(); i++) { + Expression argument = arguments.get(i); + + builder.append(argument.toString()); + + if (i < arguments.size() - 1) { + builder.append(", "); + } + } + builder.append(")"); + + return builder.toString(); + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/FieldAccess.java b/AlgebraicDataflowArchitectureModel/src/code/ast/FieldAccess.java new file mode 100644 index 0000000..bfae25d --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/FieldAccess.java @@ -0,0 +1,45 @@ +package code.ast; + +/** + * Represents field accesses in AST (Abstract Syntax Tree) + * + * @author s-yamagiwa + */ +public class FieldAccess extends Expression { + private Expression expression; + + private String fieldName; + + public FieldAccess(String fieldName) { + this(null, fieldName); + } + + public FieldAccess(Expression expression, String fieldName) { + this.expression = expression; + this.fieldName = fieldName; + } + + public Expression getExpression() { + return expression; + } + + public void setExpression(Expression expression) { + this.expression = expression; + } + + public String getFieldName() { + return fieldName; + } + + public void setFieldName(String fieldName) { + this.fieldName = fieldName; + } + + @Override + public String toString() { + if (expression == null) { + return fieldName; + } + return expression + "." + fieldName; + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/InfixExpression.java b/AlgebraicDataflowArchitectureModel/src/code/ast/InfixExpression.java new file mode 100644 index 0000000..8aa90be --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/InfixExpression.java @@ -0,0 +1,50 @@ +package code.ast; + +import models.algebra.Symbol; + +/** + * Represents an infix expression in AST (Abstract Syntax Tree) + * + * @author s-yamagiwa + */ +public class InfixExpression extends Expression { + private Symbol operator; + + private Expression leftOperand; + private Expression rightOperand; + + public InfixExpression(Symbol operator, Expression leftOperand, Expression rightOperand) { + this.operator = operator; + this.leftOperand = leftOperand; + this.rightOperand = rightOperand; + } + + public Symbol getOperator() { + return this.operator; + } + + public void setOperator(Symbol operator) { + this.operator = operator; + } + + public Expression getLeftOperand() { + return leftOperand; + } + + public void setLeftOperand(Expression expression) { + this.leftOperand = expression; + } + + public Expression getRightOperand() { + return rightOperand; + } + + public void setRightOperand(Expression expression) { + this.rightOperand = expression; + } + + @Override + public String toString() { + return leftOperand.toString() + " " + operator.toString() + " " + rightOperand.toString(); + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/Lexer.java b/AlgebraicDataflowArchitectureModel/src/code/ast/Lexer.java new file mode 100644 index 0000000..cd2974c --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/Lexer.java @@ -0,0 +1,55 @@ +package code.ast; + +/** + * The Lexer class provides functionalities for parsing and iterating through + * a source string of text character by character. + * + * @author s-yamagiwa; + */ +public class Lexer { + private final String source; + private int position; + + public Lexer(String source) { + this.source = source; + this.position = 0; + } + + /** + * Retrieves the next character without advancing the position. + * + * @return The next character. + */ + public char peek() { + if (position >= source.length()) { + return '\0'; // End of line + } + + return source.charAt(position); + } + + /** + * Retrieves the next character after advancing the position. + * + * @return The next character. + */ + public char peekNext() { + if (position + 1 >= source.length()) { + return '\0'; // End of line + } + + return source.charAt(position + 1); + } + + /** + * Advances the position by one character. + * + * @return The character that was previously at the current position. + */ + public char advance() { + char current = peek(); + position++; + + return current; + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/MethodInvocation.java b/AlgebraicDataflowArchitectureModel/src/code/ast/MethodInvocation.java new file mode 100644 index 0000000..94c90e1 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/MethodInvocation.java @@ -0,0 +1,91 @@ +package code.ast; + +import java.util.List; + +/** + * Represents a method invocation in AST (Abstract Syntax Tree) + * + * @author s-yamagiwa + * @apiNote Type arguments aren't supported because it isn't necessary in DTRAM code generator. + */ +public class MethodInvocation extends Expression { + /** + * The receiver expression of the invocation + * defaults to {@code null} + */ + private Expression receiver; + + /** + * The method name to be called by this invocation + */ + private String methodName; + + /** + * All arguments used in the called method + */ + private List arguments; + + public MethodInvocation(String methodName) { + this(null, methodName); + } + + public MethodInvocation(Expression receiver, String methodName) { + this(receiver, methodName, List.of()); + } + + public MethodInvocation(Expression receiver, String methodName, List arguments) { + this.receiver = receiver; + this.methodName = methodName; + this.arguments = arguments; + } + + public Expression getReceiver() { + return receiver; + } + + public void setReceiver(Expression receiver) { + this.receiver = receiver; + } + + public String getMethodName() { + return methodName; + } + + public void setMethodName(String methodName) { + this.methodName = methodName; + } + + public List getArguments() { + return arguments; + } + + public void setArguments(List arguments) { + this.arguments = arguments; + } + + @Override + public String toString() { + if (receiver == null) { + StringBuilder builder = new StringBuilder(); + + builder.append(methodName).append("("); + + if (!arguments.isEmpty()) { + for (int i = 0; i < arguments.size(); i++) { + Expression argument = arguments.get(i); + + builder.append(argument.toString()); + + if (i < arguments.size() - 1) { + builder.append(", "); + } + } + } + + builder.append(");"); + + return builder.toString(); + } + return super.toString(); + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/ParenthesizedExpression.java b/AlgebraicDataflowArchitectureModel/src/code/ast/ParenthesizedExpression.java new file mode 100644 index 0000000..a7b50cb --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/ParenthesizedExpression.java @@ -0,0 +1,28 @@ +package code.ast; + +/** + * Represents a parenthesized expression in AST (Abstract Syntax Tree) + * This class is used to save the priority of each expression + * + * @author s-yamagiwa + */ +public class ParenthesizedExpression extends Expression { + private Expression expression; + + public ParenthesizedExpression(Expression expression) { + this.expression = expression; + } + + public Expression getExpression() { + return expression; + } + + public void setExpression(Expression expression) { + this.expression = expression; + } + + @Override + public String toString() { + return "(" + expression.toString() + ")"; + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/PostfixExpression.java b/AlgebraicDataflowArchitectureModel/src/code/ast/PostfixExpression.java new file mode 100644 index 0000000..bc520ac --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/PostfixExpression.java @@ -0,0 +1,53 @@ +package code.ast; + +/** + * Represents a postfix expression in AST (Abstract Syntax Tree) + * + * @author s-yamagiwa + */ +public class PostfixExpression extends Expression { + public enum Operator { + INCREMENT("++"), DECREMENT("--"); + + Operator(String symbol) { + this.symbol = symbol; + } + + private final String symbol; + + @Override + public String toString() { + return symbol; + } + } + + private Expression operand; + + private Operator operator; + + public PostfixExpression(Expression operand, Operator operator) { + this.operand = operand; + this.operator = operator; + } + + public Expression getOperand() { + return operand; + } + + public void setOperand(Expression operand) { + this.operand = operand; + } + + public Operator getOperator() { + return operator; + } + + public void setOperator(Operator operator) { + this.operator = operator; + } + + @Override + public String toString() { + return operand.toString() + operator.toString(); + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/PrefixExpression.java b/AlgebraicDataflowArchitectureModel/src/code/ast/PrefixExpression.java new file mode 100644 index 0000000..2e854b7 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/PrefixExpression.java @@ -0,0 +1,53 @@ +package code.ast; + +/** + * Represents a prefix expression in AST (Abstract Syntax Tree) + * + * @author s-yamagiwa + */ +public class PrefixExpression extends Expression { + public enum Operator { + INCREMENT("++"), DECREMENT("--"), PLUS("+"), MINUS("-"), NOT("!"); + + Operator(String symbol) { + this.symbol = symbol; + } + + private final String symbol; + + @Override + public String toString() { + return symbol; + } + } + + private Expression operand; + + private Operator operator; + + public PrefixExpression(Expression operand, Operator operator) { + this.operand = operand; + this.operator = operator; + } + + public Expression getOperand() { + return operand; + } + + public void setOperand(Expression operand) { + this.operand = operand; + } + + public Operator getOperator() { + return operator; + } + + public void setOperator(Operator operator) { + this.operator = operator; + } + + @Override + public String toString() { + return operator.toString() + operand.toString(); + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/PrimitiveType.java b/AlgebraicDataflowArchitectureModel/src/code/ast/PrimitiveType.java index 9a80598..b8d252e 100644 --- a/AlgebraicDataflowArchitectureModel/src/code/ast/PrimitiveType.java +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/PrimitiveType.java @@ -10,24 +10,24 @@ * @author s-yamagiwa */ public class PrimitiveType extends AnnotatableType { - private SimpleName name; + private models.algebra.Type type; - public PrimitiveType(SimpleName name) { - this.name = name; + public PrimitiveType(models.algebra.Type type) { + this.type = type; } - public SimpleName getName() { - return name; + public models.algebra.Type getType() { + return type; } - public void setName(SimpleName name) { - this.name = name; + public void setType(models.algebra.Type type) { + this.type = type; } @Override public String toString() { if (getAnnotations().isEmpty()) { - return name.getIdentifier(); + return type.getTypeName(); } StringBuilder builder = new StringBuilder(); @@ -35,7 +35,7 @@ builder.append(annotation.toString()); builder.append(" "); } - builder.append(name.getIdentifier()); + builder.append(type.getTypeName()); return builder.toString(); } diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/SimpleName.java b/AlgebraicDataflowArchitectureModel/src/code/ast/SimpleName.java deleted file mode 100644 index e8de874..0000000 --- a/AlgebraicDataflowArchitectureModel/src/code/ast/SimpleName.java +++ /dev/null @@ -1,45 +0,0 @@ -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 index 7e66ae4..3451ee3 100644 --- a/AlgebraicDataflowArchitectureModel/src/code/ast/SimpleType.java +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/SimpleType.java @@ -7,24 +7,24 @@ * @author s-yamagiwa */ public class SimpleType extends AnnotatableType { - private SimpleName name; + private String name; - public SimpleType(SimpleName name) { + public SimpleType(String name) { this.name = name; } - public SimpleName getName() { + public String getName() { return name; } - public void setName(SimpleName name) { + public void setName(String name) { this.name = name; } @Override public String toString() { if (getAnnotations().isEmpty()) { - return name.getIdentifier(); + return name; } StringBuilder builder = new StringBuilder(); @@ -32,7 +32,7 @@ builder.append(annotation.toString()); builder.append(" "); } - builder.append(name.getIdentifier()); + builder.append(name); return builder.toString(); } diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/SuperMethodInvocation.java b/AlgebraicDataflowArchitectureModel/src/code/ast/SuperMethodInvocation.java new file mode 100644 index 0000000..e7e3573 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/SuperMethodInvocation.java @@ -0,0 +1,62 @@ +package code.ast; + +import java.util.List; + +/** + * Represents a super method invocation in AST (Abstract Syntax Tree) + * + * @author s-yamagiwa + * @apiNote Type arguments aren't supported because it isn't necessary in DTRAM code generator. + */ +public class SuperMethodInvocation extends Expression { + private String methodName; + private List arguments; + + public SuperMethodInvocation(String methodName) { + this(methodName, List.of()); + } + + public SuperMethodInvocation(String methodName, List arguments) { + this.methodName = methodName; + this.arguments = arguments; + } + + public String getMethodName() { + return methodName; + } + + public void setMethodName(String methodName) { + this.methodName = methodName; + } + + public List getArguments() { + return arguments; + } + + public void setArguments(List arguments) { + this.arguments = arguments; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + + builder.append("super.").append(methodName).append("("); + + if (!arguments.isEmpty()) { + for (int i = 0; i < arguments.size(); i++) { + Expression argument = arguments.get(i); + + builder.append(argument.toString()); + + if (i < arguments.size() - 1) { + builder.append(", "); + } + } + } + + builder.append(");"); + + return builder.toString(); + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/ThisExpression.java b/AlgebraicDataflowArchitectureModel/src/code/ast/ThisExpression.java new file mode 100644 index 0000000..aad8ee8 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/ThisExpression.java @@ -0,0 +1,4 @@ +package code.ast; + +public class ThisExpression extends Expression { +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/Type.java b/AlgebraicDataflowArchitectureModel/src/code/ast/Type.java index bdf304b..7861acc 100644 --- a/AlgebraicDataflowArchitectureModel/src/code/ast/Type.java +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/Type.java @@ -34,11 +34,4 @@ 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 diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/TypeLiteral.java b/AlgebraicDataflowArchitectureModel/src/code/ast/TypeLiteral.java new file mode 100644 index 0000000..4163b7d --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/TypeLiteral.java @@ -0,0 +1,27 @@ +package code.ast; + +/** + * Represents a type literal expression in the AST. + * + * @author s-yamagiwa + */ +public class TypeLiteral extends Expression { + private Type type; + + public TypeLiteral(Type type) { + this.type = type; + } + + public Type getType() { + return type; + } + + public void setType(Type type) { + this.type = type; + } + + @Override + public String toString() { + return type.toString(); + } +}