diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/BooleanLiteral.java b/AlgebraicDataflowArchitectureModel/src/code/ast/BooleanLiteral.java new file mode 100644 index 0000000..53c1bbe --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/BooleanLiteral.java @@ -0,0 +1,27 @@ +package code.ast; + +/** + * Represents a boolean literal in the abstract syntax tree (AST). + * + * @author s-yamagiwa + */ +public class BooleanLiteral extends Expression { + private boolean value; + + public BooleanLiteral(boolean value) { + this.value = value; + } + + public boolean getValue() { + return value; + } + + public void setValue(boolean value) { + this.value = value; + } + + @Override + public String toString() { + return Boolean.toString(value); + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/NullLiteral.java b/AlgebraicDataflowArchitectureModel/src/code/ast/NullLiteral.java new file mode 100644 index 0000000..d7aa1b3 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/NullLiteral.java @@ -0,0 +1,13 @@ +package code.ast; + +/** + * Represents a {@code null} in the AST. + * + * @author s-yamagiwa + */ +public class NullLiteral extends Expression { + @Override + public String toString() { + return "null"; + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/NumberLiteral.java b/AlgebraicDataflowArchitectureModel/src/code/ast/NumberLiteral.java new file mode 100644 index 0000000..21ca1e3 --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/NumberLiteral.java @@ -0,0 +1,27 @@ +package code.ast; + +/** + * Represents a numeric literal expression in the AST. + * + * @author s-yamagiwa + */ +public class NumberLiteral extends Expression { + private String tokenValue; + + public NumberLiteral(String tokenValue) { + this.tokenValue = tokenValue; + } + + public String getTokenValue() { + return tokenValue; + } + + public void setTokenValue(String tokenValue) { + this.tokenValue = tokenValue; + } + + @Override + public String toString() { + return tokenValue; + } +} diff --git a/AlgebraicDataflowArchitectureModel/src/code/ast/StringLiteral.java b/AlgebraicDataflowArchitectureModel/src/code/ast/StringLiteral.java new file mode 100644 index 0000000..9ebc74b --- /dev/null +++ b/AlgebraicDataflowArchitectureModel/src/code/ast/StringLiteral.java @@ -0,0 +1,180 @@ +package code.ast; + +/** + * Represents a string literal, including its escaped syntax as it appears in the source code. + * + * @author s-yamagiwa + */ +public class StringLiteral extends Expression { + /** + * The literal string with its quotes and escapes. + * Defaults to the literal for the empty string {@code "\"\""}. + */ + private String escapedValue; + + /** + * Constructs a new, empty StringLiteral instance. The default value + * of the literal is an empty string, represented as {@code "\"\""} with + * quotes and escapes included. + */ + public StringLiteral() { + this("\"\""); + } + + /** + * Constructs a new StringLiteral instance with the specified string literal token. + * + * @param escapedValue the string value of the literal, including enclosing + * double quotes and embedded escape sequences + */ + public StringLiteral(String escapedValue) { + this.escapedValue = escapedValue; + } + + /** + * Retrieves the escaped string value of this literal, including its enclosing + * double quotes and embedded escape sequences as it appears in the source code. + * + * @return The escaped string value with quotes and escape sequences. + */ + public String getEscapedValue() { + return escapedValue; + } + + /** + * Sets the escaped value of the string literal. The escaped value + * represents the string, including its enclosing double quotes and + * any embedded escape sequences. + * + * @param escapedValue the new escaped string value, including + * its quotes and escape sequences + */ + public void setEscapedValue(String escapedValue) { + this.escapedValue = escapedValue; + } + + /** + * Retrieves the literal string value of this string literal. The literal value + * does not include enclosing double quotes or escape sequences. + * + * @return The literal string value without enclosing double quotes or escape sequences. + */ + public String getLiteralValue() { + int length = escapedValue.length(); + if (length < 2) { + throw new IllegalArgumentException("Invalid string literal: " + escapedValue); + } + + char first = escapedValue.charAt(0); + char last = escapedValue.charAt(length - 1); + if (first != '\"' || last != '\"') { + throw new IllegalArgumentException("Invalid string literal: " + escapedValue); + } + + StringBuilder sb = new StringBuilder(); + for (int i = 1; i < length - 1; i++) { + char c = escapedValue.charAt(i); + + // If the character is not an escape sequence, just append it + if (c != '\\') { + sb.append(c); + continue; + } + + i++; + if (i < length - 1) { + char next = escapedValue.charAt(i); + switch (next) { + case 'n': + sb.append('\n'); + break; + case 'r': + sb.append('\r'); + break; + case 't': + sb.append('\t'); + break; + case 'b': + sb.append('\b'); + break; + case 'f': + sb.append('\f'); + break; + case '\\': + sb.append('\\'); + break; + case '\"': + sb.append('\"'); + break; + case '\'': + sb.append('\''); + break; + default: + // If it's not a known escape sequence, just append the backslash and the character + sb.append('\\'); + sb.append(next); + } + } else { + sb.append('\\'); + } + } + return sb.toString(); + } + + /** + * Sets the literal string value for this instance. The provided string is processed + * to escape any special characters such as newlines, tabs, and quotes. + * This ensures the string is correctly represented as a literal, with appropriate + * escape sequences included. + * + * @param literalValue the literal string value to set. Special characters in the + * string are escaped, and the resulting value is enclosed in + * double quotes. + */ + public void setLiteralValue(String literalValue) { + StringBuilder sb = new StringBuilder(); + sb.append('\"'); + for (int i = 0; i < literalValue.length(); i++) { + char c = literalValue.charAt(i); + switch (c) { + case '\n': + sb.append("\\n"); + break; + case '\r': + sb.append("\\r"); + break; + case '\t': + sb.append("\\t"); + break; + case '\b': + sb.append("\\b"); + break; + case '\f': + sb.append("\\f"); + break; + case '\\': + sb.append("\\\\"); + break; + case '\"': + sb.append("\\\""); + break; + default: + sb.append(c); + } + } + sb.append('\"'); + + this.escapedValue = sb.toString(); + } + + /** + * Retrieves the string value of this literal node with its quotes and escapes, + * which appears in the actual source code. + * + * @return The string value with its quotes and escapes. + */ + @Override + public String toString() { + return escapedValue; + } +} 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(); + } +}