Compare commits
55 Commits
chainedMet
...
code-gener
Author | SHA1 | Date | |
---|---|---|---|
4f688474a2 | |||
5fc3927c5d | |||
a4d08aacb8 | |||
8eba420d48 | |||
fbff03c3d7 | |||
721e1caa79 | |||
6d3e1f859e | |||
f6b34bf70b | |||
430d551f7d | |||
8e0d627505 | |||
6b6051cad8 | |||
1f8e045b1e | |||
ceb1231632 | |||
|
31929878b0 | ||
|
ea97f34398 | ||
|
2f7b310254 | ||
|
9b8155ebab | ||
|
4775c3f47e | ||
|
2e3a7850a4 | ||
|
d925a3258c | ||
|
fb5372bc8f | ||
|
f29be4fd8c | ||
|
34bb86c7f4 | ||
|
c0e197e2d0 | ||
|
ae872ed906 | ||
91552ad147 | |||
084808c3ab | |||
d19748766f | |||
|
4bdb65a6ce | ||
|
97e0c228d6 | ||
|
d4be77ceb2 | ||
ca539add98 | |||
1bcf396f95 | |||
8ba58d492b | |||
|
ed25868ff7 | ||
879fa08cdc | |||
|
0e512161a0 | ||
|
ba08c6f6b0 | ||
c5aea85965 | |||
2a1d0a0b8a | |||
f1abe5d5a8 | |||
|
a9d7e841f5 | ||
|
b7863d0684 | ||
|
7ccff3208c | ||
|
3e0e6f8327 | ||
|
ab5a51d3b1 | ||
|
a4c1c502ab | ||
|
84ecf316cd | ||
|
7ddea8f18d | ||
46154fbb01 | |||
9aac795c07 | |||
e59f4e7190 | |||
|
44c2f551de | ||
|
bad034acfd | ||
233725778f |
58
.lib/Kurzdokumentation.md
Normal file
58
.lib/Kurzdokumentation.md
Normal file
@@ -0,0 +1,58 @@
|
||||
# Kurzdokumentation
|
||||
|
||||
## Aufgabenverteilung
|
||||
|
||||
### Maximilian Stahl und Jannik Rombach:
|
||||
- **Scanner**
|
||||
- **Parser**
|
||||
- **AST**
|
||||
- **AstBuilder**
|
||||
- **Modul: ast**
|
||||
- Alle
|
||||
- **Modul: parser**
|
||||
- Alle
|
||||
- **Modul: visitor**
|
||||
- Alle
|
||||
- **Testmodul: parser**
|
||||
- AstBuildertest.java
|
||||
- Helper.java
|
||||
- **Testfiles: singleFeatureTests**
|
||||
- Alle
|
||||
|
||||
### Johannes Ehlert:
|
||||
- **Semantische Analyse**
|
||||
- **Modul: semantic**
|
||||
- **Modul: typecheck**
|
||||
- **Testmodul: parser**
|
||||
- AstBuildertest.java
|
||||
- **Testfiles: typedAstFeatureTests**
|
||||
- Großteil
|
||||
- **Testfiles: typedAstExceptionsTests**
|
||||
- Großteil
|
||||
|
||||
### David Große:
|
||||
- **Bytecodegenerator**
|
||||
- **Modul: bytecode**
|
||||
- Alle
|
||||
|
||||
### Lucas Janker:
|
||||
- **Tests**
|
||||
- **Modul: main**
|
||||
- Alle
|
||||
- **Testmodul: main**
|
||||
- Alle
|
||||
- **Testmodul: parser**
|
||||
- ScannerTest.java
|
||||
- ParserTest.java
|
||||
- **Testmodul: semantic**
|
||||
- **Testfiles: combinedFeatureTests**
|
||||
- Alle
|
||||
- **Testfiles: failureTests**
|
||||
- Alle
|
||||
- **Testfiles: Alle**
|
||||
- Refactoring
|
||||
- **Ordnerstrukturen**
|
||||
- Großteil
|
||||
- **Build**
|
||||
- **Makefile**
|
||||
- **Dokumentation**
|
31
pom.xml
31
pom.xml
@@ -20,7 +20,25 @@
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>5.9.3</version> <!-- Change the version as needed -->
|
||||
<version>5.11.0-M2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.platform</groupId>
|
||||
<artifactId>junit-platform-suite-engine</artifactId>
|
||||
<version>1.11.0-M2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>5.11.0-M2</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.13.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -44,12 +62,6 @@
|
||||
<version>3.26.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<version>5.11.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@@ -59,6 +71,11 @@
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>3.0.0-M5</version> <!-- Change the version as needed -->
|
||||
<configuration>
|
||||
<includes>
|
||||
<include>**/*Test.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
|
68
readme.md
Normal file
68
readme.md
Normal file
@@ -0,0 +1,68 @@
|
||||
# "Nicht Haskel 2.0" Java Compiler
|
||||
|
||||
Realisation of a subset of the Java Standard Compiler in the course Compiler Construction of the 4th semester Computer Science at the Duale Hochschule Suttgart (Horb).
|
||||
|
||||
This project aims to provide a simplified version of the Java compiler, focusing on key language features and demonstrating the principles of compiler construction.
|
||||
|
||||
## Realised Java syntax
|
||||
|
||||
- **Data types**: `int`, `boolean`, `char`
|
||||
- **Access modifier**: `public`, `protected`, `private`
|
||||
- **Operators**: `=` `+` `-` `*` `%` `/` `>` `<` `>=` `<=` `==` `!=` `!` `&&` `||` `++` `--`
|
||||
- **Keywords**: `class`, `this`, `while`, `do`, `if`, `else`, `for`, `return`, `new`, `switch`, `case`, `break`, `default`, `:`
|
||||
- **Statements**:
|
||||
- `if` ... `if else` ... `else`;
|
||||
- `while` ... ;
|
||||
- `do` ... `while`;
|
||||
- `for`;
|
||||
- `switch` ... `case` ... ;
|
||||
- **Comments**:
|
||||
- Single line: `// comment`
|
||||
- Multi-line: `/* comment */`
|
||||
- **Further functions**:
|
||||
- All methods are overloadable
|
||||
- High maintainability and expandability through implementation of the visitor pattern
|
||||
- Logging Input and Outputs
|
||||
- Error Handling in the Semantic Check
|
||||
|
||||
## Project Structure
|
||||
|
||||
```plain
|
||||
src/
|
||||
└── main/
|
||||
├── java/
|
||||
│ ├── ast/ -> Defining the structure of the AST
|
||||
│ ├── bytecode/ -> Generate Java bytecode
|
||||
│ ├── main/ -> Running the compiler
|
||||
│ ├── parser/
|
||||
│ │ ├── astBuilder/ -> Builder creating the AST
|
||||
│ │ ├── generated/ -> Antlr generated grammar
|
||||
│ │ └── grammar/ -> Antlr grammar
|
||||
│ ├── semantic/ -> Running the semantic check
|
||||
│ └── visitor/ -> Visitor interface
|
||||
└── resources/
|
||||
test/
|
||||
└── java/
|
||||
│ ├── main/ -> MainTest, E2ETests, UtilityTests
|
||||
│ ├── parser/ -> Performs tests on the parser
|
||||
│ └── semantic/ -> Performs tests on the semantic check
|
||||
└── resources/ -> Ressources for running the Tests
|
||||
```
|
||||
|
||||
## Class-Diagramm AST
|
||||
|
||||

|
||||
|
||||
## Used Tools
|
||||
|
||||
- [Maven 4.0](https://maven.apache.org/index.html)
|
||||
- Used for automating the build process and managing dependencies.
|
||||
- [ANTLR4 v.13.1](https://www.antlr.org/)
|
||||
- Used to parse the input Java code into the Abstract Syntax Tree.
|
||||
|
||||
|
||||
## How to run the compiler
|
||||
|
||||
## Download
|
||||
|
||||
```bash
|
@@ -9,7 +9,6 @@ public class CalculationNode extends BinaryNode {
|
||||
public CalculationNode calculationExpression;
|
||||
public EnumLineOperator operator;
|
||||
public DotNode dotExpression;
|
||||
private ITypeNode typeNode;
|
||||
|
||||
public CalculationNode(CalculationNode calculationExpression, String operator, DotNode dotExpression) {
|
||||
this.calculationExpression = calculationExpression;
|
||||
@@ -42,4 +41,4 @@ public class CalculationNode extends BinaryNode {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -5,7 +5,6 @@ import bytecode.visitor.MethodVisitor;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class MainMethodNode extends MethodNode implements Visitable {
|
||||
public BlockNode block;
|
||||
|
||||
public MainMethodNode(BlockNode block) {
|
||||
this.block = block;
|
||||
|
@@ -1,11 +0,0 @@
|
||||
package ast.statement;
|
||||
|
||||
import ast.statements.IStatementNode;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class BlockStatementNode {
|
||||
List<IStatementNode> statements;
|
||||
|
||||
public BlockStatementNode(List<IStatementNode> statements) {this.statements = statements;}
|
||||
}
|
@@ -7,7 +7,7 @@ import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class DecrementNode implements IStatementExpressionNode {
|
||||
public class DecrementNode implements IStatementExpressionNode, Visitable {
|
||||
public CrementType crementType;
|
||||
public AssignableNode assignableExpression;
|
||||
|
||||
@@ -20,4 +20,9 @@ public class DecrementNode implements IStatementExpressionNode {
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
@@ -5,8 +5,9 @@ import ast.statementexpressions.IStatementExpressionNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
import visitor.Visitable;
|
||||
|
||||
public class IncrementNode implements IStatementExpressionNode {
|
||||
public class IncrementNode implements IStatementExpressionNode, Visitable {
|
||||
public CrementType crementType;
|
||||
public AssignableNode assignableExpression;
|
||||
|
||||
@@ -19,4 +20,9 @@ public class IncrementNode implements IStatementExpressionNode {
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
}
|
||||
|
@@ -22,11 +22,6 @@ public class ChainedMethodNode implements ASTNode, Visitable {
|
||||
expressions.add(expression);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void accept(MethodVisitor methodVisitor) {
|
||||
methodVisitor.visit(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return null;
|
||||
|
@@ -2,6 +2,7 @@ package ast.statementexpressions.methodcallstatementnexpressions;
|
||||
|
||||
import ast.expressions.IExpressionNode;
|
||||
import ast.statements.IStatementNode;
|
||||
import ast.type.type.ITypeNode;
|
||||
import bytecode.visitor.MethodVisitor;
|
||||
import semantic.SemanticVisitor;
|
||||
import typechecker.TypeCheckResult;
|
||||
@@ -11,6 +12,7 @@ import java.util.List;
|
||||
|
||||
public class MethodCallNode implements IStatementNode {
|
||||
public TargetNode target;
|
||||
public ITypeNode type;
|
||||
public List<ChainedMethodNode> chainedMethods = new ArrayList<>();
|
||||
public String identifier;
|
||||
public List<IExpressionNode> parameters = new ArrayList<>();
|
||||
|
@@ -13,6 +13,11 @@ public class WhileNode implements IStatementNode {
|
||||
this.block = block;
|
||||
}
|
||||
|
||||
public void test() {
|
||||
return;
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||
return visitor.analyze(this);
|
||||
|
@@ -27,7 +27,7 @@ public class ClassCodeGen implements ClassVisitor {
|
||||
private boolean generateClassFiles;
|
||||
|
||||
public ClassCodeGen(JarOutputStream jarOutputStream, String outputDirectory, boolean generateJar, boolean generateClassFiles) {
|
||||
mapper = new Mapper();
|
||||
this.mapper = new Mapper();
|
||||
this.jarOutputStream = jarOutputStream;
|
||||
this.outputDirectory = outputDirectory;
|
||||
this.generateJar = generateJar;
|
||||
|
@@ -3,6 +3,7 @@ package bytecode;
|
||||
import ast.parameters.ParameterNode;
|
||||
import ast.type.*;
|
||||
import ast.type.type.BaseType;
|
||||
import ast.type.type.ITypeNode;
|
||||
import ast.type.type.ReferenceType;
|
||||
import ast.type.type.TypeEnum;
|
||||
import org.objectweb.asm.Opcodes;
|
||||
@@ -24,7 +25,7 @@ public class Mapper {
|
||||
return 0;
|
||||
}
|
||||
|
||||
public String generateMethodDescriptor(BaseType type, List<ParameterNode> parameters) {
|
||||
public String generateMethodDescriptor(ITypeNode type, List<ParameterNode> parameters) {
|
||||
String descriptor = "(";
|
||||
for (ParameterNode parameterNode : parameters) {
|
||||
if(parameterNode.type instanceof BaseType) {
|
||||
@@ -35,7 +36,11 @@ public class Mapper {
|
||||
}
|
||||
}
|
||||
descriptor += ")";
|
||||
descriptor += getTypeChar(type);
|
||||
if(type instanceof BaseType) {
|
||||
descriptor += getTypeChar((BaseType) type);
|
||||
} else if(type instanceof ReferenceType) {
|
||||
descriptor += "L" + ((ReferenceType) type).getIdentifier() +";";
|
||||
}
|
||||
return descriptor;
|
||||
}
|
||||
|
||||
|
@@ -14,9 +14,7 @@ import ast.statementexpressions.NewDeclarationNode;
|
||||
import ast.statementexpressions.crementexpressions.CrementType;
|
||||
import ast.statementexpressions.crementexpressions.DecrementNode;
|
||||
import ast.statementexpressions.crementexpressions.IncrementNode;
|
||||
import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNode;
|
||||
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
||||
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
||||
import ast.statements.*;
|
||||
import ast.type.ValueNode;
|
||||
import ast.type.type.BaseType;
|
||||
@@ -71,9 +69,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||
|
||||
// Visit all statements
|
||||
for (IStatementNode statementNode : constructorNode.block.statements) {
|
||||
if (statementNode != null) {
|
||||
statementNode.accept(this);
|
||||
}
|
||||
statementNode.accept(this);
|
||||
}
|
||||
|
||||
methodVisitor.visitMaxs(0, 0);
|
||||
@@ -105,7 +101,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||
public void visit(MethodNode methodNode) {
|
||||
methodVisitor = classWriter.visitMethod(mapper.mapAccessTypeToOpcode(methodNode.accesModifier),
|
||||
methodNode.getIdentifier(),
|
||||
mapper.generateMethodDescriptor((BaseType) methodNode.getType(), methodNode.parameters),
|
||||
mapper.generateMethodDescriptor(methodNode.getType(), methodNode.parameters),
|
||||
null,
|
||||
null);
|
||||
|
||||
@@ -241,7 +237,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||
nonCalculationNode.unaryExpression.accept(this);
|
||||
nonCalculationNode.expression.accept(this);
|
||||
if (nonCalculationNode.unaryExpression.getType() instanceof BaseType && nonCalculationNode.expression.getType() instanceof BaseType) {
|
||||
methodVisitor.visitJumpInsn(IF_ACMPEQ, labelFalse);
|
||||
methodVisitor.visitJumpInsn(IF_ICMPEQ, labelFalse);
|
||||
} else {
|
||||
methodVisitor.visitJumpInsn(IF_ACMPEQ, labelFalse);
|
||||
}
|
||||
@@ -266,9 +262,17 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||
@Override
|
||||
public void visit(MemberAccessNode memberAccessNode) {
|
||||
// Only used to get, not to put
|
||||
if (memberAccessNode.thisExpr) { // Field
|
||||
// methodVisitor.visitFieldInsn(GETFIELD, memberAccessNode.identifiers.get(0), memberAccessNode.identifiers.get(1), );
|
||||
} else { // Object Attribut
|
||||
int localVarIndex = localVariables.indexOf("memberAccessNode.identifier"); // TODO
|
||||
if (localVarIndex >= 0) { // local var object
|
||||
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
|
||||
} else { // this field
|
||||
methodVisitor.visitVarInsn(ALOAD, 0);
|
||||
}
|
||||
|
||||
if (memberAccessNode.getTypeNode() instanceof BaseType) {
|
||||
methodVisitor.visitFieldInsn(GETFIELD, memberAccessNode.identifiers.get(0), memberAccessNode.identifiers.get(1), mapper.getTypeChar((BaseType) memberAccessNode.getTypeNode()));
|
||||
} else if (memberAccessNode.getTypeNode() instanceof ReferenceType) {
|
||||
methodVisitor.visitFieldInsn(GETFIELD, memberAccessNode.identifiers.get(0), memberAccessNode.identifiers.get(1), "L" + ((ReferenceType) memberAccessNode.getTypeNode()).getIdentifier() + ";");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -331,6 +335,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||
// else if statements
|
||||
methodVisitor.visitJumpInsn(IFEQ, elseIfLabels[0]);
|
||||
}
|
||||
|
||||
ifElseNode.ifStatement.block.accept(this); // accept if block
|
||||
|
||||
Label endLabel = new Label();
|
||||
@@ -362,22 +367,17 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||
if (localVariableDeclarationNode.expression != null) {
|
||||
// Process expression
|
||||
localVariableDeclarationNode.expression.accept(this);
|
||||
// Store result of expression in variable
|
||||
if (localVariables.contains(localVariableDeclarationNode.identifier)) {
|
||||
if (localVariableDeclarationNode.type instanceof BaseType) {
|
||||
methodVisitor.visitVarInsn(ISTORE, localVariables.indexOf(localVariableDeclarationNode.identifier));
|
||||
} else if (localVariableDeclarationNode.type instanceof ReferenceType) {
|
||||
methodVisitor.visitVarInsn(ASTORE, localVariables.indexOf(localVariableDeclarationNode.identifier));
|
||||
}
|
||||
} else {
|
||||
// add local var to list if not in list
|
||||
if (!localVariables.contains(localVariableDeclarationNode.identifier)) {
|
||||
localVariables.add(localVariableDeclarationNode.identifier);
|
||||
if (localVariableDeclarationNode.type instanceof BaseType) {
|
||||
methodVisitor.visitVarInsn(ISTORE, localVariables.indexOf(localVariableDeclarationNode.identifier));
|
||||
} else if (localVariableDeclarationNode.type instanceof ReferenceType) {
|
||||
methodVisitor.visitVarInsn(ASTORE, localVariables.indexOf(localVariableDeclarationNode.identifier));
|
||||
}
|
||||
}
|
||||
if (localVariableDeclarationNode.type instanceof BaseType) {
|
||||
methodVisitor.visitVarInsn(ISTORE, localVariables.indexOf(localVariableDeclarationNode.identifier));
|
||||
} else if (localVariableDeclarationNode.type instanceof ReferenceType) {
|
||||
methodVisitor.visitVarInsn(ASTORE, localVariables.indexOf(localVariableDeclarationNode.identifier));
|
||||
}
|
||||
} else {
|
||||
// Local var declaration
|
||||
if (!localVariables.contains(localVariableDeclarationNode.identifier)) {
|
||||
localVariables.add(localVariableDeclarationNode.identifier);
|
||||
}
|
||||
@@ -386,40 +386,106 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||
|
||||
@Override
|
||||
public void visit(AssignNode assignNode) {
|
||||
// Process expression
|
||||
if (assignNode.assignable.memberAccess != null) { // this / object
|
||||
if (assignNode.expression instanceof IncrementNode) {
|
||||
if (((IncrementNode) assignNode.expression).crementType.equals(CrementType.PREFIX)) { // ++i
|
||||
fieldOrObjectVarPrefixCrementAssign(assignNode);
|
||||
} else { // i++
|
||||
fieldOrObjectVarSuffixCrementAssign(assignNode);
|
||||
}
|
||||
} else if (assignNode.expression instanceof DecrementNode) {
|
||||
if (((DecrementNode) assignNode.expression).crementType.equals(CrementType.PREFIX)) {
|
||||
fieldOrObjectVarPrefixCrementAssign(assignNode);
|
||||
} else {
|
||||
fieldOrObjectVarSuffixCrementAssign(assignNode);
|
||||
}
|
||||
} else {
|
||||
assignFieldOrObjectVar(assignNode);
|
||||
}
|
||||
} else { // local var
|
||||
if (assignNode.expression instanceof IncrementNode || assignNode.expression instanceof DecrementNode) {
|
||||
localVarCrementAssign(assignNode);
|
||||
} else {
|
||||
assignNode.expression.accept(this);
|
||||
assignLocalVar(assignNode);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void localVarCrementAssign(AssignNode assignNode) {
|
||||
if (assignNode.expression instanceof IncrementNode) {
|
||||
IncrementNode incrementNode = (IncrementNode) assignNode.expression;
|
||||
if (incrementNode.crementType.equals(CrementType.PREFIX)) { // ++i
|
||||
methodVisitor.visitIincInsn(localVariables.indexOf(incrementNode.assignableExpression.identifier), 1);
|
||||
assign(assignNode);
|
||||
} else if (incrementNode.crementType.equals(CrementType.SUFFIX)) { // Suffix: i++
|
||||
assign(assignNode);
|
||||
methodVisitor.visitIincInsn(localVariables.indexOf(incrementNode.assignableExpression.identifier), 1);
|
||||
incrementNode.accept(this);
|
||||
assignLocalVar(assignNode);
|
||||
} else { // i++
|
||||
loadBeforeCrement(assignNode);
|
||||
assignLocalVar(assignNode);
|
||||
incrementNode.accept(this);
|
||||
}
|
||||
} else if (assignNode.expression instanceof DecrementNode) {
|
||||
DecrementNode decrementNode = (DecrementNode) assignNode.expression;
|
||||
if (decrementNode.crementType.equals(CrementType.PREFIX)) {
|
||||
methodVisitor.visitIincInsn(localVariables.indexOf(decrementNode.assignableExpression.identifier), -1);
|
||||
assign(assignNode);
|
||||
} else if (decrementNode.crementType.equals(CrementType.SUFFIX)) {
|
||||
assign(assignNode);
|
||||
methodVisitor.visitIincInsn(localVariables.indexOf(decrementNode.assignableExpression.identifier), 1);
|
||||
decrementNode.accept(this);
|
||||
assignLocalVar(assignNode);
|
||||
} else {
|
||||
loadBeforeCrement(assignNode);
|
||||
assignLocalVar(assignNode);
|
||||
decrementNode.accept(this);
|
||||
}
|
||||
} else {
|
||||
assignNode.expression.accept(this);
|
||||
assign(assignNode);
|
||||
}
|
||||
}
|
||||
|
||||
private void assign(AssignNode assignNode) {
|
||||
if (assignNode.assignable.memberAccess.thisExpr) {
|
||||
assignField(assignNode);
|
||||
private void fieldOrObjectVarPrefixCrementAssign(AssignNode assignNode) {
|
||||
int localVarIndex = localVariables.indexOf(assignNode.assignable.identifier);
|
||||
if(localVarIndex >= 0) { // object
|
||||
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
|
||||
} else if(assignNode.assignable.memberAccess.thisExpr) { // field
|
||||
methodVisitor.visitVarInsn(ALOAD, 0);
|
||||
} else {
|
||||
assignLocalVar(assignNode);
|
||||
localVariables.add(assignNode.assignable.identifier);
|
||||
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
|
||||
}
|
||||
|
||||
assignNode.expression.accept(this);
|
||||
methodVisitor.visitInsn(DUP_X1);
|
||||
|
||||
if (assignNode.expression instanceof BaseType) {
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) assignNode.expression.getType()));
|
||||
} else if (assignNode.expression instanceof ReferenceType) {
|
||||
ReferenceType referenceType = (ReferenceType) assignNode.expression.getType();
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), "L" + referenceType.getIdentifier() + ";");
|
||||
}
|
||||
}
|
||||
|
||||
private void fieldOrObjectVarSuffixCrementAssign(AssignNode assignNode) {
|
||||
int localVarIndex = localVariables.indexOf(assignNode.assignable.identifier);
|
||||
if(localVarIndex >= 0) { // object
|
||||
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
|
||||
} else if(assignNode.assignable.memberAccess.thisExpr) { // field
|
||||
methodVisitor.visitVarInsn(ALOAD, 0);
|
||||
} else {
|
||||
localVariables.add(assignNode.assignable.identifier);
|
||||
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
|
||||
}
|
||||
|
||||
loadBeforeCrement(assignNode);
|
||||
|
||||
if (assignNode.expression instanceof BaseType) {
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) assignNode.expression.getType()));
|
||||
} else if (assignNode.expression instanceof ReferenceType) {
|
||||
ReferenceType referenceType = (ReferenceType) assignNode.expression.getType();
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), "L" + referenceType.getIdentifier() + ";");
|
||||
}
|
||||
|
||||
assignNode.expression.accept(this);
|
||||
}
|
||||
|
||||
private void assignLocalVar(AssignNode assignNode) {
|
||||
methodVisitor.visitInsn(DUP);
|
||||
if (!localVariables.contains(assignNode.assignable.identifier)) {
|
||||
localVariables.add(assignNode.assignable.identifier);
|
||||
}
|
||||
if (assignNode.expression instanceof BaseType) {
|
||||
methodVisitor.visitVarInsn(ISTORE, localVariables.indexOf(assignNode.assignable.identifier));
|
||||
} else if (assignNode.expression instanceof ReferenceType) {
|
||||
@@ -427,12 +493,105 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||
}
|
||||
}
|
||||
|
||||
private void assignField(AssignNode assignNode) {
|
||||
private void assignFieldOrObjectVar(AssignNode assignNode) {
|
||||
int localVarIndex = localVariables.indexOf(assignNode.assignable.identifier);
|
||||
if (localVarIndex >= 0) { // object
|
||||
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
|
||||
} else if (assignNode.assignable.memberAccess.thisExpr) { // this
|
||||
methodVisitor.visitVarInsn(ALOAD, 0);
|
||||
} else {
|
||||
localVariables.add(assignNode.assignable.identifier);
|
||||
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
|
||||
}
|
||||
|
||||
assignNode.expression.accept(this);
|
||||
methodVisitor.visitInsn(DUP_X1);
|
||||
|
||||
if (assignNode.expression instanceof BaseType) {
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) assignNode.expression.getType()));
|
||||
} else if (assignNode.expression instanceof ReferenceType) {
|
||||
ReferenceType referenceType = (ReferenceType) assignNode.expression.getType();
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), "L"+referenceType.getIdentifier()+";");
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), "L" + referenceType.getIdentifier() + ";");
|
||||
}
|
||||
}
|
||||
|
||||
private void loadBeforeCrement(AssignNode assignNode) {
|
||||
if(assignNode.expression instanceof IncrementNode) {
|
||||
IncrementNode incrementNode = (IncrementNode) assignNode.expression;
|
||||
if(incrementNode.assignableExpression.memberAccess != null) {
|
||||
incrementNode.assignableExpression.memberAccess.accept(this);
|
||||
} else {
|
||||
if(assignNode.assignable.getTypeNode() instanceof BaseType) {
|
||||
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignNode.assignable.identifier));
|
||||
} else if(assignNode.assignable.getTypeNode() instanceof ReferenceType) {
|
||||
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
|
||||
}
|
||||
}
|
||||
} else if(assignNode.expression instanceof DecrementNode) {
|
||||
DecrementNode decrementNode = (DecrementNode) assignNode.expression;
|
||||
if(decrementNode.assignableExpression.memberAccess != null) {
|
||||
decrementNode.assignableExpression.memberAccess.accept(this);
|
||||
} else {
|
||||
if(assignNode.assignable.getTypeNode() instanceof BaseType) {
|
||||
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignNode.assignable.identifier));
|
||||
} else if(assignNode.assignable.getTypeNode() instanceof ReferenceType) {
|
||||
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(IncrementNode incrementNode) {
|
||||
if (incrementNode.assignableExpression.memberAccess != null) { // Object var / field
|
||||
int localVarIndex = localVariables.indexOf(incrementNode.assignableExpression.identifier);
|
||||
if (localVarIndex >= 0) { // object
|
||||
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
|
||||
} else { // this
|
||||
methodVisitor.visitVarInsn(ALOAD, 0);
|
||||
}
|
||||
if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) {
|
||||
methodVisitor.visitFieldInsn(GETFIELD, incrementNode.assignableExpression.memberAccess.identifiers.get(0), incrementNode.assignableExpression.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) incrementNode.assignableExpression.memberAccess.getTypeNode()));
|
||||
} else if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) {
|
||||
methodVisitor.visitFieldInsn(GETFIELD, incrementNode.assignableExpression.memberAccess.identifiers.get(0), incrementNode.assignableExpression.memberAccess.identifiers.get(1), "L" + (((ReferenceType) incrementNode.assignableExpression.memberAccess.getTypeNode()).getIdentifier() + ";"));
|
||||
}
|
||||
methodVisitor.visitInsn(DUP);
|
||||
methodVisitor.visitInsn(ICONST_1);
|
||||
methodVisitor.visitInsn(IADD);
|
||||
if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) {
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, incrementNode.assignableExpression.memberAccess.identifiers.get(0), incrementNode.assignableExpression.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) incrementNode.assignableExpression.memberAccess.getTypeNode()));
|
||||
} else if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) {
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, incrementNode.assignableExpression.memberAccess.identifiers.get(0), incrementNode.assignableExpression.memberAccess.identifiers.get(1), "L" + (((ReferenceType) incrementNode.assignableExpression.memberAccess.getTypeNode()).getIdentifier() + ";"));
|
||||
}
|
||||
} else { // local var
|
||||
methodVisitor.visitIincInsn(localVariables.indexOf(incrementNode.assignableExpression.identifier), 1);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(DecrementNode decrementNode) {
|
||||
if (decrementNode.assignableExpression.memberAccess != null) { // Object var / field
|
||||
int localVarIndex = localVariables.indexOf(decrementNode.assignableExpression.identifier);
|
||||
if (localVarIndex >= 0) { // object
|
||||
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
|
||||
} else { // this
|
||||
methodVisitor.visitVarInsn(ALOAD, 0);
|
||||
}
|
||||
methodVisitor.visitInsn(DUP);
|
||||
if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) {
|
||||
methodVisitor.visitFieldInsn(GETFIELD, decrementNode.assignableExpression.memberAccess.identifiers.get(0), decrementNode.assignableExpression.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) decrementNode.assignableExpression.memberAccess.getTypeNode()));
|
||||
} else if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) {
|
||||
methodVisitor.visitFieldInsn(GETFIELD, decrementNode.assignableExpression.memberAccess.identifiers.get(0), decrementNode.assignableExpression.memberAccess.identifiers.get(1), "L" + (((ReferenceType) decrementNode.assignableExpression.memberAccess.getTypeNode()).getIdentifier() + ";"));
|
||||
}
|
||||
methodVisitor.visitInsn(ICONST_1);
|
||||
methodVisitor.visitInsn(ISUB);
|
||||
if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) {
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, decrementNode.assignableExpression.memberAccess.identifiers.get(0), decrementNode.assignableExpression.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) decrementNode.assignableExpression.memberAccess.getTypeNode()));
|
||||
} else if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) {
|
||||
methodVisitor.visitFieldInsn(PUTFIELD, decrementNode.assignableExpression.memberAccess.identifiers.get(0), decrementNode.assignableExpression.memberAccess.identifiers.get(1), "L" + (((ReferenceType) decrementNode.assignableExpression.memberAccess.getTypeNode()).getIdentifier() + ";"));
|
||||
}
|
||||
} else { // local var
|
||||
methodVisitor.visitIincInsn(localVariables.indexOf(decrementNode.assignableExpression.identifier), -1);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -445,8 +604,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||
expressionNode.accept(this);
|
||||
parameterNodes.add(new ParameterNode(expressionNode.getType(), ""));
|
||||
}
|
||||
methodVisitor.visitMethodInsn(INVOKESPECIAL, newDeclarationNode.identifier, "<init>", mapper.generateMethodDescriptor(new BaseType(TypeEnum.VOID),parameterNodes), false);
|
||||
localVariables.add(newDeclarationNode.identifier);
|
||||
methodVisitor.visitMethodInsn(INVOKESPECIAL, newDeclarationNode.identifier, "<init>", mapper.generateMethodDescriptor(new BaseType(TypeEnum.VOID), parameterNodes), false);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -518,19 +676,13 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||
methodVisitor.visitLabel(endOfLoopLabel);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(ChainedMethodNode chainedMethodNode) {
|
||||
// TODO: Erstmal abwarten
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visit(MethodCallNode methodCallNode) {
|
||||
List<ParameterNode> parameterNodes = new ArrayList<>();
|
||||
for(IExpressionNode expressionNode : methodCallNode.parameters) {
|
||||
for (IExpressionNode expressionNode : methodCallNode.parameters) {
|
||||
expressionNode.accept(this);
|
||||
parameterNodes.add(new ParameterNode(expressionNode.getType(), ""));
|
||||
}
|
||||
// TODO: Klassenname und Returntype
|
||||
//methodVisitor.visitMethodInsn(INVOKEVIRTUAL, classname, methodCallNode.identifier, mapper.generateMethodDescriptor(returntype, parameterNodes), false);
|
||||
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, methodCallNode.target.memberAccess.identifiers.get(0), methodCallNode.identifier, mapper.generateMethodDescriptor(methodCallNode.type, parameterNodes), false);
|
||||
}
|
||||
}
|
||||
|
@@ -5,6 +5,5 @@ import ast.members.FieldNode;
|
||||
|
||||
public interface ClassVisitor {
|
||||
void visit(ClassNode classNode);
|
||||
|
||||
void visit(FieldNode fieldNode);
|
||||
}
|
||||
|
@@ -39,12 +39,14 @@ public interface MethodVisitor {
|
||||
// statements
|
||||
void visit(IfElseNode ifElseNode);
|
||||
|
||||
void visit(IncrementNode incrementNode);
|
||||
void visit(DecrementNode decrementNode);
|
||||
|
||||
void visit(LocalVariableDeclarationNode localVariableDeclarationNode);
|
||||
void visit(ReturnNode returnNode);
|
||||
void visit(WhileNode whileNode);
|
||||
|
||||
// statement expression
|
||||
void visit(ChainedMethodNode chainedMethodNode);
|
||||
void visit(MethodCallNode methodCallNode);
|
||||
|
||||
void visit(AssignNode assignNode);
|
||||
|
@@ -13,6 +13,7 @@ import org.antlr.v4.runtime.tree.ParseTree;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
/**
|
||||
@@ -20,9 +21,9 @@ import java.nio.file.Paths;
|
||||
* <p> <code> cd .\src\test\ </code>
|
||||
* <p> <code> make clean compile-raupenpiler </code>
|
||||
* <p> Start Raupenpiler using jar:
|
||||
* <p> <code> java.exe -jar path_to_jar\JavaCompiler-1.0-jar-with-dependencies.jar 'path_to_input_file.java' 'path_to_output_directory' </code>
|
||||
* <p> <code> java.exe -DgenJar=true_OR_false -DgenClass=true_OR_false -jar path_to_jar\JavaCompiler-1.0-jar-with-dependencies.jar 'path_to_input_file.java' 'path_to_output_directory' </code>
|
||||
* <p> Example (jar needs to be in the target directory, compile with make or mvn package first):
|
||||
* <code> java.exe -jar .\target\JavaCompiler-1.0-jar-with-dependencies.jar 'src/main/resources/input/CompilerInput.java' 'src/main/resources/output' </code>
|
||||
* <code> java.exe -DgenJar=true -DgenClass=true -jar .\target\JavaCompiler-1.0-jar-with-dependencies.jar 'src/main/resources/input/CompilerInput.java' 'src/main/resources/output' </code>
|
||||
*/
|
||||
public class Main {
|
||||
public static void main(String[] args) throws Exception {
|
||||
@@ -91,16 +92,28 @@ public class Main {
|
||||
|
||||
/*------------------------- Semantic Analyzer -> typed AST -------------------------*/
|
||||
// Use the SemanticAnalyzer to generate a typed AST
|
||||
//ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
// Log the typed AST
|
||||
RaupenLogger.logSemanticAnalyzer(abstractSyntaxTree);
|
||||
RaupenLogger.logSemanticAnalyzer(typedAst);
|
||||
|
||||
if(SemanticAnalyzer.errors.isEmpty()){
|
||||
/*------------------------- Bytecode Generator -> Bytecode -------------------------*/
|
||||
// Use the ByteCodeGenerator to generate bytecode from the typed AST and output it to the specified directory
|
||||
|
||||
final boolean genJar = Optional.ofNullable(System.getProperty("genJar")).map(String::toLowerCase).map(Boolean::parseBoolean).orElse(true);
|
||||
final boolean genClass = Optional.ofNullable(System.getProperty("genClass")).map(String::toLowerCase).map(Boolean::parseBoolean).orElse(true);
|
||||
|
||||
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(outputDirectoryPath, genJar, genClass);
|
||||
assert typedAst != null;
|
||||
byteCodeGenerator.visit((ProgramNode) typedAst);
|
||||
// Log the bytecode generation
|
||||
RaupenLogger.logBytecodeGenerator();
|
||||
} else {
|
||||
for(Exception exception : SemanticAnalyzer.errors){
|
||||
exception.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*------------------------- Bytecode Generator -> Bytecode -------------------------*/
|
||||
// Use the ByteCodeGenerator to generate bytecode from the typed AST and output it to the specified directory
|
||||
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(outputDirectoryPath, true, true);
|
||||
assert abstractSyntaxTree != null;
|
||||
byteCodeGenerator.visit((ProgramNode) abstractSyntaxTree);
|
||||
// Log the bytecode generation
|
||||
RaupenLogger.logBytecodeGenerator();
|
||||
}
|
||||
}
|
@@ -281,8 +281,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
|
||||
@Override
|
||||
public ASTNode visitIfElseStatement(SimpleJavaParser.IfElseStatementContext ctx) {
|
||||
IfElseNode ifElseStatementNode = new IfElseNode((IfNode) visit(ctx.ifStatement()),
|
||||
(ElseNode) visit(ctx.elseStatement()));
|
||||
IfElseNode ifElseStatementNode;
|
||||
if(ctx.elseStatement() != null) {
|
||||
ifElseStatementNode = new IfElseNode((IfNode) visit(ctx.ifStatement()),
|
||||
(ElseNode) visit(ctx.elseStatement()));
|
||||
} else {
|
||||
ifElseStatementNode = new IfElseNode((IfNode) visit(ctx.ifStatement()), null);
|
||||
}
|
||||
|
||||
for (SimpleJavaParser.ElseIfStatementContext elseIf : ctx.elseIfStatement()){
|
||||
ifElseStatementNode.addElseIfStatement(((IfNode) visit(elseIf)));
|
||||
@@ -599,6 +604,4 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
||||
default -> new ReferenceType(identifier);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
File diff suppressed because one or more lines are too long
@@ -1,4 +1,4 @@
|
||||
// Generated from C:/Users/janni/Desktop/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
||||
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
||||
package parser.generated;
|
||||
|
||||
import org.antlr.v4.runtime.ParserRuleContext;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
// Generated from C:/Users/janni/Desktop/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
||||
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
||||
package parser.generated;
|
||||
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
// Generated from C:/Users/janni/Desktop/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
||||
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
||||
package parser.generated;
|
||||
import org.antlr.v4.runtime.Lexer;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
|
@@ -1,4 +1,4 @@
|
||||
// Generated from C:/Users/janni/Desktop/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
||||
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
||||
package parser.generated;
|
||||
import org.antlr.v4.runtime.tree.ParseTreeListener;
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,4 +1,4 @@
|
||||
// Generated from C:/Users/janni/Desktop/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
||||
// Generated from C:/Users/Maxi/Documents/DHBW/Compilerbau/NichtHaskell2.0/src/main/java/parser/grammar/SimpleJava.g4 by ANTLR 4.13.1
|
||||
package parser.generated;
|
||||
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
|
||||
|
||||
|
@@ -21,6 +21,7 @@ import ast.statementexpressions.AssignableNode;
|
||||
import ast.statementexpressions.NewDeclarationNode;
|
||||
import ast.statementexpressions.crementexpressions.DecrementNode;
|
||||
import ast.statementexpressions.crementexpressions.IncrementNode;
|
||||
import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNode;
|
||||
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
||||
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
||||
import ast.statements.*;
|
||||
@@ -29,6 +30,7 @@ import ast.type.EnumAccessModifierNode;
|
||||
import ast.type.ValueNode;
|
||||
import ast.type.type.*;
|
||||
import com.sun.jdi.IntegerType;
|
||||
import semantic.context.ClassContext;
|
||||
import semantic.context.Context;
|
||||
import semantic.exceptions.*;
|
||||
import typechecker.TypeCheckResult;
|
||||
@@ -163,6 +165,11 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(FieldNode toCheck) {
|
||||
if (toCheck.type instanceof ReferenceType referenceType) {
|
||||
if (!context.containsClass(referenceType.getIdentifier())) {
|
||||
errors.add(new NotDeclaredException(referenceType.getIdentifier() + " not declared"));
|
||||
}
|
||||
}
|
||||
if (currentFields.get(toCheck.identifier) != null) {
|
||||
errors.add(new AlreadyDeclaredException("Already declared " + toCheck.identifier));
|
||||
return new TypeCheckResult(false, null);
|
||||
@@ -170,6 +177,8 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
currentFields.put(toCheck.identifier, toCheck.type);
|
||||
}
|
||||
return new TypeCheckResult(true, null);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -224,7 +233,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
}
|
||||
for (IStatementNode statementNode : blockNode.statements) {
|
||||
var result = statementNode.accept(this);
|
||||
if(!(statementNode instanceof IncrementNode) && !(statementNode instanceof DecrementNode)){
|
||||
if (!(statementNode instanceof IncrementNode) && !(statementNode instanceof DecrementNode)) {
|
||||
if (result.getType() != null) {
|
||||
if (blockReturnType == null) {
|
||||
blockReturnType = result.getType();
|
||||
@@ -244,11 +253,16 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
|
||||
if (toCheck.memberAccess != null) {
|
||||
var result = toCheck.memberAccess.accept(this);
|
||||
toCheck.identifier = toCheck.memberAccess.identifiers.getLast();
|
||||
toCheck.setTypeNode(result.getType());
|
||||
return result;
|
||||
} else {
|
||||
if (currentFields.get(toCheck.identifier) != null) {
|
||||
var type = currentFields.get(toCheck.identifier);
|
||||
MemberAccessNode memberAccessNode = new MemberAccessNode(false);
|
||||
memberAccessNode.identifiers.add(currentClass.identifier);
|
||||
memberAccessNode.identifiers.add(toCheck.identifier);
|
||||
toCheck.memberAccess = memberAccessNode;
|
||||
toCheck.setTypeNode(type);
|
||||
return new TypeCheckResult(true, type);
|
||||
} else if (currentScope.getLocalVar(toCheck.identifier) != null) {
|
||||
@@ -275,13 +289,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
currentNullType = oldNullType;
|
||||
var valid = true;
|
||||
|
||||
// This check currently handles things like :
|
||||
/**
|
||||
* private int i;
|
||||
* void foo(int i){
|
||||
* i = i;
|
||||
* }
|
||||
*/
|
||||
if (assignable.equals(rExpression)) {
|
||||
errors.add(new TypeMismatchException("Cannot assign to self"));
|
||||
valid = false;
|
||||
@@ -310,41 +317,84 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
@Override
|
||||
public TypeCheckResult analyze(IfElseNode toCheck) {
|
||||
var resultIf = toCheck.ifStatement.accept(this);
|
||||
var resultElse = toCheck.elseStatement.accept(this);
|
||||
if(toCheck.elseStatement != null){
|
||||
var resultElse = toCheck.elseStatement.accept(this);
|
||||
return new TypeCheckResult(resultIf.isValid() && resultElse.isValid(), new BaseType(TypeEnum.VOID));
|
||||
}
|
||||
|
||||
return new TypeCheckResult(resultIf.isValid() && resultElse.isValid(), new BaseType(TypeEnum.VOID));
|
||||
|
||||
return new TypeCheckResult(resultIf.isValid(), new BaseType(TypeEnum.VOID));
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult analyze(MethodCallNode toCheck) {
|
||||
|
||||
if (toCheck.target.identifier != null) {
|
||||
var targetType = currentScope.getLocalVar(toCheck.target.identifier);
|
||||
if (targetType == null) {
|
||||
targetType = currentFields.get(toCheck.target.identifier);
|
||||
if (toCheck.target != null) {
|
||||
if(toCheck.target.memberAccess == null){
|
||||
MemberAccessNode memberAccessNode = new MemberAccessNode(false);
|
||||
memberAccessNode.identifiers.add(currentClass.identifier);
|
||||
memberAccessNode.identifiers.add(toCheck.target.identifier);
|
||||
toCheck.target.memberAccess = memberAccessNode;
|
||||
}
|
||||
if (targetType instanceof ReferenceType reference) {
|
||||
var type = getTypeFromMethod(toCheck, reference);
|
||||
if (type != null) {
|
||||
return new TypeCheckResult(true, type);
|
||||
} else {
|
||||
return new TypeCheckResult(false, null);
|
||||
if (toCheck.target.identifier != null) {
|
||||
var targetType = currentScope.getLocalVar(toCheck.target.identifier);
|
||||
if (targetType == null) {
|
||||
targetType = currentFields.get(toCheck.target.identifier);
|
||||
}
|
||||
if (targetType instanceof ReferenceType reference) {
|
||||
if (!toCheck.chainedMethods.isEmpty()) {
|
||||
for (ChainedMethodNode chainedMethod : toCheck.chainedMethods) {
|
||||
var type = getTypeFromMethod(chainedMethod, reference);
|
||||
if (type instanceof ReferenceType referenceType)
|
||||
reference = referenceType;
|
||||
else
|
||||
errors.add(new TypeMismatchException("Ein Basetyp hat keine funktionen"));
|
||||
}
|
||||
}
|
||||
var type = getTypeFromMethod(toCheck, reference);
|
||||
if (type != null) {
|
||||
return new TypeCheckResult(true, type);
|
||||
} else {
|
||||
return new TypeCheckResult(false, null);
|
||||
}
|
||||
|
||||
}
|
||||
} else {
|
||||
if (toCheck.target.thisTar) {
|
||||
var type = getTypeFromMethod(toCheck, new ReferenceType(currentClass.identifier));
|
||||
if (type != null) {
|
||||
return new TypeCheckResult(true, type);
|
||||
}
|
||||
} else {
|
||||
var result = toCheck.target.accept(this);
|
||||
if (result.getType() instanceof ReferenceType reference) {
|
||||
return new TypeCheckResult(true, getTypeFromMethod(toCheck, reference));
|
||||
if (toCheck.target.thisTar != null) {
|
||||
if (toCheck.target.thisTar) {
|
||||
var type = getTypeFromMethod(toCheck, new ReferenceType(currentClass.identifier));
|
||||
if (type != null) {
|
||||
return new TypeCheckResult(true, type);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var result = toCheck.target.accept(this);
|
||||
if (result.getType() instanceof ReferenceType reference) {
|
||||
return new TypeCheckResult(true, getTypeFromMethod(toCheck, reference));
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
||||
ReferenceType reference = new ReferenceType(currentClass.identifier);
|
||||
if (!toCheck.chainedMethods.isEmpty()) {
|
||||
for (ChainedMethodNode chainedMethod : toCheck.chainedMethods) {
|
||||
var type = getTypeFromMethod(chainedMethod, reference);
|
||||
if (type instanceof ReferenceType referenceType)
|
||||
reference = referenceType;
|
||||
else
|
||||
errors.add(new TypeMismatchException("Ein Basetyp hat keine funktionen"));
|
||||
}
|
||||
}
|
||||
var type = getTypeFromMethod(toCheck, reference);
|
||||
if (type != null) {
|
||||
return new TypeCheckResult(true, type);
|
||||
} else {
|
||||
return new TypeCheckResult(false, null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return new TypeCheckResult(false, null);
|
||||
}
|
||||
|
||||
@@ -383,9 +433,9 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
|
||||
if (context.containsClass(toCheck.identifier)) {
|
||||
return new TypeCheckResult(true, new ReferenceType(toCheck.identifier));
|
||||
} else {
|
||||
throw new RuntimeException("Cannot find class " + toCheck.identifier);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -409,6 +459,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
case PLUS, MINUS:
|
||||
if (calcRes.getType() instanceof BaseType calcType && dotRes.getType() instanceof BaseType dotType &&
|
||||
calcType.getTypeEnum().equals(TypeEnum.INT) && dotType.getTypeEnum().equals(TypeEnum.INT)) {
|
||||
calcNode.setType(new BaseType(TypeEnum.INT));
|
||||
return new TypeCheckResult(true, new BaseType(TypeEnum.INT));
|
||||
}
|
||||
break;
|
||||
@@ -416,10 +467,12 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
}
|
||||
|
||||
} else {
|
||||
calcNode.setType(calcNode.getType());
|
||||
return new TypeCheckResult(calcRes.isValid(), calcRes.getType());
|
||||
}
|
||||
} else if (calcNode.dotExpression != null) {
|
||||
var dotRes = calcNode.dotExpression.accept(this);
|
||||
calcNode.setType(dotRes.getType());
|
||||
return new TypeCheckResult(dotRes.isValid(), dotRes.getType());
|
||||
}
|
||||
return new TypeCheckResult(false, null);
|
||||
@@ -428,7 +481,9 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
@Override
|
||||
public TypeCheckResult analyze(DotNode toCheck) {
|
||||
if (toCheck.dotSubstractionExpression != null) {
|
||||
return toCheck.dotSubstractionExpression.accept(this);
|
||||
var result = toCheck.dotSubstractionExpression.accept(this);
|
||||
toCheck.setType(result.getType());
|
||||
return result;
|
||||
}
|
||||
return new TypeCheckResult(false, null);
|
||||
}
|
||||
@@ -436,19 +491,35 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
@Override
|
||||
public TypeCheckResult analyze(DotSubstractionNode toCheck) {
|
||||
if (toCheck.value != null) {
|
||||
return toCheck.value.accept(this);
|
||||
var result = toCheck.value.accept(this);
|
||||
toCheck.setType(result.getType());
|
||||
return result;
|
||||
} else if (toCheck.memberAccess != null) {
|
||||
return toCheck.memberAccess.accept(this);
|
||||
var result = toCheck.memberAccess.accept(this);
|
||||
toCheck.setType(result.getType());
|
||||
return result;
|
||||
} else if (toCheck.methodCall != null) {
|
||||
return toCheck.methodCall.accept(this);
|
||||
var result = toCheck.methodCall.accept(this);
|
||||
toCheck.setType(result.getType());
|
||||
return result;
|
||||
} else if (toCheck.identifier != null) {
|
||||
if (currentScope.contains(toCheck.identifier)) {
|
||||
return new TypeCheckResult(true, currentScope.getLocalVar(toCheck.identifier));
|
||||
var type = currentScope.getLocalVar(toCheck.identifier);
|
||||
toCheck.setType(type);
|
||||
return new TypeCheckResult(true, type);
|
||||
} else if (currentFields.get(toCheck.identifier) != null) {
|
||||
return new TypeCheckResult(true, currentFields.get(toCheck.identifier));
|
||||
var type = currentFields.get(toCheck.identifier);
|
||||
toCheck.setType(type);
|
||||
MemberAccessNode memberAccessNode = new MemberAccessNode(false);
|
||||
memberAccessNode.identifiers.add(currentClass.identifier);
|
||||
memberAccessNode.identifiers.add(toCheck.identifier);
|
||||
toCheck.memberAccess = memberAccessNode;
|
||||
return new TypeCheckResult(true, type);
|
||||
}
|
||||
} else if (toCheck.calculationExpression != null) {
|
||||
return toCheck.calculationExpression.accept(this);
|
||||
var result = toCheck.calculationExpression.accept(this);
|
||||
toCheck.setType(result.getType());
|
||||
return result;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -468,8 +539,8 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
}
|
||||
break;
|
||||
case OR, AND:
|
||||
if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.INT) &&
|
||||
unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.INT)) {
|
||||
if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.BOOL) &&
|
||||
unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.BOOL)) {
|
||||
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL));
|
||||
} else {
|
||||
errors.add(new TypeMismatchException("Both types must be Boolean"));
|
||||
@@ -493,9 +564,17 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
|
||||
if (unary.identifier != null) {
|
||||
if (currentScope.contains(unary.identifier)) {
|
||||
return new TypeCheckResult(valid, currentScope.getLocalVar(unary.identifier));
|
||||
var type = currentScope.getLocalVar(unary.identifier);
|
||||
unary.setType(type);
|
||||
return new TypeCheckResult(valid, type);
|
||||
} else if (currentFields.get(unary.identifier) != null) {
|
||||
return new TypeCheckResult(valid, currentFields.get(unary.identifier));
|
||||
MemberAccessNode memberAccessNode = new MemberAccessNode(false);
|
||||
memberAccessNode.identifiers.add(currentClass.identifier);
|
||||
memberAccessNode.identifiers.add(unary.identifier);
|
||||
unary.memberAccess = memberAccessNode;
|
||||
var type = currentFields.get(unary.identifier);
|
||||
unary.setType(type);
|
||||
return new TypeCheckResult(valid,type );
|
||||
} else if (unary.statement != null) {
|
||||
var result = unary.statement.accept(this);
|
||||
unary.setType(result.getType());
|
||||
@@ -505,15 +584,19 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
}
|
||||
} else if (unary.statement != null) {
|
||||
var result = unary.statement.accept(this);
|
||||
unary.setType(result.getType());
|
||||
return new TypeCheckResult(result.isValid(), result.getType());
|
||||
} else if (unary.value != null) {
|
||||
var result = unary.value.accept(this);
|
||||
unary.setType(result.getType());
|
||||
return new TypeCheckResult(result.isValid(), result.getType());
|
||||
} else if (unary.memberAccess != null) {
|
||||
var result = unary.memberAccess.accept(this);
|
||||
unary.setType(result.getType());
|
||||
return new TypeCheckResult(result.isValid(), result.getType());
|
||||
} else if (unary.expression != null) {
|
||||
var result = unary.expression.accept(this);
|
||||
unary.setType(result.getType());
|
||||
return new TypeCheckResult(result.isValid(), result.getType());
|
||||
}
|
||||
|
||||
@@ -524,8 +607,20 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
public TypeCheckResult analyze(MemberAccessNode memberAccessNode) {
|
||||
|
||||
ITypeNode currentType = null;
|
||||
int start = 0;
|
||||
if(!memberAccessNode.identifiers.isEmpty()){
|
||||
if(currentFields.get(memberAccessNode.identifiers.getFirst()) != null){
|
||||
memberAccessNode.identifiers.addFirst(currentClass.identifier);
|
||||
start++;
|
||||
}
|
||||
}
|
||||
if(context.getClasses().get(memberAccessNode.identifiers.getFirst()) == null){
|
||||
memberAccessNode.identifiers.addFirst(currentClass.identifier);
|
||||
start++;
|
||||
}
|
||||
for (int i = start; i < memberAccessNode.identifiers.size(); i++) {
|
||||
|
||||
for (String s : memberAccessNode.identifiers) {
|
||||
String s = memberAccessNode.identifiers.get(i);
|
||||
if (currentType == null) {
|
||||
if (currentScope.getLocalVar(s) != null) {
|
||||
currentType = currentScope.getLocalVar(s);
|
||||
@@ -540,7 +635,8 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
} else {
|
||||
if (currentType instanceof ReferenceType reference) {
|
||||
var currentTypeClass = context.getClass(reference.getIdentifier());
|
||||
|
||||
memberAccessNode.identifiers.add(i, reference.getIdentifier());
|
||||
i++;
|
||||
var currentField = currentTypeClass.getField(s);
|
||||
if (currentField.getAccessModifier().accessType == EnumAccessModifierNode.PUBLIC) {
|
||||
currentType = currentField.getType();
|
||||
@@ -561,6 +657,11 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
|
||||
if (targetNode.memberAccess != null) {
|
||||
return targetNode.memberAccess.accept(this);
|
||||
} else if(targetNode.identifier != null) {
|
||||
MemberAccessNode memberAccessNode = new MemberAccessNode(false);
|
||||
memberAccessNode.identifiers.add(currentClass.identifier);
|
||||
memberAccessNode.identifiers.add(targetNode.identifier);
|
||||
targetNode.memberAccess = memberAccessNode;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -620,4 +721,37 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
return null;
|
||||
}
|
||||
|
||||
private ITypeNode getTypeFromMethod(ChainedMethodNode toCheck, ReferenceType reference) {
|
||||
var classContext = context.getClass(reference.getIdentifier());
|
||||
|
||||
var methods = classContext.getMethods();
|
||||
for (var method : methods) {
|
||||
if (toCheck.identifier.equals(method.getIdentifier())) {
|
||||
if (method.getParameters().size() == toCheck.expressions.size() && !(method instanceof ConstructorNode)) {
|
||||
boolean same = true;
|
||||
for (int i = 0; i < method.getParameters().size(); i++) {
|
||||
var result1 = method.getParameters().get(i).accept(this);
|
||||
var result2 = toCheck.expressions.get(i).accept(this);
|
||||
if (!Objects.equals(result1.getType(), result2.getType())) {
|
||||
same = false;
|
||||
}
|
||||
}
|
||||
if (same) {
|
||||
if (method.accesModifier.accessType == EnumAccessModifierNode.PUBLIC) {
|
||||
if (method.getType() == null) {
|
||||
return new BaseType(TypeEnum.VOID);
|
||||
}
|
||||
return method.getType();
|
||||
} else {
|
||||
errors.add(new NotVisibleException("This Method is not Visible"));
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@@ -21,6 +21,10 @@ public class Context {
|
||||
return classes.get(identifier);
|
||||
}
|
||||
|
||||
public HashMap<String, ClassContext> getClasses() {
|
||||
return classes;
|
||||
}
|
||||
|
||||
public boolean containsClass(String identifier) {
|
||||
return classes.containsKey(identifier);
|
||||
}
|
||||
|
3
src/main/resources/META-INF/MANIFEST.MF
Normal file
3
src/main/resources/META-INF/MANIFEST.MF
Normal file
@@ -0,0 +1,3 @@
|
||||
Manifest-Version: 1.0
|
||||
Main-Class: main.Main
|
||||
|
@@ -2,9 +2,6 @@ public class Compiler {
|
||||
public int add(int i, int j) {
|
||||
return i+j;
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
int a = 1;
|
||||
}
|
||||
}
|
||||
|
||||
public class Node {
|
||||
|
Binary file not shown.
@@ -9,15 +9,9 @@ compile-javac:
|
||||
|
||||
compile-raupenpiler:
|
||||
cd ../.. ; mvn -DskipTests install
|
||||
cd ../.. ; mvn exec:java -Dexec.mainClass="main.Main" -Dexec.args="'src/main/resources/input/CompilerInput.java' 'src/main/resources/output' "
|
||||
cd ../.. ; mvn exec:java -DgenJar=true -DgenClass=true -Dexec.mainClass="main.Main" -Dexec.args="'src/main/resources/input/CompilerInput.java' 'src/main/resources/output'"
|
||||
# cp ../main/resources/output/CompilerInput.class .java/resources/output/raupenpiler
|
||||
|
||||
test: compile-javac compile-raupenpiler test-javac test-raupenpiler
|
||||
|
||||
test-javac:
|
||||
# gleich wie bei raupenpiler, kann ich ohne funktionierenden Compiler nicht testen
|
||||
|
||||
|
||||
test-raupenpiler:
|
||||
# move the compiled class to the test/main folder
|
||||
mv ../main/resources/output/CompilerInput.class .java/main/
|
||||
@@ -29,15 +23,19 @@ test-raupenpiler:
|
||||
|
||||
|
||||
clean:
|
||||
# clean output folders
|
||||
# clean main output folders
|
||||
rm -f ../main/resources/output/*.class
|
||||
rm -f ../main/resources/output/*.jar
|
||||
# clean resources output folders
|
||||
rm -f ./resources/output/javac/*.class
|
||||
rm -f ./resources/output/raupenpiler/*.class
|
||||
rm -f ./resources/output/raupenpiler/*.jar
|
||||
# clean logs
|
||||
rm -f ../main/resources/logs/*.log
|
||||
# clean test/main folders from .class files for End-to-End tests
|
||||
rm -f ../main/resources/logs/*
|
||||
# clean test/java/main folders from .class files for End-to-End tests
|
||||
rm -f ./java/main/*.class
|
||||
# clean javac output from featureTests
|
||||
rm -f ./resources/input/featureTests/*.class
|
||||
# clean javac output from every folder
|
||||
rm -f ./resources/input/*/*.class
|
||||
# clean test results from maven surefire plugin
|
||||
rm -f ../../target/surefire-reports/*.txt
|
||||
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -1,48 +0,0 @@
|
||||
package main;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.tools.JavaCompiler;
|
||||
import javax.tools.ToolProvider;
|
||||
import java.io.File;
|
||||
|
||||
public class FailureTest {
|
||||
/**
|
||||
* This test method checks if invalid Java files fail to compile as expected.
|
||||
* It uses the JavaCompiler from the ToolProvider to compile the files.
|
||||
* The test passes if all the files fail to compile.
|
||||
*/
|
||||
@Test
|
||||
public void areTestFilesActuallyFailTest() {
|
||||
// Get the system Java compiler
|
||||
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
|
||||
// Assert that the compiler is available
|
||||
assertNotNull(javac, "Java Compiler is not available");
|
||||
|
||||
String directoryPath = "src/test/resources/input/failureTests";
|
||||
File folder = new File(directoryPath);
|
||||
|
||||
if (folder.isDirectory()) {
|
||||
File[] files = folder.listFiles((dir, name) -> name.endsWith(".java"));
|
||||
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
// Try to compile the file and get the result
|
||||
// The run method returns 0 if the compilation was successful, and non-zero otherwise
|
||||
int result = javac.run(null, null, null, file.getPath());
|
||||
|
||||
// Assert that the compilation failed (i.e., the result is non-zero)
|
||||
assertTrue(result != 0, "Expected compilation failure for " + file.getName());
|
||||
}
|
||||
} else {
|
||||
System.out.println("No files found in the directory.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("The provided path is not a directory.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,46 +0,0 @@
|
||||
package main;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.tools.JavaCompiler;
|
||||
import javax.tools.ToolProvider;
|
||||
import java.io.File;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class FeatureTest {
|
||||
/**
|
||||
* This test method checks if valid Java files compile successfully.
|
||||
* It uses the JavaCompiler from the ToolProvider to compile the files.
|
||||
* The test passes if all the files compile without errors.
|
||||
*/
|
||||
@Test
|
||||
public void areTestFilesActuallyValid() {
|
||||
// Get the system Java compiler
|
||||
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
|
||||
// Assert that the compiler is available
|
||||
assertNotNull(javac, "Java Compiler is not available");
|
||||
|
||||
String directoryPath = "src/test/resources/input/featureTests";
|
||||
File folder = new File(directoryPath);
|
||||
|
||||
if (folder.isDirectory()) {
|
||||
File[] files = folder.listFiles((dir, name) -> name.endsWith(".java"));
|
||||
|
||||
if (files != null) {
|
||||
for (File file : files) {
|
||||
// Try to compile the file and get the result
|
||||
// The run method returns 0 if the compilation was successful, and non-zero otherwise
|
||||
int result = javac.run(null, null, null, file.getPath());
|
||||
|
||||
// Assert that the compilation succeeded (i.e., the result is zero)
|
||||
assertEquals(0, result, "Expected compilation success for " + file.getName());
|
||||
}
|
||||
} else {
|
||||
System.out.println("No files found in the directory.");
|
||||
}
|
||||
} else {
|
||||
System.out.println("The provided path is not a directory.");
|
||||
}
|
||||
}
|
||||
}
|
236
src/test/java/main/InputFilesTest.java
Normal file
236
src/test/java/main/InputFilesTest.java
Normal file
@@ -0,0 +1,236 @@
|
||||
package main;
|
||||
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.tools.JavaCompiler;
|
||||
import javax.tools.ToolProvider;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
public class InputFilesTest {
|
||||
|
||||
/**
|
||||
* This test method checks if valid Java files compile successfully.
|
||||
* It uses the JavaCompiler from the ToolProvider to compile the files.
|
||||
* The test passes if all the files compile without errors.
|
||||
*/
|
||||
@Test
|
||||
public void areTestFilesActuallyValid() throws IOException {
|
||||
// Get the system Java compiler
|
||||
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
|
||||
// Assert that the compiler is available
|
||||
assertNotNull(javac, "Java Compiler is not available");
|
||||
|
||||
File combinedFeatureTests = new File("src/test/resources/input/combinedFeatureTests");
|
||||
File endabgabeTests = new File("src/test/resources/input/endabgabeTests");
|
||||
File singleFeatureSemanticTests = new File("src/test/resources/input/singleFeatureSemanticTests");
|
||||
File singleFeatureTests = new File("src/test/resources/input/singleFeatureTests");
|
||||
File typedAstFeatureTests = new File("src/test/resources/input/typedAstFeatureTests");
|
||||
|
||||
List<File> files = getJavaFilesFromDirectory(combinedFeatureTests);
|
||||
// files.addAll(getJavaFilesFromDirectory(endabgabeTests));
|
||||
// files.addAll(getJavaFilesFromDirectory(singleFeatureSemanticTests));
|
||||
files.addAll(getJavaFilesFromDirectory(singleFeatureTests));
|
||||
// files.addAll(getJavaFilesFromDirectory(typedAstFeatureTests));
|
||||
|
||||
if (!files.isEmpty()) {
|
||||
for (File file : files) {
|
||||
// Try to compile the file and get the result
|
||||
int result = javac.run(null, null, null, file.getPath());
|
||||
|
||||
// Assert that the compilation succeeded (i.e., the result is zero)
|
||||
assertEquals(0, result, "Expected compilation success for " + file.getName());
|
||||
}
|
||||
} else {
|
||||
System.out.println("No files found in the directories.");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void areCombinedFeatureTestsValid() throws IOException {
|
||||
// Get the system Java compiler
|
||||
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
|
||||
// Assert that the compiler is available
|
||||
assertNotNull(javac, "Java Compiler is not available");
|
||||
|
||||
File combinedFeatureTests = new File("src/test/resources/input/combinedFeatureTests");
|
||||
|
||||
List<File> files = getJavaFilesFromDirectory(combinedFeatureTests);
|
||||
|
||||
if (!files.isEmpty()) {
|
||||
for (File file : files) {
|
||||
// Try to compile the file and get the result
|
||||
int result = javac.run(null, null, null, file.getPath());
|
||||
|
||||
// Assert that the compilation succeeded (i.e., the result is zero)
|
||||
assertEquals(0, result, "Expected compilation success for " + file.getName());
|
||||
}
|
||||
} else {
|
||||
System.out.println("No files found in the directories.");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void areEndabgabeTestsActuallyValid() throws IOException {
|
||||
// Get the system Java compiler
|
||||
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
|
||||
// Assert that the compiler is available
|
||||
assertNotNull(javac, "Java Compiler is not available");
|
||||
|
||||
File endabgabeTests = new File("src/test/resources/input/endabgabeTests");
|
||||
|
||||
List<File> files = getJavaFilesFromDirectory(endabgabeTests);
|
||||
|
||||
if (!files.isEmpty()) {
|
||||
for (File file : files) {
|
||||
// Try to compile the file and get the result
|
||||
int result = javac.run(null, null, null, file.getPath());
|
||||
|
||||
// Assert that the compilation succeeded (i.e., the result is zero)
|
||||
assertEquals(0, result, "Expected compilation success for " + file.getName());
|
||||
}
|
||||
} else {
|
||||
System.out.println("No files found in the directories.");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void areSingleFeatureSemanticTestsActuallyValid() throws IOException {
|
||||
// Get the system Java compiler
|
||||
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
|
||||
// Assert that the compiler is available
|
||||
assertNotNull(javac, "Java Compiler is not available");
|
||||
|
||||
File singleFeatureSemanticTests = new File("src/test/resources/input/singleFeatureSemanticTests");
|
||||
|
||||
List<File> files = getJavaFilesFromDirectory(singleFeatureSemanticTests);
|
||||
|
||||
if (!files.isEmpty()) {
|
||||
for (File file : files) {
|
||||
// Try to compile the file and get the result
|
||||
int result = javac.run(null, null, null, file.getPath());
|
||||
|
||||
// Assert that the compilation succeeded (i.e., the result is zero)
|
||||
assertEquals(0, result, "Expected compilation success for " + file.getName());
|
||||
}
|
||||
} else {
|
||||
System.out.println("No files found in the directories.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void areSingleFeatureTestsActuallyValid() throws IOException {
|
||||
// Get the system Java compiler
|
||||
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
|
||||
// Assert that the compiler is available
|
||||
assertNotNull(javac, "Java Compiler is not available");
|
||||
|
||||
File singleFeatureTests = new File("src/test/resources/input/singleFeatureTests");
|
||||
|
||||
List<File> files = getJavaFilesFromDirectory(singleFeatureTests);
|
||||
|
||||
if (!files.isEmpty()) {
|
||||
for (File file : files) {
|
||||
// Try to compile the file and get the result
|
||||
int result = javac.run(null, null, null, file.getPath());
|
||||
|
||||
// Assert that the compilation succeeded (i.e., the result is zero)
|
||||
assertEquals(0, result, "Expected compilation success for " + file.getName());
|
||||
}
|
||||
} else {
|
||||
System.out.println("No files found in the directories.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Disabled
|
||||
public void areTypedAstFeatureTestsActuallyValid() throws IOException {
|
||||
// Get the system Java compiler
|
||||
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
|
||||
// Assert that the compiler is available
|
||||
assertNotNull(javac, "Java Compiler is not available");
|
||||
|
||||
File typedAstFeatureTests = new File("src/test/resources/input/typedAstFeatureTests");
|
||||
|
||||
List<File> files = getJavaFilesFromDirectory(typedAstFeatureTests);
|
||||
|
||||
if (!files.isEmpty()) {
|
||||
for (File file : files) {
|
||||
// Try to compile the file and get the result
|
||||
int result = javac.run(null, null, null, file.getPath());
|
||||
|
||||
// Assert that the compilation succeeded (i.e., the result is zero)
|
||||
assertEquals(0, result, "Expected compilation success for " + file.getName());
|
||||
}
|
||||
} else {
|
||||
System.out.println("No files found in the directories.");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* This test method checks if invalid Java files fail to compile as expected.
|
||||
* It uses the JavaCompiler from the ToolProvider to compile the files.
|
||||
* The test passes if all the files fail to compile.
|
||||
*/
|
||||
@Test
|
||||
public void areTestFilesActuallyFails() throws IOException {
|
||||
// Get the system Java compiler
|
||||
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
|
||||
// Assert that the compiler is available
|
||||
assertNotNull(javac, "Java Compiler is not available");
|
||||
|
||||
|
||||
File folder1 = new File("src/test/resources/input/failureTests");
|
||||
File folder2 = new File("src/test/resources/input/typedAstExceptionsTest");
|
||||
|
||||
List<File> files = getJavaFilesFromDirectory(folder1);
|
||||
files.addAll(getJavaFilesFromDirectory(folder2));
|
||||
|
||||
|
||||
if (!files.isEmpty()) {
|
||||
for (File file : files) {
|
||||
// Try to compile the file and get the result
|
||||
// The run method returns 0 if the compilation was successful, and non-zero otherwise
|
||||
int result = javac.run(null, null, null, file.getPath());
|
||||
|
||||
// Assert that the compilation failed (i.e., the result is non-zero)
|
||||
assertTrue(result != 0, "Expected compilation failure for " + file.getName());
|
||||
}
|
||||
} else {
|
||||
System.out.println("No files found in the directory.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method to get all .java files from a directory.
|
||||
*
|
||||
* @param directory the directory to search for .java files
|
||||
* @return a list of .java files
|
||||
* @throws IOException if an I/O error occurs
|
||||
*/
|
||||
private List<File> getJavaFilesFromDirectory(File directory) throws IOException {
|
||||
if (directory.isDirectory()) {
|
||||
return Files.list(directory.toPath())
|
||||
.filter(path -> path.toString().endsWith(".java"))
|
||||
.map(java.nio.file.Path::toFile)
|
||||
.collect(Collectors.toList());
|
||||
} else {
|
||||
System.out.println("The provided path is not a directory: " + directory.getPath());
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,28 +1,25 @@
|
||||
package main;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Suite;
|
||||
import parser.ParserTest;
|
||||
import parser.ScannerTest;
|
||||
import semantic.EndToTypedAstTest;
|
||||
import semantic.SemanticTest;
|
||||
|
||||
/**
|
||||
* run every test: mvn test
|
||||
* Nutzen dieser Klasse: Eigentlich nicht vorhanden, in der Main gibts nichts zu testen
|
||||
* This class is a test suite that runs all the test classes in the project.
|
||||
* <p> run: <code> mvn test </code>
|
||||
* <p> check results in console or <code> target/surefire-reports </code>
|
||||
*/
|
||||
@RunWith(Suite.class)
|
||||
@Suite.SuiteClasses({
|
||||
InputFilesTest.class,
|
||||
ScannerTest.class,
|
||||
ParserTest.class,
|
||||
SemanticTest.class,
|
||||
EndToTypedAstTest.class
|
||||
})
|
||||
public class MainTest {
|
||||
@Test
|
||||
void test() {
|
||||
CharStream codeCharStream = null;
|
||||
try {
|
||||
codeCharStream = CharStreams.fromPath(Paths.get("src/main/test/resources/CompilerInput.java"));
|
||||
Main.compileFile(codeCharStream, "src/main/test/resources/output");
|
||||
} catch (IOException e) {
|
||||
System.err.println("Error reading the file: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
// This class remains empty, it is used only as a holder for the above annotations
|
||||
}
|
||||
|
||||
|
||||
|
@@ -14,23 +14,19 @@ import ast.members.*;
|
||||
import ast.parameters.ParameterNode;
|
||||
import ast.statementexpressions.AssignNode;
|
||||
import ast.statementexpressions.AssignableNode;
|
||||
import ast.statementexpressions.NewDeclarationNode;
|
||||
import ast.statementexpressions.crementexpressions.CrementType;
|
||||
import ast.statementexpressions.crementexpressions.DecrementNode;
|
||||
import ast.statementexpressions.crementexpressions.IncrementNode;
|
||||
import ast.statementexpressions.NewDeclarationNode;
|
||||
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
||||
import ast.statements.*;
|
||||
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
||||
import ast.statements.BlockNode;
|
||||
import ast.statements.LocalVariableDeclarationNode;
|
||||
import ast.statements.ReturnNode;
|
||||
import ast.statements.*;
|
||||
import ast.type.AccessModifierNode;
|
||||
import ast.type.EnumValueNode;
|
||||
import ast.type.ValueNode;
|
||||
import ast.type.type.BaseType;
|
||||
import ast.type.type.ReferenceType;
|
||||
import ast.type.type.TypeEnum;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
@@ -39,104 +35,106 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
@DisplayName("Untyped Abstract Syntax Tree")
|
||||
class AstBuilderTest {
|
||||
|
||||
private final static String directoryPath = "src/test/resources/input/singleFeatureTests/";
|
||||
|
||||
@Test
|
||||
@DisplayName("Empty Class Test")
|
||||
public void emptyClassTest(){
|
||||
ClassNode emptyClass = Helper.generateEmptyClass("TestClass");
|
||||
public void emptyClassTest() {
|
||||
ClassNode emptyClass = Helper.generateEmptyClass("EmptyClass");
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(emptyClass);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/EmptyClass.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "EmptyClass.java");
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Multiple Empty Classes Test")
|
||||
public void multipleEmptyClassesTest() {
|
||||
ClassNode class1 = Helper.generateEmptyClass("TestClass1");
|
||||
ClassNode class1 = Helper.generateEmptyClass("MultipleClasses");
|
||||
ClassNode class2 = Helper.generateEmptyClass("TestClass2");
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
expected.addClass(class2);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/MultipleClasses.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "MultipleClasses.java");
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Empty Class Test with Constructor")
|
||||
public void emptyClassWithConstructorTest() {
|
||||
ClassNode class1 = Helper.generateEmptyClass("TestClass");
|
||||
ClassNode class1 = Helper.generateEmptyClass("EmptyClassWithConstructor");
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/EmptyClassWithConstructor.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "EmptyClassWithConstructor.java");
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Field Test")
|
||||
public void fieldTest() {
|
||||
ClassNode class1 = Helper.generateEmptyClass("TestClass");
|
||||
ClassNode class1 = Helper.generateEmptyClass("Field");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Field.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "Field.java");
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Field Test with Accessmodifier")
|
||||
public void fieldTestWithModifier() {
|
||||
ClassNode class1 = Helper.generateEmptyClass("TestClass");
|
||||
ClassNode class1 = Helper.generateEmptyClass("FieldWithAccessModifier");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/FieldWithAccessModifier.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "FieldWithAccessModifier.java");
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Commments Ignore Test")
|
||||
public void commmentsIgnoreTest(){
|
||||
ClassNode class1 = Helper.generateEmptyClass("TestClass");
|
||||
@DisplayName("Comments Ignore Test")
|
||||
public void commentsIgnoreTest() {
|
||||
ClassNode class1 = Helper.generateEmptyClass("Comments");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("private"), new BaseType(TypeEnum.INT), "a"));
|
||||
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Comments.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "Comments.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Constructor Paramerter Test")
|
||||
public void constructorParameterTest(){
|
||||
@DisplayName("Constructor Parameter Test")
|
||||
public void constructorParameterTest() {
|
||||
BlockNode block = new BlockNode();
|
||||
block.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", block);
|
||||
ConstructorNode constructor = new ConstructorNode("public", "ConstructorParameter", block);
|
||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b"));
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
||||
ClassNode class1 = new ClassNode("public", "ConstructorParameter");
|
||||
class1.addMember(constructor);
|
||||
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ConstructorParameter.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "ConstructorParameter.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("This Dot Test")
|
||||
public void thisDotTest(){
|
||||
public void thisDotTest() {
|
||||
BlockNode block = new BlockNode();
|
||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||
memberAccess.addIdentifier("a");
|
||||
@@ -148,23 +146,23 @@ class AstBuilderTest {
|
||||
|
||||
block.addStatement(new AssignNode(assignable, expression));
|
||||
block.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", block);
|
||||
ConstructorNode constructor = new ConstructorNode("public", "ThisDot", block);
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
||||
ClassNode class1 = new ClassNode("public", "ThisDot");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||
class1.addMember(constructor);
|
||||
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ThisDot.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "ThisDot.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Constructor This Dot Test")
|
||||
public void constructorThisDotTest(){
|
||||
public void constructorThisDotTest() {
|
||||
BlockNode block = new BlockNode();
|
||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||
memberAccess.addIdentifier("a");
|
||||
@@ -175,25 +173,25 @@ class AstBuilderTest {
|
||||
|
||||
block.addStatement(new AssignNode(assignable, expression));
|
||||
block.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", block);
|
||||
ConstructorNode constructor = new ConstructorNode("public", "ConstructorThisDot", block);
|
||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
||||
ClassNode class1 = new ClassNode("public", "ConstructorThisDot");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("private"), new BaseType(TypeEnum.INT), "a"));
|
||||
class1.addMember(constructor);
|
||||
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ConstructorThisDot.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "ConstructorThisDot.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Void Methoden Test")
|
||||
public void voidMethodenTest(){
|
||||
ClassNode class1 = Helper.generateEmptyClass("TestClass");
|
||||
public void voidMethodenTest() {
|
||||
ClassNode class1 = Helper.generateEmptyClass("VoidMethod");
|
||||
BlockNode block = new BlockNode();
|
||||
block.addStatement(new ReturnNode(null));
|
||||
class1.addMember(new MethodNode("public", null, true, "test", block));
|
||||
@@ -201,14 +199,14 @@ class AstBuilderTest {
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/VoidMethod.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "VoidMethod.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Constructor Method call Test")
|
||||
public void constructorMethodCallTest(){
|
||||
public void constructorMethodCallTest() {
|
||||
BlockNode blockCon = new BlockNode();
|
||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||
memberAccess.addIdentifier("a");
|
||||
@@ -219,13 +217,13 @@ class AstBuilderTest {
|
||||
|
||||
blockCon.addStatement(new AssignNode(assignable, expression));
|
||||
blockCon.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon);
|
||||
ConstructorNode constructor = new ConstructorNode("public", "ConstructorMethodCall", blockCon);
|
||||
|
||||
BlockNode blockMethod = new BlockNode();
|
||||
blockMethod.addStatement(new ReturnNode(new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "1"))));
|
||||
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod", blockMethod);
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
||||
ClassNode class1 = new ClassNode("public", "ConstructorMethodCall");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||
class1.addMember(constructor);
|
||||
class1.addMember(method);
|
||||
@@ -233,14 +231,14 @@ class AstBuilderTest {
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ConstructorMethodCall.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "ConstructorMethodCall.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Constructor Method call Parameters Test")
|
||||
public void constructorMethodCallParametersTest(){
|
||||
public void constructorMethodCallParametersTest() {
|
||||
BlockNode blockCon = new BlockNode();
|
||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||
memberAccess.addIdentifier("a");
|
||||
@@ -253,7 +251,7 @@ class AstBuilderTest {
|
||||
|
||||
blockCon.addStatement(new AssignNode(assignable, expression));
|
||||
blockCon.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon);
|
||||
ConstructorNode constructor = new ConstructorNode("public", "ConstructorMethodCallParameters", blockCon);
|
||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
||||
|
||||
BlockNode blockMethod = new BlockNode();
|
||||
@@ -261,7 +259,7 @@ class AstBuilderTest {
|
||||
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod", blockMethod);
|
||||
method.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
||||
ClassNode class1 = new ClassNode("public", "ConstructorMethodCallParameters");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||
class1.addMember(constructor);
|
||||
class1.addMember(method);
|
||||
@@ -269,14 +267,14 @@ class AstBuilderTest {
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/ConstructorMethodCallParameters.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "ConstructorMethodCallParameters.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Char Test")
|
||||
public void charTest(){
|
||||
public void charTest() {
|
||||
BlockNode blockCon = new BlockNode();
|
||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||
memberAccess.addIdentifier("a");
|
||||
@@ -289,7 +287,7 @@ class AstBuilderTest {
|
||||
|
||||
blockCon.addStatement(new AssignNode(assignable, expression));
|
||||
blockCon.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon);
|
||||
ConstructorNode constructor = new ConstructorNode("public", "Char", blockCon);
|
||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.CHAR), "a"));
|
||||
|
||||
BlockNode blockMethod = new BlockNode();
|
||||
@@ -297,7 +295,7 @@ class AstBuilderTest {
|
||||
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.CHAR), false, "testMethod", blockMethod);
|
||||
method.addParameter(new ParameterNode(new BaseType(TypeEnum.CHAR), "a"));
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
||||
ClassNode class1 = new ClassNode("public", "Char");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.CHAR), "a"));
|
||||
class1.addMember(constructor);
|
||||
class1.addMember(method);
|
||||
@@ -305,14 +303,14 @@ class AstBuilderTest {
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Char.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "Char.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Null Test")
|
||||
public void nullTest(){
|
||||
public void nullTest() {
|
||||
BlockNode blockCon = new BlockNode();
|
||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||
memberAccess.addIdentifier("a");
|
||||
@@ -321,16 +319,102 @@ class AstBuilderTest {
|
||||
|
||||
blockCon.addStatement(new AssignNode(assignable, new UnaryNode(new ValueNode(EnumValueNode.NULL_VALUE, "null"))));
|
||||
blockCon.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon);
|
||||
ConstructorNode constructor = new ConstructorNode("public", "Null", blockCon);
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||
ClassNode class1 = new ClassNode("public", "Null");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new ReferenceType("Null"), "a"));
|
||||
class1.addMember(constructor);
|
||||
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Null.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "Null.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("If Test")
|
||||
public void ifTest() {
|
||||
BlockNode block = new BlockNode();
|
||||
block.addStatement(new LocalVariableDeclarationNode(new BaseType(TypeEnum.INT), "intValue", "=", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "5"))));
|
||||
|
||||
BlockNode ifBlock = new BlockNode();
|
||||
ifBlock.addStatement(new DecrementNode(CrementType.SUFFIX, new AssignableNode("intValue")));
|
||||
block.addStatement(new IfElseNode(new IfNode(new NonCalculationNode(new UnaryNode("intValue"), "==", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "5"))), ifBlock), null));
|
||||
|
||||
block.addStatement(new ReturnNode(null));
|
||||
|
||||
ConstructorNode constructor = new ConstructorNode("public", "If", block);
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "If");
|
||||
class1.addMember(constructor);
|
||||
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "If.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("If Else Test")
|
||||
public void ifElseTest() {
|
||||
BlockNode block = new BlockNode();
|
||||
block.addStatement(new LocalVariableDeclarationNode(new BaseType(TypeEnum.INT), "intValue", "=", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "5"))));
|
||||
|
||||
BlockNode ifBlock = new BlockNode();
|
||||
ifBlock.addStatement(new DecrementNode(CrementType.SUFFIX, new AssignableNode("intValue")));
|
||||
BlockNode elseBlock = new BlockNode();
|
||||
elseBlock.addStatement(new IncrementNode(CrementType.SUFFIX, new AssignableNode("intValue")));
|
||||
block.addStatement(new IfElseNode(new IfNode(new NonCalculationNode(new UnaryNode("intValue"), "==", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "5"))), ifBlock), new ElseNode(elseBlock)));
|
||||
|
||||
block.addStatement(new ReturnNode(null));
|
||||
|
||||
ConstructorNode constructor = new ConstructorNode("public", "IfElse", block);
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "IfElse");
|
||||
class1.addMember(constructor);
|
||||
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "IfElse.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("If Else If ElseTest")
|
||||
public void ifElseIfElseTest() {
|
||||
BlockNode block = new BlockNode();
|
||||
block.addStatement(new LocalVariableDeclarationNode(new BaseType(TypeEnum.INT), "intValue", "=", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "5"))));
|
||||
|
||||
BlockNode ifBlock = new BlockNode();
|
||||
ifBlock.addStatement(new DecrementNode(CrementType.SUFFIX, new AssignableNode("intValue")));
|
||||
BlockNode elseBlock = new BlockNode();
|
||||
elseBlock.addStatement(new IncrementNode(CrementType.SUFFIX, new AssignableNode("intValue")));
|
||||
|
||||
IfElseNode ifElseStatement = new IfElseNode(new IfNode(new NonCalculationNode(new UnaryNode("intValue"), "==", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "5"))), ifBlock), new ElseNode(elseBlock));
|
||||
|
||||
BlockNode ifElseBlock = new BlockNode();
|
||||
ifElseBlock.addStatement(new IncrementNode(CrementType.SUFFIX, new AssignableNode("intValue")));
|
||||
ifElseStatement.addElseIfStatement(new IfNode(new NonCalculationNode(new UnaryNode("intValue"), "==", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "4"))), ifElseBlock));
|
||||
|
||||
block.addStatement(ifElseStatement);
|
||||
|
||||
block.addStatement(new ReturnNode(null));
|
||||
|
||||
ConstructorNode constructor = new ConstructorNode("public", "IfElseIfElse", block);
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "IfElseIfElse");
|
||||
class1.addMember(constructor);
|
||||
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "IfElseIfElse.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
@@ -339,9 +423,9 @@ class AstBuilderTest {
|
||||
@DisplayName("Self Reference Test")
|
||||
public void selfReferneceTest(){
|
||||
|
||||
ClassNode testClass = Helper.generateEmptyClass("TestClass");
|
||||
ClassNode testClass = Helper.generateEmptyClass("SelfReference");
|
||||
|
||||
MemberNode testClassObject = new FieldNode(new AccessModifierNode("public"), new ReferenceType("TestClass"),"testClass");
|
||||
MemberNode testClassObject = new FieldNode(new AccessModifierNode("public"), new ReferenceType("SelfReference"),"selfReference");
|
||||
|
||||
BlockNode testMethod1Block = new BlockNode();
|
||||
testMethod1Block.addStatement(new ReturnNode(new UnaryNode(new MethodCallNode(new TargetNode(true), "testMethod2"))));
|
||||
@@ -352,10 +436,10 @@ class AstBuilderTest {
|
||||
MethodNode testMethod2 = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod2", testMethod2Block);
|
||||
|
||||
BlockNode testMethod3Block = new BlockNode();
|
||||
testMethod3Block.addStatement(new LocalVariableDeclarationNode(new ReferenceType("TestClass"),"testClass1", "=", new UnaryNode(new NewDeclarationNode("TestClass")))); // Assing einfach "=" ?
|
||||
testMethod3Block.addStatement(new LocalVariableDeclarationNode(new ReferenceType("SelfReference"),"selfReference1", "=", new UnaryNode(new NewDeclarationNode("SelfReference")))); // Assing einfach "=" ?
|
||||
MemberAccessNode methodAccess = new MemberAccessNode(false);
|
||||
methodAccess.addIdentifier("testClass1");
|
||||
methodAccess.addIdentifier("testClass");
|
||||
methodAccess.addIdentifier("selfReference1");
|
||||
methodAccess.addIdentifier("selfReference");
|
||||
TargetNode methodTarget = new TargetNode(methodAccess);
|
||||
testMethod3Block.addStatement(new ReturnNode(new UnaryNode(new MethodCallNode(methodTarget,"testMethod1"))));
|
||||
MethodNode testMethod3 = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod3", testMethod3Block);
|
||||
@@ -368,7 +452,7 @@ class AstBuilderTest {
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(testClass);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/SelfReference.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "SelfReference.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
@@ -376,7 +460,7 @@ class AstBuilderTest {
|
||||
@Test
|
||||
@DisplayName("Variable Compare Test")
|
||||
public void variableCompareTest(){
|
||||
ClassNode class1 = Helper.generateEmptyClass("TestClass");
|
||||
ClassNode class1 = Helper.generateEmptyClass("VariableCompare");
|
||||
UnaryNode trueValue = new UnaryNode(new ValueNode(EnumValueNode.BOOLEAN_VALUE,"true"));
|
||||
UnaryNode falseValue = new UnaryNode(new ValueNode(EnumValueNode.BOOLEAN_VALUE,"false"));
|
||||
|
||||
@@ -419,7 +503,7 @@ class AstBuilderTest {
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/variableCompare.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "VariableCompare.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
@@ -427,7 +511,7 @@ class AstBuilderTest {
|
||||
@Test
|
||||
@DisplayName("Variable Calculation Test")
|
||||
public void variableCalculationTest(){
|
||||
ClassNode class1 = Helper.generateEmptyClass("TestClass");
|
||||
ClassNode class1 = Helper.generateEmptyClass("VariableCalculation");
|
||||
|
||||
BlockNode aPlusBBlock = new BlockNode();
|
||||
aPlusBBlock.addStatement(new ReturnNode(new CalculationNode(new CalculationNode(new DotNode(new DotSubstractionNode("a"))), "+", new DotNode(new DotSubstractionNode("b")))));
|
||||
@@ -489,7 +573,7 @@ class AstBuilderTest {
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/variableCalculation.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "VariableCalculation.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
@@ -497,7 +581,7 @@ class AstBuilderTest {
|
||||
@Test
|
||||
@DisplayName("Main Method Test")
|
||||
public void mainMethodTest(){
|
||||
ClassNode class1 = Helper.generateEmptyClass("TestClass");
|
||||
ClassNode class1 = Helper.generateEmptyClass("MainMethod");
|
||||
|
||||
BlockNode block = new BlockNode();
|
||||
block.addStatement(new ReturnNode(null));
|
||||
@@ -507,7 +591,7 @@ class AstBuilderTest {
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/MainMethod.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "MainMethod.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
@@ -528,13 +612,13 @@ class AstBuilderTest {
|
||||
blockCon.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon);
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
||||
ClassNode class1 = new ClassNode("public", "While");
|
||||
class1.addMember(constructor);
|
||||
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/While.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "While.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
@@ -559,13 +643,13 @@ class AstBuilderTest {
|
||||
blockCon.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon);
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
||||
ClassNode class1 = new ClassNode("public", "DoWhile");
|
||||
class1.addMember(constructor);
|
||||
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/DoWhile.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "DoWhile.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
@@ -595,13 +679,13 @@ class AstBuilderTest {
|
||||
blockCon.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon);
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
||||
ClassNode class1 = new ClassNode("public", "For");
|
||||
class1.addMember(constructor);
|
||||
|
||||
ProgramNode expected = new ProgramNode();
|
||||
expected.addClass(class1);
|
||||
|
||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/For.java");
|
||||
ASTNode actual = Helper.generateAST(directoryPath + "For.java");
|
||||
|
||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||
}
|
||||
|
@@ -1,45 +0,0 @@
|
||||
package semantic;
|
||||
|
||||
import ast.ProgramNode;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import parser.astBuilder.ASTBuilder;
|
||||
import parser.generated.SimpleJavaLexer;
|
||||
import parser.generated.SimpleJavaParser;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class BeginnToTAST {
|
||||
|
||||
@Test
|
||||
public void FieldTests() {
|
||||
SemanticAnalyzer.clearAnalyzer();
|
||||
|
||||
CharStream codeCharStream = null;
|
||||
try {
|
||||
codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/johnsTests/FieldTests.java"));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
|
||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||
|
||||
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||
ParseTree parseTree = parser.program(); // parse the input
|
||||
|
||||
/* ------------------------- AST builder -> AST ------------------------- */
|
||||
ASTBuilder astBuilder = new ASTBuilder();
|
||||
ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree);
|
||||
|
||||
var result = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
}
|
||||
|
||||
}
|
@@ -26,6 +26,8 @@ import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import semantic.exceptions.*;
|
||||
import semantic.exceptions.NotDeclaredException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@@ -38,17 +40,14 @@ public class EndToTypedAstTest {
|
||||
|
||||
CharStream codeCharStream = null;
|
||||
try {
|
||||
codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/typedAstFeaturesTests/CorrectTest.java"));
|
||||
codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/typedAstFeatureTests/CorrectTest.java"));
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
|
||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||
|
||||
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||
ParseTree parseTree = parser.program(); // parse the input
|
||||
|
||||
/* ------------------------- AST builder -> AST ------------------------- */
|
||||
ParseTree parseTree = parser.program();
|
||||
ASTBuilder astBuilder = new ASTBuilder();
|
||||
ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree);
|
||||
|
||||
@@ -115,7 +114,7 @@ public class EndToTypedAstTest {
|
||||
|
||||
@Test
|
||||
public void featureTest() {
|
||||
String directoryPath = "src/test/resources/input/typedAstFeaturesTests";
|
||||
String directoryPath = "src/test/resources/input/typedAstFeatureTests";
|
||||
File folder = new File(directoryPath);
|
||||
if (folder.isDirectory()) {
|
||||
File[] files = folder.listFiles((_, name) -> name.endsWith(".java"));
|
||||
@@ -155,6 +154,200 @@ public class EndToTypedAstTest {
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@Test
|
||||
public void notDeclared() {
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/johnsTests/NotDeclared.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertFalse(SemanticAnalyzer.errors.isEmpty());
|
||||
assertTrue(SemanticAnalyzer.errors.stream().anyMatch(c -> c instanceof NotDeclaredException));
|
||||
}*/
|
||||
|
||||
@Test
|
||||
public void typeMismatchTest() {
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/TypeMismatchIntBool.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertFalse(SemanticAnalyzer.errors.isEmpty());
|
||||
assertTrue(SemanticAnalyzer.errors.stream().anyMatch(c -> c instanceof TypeMismatchException));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parameterAlreadyDecleared() {
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/ParameterAlreadyDecleared.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertFalse(SemanticAnalyzer.errors.isEmpty());
|
||||
assertTrue(SemanticAnalyzer.errors.stream().anyMatch(c -> c instanceof AlreadyDeclaredException));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fieldAlreadyDecleared(){
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/FieldAlreadyDecleared.java");
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertFalse(SemanticAnalyzer.errors.isEmpty());
|
||||
assertTrue(SemanticAnalyzer.errors.stream().anyMatch(c -> c instanceof AlreadyDeclaredException));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void typeMismatchRefType(){
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/TypeMismatchRefType.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertFalse(SemanticAnalyzer.errors.isEmpty());
|
||||
assertTrue(SemanticAnalyzer.errors.stream().anyMatch(c -> c instanceof TypeMismatchException));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void correctRetType(){
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/CorrectRetType.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void retTypeMismatch(){
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/RetTypeMismatch.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertFalse(SemanticAnalyzer.errors.isEmpty());
|
||||
assertTrue(SemanticAnalyzer.errors.stream().anyMatch(c -> c instanceof TypeMismatchException));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void multipleRetType(){
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/MultipleRetTypes.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertFalse(SemanticAnalyzer.errors.isEmpty());
|
||||
assertTrue(SemanticAnalyzer.errors.stream().anyMatch(c -> c instanceof MultipleReturnTypes));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void BothTypesMustBeSameGreaterSmallerEqual(){
|
||||
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/BothTypesMustBeSame.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertFalse(SemanticAnalyzer.errors.isEmpty());
|
||||
assertTrue(SemanticAnalyzer.errors.stream().anyMatch(c -> c instanceof TypeMismatchException));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void NoSuchType(){
|
||||
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/ClassNotDeclared.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertFalse(SemanticAnalyzer.errors.isEmpty());
|
||||
assertTrue(SemanticAnalyzer.errors.stream().anyMatch(c -> c instanceof NotDeclaredException));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void FieldIsNotVisible(){
|
||||
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/FieldIsNotVisible.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertFalse(SemanticAnalyzer.errors.isEmpty());
|
||||
assertTrue(SemanticAnalyzer.errors.stream().anyMatch(c -> c instanceof NotVisibleException));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void ChainedMethods(){
|
||||
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/ChainedMethods.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void VariableCompare(){
|
||||
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/VariableCompare.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void IfExpressionInt(){
|
||||
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/IfExpressionInt.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertFalse(SemanticAnalyzer.errors.isEmpty());
|
||||
assertInstanceOf(TypeMismatchException.class, SemanticAnalyzer.errors.getFirst());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void SelectWrongMethodCauseParameter(){
|
||||
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/SelectWrongMethodCauseParameter.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertFalse(SemanticAnalyzer.errors.isEmpty());
|
||||
assertInstanceOf(TypeMismatchException.class, SemanticAnalyzer.errors.getFirst());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void SelectRightMethodCauseParameter(){
|
||||
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/SelectRightMethodCauseParameter.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void VariableCalculation(){
|
||||
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/VariableCalculation.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Expression(){
|
||||
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/Expression.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
|
||||
}
|
||||
|
||||
// ------------------ Helpers ------------------
|
||||
|
||||
|
35
src/test/java/semantic/SemanticHelper.java
Normal file
35
src/test/java/semantic/SemanticHelper.java
Normal file
@@ -0,0 +1,35 @@
|
||||
package semantic;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.ProgramNode;
|
||||
import org.antlr.v4.runtime.CharStream;
|
||||
import org.antlr.v4.runtime.CharStreams;
|
||||
import org.antlr.v4.runtime.CommonTokenStream;
|
||||
import org.antlr.v4.runtime.tree.ParseTree;
|
||||
import parser.astBuilder.ASTBuilder;
|
||||
import parser.generated.SimpleJavaLexer;
|
||||
import parser.generated.SimpleJavaParser;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
public class SemanticHelper {
|
||||
public static ASTNode generateTypedASTFrom(String filePath) {
|
||||
|
||||
SemanticAnalyzer.clearAnalyzer();
|
||||
|
||||
CharStream testFile = null;
|
||||
try {
|
||||
testFile = CharStreams.fromFileName(filePath);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
SimpleJavaLexer lexer = new SimpleJavaLexer(testFile);
|
||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||
tokenStream.fill();
|
||||
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||
ParseTree parseTree = parser.program();
|
||||
ASTBuilder astBuilder = new ASTBuilder();
|
||||
|
||||
return (ProgramNode) astBuilder.visit(parseTree);
|
||||
}
|
||||
}
|
@@ -1,113 +1,441 @@
|
||||
package semantic;
|
||||
|
||||
import ast.ASTNode;
|
||||
import ast.ClassNode;
|
||||
import ast.ProgramNode;
|
||||
import ast.expressions.IExpressionNode;
|
||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
||||
import ast.expressions.unaryexpressions.UnaryNode;
|
||||
import ast.members.ConstructorNode;
|
||||
import ast.members.FieldNode;
|
||||
import ast.members.MethodNode;
|
||||
import ast.parameters.ParameterNode;
|
||||
import ast.statementexpressions.AssignNode;
|
||||
import ast.statementexpressions.AssignableNode;
|
||||
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
||||
import ast.statements.BlockNode;
|
||||
import ast.statements.ReturnNode;
|
||||
import ast.type.AccessModifierNode;
|
||||
import ast.type.EnumValueNode;
|
||||
import ast.type.ValueNode;
|
||||
import ast.type.type.BaseType;
|
||||
import ast.type.type.TypeEnum;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import parser.Helper;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class SemanticTest {
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
SemanticAnalyzer.clearAnalyzer();
|
||||
}
|
||||
|
||||
public void test(){
|
||||
@Test
|
||||
@DisplayName("Empty Class Test")
|
||||
public void emptyClassTest() {
|
||||
ClassNode emptyClass = Helper.generateEmptyClass("EmptyClass");
|
||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
||||
abstractSyntaxTree.addClass(emptyClass);
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
||||
runtimeException.printStackTrace();
|
||||
}
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
assertNotNull(typedAst);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@DisplayName("Multiple Empty Classes Test")
|
||||
public void multipleEmptyClassesTest() {
|
||||
ClassNode class1 = Helper.generateEmptyClass("MultipleClasses");
|
||||
ClassNode class2 = Helper.generateEmptyClass("TestClass2");
|
||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
||||
abstractSyntaxTree.addClass(class1);
|
||||
abstractSyntaxTree.addClass(class2);
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
||||
runtimeException.printStackTrace();
|
||||
}
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
assertNotNull(typedAst);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@DisplayName("Empty Class Test with Constructor")
|
||||
public void emptyClassWithConstructorTest() {
|
||||
ClassNode class1 = Helper.generateEmptyClass("EmptyClassWithConstructor");
|
||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
||||
abstractSyntaxTree.addClass(class1);
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
||||
runtimeException.printStackTrace();
|
||||
}
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
assertNotNull(typedAst);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Field Test")
|
||||
public void fieldTest() {
|
||||
ClassNode class1 = Helper.generateEmptyClass("Field");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||
|
||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
||||
abstractSyntaxTree.addClass(class1);
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
||||
runtimeException.printStackTrace();
|
||||
}
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
assertNotNull(typedAst);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Field Test with Accessmodifier")
|
||||
public void fieldTestWithModifier() {
|
||||
ClassNode class1 = Helper.generateEmptyClass("FieldWithAccessModifier");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||
|
||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
||||
abstractSyntaxTree.addClass(class1);
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
||||
runtimeException.printStackTrace();
|
||||
}
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
assertNotNull(typedAst);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Comments Ignore Test")
|
||||
public void commentsIgnoreTest() {
|
||||
ClassNode class1 = Helper.generateEmptyClass("Comments");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("private"), new BaseType(TypeEnum.INT), "a"));
|
||||
|
||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
||||
abstractSyntaxTree.addClass(class1);
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
||||
runtimeException.printStackTrace();
|
||||
}
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
assertNotNull(typedAst);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Constructor Parameter Test")
|
||||
public void constructorParameterTest() {
|
||||
BlockNode block = new BlockNode();
|
||||
block.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "ConstructorParameter", block);
|
||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b"));
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "ConstructorParameter");
|
||||
class1.addMember(constructor);
|
||||
|
||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
||||
abstractSyntaxTree.addClass(class1);
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
||||
runtimeException.printStackTrace();
|
||||
}
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
assertNotNull(typedAst);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("This Dot Test")
|
||||
public void thisDotTest() {
|
||||
BlockNode block = new BlockNode();
|
||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||
memberAccess.addIdentifier("a");
|
||||
|
||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
||||
|
||||
ValueNode value = new ValueNode(EnumValueNode.INT_VALUE, "1");
|
||||
IExpressionNode expression = new UnaryNode(value);
|
||||
|
||||
block.addStatement(new AssignNode(assignable, expression));
|
||||
block.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "ThisDot", block);
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "ThisDot");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||
class1.addMember(constructor);
|
||||
|
||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
||||
abstractSyntaxTree.addClass(class1);
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
||||
runtimeException.printStackTrace();
|
||||
}
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
assertNotNull(typedAst);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Constructor This Dot Test")
|
||||
public void constructorThisDotTest() {
|
||||
BlockNode block = new BlockNode();
|
||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||
memberAccess.addIdentifier("a");
|
||||
|
||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
||||
|
||||
IExpressionNode expression = new UnaryNode("a");
|
||||
|
||||
block.addStatement(new AssignNode(assignable, expression));
|
||||
block.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "ConstructorThisDot", block);
|
||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "ConstructorThisDot");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("private"), new BaseType(TypeEnum.INT), "a"));
|
||||
class1.addMember(constructor);
|
||||
|
||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
||||
abstractSyntaxTree.addClass(class1);
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
||||
runtimeException.printStackTrace();
|
||||
}
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
assertNotNull(typedAst);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Void Methoden Test")
|
||||
public void voidMethodenTest() {
|
||||
ClassNode class1 = Helper.generateEmptyClass("VoidMethod");
|
||||
BlockNode block = new BlockNode();
|
||||
block.addStatement(new ReturnNode(null));
|
||||
class1.addMember(new MethodNode("public", null, true, "test", block));
|
||||
|
||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
||||
abstractSyntaxTree.addClass(class1);
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
||||
runtimeException.printStackTrace();
|
||||
}
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
assertNotNull(typedAst);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Constructor Method call Test")
|
||||
public void constructorMethodCallTest() {
|
||||
BlockNode blockCon = new BlockNode();
|
||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||
memberAccess.addIdentifier("a");
|
||||
|
||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
||||
|
||||
IExpressionNode expression = new UnaryNode(new MethodCallNode(null, "testMethod"));
|
||||
|
||||
blockCon.addStatement(new AssignNode(assignable, expression));
|
||||
blockCon.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "ConstructorMethodCall", blockCon);
|
||||
|
||||
BlockNode blockMethod = new BlockNode();
|
||||
blockMethod.addStatement(new ReturnNode(new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "1"))));
|
||||
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod", blockMethod);
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "ConstructorMethodCall");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||
class1.addMember(constructor);
|
||||
class1.addMember(method);
|
||||
|
||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
||||
abstractSyntaxTree.addClass(class1);
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
||||
runtimeException.printStackTrace();
|
||||
}
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
assertNotNull(typedAst);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Constructor Method call Parameters Test")
|
||||
public void constructorMethodCallParametersTest() {
|
||||
BlockNode blockCon = new BlockNode();
|
||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||
memberAccess.addIdentifier("a");
|
||||
|
||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
||||
|
||||
MethodCallNode methodCall = new MethodCallNode(null, "testMethod");
|
||||
methodCall.addExpression(new UnaryNode("a"));
|
||||
IExpressionNode expression = new UnaryNode(methodCall);
|
||||
|
||||
blockCon.addStatement(new AssignNode(assignable, expression));
|
||||
blockCon.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "ConstructorMethodCallParameters", blockCon);
|
||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
||||
|
||||
BlockNode blockMethod = new BlockNode();
|
||||
blockMethod.addStatement(new ReturnNode(new UnaryNode("a")));
|
||||
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod", blockMethod);
|
||||
method.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "ConstructorMethodCallParameters");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||
class1.addMember(constructor);
|
||||
class1.addMember(method);
|
||||
|
||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
||||
abstractSyntaxTree.addClass(class1);
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
||||
runtimeException.printStackTrace();
|
||||
}
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
assertNotNull(typedAst);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Char Test")
|
||||
public void charTest() {
|
||||
BlockNode blockCon = new BlockNode();
|
||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||
memberAccess.addIdentifier("a");
|
||||
|
||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
||||
|
||||
MethodCallNode methodCall = new MethodCallNode(null, "testMethod");
|
||||
methodCall.addExpression(new UnaryNode("a"));
|
||||
IExpressionNode expression = new UnaryNode(methodCall);
|
||||
|
||||
blockCon.addStatement(new AssignNode(assignable, expression));
|
||||
blockCon.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "Char", blockCon);
|
||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.CHAR), "a"));
|
||||
|
||||
BlockNode blockMethod = new BlockNode();
|
||||
blockMethod.addStatement(new ReturnNode(new UnaryNode("a")));
|
||||
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.CHAR), false, "testMethod", blockMethod);
|
||||
method.addParameter(new ParameterNode(new BaseType(TypeEnum.CHAR), "a"));
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "Char");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.CHAR), "a"));
|
||||
class1.addMember(constructor);
|
||||
class1.addMember(method);
|
||||
|
||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
||||
abstractSyntaxTree.addClass(class1);
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
||||
runtimeException.printStackTrace();
|
||||
}
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
assertNotNull(typedAst);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Null Test")
|
||||
public void nullTest() {
|
||||
BlockNode blockCon = new BlockNode();
|
||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||
memberAccess.addIdentifier("a");
|
||||
|
||||
AssignableNode assignable = new AssignableNode(memberAccess);
|
||||
|
||||
blockCon.addStatement(new AssignNode(assignable, new UnaryNode(new ValueNode(EnumValueNode.NULL_VALUE, "null"))));
|
||||
blockCon.addStatement(new ReturnNode(null));
|
||||
ConstructorNode constructor = new ConstructorNode("public", "Null", blockCon);
|
||||
|
||||
ClassNode class1 = new ClassNode("public", "Null");
|
||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||
class1.addMember(constructor);
|
||||
|
||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
||||
abstractSyntaxTree.addClass(class1);
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
||||
runtimeException.printStackTrace();
|
||||
}
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
assertNotNull(typedAst);
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Self Reference Test")
|
||||
public void selfReferenceTest() {
|
||||
|
||||
}
|
||||
|
||||
public void test(int a, boolean b){
|
||||
@Test
|
||||
@DisplayName("Variable Compare Test")
|
||||
public void variableCompareTest() {
|
||||
|
||||
}
|
||||
|
||||
public void test(boolean b, int a){
|
||||
@Test
|
||||
@DisplayName("Variable Calculation Test")
|
||||
public void variableCalculationTest() {
|
||||
|
||||
}
|
||||
|
||||
// @Test
|
||||
// public void alreadyDeclaredLocalFieldVar() {
|
||||
// ProgramNode programNode = new ProgramNode();
|
||||
// List<ClassNode> classList = new ArrayList<>();
|
||||
// AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC);
|
||||
// ClassNode classNode = new ClassNode(accessTypeNode, "testClass");
|
||||
//
|
||||
// SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
|
||||
// ASTNode tast = semanticAnalyzer.generateTast(ast);
|
||||
//
|
||||
// MemberNode memberNode2 = new FieldNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar");
|
||||
// classNode.members.add(memberNode2);
|
||||
//
|
||||
// classList.add(classNode);
|
||||
// programNode.classes = classList;
|
||||
//
|
||||
// ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
|
||||
//
|
||||
// assertEquals(1, SemanticAnalyzer.errors.size());
|
||||
// assertInstanceOf(AlreadyDeclaredException.class, SemanticAnalyzer.errors.getFirst());
|
||||
// assertNull(typedAst);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// public void shouldWorkWithNoError() {
|
||||
// ProgramNode programNode = new ProgramNode();
|
||||
// List<ClassNode> classList = new ArrayList<>();
|
||||
// AccessTypeNode accessTypeNode = new AccessTypeNode(EnumAccessTypeNode.PUBLIC);
|
||||
// ClassNode classNode = new ClassNode(accessTypeNode, "testClass");
|
||||
//
|
||||
// SemanticAnalyzer semanticAnalyzer = new SemanticAnalyzer();
|
||||
// ASTNode tast = semanticAnalyzer.generateTast(ast);
|
||||
//
|
||||
// assertEquals(semanticAnalyzer.errors.size(), 0);
|
||||
// assertNotNull(tast);
|
||||
//
|
||||
// MemberNode memberNode3 = getMemberNode(accessTypeNode);
|
||||
// classNode.members.add(memberNode3);
|
||||
//
|
||||
// classList.add(classNode);
|
||||
// programNode.classes = classList;
|
||||
//
|
||||
// ASTNode typedAst = SemanticAnalyzer.generateTast(programNode);
|
||||
//
|
||||
// assertEquals(0, SemanticAnalyzer.errors.size());
|
||||
// assertEquals(programNode, typedAst);
|
||||
// }
|
||||
//
|
||||
// /**
|
||||
// * This method is used to create a MemberNode representing a method.
|
||||
// * It first creates a list of ParameterNodes and adds a ParameterNode to it.
|
||||
// * Then, it creates a ParameterListNode using the list of ParameterNodes.
|
||||
// * After that, it creates a list of StatementNodes and adds a StatementNode to it by calling the getStatementNode method.
|
||||
// * Finally, it creates a MethodNode using the provided AccessTypeNode, a BaseTypeNode representing the return type of the method,
|
||||
// * the method name, the ParameterListNode, and the list of StatementNodes, and returns this MethodNode.
|
||||
// *
|
||||
// * @param accessTypeNode The AccessTypeNode representing the access type of the method.
|
||||
// * @return The created MemberNode representing the method.
|
||||
// */
|
||||
//private static MemberNode getMemberNode(AccessTypeNode accessTypeNode) {
|
||||
// List<ParameterNode> parameterNodeList = new ArrayList<>();
|
||||
// ParameterNode parameterNode1 = new ParameterNode(new BaseTypeNode(EnumTypeNode.INT), "param1");
|
||||
// parameterNodeList.add(parameterNode1);
|
||||
// ParameterListNode parameterListNode = new ParameterListNode(parameterNodeList);
|
||||
//
|
||||
// List<StatementNode> statementNodeList = new ArrayList<>();
|
||||
//
|
||||
// StatementNode statementNode1 = getStatementNode();
|
||||
// statementNodeList.add(statementNode1);
|
||||
//
|
||||
// return new MethodNode(accessTypeNode, new BaseTypeNode(EnumTypeNode.INT), "testVar2", parameterListNode, statementNodeList);
|
||||
//}
|
||||
//
|
||||
// /**
|
||||
// * This method is used to create a StatementNode for an assignment operation.
|
||||
// * It first creates two IdentifierExpressionNodes for 'this' and 'objectVar'.
|
||||
// * Then, it creates a BinaryExpressionNode to represent the operation 'this.objectVar'.
|
||||
// * After that, it creates a LiteralNode to represent the integer value 1.
|
||||
// * Finally, it creates another BinaryExpressionNode to represent the assignment operation 'this.objectVar = 1',
|
||||
// * and wraps this expression in an AssignmentStatementNode.
|
||||
// *
|
||||
// * @return The created AssignmentStatementNode representing the assignment operation 'this.objectVar = 1'.
|
||||
// */
|
||||
//private static StatementNode getStatementNode() {
|
||||
// ExpressionNode expressionNodeObjectVariableLeft = new IdentifierExpressionNode("this");
|
||||
// ExpressionNode expressionNodeObjectVariableRight = new IdentifierExpressionNode("objectVar");
|
||||
//
|
||||
// ExpressionNode expressionNodeLeft = new BinaryExpressionNode(expressionNodeObjectVariableLeft, expressionNodeObjectVariableRight, ExpresssionOperator.DOT);
|
||||
//
|
||||
// ExpressionNode expressionNodeRight = new LiteralNode(1);
|
||||
//
|
||||
// BinaryExpressionNode expressionNode = new BinaryExpressionNode(expressionNodeLeft, expressionNodeRight, ExpresssionOperator.ASSIGNMENT);
|
||||
//
|
||||
// return new AssignmentStatementNode(expressionNode);
|
||||
//}
|
||||
}
|
||||
@Test
|
||||
@DisplayName("Main Method Test")
|
||||
public void mainMethodTest() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("While Test")
|
||||
public void whileTest() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Do While Test")
|
||||
public void doWhileTest() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("For Test")
|
||||
public void forTest() {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("Increment Test")
|
||||
public void incrementTest() {
|
||||
ClassNode classNode = Helper.generateEmptyClass("Increment");
|
||||
classNode.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||
|
||||
ProgramNode abstractSyntaxTree = new ProgramNode();
|
||||
abstractSyntaxTree.addClass(classNode);
|
||||
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
for (Exception runtimeException : SemanticAnalyzer.errors) {
|
||||
runtimeException.printStackTrace();
|
||||
}
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
assertNotNull(typedAst);
|
||||
}
|
||||
}
|
@@ -1,23 +0,0 @@
|
||||
public class CombinedExample {
|
||||
int number;
|
||||
boolean flag;
|
||||
char letter;
|
||||
|
||||
public CombinedExample(int number, boolean flag, char letter) {
|
||||
this.number = number;
|
||||
this.flag = flag;
|
||||
this.letter = letter;
|
||||
}
|
||||
|
||||
public void displayValues() {
|
||||
System.out.println("Number: " + number);
|
||||
System.out.println("Flag: " + flag);
|
||||
System.out.println("Letter: " + letter);
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
CombinedExample obj = new CombinedExample(10, true, 'X');
|
||||
obj.displayValues();
|
||||
}
|
||||
}
|
||||
|
@@ -1,16 +0,0 @@
|
||||
public class CompilerInput {
|
||||
|
||||
public int a;
|
||||
|
||||
public static int testMethod(char x){
|
||||
return 0;
|
||||
}
|
||||
|
||||
public class Test {
|
||||
|
||||
public static int testMethod(char x, int a){
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +0,0 @@
|
||||
public class MoreFeaturesClassExample {
|
||||
int hallo;
|
||||
private class Inner {
|
||||
int hallo2;
|
||||
}
|
||||
}
|
@@ -4,7 +4,7 @@ public class AllFeaturesClassExample {
|
||||
char c;
|
||||
|
||||
// Konstruktor
|
||||
AllFeaturesClassExample(int a, boolean b, char c) {
|
||||
public AllFeaturesClassExample(int a, boolean b, char c) {
|
||||
this.a = a;
|
||||
this.b = b;
|
||||
this.c = c;
|
||||
@@ -14,47 +14,54 @@ public class AllFeaturesClassExample {
|
||||
void controlStructures() {
|
||||
// if-else Anweisung
|
||||
if (a > 10) {
|
||||
// System.out.println("a ist größer als 10");
|
||||
} else {
|
||||
// System.out.println("a ist nicht größer als 10");
|
||||
}
|
||||
|
||||
// while Schleife
|
||||
while (a > 0) {
|
||||
// System.out.println("a ist " + a);
|
||||
a--;
|
||||
}
|
||||
|
||||
int c = 0;
|
||||
// for Schleife
|
||||
for (int i = 0; i < 5; i++) {
|
||||
// System.out.println("for Schleife Iteration: " + i);
|
||||
c++;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Methode zur Arbeit mit logischen Operatoren
|
||||
void logicalOperations() {
|
||||
// Logische UND-Operation
|
||||
if (b && a > 5) {
|
||||
// System.out.println("a ist größer als 5 und b ist wahr");
|
||||
}
|
||||
|
||||
// Logische ODER-Operation
|
||||
if (b || a < 5) {
|
||||
// System.out.println("b ist wahr oder a ist kleiner als 5");
|
||||
}
|
||||
}
|
||||
|
||||
void mathOperations() {
|
||||
// Addition
|
||||
int sum = a + 5;
|
||||
// Subtraktion
|
||||
int difference = a - 5;
|
||||
// Multiplikation
|
||||
int product = a * 5;
|
||||
// Division
|
||||
int quotient = a / 5;
|
||||
// Modulo
|
||||
int remainder = a % 5;
|
||||
int add(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
int subtract(int a, int b) {
|
||||
return a - b;
|
||||
}
|
||||
|
||||
int multiply(int a, int b) {
|
||||
return a * b;
|
||||
}
|
||||
|
||||
int divide(int a, int b) {
|
||||
return a / b;
|
||||
}
|
||||
|
||||
int modulo(int a, int b) {
|
||||
return a % b;
|
||||
}
|
||||
|
||||
boolean greaterThan(int a, int b) {
|
||||
return a > b;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
25
src/test/resources/input/endabgabeTests/Car.java
Normal file
25
src/test/resources/input/endabgabeTests/Car.java
Normal file
@@ -0,0 +1,25 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
Car myCar = new Car(2020);
|
||||
int tires = 0;
|
||||
int year = myCar.getYear();
|
||||
|
||||
if (year == 2020) {
|
||||
tires = 4;
|
||||
} else {
|
||||
tires = 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class Car {
|
||||
private int year;
|
||||
|
||||
public Car(int year) {
|
||||
this.year = year;
|
||||
}
|
||||
|
||||
public int getYear() {
|
||||
return this.year;
|
||||
}
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
public class ControlStructures {
|
||||
|
||||
public int sum(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
|
||||
public cahr checkNumber(int num) {
|
||||
if (num > 0) {
|
||||
return "p";
|
||||
} else if (num < 0) {
|
||||
return "n";
|
||||
} else {
|
||||
return "z";
|
||||
}
|
||||
}
|
||||
|
||||
public void printNumbersUpTo(int limit) {
|
||||
int even = 0;
|
||||
int uneven = 0;
|
||||
int i = 0;
|
||||
while (i < limit) {
|
||||
if (i % 2 == 0) {
|
||||
even++;
|
||||
} else {
|
||||
uneven = uneven + 1;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ControlStructures cs = new ControlStructures();
|
||||
cs.printNumbersUpTo(5);
|
||||
int result = cs.sum(5, 5);
|
||||
}
|
||||
}
|
11
src/test/resources/input/endabgabeTests/Person.java
Normal file
11
src/test/resources/input/endabgabeTests/Person.java
Normal file
@@ -0,0 +1,11 @@
|
||||
public class Person {
|
||||
private int age;
|
||||
|
||||
public Person(int age) {
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return this.age;
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -1,5 +0,0 @@
|
||||
class TestClass {
|
||||
public TestClass(int a, int b){
|
||||
|
||||
}
|
||||
}
|
@@ -1,8 +0,0 @@
|
||||
class TestClass{
|
||||
|
||||
private int a;
|
||||
|
||||
public TestClass(int a){
|
||||
this.a = a;
|
||||
}
|
||||
}
|
@@ -1 +0,0 @@
|
||||
class TestClass {}
|
@@ -1,5 +0,0 @@
|
||||
public class TestClass {
|
||||
public TestClass() {
|
||||
|
||||
}
|
||||
}
|
@@ -1,3 +0,0 @@
|
||||
public class TestClass {
|
||||
int a;
|
||||
}
|
@@ -1,3 +0,0 @@
|
||||
public class TestClass {
|
||||
public int a;
|
||||
}
|
@@ -1,3 +0,0 @@
|
||||
class TestClass1 {}
|
||||
|
||||
class TestClass2{}
|
@@ -1,8 +0,0 @@
|
||||
class TestClass{
|
||||
|
||||
int a;
|
||||
|
||||
public TestClass(){
|
||||
this.a = null;
|
||||
}
|
||||
}
|
@@ -1,18 +0,0 @@
|
||||
class TestClass{
|
||||
|
||||
TestClass testClass;
|
||||
|
||||
int testMethod1() {
|
||||
return this.testMethod2();
|
||||
}
|
||||
|
||||
int testMethod2() {
|
||||
return 1;
|
||||
}
|
||||
|
||||
int testMethod3(){
|
||||
TestClass testClass1 = new TestClass();
|
||||
return testClass1.testClass.testMethod1();
|
||||
}
|
||||
|
||||
}
|
@@ -1,10 +0,0 @@
|
||||
class TestClass{
|
||||
|
||||
TestClasd (){
|
||||
switch (a){
|
||||
case 1:
|
||||
b = 1;
|
||||
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,3 +0,0 @@
|
||||
class TestClass{
|
||||
void test(){}
|
||||
}
|
@@ -5,5 +5,18 @@ public class AllFeaturesClassExample {
|
||||
while (a > bool) {
|
||||
a--;
|
||||
}
|
||||
|
||||
if (a == bool) {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
|
||||
if (a < bool) {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,29 @@
|
||||
public class Test {
|
||||
|
||||
public House h;
|
||||
|
||||
public int test(House h){
|
||||
return h.getW().getSize();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public class House {
|
||||
|
||||
private Window w;
|
||||
|
||||
public Window getW(){
|
||||
return w;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public class Window{
|
||||
|
||||
private int size;
|
||||
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
// @expected: NotDeclaredException
|
||||
public class Test {
|
||||
|
||||
public House1 h;
|
||||
|
||||
|
||||
}
|
@@ -1,7 +1,5 @@
|
||||
public class Example {
|
||||
|
||||
public class CorrectRetType {
|
||||
public static int testMethod(int x){
|
||||
return x;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,10 @@
|
||||
class VariableCompare{
|
||||
|
||||
void trueMethod(boolean a, int c) {
|
||||
if(a && c == 10){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,4 @@
|
||||
public class FieldAlreadyDecleared {
|
||||
public int a;
|
||||
public int a;
|
||||
}
|
@@ -1,4 +1,3 @@
|
||||
// @expected: TypeMismatchException
|
||||
public class Test{
|
||||
|
||||
public void test(int x){
|
@@ -0,0 +1,6 @@
|
||||
public class MultipleRetTypes {
|
||||
public static int testMethod(int x, char c){
|
||||
return x;
|
||||
return c;
|
||||
}
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
public class NotDeclared {
|
||||
public void Method() {
|
||||
i = 10;
|
||||
}
|
||||
}
|
@@ -0,0 +1,3 @@
|
||||
public class ParameterAlreadyDecleared {
|
||||
public void Method(int a, int a) {}
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
public class Example {
|
||||
public static int testMethod(char x){
|
||||
return x;
|
||||
}
|
||||
}
|
@@ -0,0 +1,22 @@
|
||||
// @expected: TypeMismatchException
|
||||
public class Test{
|
||||
|
||||
public int i;
|
||||
public boolean b;
|
||||
|
||||
public int test(){
|
||||
|
||||
return test(b);
|
||||
|
||||
}
|
||||
|
||||
public void test(int a){
|
||||
|
||||
}
|
||||
|
||||
public int test(boolean bool){
|
||||
int ret = 1;
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
@@ -6,7 +6,7 @@ public class Test{
|
||||
|
||||
public int test(){
|
||||
|
||||
return this.test(i);
|
||||
return test(i);
|
||||
|
||||
}
|
||||
|
||||
@@ -19,4 +19,4 @@ public class Test{
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,6 @@
|
||||
public class TypeMismatchIntBool {
|
||||
public void Method() {
|
||||
int intVariable;
|
||||
intVariable = true;
|
||||
}
|
||||
}
|
@@ -0,0 +1,13 @@
|
||||
public class TypeMismatchRefType {
|
||||
public static int Methode(Class1 class1, Class2 class2){
|
||||
class1 = class2;
|
||||
}
|
||||
}
|
||||
|
||||
public class Class1{
|
||||
public int a;
|
||||
}
|
||||
|
||||
public class Class2{
|
||||
public boolean a;
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
class TestClass{
|
||||
class VariableCalculation{
|
||||
|
||||
int aPlusB(int a, int b){
|
||||
return a + b;
|
@@ -1,4 +1,4 @@
|
||||
class TestClass{
|
||||
class VariableCompare{
|
||||
|
||||
boolean trueMethod() {
|
||||
return true;
|
@@ -1,8 +1,8 @@
|
||||
class TestClass{
|
||||
class Char {
|
||||
|
||||
char a;
|
||||
|
||||
public TestClass(char a){
|
||||
public Char(char a){
|
||||
this.a = testMethod(a);
|
||||
}
|
||||
|
@@ -3,6 +3,6 @@
|
||||
Mutliple Line Comment. Ignore
|
||||
|
||||
*/
|
||||
class TestClass{
|
||||
class Comments{
|
||||
private int a; // Ignore
|
||||
}
|
@@ -1,8 +1,8 @@
|
||||
class TestClass {
|
||||
public class ConstructorMethodCall {
|
||||
|
||||
int a;
|
||||
|
||||
public TestClass(){
|
||||
public ConstructorMethodCall(){
|
||||
this.a = testMethod();
|
||||
}
|
||||
|
@@ -1,8 +1,8 @@
|
||||
class TestClass {
|
||||
class ConstructorMethodCallParameters {
|
||||
|
||||
int a;
|
||||
|
||||
public TestClass(int a){
|
||||
public ConstructorMethodCallParameters(int a){
|
||||
this.a = testMethod(a);
|
||||
}
|
||||
|
@@ -0,0 +1,5 @@
|
||||
class ConstructorParameter {
|
||||
public ConstructorParameter(int a, int b){
|
||||
|
||||
}
|
||||
}
|
@@ -0,0 +1,8 @@
|
||||
class ConstructorThisDot {
|
||||
|
||||
private int a;
|
||||
|
||||
public ConstructorThisDot(int a){
|
||||
this.a = a;
|
||||
}
|
||||
}
|
@@ -1,6 +1,6 @@
|
||||
class TestClass{
|
||||
class DoWhile{
|
||||
|
||||
public TestClass(){
|
||||
public DoWhile(){
|
||||
int i = 0;
|
||||
|
||||
do{
|
@@ -0,0 +1 @@
|
||||
class EmptyClass {}
|
@@ -0,0 +1,5 @@
|
||||
public class EmptyClassWithConstructor {
|
||||
public EmptyClassWithConstructor() {
|
||||
|
||||
}
|
||||
}
|
3
src/test/resources/input/singleFeatureTests/Field.java
Normal file
3
src/test/resources/input/singleFeatureTests/Field.java
Normal file
@@ -0,0 +1,3 @@
|
||||
class Field {
|
||||
int a;
|
||||
}
|
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user