Compare commits
51 Commits
903e87901e
...
johns-bran
Author | SHA1 | Date | |
---|---|---|---|
|
ab5a51d3b1 | ||
|
a4c1c502ab | ||
|
7d441116bd | ||
|
8afa3b8461 | ||
|
84ecf316cd | ||
|
7ddea8f18d | ||
46154fbb01 | |||
9aac795c07 | |||
e59f4e7190 | |||
|
e6a2b0fe9d | ||
|
44c2f551de | ||
|
bad034acfd | ||
|
ba73e1bd45 | ||
|
b7affd75ae | ||
|
2ef50f93a9 | ||
|
1cf1aaf837 | ||
|
2bf73385af | ||
|
b072af346b | ||
|
27baa429b4 | ||
bd76135895 | |||
|
6cec17eb45 | ||
87be850a0e | |||
72f82ff863 | |||
7419953510 | |||
449b895d20 | |||
f0dd6d5eb6 | |||
8956362033 | |||
|
ed4aa2d59b | ||
|
6760a17f00 | ||
|
671317f28b | ||
|
e862a7427b | ||
233725778f | |||
3227d69fc1 | |||
4ca6972ccd | |||
0047f6c08e | |||
88ce9e52f0 | |||
|
437de74cc6 | ||
|
ca77307f0c | ||
|
5f46130439 | ||
3e1456351c | |||
d26cd0c13a | |||
|
cfde5219a4 | ||
|
f414e278bb | ||
|
741a56cb99 | ||
|
02e5f3a729 | ||
|
f7338a06b3 | ||
|
3996082fa7 | ||
|
2537051668 | ||
|
82356ec189 | ||
|
561eafbf4c | ||
b58fa00c9a |
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**
|
8
pom.xml
8
pom.xml
@@ -20,7 +20,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.junit.jupiter</groupId>
|
<groupId>org.junit.jupiter</groupId>
|
||||||
<artifactId>junit-jupiter-engine</artifactId>
|
<artifactId>junit-jupiter-engine</artifactId>
|
||||||
<version>5.9.3</version> <!-- Change the version as needed -->
|
<version>5.11.0-M2</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
@@ -44,6 +44,12 @@
|
|||||||
<version>3.26.0</version>
|
<version>3.26.0</version>
|
||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter-api</artifactId>
|
||||||
|
<version>5.11.0-M2</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.mockito</groupId>
|
<groupId>org.mockito</groupId>
|
||||||
<artifactId>mockito-core</artifactId>
|
<artifactId>mockito-core</artifactId>
|
||||||
|
@@ -4,6 +4,7 @@ import ast.type.AccessModifierNode;
|
|||||||
import ast.members.ConstructorNode;
|
import ast.members.ConstructorNode;
|
||||||
import ast.members.MemberNode;
|
import ast.members.MemberNode;
|
||||||
import ast.members.MethodNode;
|
import ast.members.MethodNode;
|
||||||
|
import bytecode.visitor.ClassVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
import visitor.Visitable;
|
import visitor.Visitable;
|
||||||
@@ -44,4 +45,9 @@ public class ClassNode implements ASTNode, Visitable {
|
|||||||
return visitor.analyze(this);
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void accept(ClassVisitor classVisitor) {
|
||||||
|
classVisitor.visit(this);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -1,6 +1,5 @@
|
|||||||
package ast.expressions.binaryexpressions;
|
package ast.expressions.binaryexpressions;
|
||||||
|
|
||||||
import ast.type.type.*;
|
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
package ast.literal;
|
package ast.literal;
|
||||||
|
|
||||||
import ast.expressions.IExpressionNode;
|
import ast.expressions.IExpressionNode;
|
||||||
import ast.type.type.ITypeNode;
|
import ast.type.type.ITypeNode;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
|
@@ -20,10 +20,4 @@ public class DecrementNode implements IStatementExpressionNode {
|
|||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.analyze(this);
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void accept(MethodVisitor methodVisitor) {
|
|
||||||
methodVisitor.visit(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -19,10 +19,4 @@ public class IncrementNode implements IStatementExpressionNode {
|
|||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.analyze(this);
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void accept(MethodVisitor methodVisitor) {
|
|
||||||
methodVisitor.visit(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -30,11 +30,6 @@ public class TargetNode implements ASTNode, Visitable {
|
|||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void accept(MethodVisitor methodVisitor) {
|
|
||||||
methodVisitor.visit(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.analyze(this);
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
|
@@ -18,49 +18,60 @@ public class ByteCodeGenerator implements ProgramVisitor {
|
|||||||
private JarOutputStream jarOutputStream;
|
private JarOutputStream jarOutputStream;
|
||||||
private ByteArrayOutputStream byteArrayOutputStream;
|
private ByteArrayOutputStream byteArrayOutputStream;
|
||||||
private String outputDirectory;
|
private String outputDirectory;
|
||||||
|
private boolean generateJar;
|
||||||
|
private boolean generateClassFiles;
|
||||||
|
|
||||||
public ByteCodeGenerator(String outputDirectory) {
|
public ByteCodeGenerator(String outputDirectory, boolean generateJar, boolean generateClassFiles) {
|
||||||
this.outputDirectory = outputDirectory;
|
this.outputDirectory = outputDirectory;
|
||||||
|
this.generateJar = generateJar;
|
||||||
|
this.generateClassFiles = generateClassFiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(ProgramNode programNode) {
|
public void visit(ProgramNode programNode) {
|
||||||
byteArrayOutputStream = new ByteArrayOutputStream();
|
if(generateJar) {
|
||||||
try {
|
byteArrayOutputStream = new ByteArrayOutputStream();
|
||||||
Manifest manifest = new Manifest();
|
try {
|
||||||
manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
|
Manifest manifest = new Manifest();
|
||||||
boolean foundMainClass = false;
|
manifest.getMainAttributes().putValue("Manifest-Version", "1.0");
|
||||||
for (ClassNode classNode : programNode.classes) {
|
boolean foundMainClass = false;
|
||||||
if (foundMainClass) {
|
for (ClassNode classNode : programNode.classes) {
|
||||||
break;
|
if (foundMainClass) {
|
||||||
}
|
|
||||||
for (MemberNode memberNode : classNode.members) {
|
|
||||||
if (memberNode instanceof MainMethodNode) {
|
|
||||||
manifest.getMainAttributes().putValue("Main-Class", classNode.identifier);
|
|
||||||
foundMainClass = true;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
for (MemberNode memberNode : classNode.members) {
|
||||||
|
if (memberNode instanceof MainMethodNode) {
|
||||||
|
manifest.getMainAttributes().putValue("Main-Class", classNode.identifier);
|
||||||
|
foundMainClass = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
jarOutputStream = new JarOutputStream(byteArrayOutputStream, manifest);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (ClassNode classDeclarationNode : programNode.classes) {
|
||||||
|
ClassCodeGen classCodeGen = new ClassCodeGen(jarOutputStream, outputDirectory, generateJar, generateClassFiles);
|
||||||
|
classDeclarationNode.accept(classCodeGen);
|
||||||
|
}
|
||||||
|
|
||||||
jarOutputStream = new JarOutputStream(byteArrayOutputStream, manifest);
|
try {
|
||||||
} catch (IOException e) {
|
jarOutputStream.close();
|
||||||
throw new RuntimeException(e);
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
saveJarFile(byteArrayOutputStream.toByteArray(), "output.jar");
|
||||||
|
} else {
|
||||||
|
for (ClassNode classDeclarationNode : programNode.classes) {
|
||||||
|
ClassCodeGen classCodeGen = new ClassCodeGen(jarOutputStream, outputDirectory, generateJar, generateClassFiles);
|
||||||
|
classDeclarationNode.accept(classCodeGen);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (ClassNode classDeclarationNode : programNode.classes) {
|
|
||||||
ClassCodeGen classCodeGen = new ClassCodeGen(jarOutputStream, outputDirectory);
|
|
||||||
classDeclarationNode.accept(classCodeGen);
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
jarOutputStream.close();
|
|
||||||
} catch (IOException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
|
|
||||||
saveJarFile(byteArrayOutputStream.toByteArray(), "output.jar");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void saveJarFile(byte[] jarBytes, String jarFileName) {
|
private void saveJarFile(byte[] jarBytes, String jarFileName) {
|
||||||
|
@@ -23,11 +23,15 @@ public class ClassCodeGen implements ClassVisitor {
|
|||||||
private ClassWriter classWriter;
|
private ClassWriter classWriter;
|
||||||
private JarOutputStream jarOutputStream;
|
private JarOutputStream jarOutputStream;
|
||||||
private String outputDirectory;
|
private String outputDirectory;
|
||||||
|
private boolean generateJar;
|
||||||
|
private boolean generateClassFiles;
|
||||||
|
|
||||||
public ClassCodeGen(JarOutputStream jarOutputStream, String outputDirectory) {
|
public ClassCodeGen(JarOutputStream jarOutputStream, String outputDirectory, boolean generateJar, boolean generateClassFiles) {
|
||||||
mapper = new Mapper();
|
mapper = new Mapper();
|
||||||
this.jarOutputStream = jarOutputStream;
|
this.jarOutputStream = jarOutputStream;
|
||||||
this.outputDirectory = outputDirectory;
|
this.outputDirectory = outputDirectory;
|
||||||
|
this.generateJar = generateJar;
|
||||||
|
this.generateClassFiles = generateClassFiles;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -45,9 +49,12 @@ public class ClassCodeGen implements ClassVisitor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
classWriter.visitEnd();
|
if (generateJar) {
|
||||||
printIntoClassFile(classWriter.toByteArray(), classNode.identifier, outputDirectory);
|
writeToJar(classWriter.toByteArray(), classNode.identifier);
|
||||||
writeToJar(classWriter.toByteArray(), classNode.identifier);
|
}
|
||||||
|
if (generateClassFiles) {
|
||||||
|
printIntoClassFile(classWriter.toByteArray(), classNode.identifier, outputDirectory);
|
||||||
|
}
|
||||||
|
|
||||||
classWriter.visitEnd();
|
classWriter.visitEnd();
|
||||||
}
|
}
|
||||||
|
@@ -27,18 +27,18 @@ public class Mapper {
|
|||||||
public String generateMethodDescriptor(BaseType type, List<ParameterNode> parameters) {
|
public String generateMethodDescriptor(BaseType type, List<ParameterNode> parameters) {
|
||||||
String descriptor = "(";
|
String descriptor = "(";
|
||||||
for (ParameterNode parameterNode : parameters) {
|
for (ParameterNode parameterNode : parameters) {
|
||||||
descriptor += getTypeChar((BaseType) parameterNode.type);
|
if(parameterNode.type instanceof BaseType) {
|
||||||
|
descriptor += getTypeChar((BaseType) parameterNode.type);
|
||||||
|
} else {
|
||||||
|
ReferenceType referenceType = (ReferenceType) parameterNode.type;
|
||||||
|
descriptor += "L" + referenceType.getIdentifier() + ";";
|
||||||
|
}
|
||||||
}
|
}
|
||||||
descriptor += ")";
|
descriptor += ")";
|
||||||
descriptor += getTypeChar(type);
|
descriptor += getTypeChar(type);
|
||||||
return descriptor;
|
return descriptor;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String generateMethodDescriptor(ReferenceType type, List<ParameterNode> parameters) {
|
|
||||||
String descriptor = "()V";
|
|
||||||
return descriptor;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getTypeChar(BaseType type) {
|
public String getTypeChar(BaseType type) {
|
||||||
String typeChar = "";
|
String typeChar = "";
|
||||||
switch (type.getTypeEnum()) {
|
switch (type.getTypeEnum()) {
|
||||||
|
@@ -1,5 +1,6 @@
|
|||||||
package bytecode;
|
package bytecode;
|
||||||
|
|
||||||
|
import ast.expressions.IExpressionNode;
|
||||||
import ast.expressions.binaryexpressions.*;
|
import ast.expressions.binaryexpressions.*;
|
||||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
import ast.expressions.unaryexpressions.MemberAccessNode;
|
||||||
import ast.expressions.unaryexpressions.NotNode;
|
import ast.expressions.unaryexpressions.NotNode;
|
||||||
@@ -10,6 +11,7 @@ import ast.members.MethodNode;
|
|||||||
import ast.parameters.ParameterNode;
|
import ast.parameters.ParameterNode;
|
||||||
import ast.statementexpressions.AssignNode;
|
import ast.statementexpressions.AssignNode;
|
||||||
import ast.statementexpressions.NewDeclarationNode;
|
import ast.statementexpressions.NewDeclarationNode;
|
||||||
|
import ast.statementexpressions.crementexpressions.CrementType;
|
||||||
import ast.statementexpressions.crementexpressions.DecrementNode;
|
import ast.statementexpressions.crementexpressions.DecrementNode;
|
||||||
import ast.statementexpressions.crementexpressions.IncrementNode;
|
import ast.statementexpressions.crementexpressions.IncrementNode;
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNode;
|
import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNode;
|
||||||
@@ -18,6 +20,7 @@ import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
|||||||
import ast.statements.*;
|
import ast.statements.*;
|
||||||
import ast.type.ValueNode;
|
import ast.type.ValueNode;
|
||||||
import ast.type.type.BaseType;
|
import ast.type.type.BaseType;
|
||||||
|
import ast.type.type.ReferenceType;
|
||||||
import ast.type.type.TypeEnum;
|
import ast.type.type.TypeEnum;
|
||||||
import org.objectweb.asm.ClassWriter;
|
import org.objectweb.asm.ClassWriter;
|
||||||
import org.objectweb.asm.Label;
|
import org.objectweb.asm.Label;
|
||||||
@@ -36,12 +39,12 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
private Mapper mapper;
|
private Mapper mapper;
|
||||||
private MethodVisitor methodVisitor;
|
private MethodVisitor methodVisitor;
|
||||||
|
|
||||||
private List<String> localVaribales;
|
private List<String> localVariables;
|
||||||
|
|
||||||
public MethodCodeGen(ClassWriter classWriter) {
|
public MethodCodeGen(ClassWriter classWriter) {
|
||||||
this.classWriter = classWriter;
|
this.classWriter = classWriter;
|
||||||
mapper = new Mapper();
|
mapper = new Mapper();
|
||||||
localVaribales = new ArrayList<>();
|
localVariables = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -57,10 +60,10 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
null);
|
null);
|
||||||
|
|
||||||
methodVisitor.visitCode();
|
methodVisitor.visitCode();
|
||||||
localVaribales.add("this");
|
localVariables.add("this");
|
||||||
// Add all method parameters to localVariables
|
// Add all method parameters to localVariables
|
||||||
for (ParameterNode parameterNode : constructorNode.parameters) {
|
for (ParameterNode parameterNode : constructorNode.parameters) {
|
||||||
localVaribales.add(parameterNode.identifier);
|
localVariables.add(parameterNode.identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
methodVisitor.visitVarInsn(ALOAD, 0);
|
methodVisitor.visitVarInsn(ALOAD, 0);
|
||||||
@@ -68,7 +71,9 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
|
|
||||||
// Visit all statements
|
// Visit all statements
|
||||||
for (IStatementNode statementNode : constructorNode.block.statements) {
|
for (IStatementNode statementNode : constructorNode.block.statements) {
|
||||||
statementNode.accept(this);
|
if (statementNode != null) {
|
||||||
|
statementNode.accept(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
methodVisitor.visitMaxs(0, 0);
|
methodVisitor.visitMaxs(0, 0);
|
||||||
@@ -84,8 +89,8 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
null);
|
null);
|
||||||
|
|
||||||
methodVisitor.visitCode();
|
methodVisitor.visitCode();
|
||||||
localVaribales.add("this");
|
localVariables.add("this");
|
||||||
localVaribales.add("args");
|
localVariables.add("args");
|
||||||
|
|
||||||
// Visit all statements
|
// Visit all statements
|
||||||
for (IStatementNode statementNode : mainMethodNode.block.statements) {
|
for (IStatementNode statementNode : mainMethodNode.block.statements) {
|
||||||
@@ -105,10 +110,10 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
null);
|
null);
|
||||||
|
|
||||||
methodVisitor.visitCode();
|
methodVisitor.visitCode();
|
||||||
localVaribales.add("this");
|
localVariables.add("this");
|
||||||
// Add all method parameters to localVariables
|
// Add all method parameters to localVariables
|
||||||
for (ParameterNode parameterNode : methodNode.parameters) {
|
for (ParameterNode parameterNode : methodNode.parameters) {
|
||||||
localVaribales.add(parameterNode.identifier);
|
localVariables.add(parameterNode.identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Visit all statements
|
// Visit all statements
|
||||||
@@ -130,32 +135,44 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(CalculationNode calculationNode) {
|
public void visit(CalculationNode calculationNode) {
|
||||||
calculationNode.dotExpression.accept(this);
|
if (calculationNode.dotExpression != null) {
|
||||||
calculationNode.calculationExpression.accept(this);
|
calculationNode.dotExpression.accept(this);
|
||||||
switch (calculationNode.operator) {
|
}
|
||||||
case PLUS:
|
if (calculationNode.calculationExpression != null) {
|
||||||
methodVisitor.visitInsn(IADD);
|
calculationNode.calculationExpression.accept(this);
|
||||||
break;
|
}
|
||||||
case MINUS:
|
if (calculationNode.operator != null) {
|
||||||
methodVisitor.visitInsn(ISUB);
|
switch (calculationNode.operator) {
|
||||||
break;
|
case PLUS:
|
||||||
|
methodVisitor.visitInsn(IADD);
|
||||||
|
break;
|
||||||
|
case MINUS:
|
||||||
|
methodVisitor.visitInsn(ISUB);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(DotNode dotNode) {
|
public void visit(DotNode dotNode) {
|
||||||
dotNode.dotExpression.accept(this);
|
if (dotNode.dotExpression != null) {
|
||||||
dotNode.dotSubstractionExpression.accept(this);
|
dotNode.dotExpression.accept(this);
|
||||||
switch (dotNode.operator) {
|
}
|
||||||
case DIV:
|
if (dotNode.dotSubstractionExpression != null) {
|
||||||
methodVisitor.visitInsn(IDIV);
|
dotNode.dotSubstractionExpression.accept(this);
|
||||||
break;
|
}
|
||||||
case MULT:
|
if (dotNode.operator != null) {
|
||||||
methodVisitor.visitInsn(IMUL);
|
switch (dotNode.operator) {
|
||||||
break;
|
case DIV:
|
||||||
case MOD:
|
methodVisitor.visitInsn(IDIV);
|
||||||
methodVisitor.visitInsn(IREM);
|
break;
|
||||||
break;
|
case MULT:
|
||||||
|
methodVisitor.visitInsn(IMUL);
|
||||||
|
break;
|
||||||
|
case MOD:
|
||||||
|
methodVisitor.visitInsn(IREM);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -164,7 +181,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
if (dotSubstractionNode.value != null) {
|
if (dotSubstractionNode.value != null) {
|
||||||
dotSubstractionNode.value.accept(this);
|
dotSubstractionNode.value.accept(this);
|
||||||
} else if (dotSubstractionNode.identifier != null) {
|
} else if (dotSubstractionNode.identifier != null) {
|
||||||
methodVisitor.visitVarInsn(ILOAD, localVaribales.indexOf(dotSubstractionNode.identifier));
|
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(dotSubstractionNode.identifier));
|
||||||
} else if (dotSubstractionNode.memberAccess != null) {
|
} else if (dotSubstractionNode.memberAccess != null) {
|
||||||
dotSubstractionNode.memberAccess.accept(this);
|
dotSubstractionNode.memberAccess.accept(this);
|
||||||
} else if (dotSubstractionNode.methodCall != null) {
|
} else if (dotSubstractionNode.methodCall != null) {
|
||||||
@@ -248,8 +265,10 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(MemberAccessNode memberAccessNode) {
|
public void visit(MemberAccessNode memberAccessNode) {
|
||||||
if (memberAccessNode.thisExpr) {
|
// Only used to get, not to put
|
||||||
//methodVisitor.visitFieldInsn(PUTFIELD);
|
if (memberAccessNode.thisExpr) { // Field
|
||||||
|
// methodVisitor.visitFieldInsn(GETFIELD, memberAccessNode.identifiers.get(0), memberAccessNode.identifiers.get(1), );
|
||||||
|
} else { // Object Attribut
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -275,7 +294,21 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(UnaryNode unaryNode) {
|
public void visit(UnaryNode unaryNode) {
|
||||||
|
if (unaryNode.thisExp != null) {
|
||||||
|
methodVisitor.visitVarInsn(ALOAD, 0); // this
|
||||||
|
} else if (unaryNode.identifier != null) {
|
||||||
|
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(unaryNode.identifier));
|
||||||
|
} else if (unaryNode.memberAccess != null) {
|
||||||
|
unaryNode.memberAccess.accept(this);
|
||||||
|
} else if (unaryNode.value != null) {
|
||||||
|
unaryNode.value.accept(this);
|
||||||
|
} else if (unaryNode.notExpression != null) {
|
||||||
|
unaryNode.notExpression.accept(this);
|
||||||
|
} else if (unaryNode.statement != null) {
|
||||||
|
unaryNode.statement.accept(this);
|
||||||
|
} else if (unaryNode.expression != null) {
|
||||||
|
unaryNode.expression.accept(this);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -286,7 +319,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
Label elseLabel = new Label();
|
Label elseLabel = new Label();
|
||||||
|
|
||||||
Label[] elseIfLabels = new Label[ifElseNode.elseIfStatements.size()];
|
Label[] elseIfLabels = new Label[ifElseNode.elseIfStatements.size()];
|
||||||
for(int i = 0; i < ifElseNode.elseIfStatements.size(); i++) {
|
for (int i = 0; i < ifElseNode.elseIfStatements.size(); i++) {
|
||||||
elseIfLabels[i] = new Label();
|
elseIfLabels[i] = new Label();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -303,12 +336,12 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
Label endLabel = new Label();
|
Label endLabel = new Label();
|
||||||
methodVisitor.visitJumpInsn(GOTO, endLabel);
|
methodVisitor.visitJumpInsn(GOTO, endLabel);
|
||||||
|
|
||||||
for(int i = 0; i < ifElseNode.elseIfStatements.size(); i++) {
|
for (int i = 0; i < ifElseNode.elseIfStatements.size(); i++) {
|
||||||
methodVisitor.visitLabel(elseIfLabels[i]);
|
methodVisitor.visitLabel(elseIfLabels[i]);
|
||||||
ifElseNode.elseIfStatements.get(i).expression.accept(this);
|
ifElseNode.elseIfStatements.get(i).expression.accept(this);
|
||||||
if(i + 1 < elseIfLabels.length) {
|
if (i + 1 < elseIfLabels.length) {
|
||||||
// at least one more else if
|
// at least one more else if
|
||||||
methodVisitor.visitJumpInsn(IFEQ, elseIfLabels[i+1]);
|
methodVisitor.visitJumpInsn(IFEQ, elseIfLabels[i + 1]);
|
||||||
} else {
|
} else {
|
||||||
methodVisitor.visitJumpInsn(IFEQ, elseLabel);
|
methodVisitor.visitJumpInsn(IFEQ, elseLabel);
|
||||||
}
|
}
|
||||||
@@ -316,7 +349,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
methodVisitor.visitJumpInsn(GOTO, endLabel);
|
methodVisitor.visitJumpInsn(GOTO, endLabel);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(ifElseNode.elseStatement != null) {
|
if (ifElseNode.elseStatement != null) {
|
||||||
methodVisitor.visitLabel(elseLabel);
|
methodVisitor.visitLabel(elseLabel);
|
||||||
ifElseNode.elseStatement.block.accept(this);
|
ifElseNode.elseStatement.block.accept(this);
|
||||||
}
|
}
|
||||||
@@ -326,29 +359,94 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(LocalVariableDeclarationNode localVariableDeclarationNode) {
|
public void visit(LocalVariableDeclarationNode localVariableDeclarationNode) {
|
||||||
// Process expression
|
if (localVariableDeclarationNode.expression != null) {
|
||||||
localVariableDeclarationNode.expression.accept(this);
|
// Process expression
|
||||||
// Store result of expression in variable
|
localVariableDeclarationNode.expression.accept(this);
|
||||||
methodVisitor.visitVarInsn(ISTORE, localVaribales.indexOf(localVariableDeclarationNode.identifier));
|
// 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 {
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (!localVariables.contains(localVariableDeclarationNode.identifier)) {
|
||||||
|
localVariables.add(localVariableDeclarationNode.identifier);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(AssignNode assignNode) {
|
public void visit(AssignNode assignNode) {
|
||||||
// Process expression
|
// Process expression
|
||||||
assignNode.expression.accept(this);
|
if (assignNode.expression instanceof IncrementNode) {
|
||||||
// Store result of expression in variable
|
IncrementNode incrementNode = (IncrementNode) assignNode.expression;
|
||||||
if (assignNode.assignable.memberAccess.thisExpr) {
|
if (incrementNode.crementType.equals(CrementType.PREFIX)) { // ++i
|
||||||
// Global var
|
methodVisitor.visitIincInsn(localVariables.indexOf(incrementNode.assignableExpression.identifier), 1);
|
||||||
// /methodVisitor.visitFieldInsn(PUTFIELD, identifierExpressionNode.name, identifierExpressionNode1.name, mapper.getTypeChar(((BaseTypeNode) type).enumType));
|
assign(assignNode);
|
||||||
|
} else if (incrementNode.crementType.equals(CrementType.SUFFIX)) { // Suffix: i++
|
||||||
|
assign(assignNode);
|
||||||
|
methodVisitor.visitIincInsn(localVariables.indexOf(incrementNode.assignableExpression.identifier), 1);
|
||||||
|
}
|
||||||
|
} 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);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// Local var
|
assignNode.expression.accept(this);
|
||||||
methodVisitor.visitVarInsn(ISTORE, localVaribales.indexOf(assignNode.assignable.identifier));
|
assign(assignNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assign(AssignNode assignNode) {
|
||||||
|
if (assignNode.assignable.memberAccess.thisExpr) {
|
||||||
|
assignField(assignNode);
|
||||||
|
} else {
|
||||||
|
assignLocalVar(assignNode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assignLocalVar(AssignNode assignNode) {
|
||||||
|
if (assignNode.expression instanceof BaseType) {
|
||||||
|
methodVisitor.visitVarInsn(ISTORE, localVariables.indexOf(assignNode.assignable.identifier));
|
||||||
|
} else if (assignNode.expression instanceof ReferenceType) {
|
||||||
|
methodVisitor.visitVarInsn(ASTORE, localVariables.indexOf(assignNode.assignable.identifier));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void assignField(AssignNode 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()+";");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(NewDeclarationNode newDeclarationNode) {
|
public void visit(NewDeclarationNode newDeclarationNode) {
|
||||||
|
methodVisitor.visitTypeInsn(NEW, newDeclarationNode.identifier);
|
||||||
|
methodVisitor.visitInsn(DUP);
|
||||||
|
List<ParameterNode> parameterNodes = new ArrayList<>();
|
||||||
|
for (IExpressionNode expressionNode : newDeclarationNode.expressions) {
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -395,7 +493,11 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
// Process expression
|
// Process expression
|
||||||
returnNode.expression.accept(this);
|
returnNode.expression.accept(this);
|
||||||
// Return result of expression
|
// Return result of expression
|
||||||
methodVisitor.visitInsn(IRETURN);
|
if (returnNode.expression.getType() instanceof BaseType) {
|
||||||
|
methodVisitor.visitInsn(IRETURN);
|
||||||
|
} else if (returnNode.expression.getType() instanceof ReferenceType) {
|
||||||
|
methodVisitor.visitInsn(ARETURN);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -416,61 +518,19 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
methodVisitor.visitLabel(endOfLoopLabel);
|
methodVisitor.visitLabel(endOfLoopLabel);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visit(DecrementNode decrementNode) {
|
|
||||||
switch (decrementNode.crementType) {
|
|
||||||
case PREFIX: // --i
|
|
||||||
if (decrementNode.assignableExpression.memberAccess == null) { // local Var
|
|
||||||
methodVisitor.visitIincInsn(localVaribales.indexOf(decrementNode.assignableExpression.identifier), -1);
|
|
||||||
} else { // Field or var from other object
|
|
||||||
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case SUFFIX: // i--
|
|
||||||
if (decrementNode.assignableExpression.memberAccess == null) { // local Var
|
|
||||||
methodVisitor.visitIincInsn(localVaribales.indexOf(decrementNode.assignableExpression.identifier), -1);
|
|
||||||
} else { // Field or var from other object
|
|
||||||
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void visit(IncrementNode incrementNode) {
|
|
||||||
switch (incrementNode.crementType) {
|
|
||||||
case PREFIX: // ++i
|
|
||||||
if (incrementNode.assignableExpression.memberAccess == null) { // local Var
|
|
||||||
methodVisitor.visitIincInsn(localVaribales.indexOf(incrementNode.assignableExpression.identifier), 1);
|
|
||||||
methodVisitor.visitVarInsn(ISTORE, localVaribales.indexOf(incrementNode.assignableExpression.identifier));
|
|
||||||
|
|
||||||
} else { // Field or var from other object
|
|
||||||
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case SUFFIX: // i++
|
|
||||||
if (incrementNode.assignableExpression.memberAccess == null) { // local Var
|
|
||||||
methodVisitor.visitVarInsn(ISTORE, localVaribales.indexOf(incrementNode.assignableExpression.identifier));
|
|
||||||
methodVisitor.visitIincInsn(localVaribales.indexOf(incrementNode.assignableExpression.identifier), 1);
|
|
||||||
} else { // Field or var from other object
|
|
||||||
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(ChainedMethodNode chainedMethodNode) {
|
public void visit(ChainedMethodNode chainedMethodNode) {
|
||||||
|
// TODO: Erstmal abwarten
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(MethodCallNode methodCallNode) {
|
public void visit(MethodCallNode methodCallNode) {
|
||||||
|
List<ParameterNode> parameterNodes = new ArrayList<>();
|
||||||
}
|
for(IExpressionNode expressionNode : methodCallNode.parameters) {
|
||||||
|
expressionNode.accept(this);
|
||||||
@Override
|
parameterNodes.add(new ParameterNode(expressionNode.getType(), ""));
|
||||||
public void visit(TargetNode targetNode) {
|
}
|
||||||
|
// TODO: Klassenname und Returntype
|
||||||
|
//methodVisitor.visitMethodInsn(INVOKEVIRTUAL, classname, methodCallNode.identifier, mapper.generateMethodDescriptor(returntype, parameterNodes), false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -44,12 +44,8 @@ public interface MethodVisitor {
|
|||||||
void visit(WhileNode whileNode);
|
void visit(WhileNode whileNode);
|
||||||
|
|
||||||
// statement expression
|
// statement expression
|
||||||
void visit(DecrementNode decrementNode);
|
|
||||||
void visit(IncrementNode incrementNode);
|
|
||||||
|
|
||||||
void visit(ChainedMethodNode chainedMethodNode);
|
void visit(ChainedMethodNode chainedMethodNode);
|
||||||
void visit(MethodCallNode methodCallNode);
|
void visit(MethodCallNode methodCallNode);
|
||||||
void visit(TargetNode targetNode);
|
|
||||||
|
|
||||||
void visit(AssignNode assignNode);
|
void visit(AssignNode assignNode);
|
||||||
void visit(NewDeclarationNode newDeclarationNode);
|
void visit(NewDeclarationNode newDeclarationNode);
|
||||||
|
@@ -13,6 +13,7 @@ import org.antlr.v4.runtime.tree.ParseTree;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.Paths;
|
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> cd .\src\test\ </code>
|
||||||
* <p> <code> make clean compile-raupenpiler </code>
|
* <p> <code> make clean compile-raupenpiler </code>
|
||||||
* <p> Start Raupenpiler using jar:
|
* <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):
|
* <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 class Main {
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
@@ -91,15 +92,19 @@ public class Main {
|
|||||||
|
|
||||||
/*------------------------- Semantic Analyzer -> typed AST -------------------------*/
|
/*------------------------- Semantic Analyzer -> typed AST -------------------------*/
|
||||||
// Use the SemanticAnalyzer to generate a typed AST
|
// Use the SemanticAnalyzer to generate a typed AST
|
||||||
//ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||||
// Log the typed AST
|
// Log the typed AST
|
||||||
RaupenLogger.logSemanticAnalyzer(abstractSyntaxTree);
|
RaupenLogger.logSemanticAnalyzer(typedAst);
|
||||||
|
|
||||||
/*------------------------- Bytecode Generator -> Bytecode -------------------------*/
|
/*------------------------- Bytecode Generator -> Bytecode -------------------------*/
|
||||||
// Use the ByteCodeGenerator to generate bytecode from the typed AST and output it to the specified directory
|
// Use the ByteCodeGenerator to generate bytecode from the typed AST and output it to the specified directory
|
||||||
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(outputDirectoryPath);
|
|
||||||
assert abstractSyntaxTree != null;
|
final boolean genJar = Optional.ofNullable(System.getProperty("genJar")).map(String::toLowerCase).map(Boolean::parseBoolean).orElse(true);
|
||||||
byteCodeGenerator.visit((ProgramNode) abstractSyntaxTree);
|
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
|
// Log the bytecode generation
|
||||||
RaupenLogger.logBytecodeGenerator();
|
RaupenLogger.logBytecodeGenerator();
|
||||||
}
|
}
|
||||||
|
@@ -75,12 +75,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ASTNode visitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) {
|
public ASTNode visitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) {
|
||||||
ConstructorNode constructorNode;
|
ConstructorNode constructorNode = new ConstructorNode(ctx.AccessModifier().getText(), ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
|
||||||
if(ctx.AccessModifier() != null) {
|
|
||||||
constructorNode = new ConstructorNode(ctx.AccessModifier().getText(), ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
|
|
||||||
} else {
|
|
||||||
constructorNode = new ConstructorNode(null, ctx.Identifier().getText(), (BlockNode) visit(ctx.blockStatement()));
|
|
||||||
}
|
|
||||||
if(ctx.parameterList() != null) {
|
if(ctx.parameterList() != null) {
|
||||||
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
|
for(SimpleJavaParser.ParameterContext parameter : ctx.parameterList().parameter()) {
|
||||||
constructorNode.addParameter((ParameterNode) visit(parameter));
|
constructorNode.addParameter((ParameterNode) visit(parameter));
|
||||||
|
@@ -21,6 +21,7 @@ import ast.statementexpressions.AssignableNode;
|
|||||||
import ast.statementexpressions.NewDeclarationNode;
|
import ast.statementexpressions.NewDeclarationNode;
|
||||||
import ast.statementexpressions.crementexpressions.DecrementNode;
|
import ast.statementexpressions.crementexpressions.DecrementNode;
|
||||||
import ast.statementexpressions.crementexpressions.IncrementNode;
|
import ast.statementexpressions.crementexpressions.IncrementNode;
|
||||||
|
import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNode;
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
||||||
import ast.statements.*;
|
import ast.statements.*;
|
||||||
@@ -29,6 +30,7 @@ import ast.type.EnumAccessModifierNode;
|
|||||||
import ast.type.ValueNode;
|
import ast.type.ValueNode;
|
||||||
import ast.type.type.*;
|
import ast.type.type.*;
|
||||||
import com.sun.jdi.IntegerType;
|
import com.sun.jdi.IntegerType;
|
||||||
|
import semantic.context.ClassContext;
|
||||||
import semantic.context.Context;
|
import semantic.context.Context;
|
||||||
import semantic.exceptions.*;
|
import semantic.exceptions.*;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
@@ -163,6 +165,11 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(FieldNode toCheck) {
|
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) {
|
if (currentFields.get(toCheck.identifier) != null) {
|
||||||
errors.add(new AlreadyDeclaredException("Already declared " + toCheck.identifier));
|
errors.add(new AlreadyDeclaredException("Already declared " + toCheck.identifier));
|
||||||
return new TypeCheckResult(false, null);
|
return new TypeCheckResult(false, null);
|
||||||
@@ -170,6 +177,8 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
currentFields.put(toCheck.identifier, toCheck.type);
|
currentFields.put(toCheck.identifier, toCheck.type);
|
||||||
}
|
}
|
||||||
return new TypeCheckResult(true, null);
|
return new TypeCheckResult(true, null);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -275,13 +284,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
currentNullType = oldNullType;
|
currentNullType = oldNullType;
|
||||||
var valid = true;
|
var valid = true;
|
||||||
|
|
||||||
// This check currently handles things like :
|
|
||||||
/**
|
|
||||||
* private int i;
|
|
||||||
* void foo(int i){
|
|
||||||
* i = i;
|
|
||||||
* }
|
|
||||||
*/
|
|
||||||
if (assignable.equals(rExpression)) {
|
if (assignable.equals(rExpression)) {
|
||||||
errors.add(new TypeMismatchException("Cannot assign to self"));
|
errors.add(new TypeMismatchException("Cannot assign to self"));
|
||||||
valid = false;
|
valid = false;
|
||||||
@@ -324,6 +326,15 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
targetType = currentFields.get(toCheck.target.identifier);
|
targetType = currentFields.get(toCheck.target.identifier);
|
||||||
}
|
}
|
||||||
if (targetType instanceof ReferenceType reference) {
|
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);
|
var type = getTypeFromMethod(toCheck, reference);
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
return new TypeCheckResult(true, type);
|
return new TypeCheckResult(true, type);
|
||||||
@@ -620,4 +631,37 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
return null;
|
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);
|
return classes.get(identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public HashMap<String, ClassContext> getClasses() {
|
||||||
|
return classes;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean containsClass(String identifier) {
|
public boolean containsClass(String identifier) {
|
||||||
return classes.containsKey(identifier);
|
return classes.containsKey(identifier);
|
||||||
}
|
}
|
||||||
|
@@ -2,15 +2,13 @@ public class Compiler {
|
|||||||
public int add(int i, int j) {
|
public int add(int i, int j) {
|
||||||
return i+j;
|
return i+j;
|
||||||
}
|
}
|
||||||
public static void main(String[] args) {
|
|
||||||
int a = 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Node {
|
public class Node {
|
||||||
public void main() {
|
public void main() {
|
||||||
Compiler compiler = new Compiler();
|
Compiler compiler = new Compiler();
|
||||||
int i = compiler.add(5, 8);
|
int i = compiler.add(5, 8);
|
||||||
|
return i;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Binary file not shown.
@@ -9,8 +9,8 @@ compile-javac:
|
|||||||
|
|
||||||
compile-raupenpiler:
|
compile-raupenpiler:
|
||||||
cd ../.. ; mvn -DskipTests install
|
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
|
cp ../main/resources/output/CompilerInput.class .java/resources/output/raupenpiler
|
||||||
|
|
||||||
test: compile-javac compile-raupenpiler test-javac test-raupenpiler
|
test: compile-javac compile-raupenpiler test-javac test-raupenpiler
|
||||||
|
|
||||||
@@ -36,8 +36,10 @@ clean:
|
|||||||
rm -f ./resources/output/raupenpiler/*.class
|
rm -f ./resources/output/raupenpiler/*.class
|
||||||
# clean logs
|
# clean logs
|
||||||
rm -f ../main/resources/logs/*.log
|
rm -f ../main/resources/logs/*.log
|
||||||
# clean test/main folders from .class files for End-to-End tests
|
# clean test/java/main folders from .class files for End-to-End tests
|
||||||
rm -f ./java/main/*.class
|
rm -f ./java/main/*.class
|
||||||
# clean javac output from featureTests
|
# clean javac output from combinedFeatureTests
|
||||||
rm -f ./resources/input/featureTests/*.class
|
rm -f ./resources/input/combinedFeatureTests/*.class
|
||||||
|
rm -f ./resources/input/singleFeatureTests/*.class
|
||||||
|
rm -f ./resources/input/typedAstFeatureTests/*.class
|
||||||
|
|
||||||
|
@@ -1,177 +1,95 @@
|
|||||||
package main;
|
package main;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import java.lang.reflect.Constructor;
|
||||||
import ast.ProgramNode;
|
import java.lang.reflect.Field;
|
||||||
import bytecode.ByteCodeGenerator;
|
import java.lang.reflect.Method;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import java.util.Arrays;
|
||||||
|
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.antlr.v4.runtime.CharStream;
|
|
||||||
import org.antlr.v4.runtime.CommonTokenStream;
|
import javax.tools.JavaCompiler;
|
||||||
import org.antlr.v4.runtime.tree.ParseTree;
|
import javax.tools.ToolProvider;
|
||||||
import parser.astBuilder.ASTBuilder;
|
|
||||||
import parser.generated.SimpleJavaLexer;
|
|
||||||
import parser.generated.SimpleJavaParser;
|
|
||||||
import semantic.SemanticAnalyzer;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
import static org.mockito.Mockito.*;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
|
|
||||||
public class E2EReflectionsTest {
|
public class E2EReflectionsTest {
|
||||||
|
|
||||||
|
|
||||||
private CharStream mockInputCharStream;
|
|
||||||
private String outputDirectoryPath;
|
|
||||||
private SimpleJavaLexer mockLexer;
|
|
||||||
private CommonTokenStream mockTokenStream;
|
|
||||||
private SimpleJavaParser mockParser;
|
|
||||||
private ParseTree mockParseTree;
|
|
||||||
private ASTBuilder mockASTBuilder;
|
|
||||||
private ASTNode mockASTNode;
|
|
||||||
private SemanticAnalyzer mockSemanticAnalyzer;
|
|
||||||
private ASTNode mockTypedAST;
|
|
||||||
private ByteCodeGenerator mockByteCodeGenerator;
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
public void setUp() {
|
|
||||||
mockInputCharStream = mock(CharStream.class);
|
|
||||||
outputDirectoryPath = "path/to/output";
|
|
||||||
mockLexer = mock(SimpleJavaLexer.class);
|
|
||||||
mockTokenStream = mock(CommonTokenStream.class);
|
|
||||||
mockParser = mock(SimpleJavaParser.class);
|
|
||||||
mockParseTree = mock(ParseTree.class);
|
|
||||||
mockASTBuilder = mock(ASTBuilder.class);
|
|
||||||
mockASTNode = mock(ASTNode.class);
|
|
||||||
mockSemanticAnalyzer = mock(SemanticAnalyzer.class);
|
|
||||||
mockTypedAST = mock(ASTNode.class);
|
|
||||||
mockByteCodeGenerator = mock(ByteCodeGenerator.class);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testCompileFile() throws Exception {
|
public void AllFeaturesClassExampleTest(){
|
||||||
// Mock the dependencies
|
try {
|
||||||
SimpleJavaLexer mockLexer = mock(SimpleJavaLexer.class);
|
Main.main(new String[]{"src/test/resources/input/combinedFeatureTests/AllFeaturesClassExample.java", "src/test/resources/output/raupenpiler"});
|
||||||
CommonTokenStream mockTokenStream = mock(CommonTokenStream.class);
|
// Get the system Java compiler
|
||||||
SimpleJavaParser mockParser = mock(SimpleJavaParser.class);
|
JavaCompiler javac = ToolProvider.getSystemJavaCompiler();
|
||||||
ParseTree mockParseTree = mock(ParseTree.class);
|
// Assert that the compiler is available
|
||||||
ASTBuilder mockASTBuilder = mock(ASTBuilder.class);
|
assertNotNull(javac, "Java Compiler is not available");
|
||||||
ASTNode mockASTNode = mock(ASTNode.class);
|
javac.run(null, null, null, "src/test/resources/input/combinedFeatureTests/AllFeaturesClassExample.java");
|
||||||
SemanticAnalyzer mockSemanticAnalyzer = mock(SemanticAnalyzer.class);
|
|
||||||
ASTNode mockTypedAST = mock(ASTNode.class);
|
|
||||||
ByteCodeGenerator mockByteCodeGenerator = mock(ByteCodeGenerator.class);
|
|
||||||
|
|
||||||
// Mock the behavior
|
Class<?> clazz = Class.forName("src.resources.input.combinedFeatureTests.AllFeaturesClassExample");
|
||||||
when(mockLexer.nextToken()).thenReturn(null);
|
ClassLoader classLoader = getClass().getClassLoader();
|
||||||
when(mockTokenStream.getTokens()).thenReturn(new ArrayList<>());
|
// Class<?> clazz = classLoader.loadClass("main.AllFeaturesClassExample");
|
||||||
when(mockParser.program()).thenReturn((SimpleJavaParser.ProgramContext) mockParseTree);
|
|
||||||
when(mockASTBuilder.visit(mockParseTree)).thenReturn(mockASTNode);
|
|
||||||
when(SemanticAnalyzer.generateTast(mockASTNode)).thenReturn(mockTypedAST);
|
|
||||||
|
|
||||||
// Use reflection to invoke the compileFile method
|
// Class Name
|
||||||
Method compileFileMethod = main.Main.class.getDeclaredMethod("compileFile", CharStream.class, String.class);
|
assertEquals("main.AllFeaturesClassExample", clazz.getName());
|
||||||
compileFileMethod.setAccessible(true);
|
|
||||||
|
|
||||||
compileFileMethod.invoke(null, mockInputCharStream, outputDirectoryPath);
|
// Constructors
|
||||||
|
Constructor<?>[] actualConstructors = clazz.getDeclaredConstructors();
|
||||||
|
assertTrue(actualConstructors.length > 0, "No constructors found");
|
||||||
|
|
||||||
// Verify each step
|
Constructor<?> expectedConstructor = clazz.getConstructor(int.class, boolean.class, char.class);
|
||||||
verify(mockLexer, times(1)).nextToken();
|
|
||||||
verify(mockTokenStream, times(1)).getTokens();
|
boolean constructorFound = false;
|
||||||
verify(mockParser, times(1)).program();
|
for (Constructor<?> constructor : actualConstructors) {
|
||||||
verify(mockASTBuilder, times(1)).visit(mockParseTree);
|
if (constructor.equals(expectedConstructor)) {
|
||||||
verify(mockSemanticAnalyzer, times(1)).generateTast(mockASTNode);
|
constructorFound = true;
|
||||||
verify(mockByteCodeGenerator, times(1)).visit((ProgramNode) mockTypedAST);
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertTrue(constructorFound, "Expected constructor not found in actual constructors");
|
||||||
|
|
||||||
|
// Methods
|
||||||
|
Method[] actualMethodNames = clazz.getDeclaredMethods();
|
||||||
|
assertTrue(actualMethodNames.length > 0);
|
||||||
|
for (Method method : actualMethodNames) {
|
||||||
|
System.out.println("Method: " + method.getName());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Method Names
|
||||||
|
String[] expectedMethodNames = {
|
||||||
|
"controlStructures",
|
||||||
|
"logicalOperations",
|
||||||
|
"add",
|
||||||
|
"subtract",
|
||||||
|
"multiply",
|
||||||
|
"divide",
|
||||||
|
"modulo",
|
||||||
|
"main"
|
||||||
|
};
|
||||||
|
|
||||||
|
for (Method method : actualMethodNames) {
|
||||||
|
assertTrue(Arrays.asList(expectedMethodNames).contains(method.getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fields
|
||||||
|
Field[] actualFields = clazz.getDeclaredFields();
|
||||||
|
assertTrue(actualFields.length > 0, "No fields found");
|
||||||
|
|
||||||
|
Field expectedField = clazz.getDeclaredField("c");
|
||||||
|
assertEquals(expectedField.getType(), char.class);
|
||||||
|
|
||||||
|
boolean fieldFound = false;
|
||||||
|
for (Field field : actualFields) {
|
||||||
|
if (field.equals(expectedField)) {
|
||||||
|
fieldFound = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertTrue(fieldFound, "Expected field not found in actual fields");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} catch (ClassNotFoundException | NoSuchFieldException | NoSuchMethodException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@Test
|
|
||||||
public void testCompileFile2() throws Exception {
|
|
||||||
// Mock the behavior
|
|
||||||
when(mockLexer.nextToken()).thenReturn(null);
|
|
||||||
when(mockTokenStream.getTokens()).thenReturn(new ArrayList<>());
|
|
||||||
when(mockParser.program()).thenReturn((SimpleJavaParser.ProgramContext) mockParseTree);
|
|
||||||
when(mockASTBuilder.visit(mockParseTree)).thenReturn(mockASTNode);
|
|
||||||
when(SemanticAnalyzer.generateTast(mockASTNode)).thenReturn(mockTypedAST);
|
|
||||||
|
|
||||||
// Use reflection to invoke the compileFile method
|
|
||||||
Method compileFileMethod = main.Main.class.getDeclaredMethod("compileFile", CharStream.class, String.class);
|
|
||||||
compileFileMethod.setAccessible(true);
|
|
||||||
|
|
||||||
compileFileMethod.invoke(null, mockInputCharStream, outputDirectoryPath);
|
|
||||||
|
|
||||||
// Verify each step
|
|
||||||
verify(mockLexer, times(1)).nextToken();
|
|
||||||
verify(mockTokenStream, times(1)).getTokens();
|
|
||||||
verify(mockParser, times(1)).program();
|
|
||||||
verify(mockASTBuilder, times(1)).visit(mockParseTree);
|
|
||||||
verify(mockSemanticAnalyzer, times(1)).generateTast(mockASTNode);
|
|
||||||
verify(mockByteCodeGenerator, times(1)).visit((ProgramNode) mockTypedAST);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testLexer() {
|
|
||||||
// Mock the behavior
|
|
||||||
when(mockLexer.nextToken()).thenReturn(null);
|
|
||||||
|
|
||||||
// Test the lexer
|
|
||||||
SimpleJavaLexer lexer = new SimpleJavaLexer(mockInputCharStream);
|
|
||||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
|
||||||
tokenStream.fill();
|
|
||||||
|
|
||||||
assertNotNull(tokenStream.getTokens());
|
|
||||||
verify(mockLexer, atLeastOnce()).nextToken();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testParser() {
|
|
||||||
// Mock the behavior
|
|
||||||
when(mockParser.program()).thenReturn((SimpleJavaParser.ProgramContext) mockParseTree);
|
|
||||||
|
|
||||||
// Test the parser
|
|
||||||
SimpleJavaParser parser = new SimpleJavaParser(mockTokenStream);
|
|
||||||
ParseTree parseTree = parser.program();
|
|
||||||
|
|
||||||
assertNotNull(parseTree);
|
|
||||||
verify(mockParser, times(1)).program();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testASTBuilder() {
|
|
||||||
// Mock the behavior
|
|
||||||
when(mockASTBuilder.visit(mockParseTree)).thenReturn(mockASTNode);
|
|
||||||
|
|
||||||
// Test the AST builder
|
|
||||||
ASTBuilder astBuilder = new ASTBuilder();
|
|
||||||
ASTNode abstractSyntaxTree = astBuilder.visit(mockParseTree);
|
|
||||||
|
|
||||||
assertNotNull(abstractSyntaxTree);
|
|
||||||
verify(mockASTBuilder, times(1)).visit(mockParseTree);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testSemanticAnalyzer() {
|
|
||||||
// Mock the behavior
|
|
||||||
when(SemanticAnalyzer.generateTast(mockASTNode)).thenReturn(mockTypedAST);
|
|
||||||
|
|
||||||
// Test the semantic analyzer
|
|
||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(mockASTNode);
|
|
||||||
|
|
||||||
assertNotNull(typedAst);
|
|
||||||
verify(mockSemanticAnalyzer, times(1)).generateTast(mockASTNode);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void testByteCodeGenerator() {
|
|
||||||
// Test the bytecode generator
|
|
||||||
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(outputDirectoryPath);
|
|
||||||
byteCodeGenerator.visit((ProgramNode) mockTypedAST);
|
|
||||||
|
|
||||||
verify(mockByteCodeGenerator, times(1)).visit((ProgramNode) mockTypedAST);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@@ -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.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
104
src/test/java/main/InputFilesTest.java
Normal file
104
src/test/java/main/InputFilesTest.java
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
package main;
|
||||||
|
|
||||||
|
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 folder1 = new File("src/test/resources/input/combinedFeatureTests");
|
||||||
|
File folder2 = new File("src/test/resources/input/singleFeatureTests");
|
||||||
|
File folder3 = new File("src/test/resources/input/typedAstFeatureTests");
|
||||||
|
|
||||||
|
List<File> files = getJavaFilesFromDirectory(folder1);
|
||||||
|
files.addAll(getJavaFilesFromDirectory(folder2));
|
||||||
|
files.addAll(getJavaFilesFromDirectory(folder3));
|
||||||
|
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
145
src/test/java/main/ReflectionsTest.java
Normal file
145
src/test/java/main/ReflectionsTest.java
Normal file
@@ -0,0 +1,145 @@
|
|||||||
|
package main;
|
||||||
|
|
||||||
|
import java.lang.reflect.Constructor;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.antlr.v4.runtime.CharStream;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
|
||||||
|
public class ReflectionsTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleJavaLexerClass() throws ClassNotFoundException, NoSuchMethodException {
|
||||||
|
Class<?> clazz = Class.forName("parser.generated.SimpleJavaLexer");
|
||||||
|
|
||||||
|
// Class Name
|
||||||
|
assertEquals("parser.generated.SimpleJavaLexer", clazz.getName());
|
||||||
|
|
||||||
|
// Constructors
|
||||||
|
Constructor<?>[] actualConstructors = clazz.getDeclaredConstructors();
|
||||||
|
assertTrue(actualConstructors.length > 0, "No constructors found");
|
||||||
|
|
||||||
|
Constructor<?> expectedConstructor = clazz.getConstructor(CharStream.class);
|
||||||
|
|
||||||
|
boolean constructorFound = false;
|
||||||
|
for (Constructor<?> constructor : actualConstructors) {
|
||||||
|
if (constructor.equals(expectedConstructor)) {
|
||||||
|
constructorFound = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertTrue(constructorFound, "Expected constructor not found in actual constructors");
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Methods
|
||||||
|
Method[] actualMethodNames = clazz.getDeclaredMethods();
|
||||||
|
assertTrue(actualMethodNames.length > 0);
|
||||||
|
Arrays.stream(actualMethodNames).forEach(method -> System.out.println("Method: " + method.getName()));
|
||||||
|
|
||||||
|
List<String> expectedMethodNames = Arrays.asList(
|
||||||
|
"getTokenNames",
|
||||||
|
"getVocabulary",
|
||||||
|
"getGrammarFileName",
|
||||||
|
"getRuleNames",
|
||||||
|
"getSerializedATN",
|
||||||
|
"getChannelNames",
|
||||||
|
"getModeNames",
|
||||||
|
"getATN",
|
||||||
|
"makeRuleNames",
|
||||||
|
"makeLiteralNames",
|
||||||
|
"makeSymbolicNames"
|
||||||
|
);
|
||||||
|
|
||||||
|
for (Method method : actualMethodNames) {
|
||||||
|
assertTrue(expectedMethodNames.contains(method.getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
for (String expectedMethodName : expectedMethodNames) {
|
||||||
|
boolean methodFound = false;
|
||||||
|
for (Method method : actualMethodNames) {
|
||||||
|
if (method.getName().equals(expectedMethodName)) {
|
||||||
|
methodFound = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertTrue(methodFound, "Expected method " + expectedMethodName + " not found in actual methods");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// Fields
|
||||||
|
Field[] actualFieldNames = clazz.getDeclaredFields();
|
||||||
|
assertTrue(actualFieldNames.length > 0);
|
||||||
|
Arrays.stream(actualFieldNames).forEach(field -> System.out.println("Field: " + field.getName()));
|
||||||
|
|
||||||
|
List<String> expectedFieldNames = Arrays.asList(
|
||||||
|
"_decisionToDFA",
|
||||||
|
"_sharedContextCache",
|
||||||
|
"channelNames",
|
||||||
|
"modeNames",
|
||||||
|
"ruleNames",
|
||||||
|
"_LITERAL_NAMES",
|
||||||
|
"_SYMBOLIC_NAMES",
|
||||||
|
"VOCABULARY",
|
||||||
|
"tokenNames",
|
||||||
|
"_serializedATN",
|
||||||
|
"_ATN"
|
||||||
|
);
|
||||||
|
|
||||||
|
for (Field field : actualFieldNames) {
|
||||||
|
assertTrue(expectedFieldNames.contains(field.getName()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleJavaParserClass() throws ClassNotFoundException {
|
||||||
|
Class<?> clazz = Class.forName("parser.generated.SimpleJavaParser");
|
||||||
|
|
||||||
|
// Class Name
|
||||||
|
assertEquals("parser.generated.SimpleJavaParser", clazz.getName());
|
||||||
|
|
||||||
|
// Constructors
|
||||||
|
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
|
||||||
|
assertTrue(constructors.length > 0);
|
||||||
|
|
||||||
|
// Methods
|
||||||
|
Method[] methods = clazz.getDeclaredMethods();
|
||||||
|
assertTrue(methods.length > 0);
|
||||||
|
Arrays.stream(methods).forEach(method -> System.out.println("Method: " + method.getName()));
|
||||||
|
|
||||||
|
// Fields
|
||||||
|
Field[] fields = clazz.getDeclaredFields();
|
||||||
|
assertTrue(fields.length > 0);
|
||||||
|
Arrays.stream(fields).forEach(field -> System.out.println("Field: " + field.getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testASTBuilderClass() throws ClassNotFoundException {
|
||||||
|
Class<?> clazz = Class.forName("parser.astBuilder.ASTBuilder");
|
||||||
|
|
||||||
|
// Class Name
|
||||||
|
assertEquals("parser.astBuilder.ASTBuilder", clazz.getName());
|
||||||
|
|
||||||
|
// Constructors
|
||||||
|
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
|
||||||
|
assertTrue(constructors.length > 0);
|
||||||
|
|
||||||
|
// Methods
|
||||||
|
Method[] methods = clazz.getDeclaredMethods();
|
||||||
|
assertTrue(methods.length > 0);
|
||||||
|
Arrays.stream(methods).forEach(method -> System.out.println("Method: " + method.getName()));
|
||||||
|
|
||||||
|
// Fields
|
||||||
|
Field[] fields = clazz.getDeclaredFields();
|
||||||
|
assertTrue(fields.length > 0);
|
||||||
|
Arrays.stream(fields).forEach(field -> System.out.println("Field: " + field.getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Similarly, you can add tests for SemanticAnalyzer and ByteCodeGenerator
|
||||||
|
}
|
@@ -43,104 +43,106 @@ import static org.assertj.core.api.Assertions.assertThat;
|
|||||||
@DisplayName("Untyped Abstract Syntax Tree")
|
@DisplayName("Untyped Abstract Syntax Tree")
|
||||||
class AstBuilderTest {
|
class AstBuilderTest {
|
||||||
|
|
||||||
|
private final static String directoryPath = "src/test/resources/input/singleFeatureTests/";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Empty Class Test")
|
@DisplayName("Empty Class Test")
|
||||||
public void emptyClassTest(){
|
public void emptyClassTest() {
|
||||||
ClassNode emptyClass = Helper.generateEmptyClass("TestClass");
|
ClassNode emptyClass = Helper.generateEmptyClass("EmptyClass");
|
||||||
ProgramNode expected = new ProgramNode();
|
ProgramNode expected = new ProgramNode();
|
||||||
expected.addClass(emptyClass);
|
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);
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Multiple Empty Classes Test")
|
@DisplayName("Multiple Empty Classes Test")
|
||||||
public void multipleEmptyClassesTest() {
|
public void multipleEmptyClassesTest() {
|
||||||
ClassNode class1 = Helper.generateEmptyClass("TestClass1");
|
ClassNode class1 = Helper.generateEmptyClass("MultipleClasses");
|
||||||
ClassNode class2 = Helper.generateEmptyClass("TestClass2");
|
ClassNode class2 = Helper.generateEmptyClass("TestClass2");
|
||||||
ProgramNode expected = new ProgramNode();
|
ProgramNode expected = new ProgramNode();
|
||||||
expected.addClass(class1);
|
expected.addClass(class1);
|
||||||
expected.addClass(class2);
|
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);
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Empty Class Test with Constructor")
|
@DisplayName("Empty Class Test with Constructor")
|
||||||
public void emptyClassWithConstructorTest() {
|
public void emptyClassWithConstructorTest() {
|
||||||
ClassNode class1 = Helper.generateEmptyClass("TestClass");
|
ClassNode class1 = Helper.generateEmptyClass("EmptyClassWithConstructor");
|
||||||
ProgramNode expected = new ProgramNode();
|
ProgramNode expected = new ProgramNode();
|
||||||
expected.addClass(class1);
|
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);
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Field Test")
|
@DisplayName("Field Test")
|
||||||
public void fieldTest() {
|
public void fieldTest() {
|
||||||
ClassNode class1 = Helper.generateEmptyClass("TestClass");
|
ClassNode class1 = Helper.generateEmptyClass("Field");
|
||||||
class1.addMember(new FieldNode(null, new BaseType(TypeEnum.INT), "a"));
|
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
ProgramNode expected = new ProgramNode();
|
||||||
expected.addClass(class1);
|
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);
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Field Test with Accessmodifier")
|
@DisplayName("Field Test with Accessmodifier")
|
||||||
public void fieldTestWithModifier() {
|
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"));
|
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
ProgramNode expected = new ProgramNode();
|
||||||
expected.addClass(class1);
|
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);
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Commments Ignore Test")
|
@DisplayName("Comments Ignore Test")
|
||||||
public void commmentsIgnoreTest(){
|
public void commentsIgnoreTest() {
|
||||||
ClassNode class1 = Helper.generateEmptyClass("TestClass");
|
ClassNode class1 = Helper.generateEmptyClass("Comments");
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("private"), new BaseType(TypeEnum.INT), "a"));
|
class1.addMember(new FieldNode(new AccessModifierNode("private"), new BaseType(TypeEnum.INT), "a"));
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
ProgramNode expected = new ProgramNode();
|
||||||
expected.addClass(class1);
|
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);
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Constructor Paramerter Test")
|
@DisplayName("Constructor Parameter Test")
|
||||||
public void constructorParameterTest(){
|
public void constructorParameterTest() {
|
||||||
BlockNode block = new BlockNode();
|
BlockNode block = new BlockNode();
|
||||||
block.addStatement(new ReturnNode(null));
|
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), "a"));
|
||||||
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b"));
|
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b"));
|
||||||
|
|
||||||
ClassNode class1 = new ClassNode("public", "TestClass");
|
ClassNode class1 = new ClassNode("public", "ConstructorParameter");
|
||||||
class1.addMember(constructor);
|
class1.addMember(constructor);
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
ProgramNode expected = new ProgramNode();
|
||||||
expected.addClass(class1);
|
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);
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("This Dot Test")
|
@DisplayName("This Dot Test")
|
||||||
public void thisDotTest(){
|
public void thisDotTest() {
|
||||||
BlockNode block = new BlockNode();
|
BlockNode block = new BlockNode();
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||||
memberAccess.addIdentifier("a");
|
memberAccess.addIdentifier("a");
|
||||||
@@ -152,23 +154,23 @@ class AstBuilderTest {
|
|||||||
|
|
||||||
block.addStatement(new AssignNode(assignable, expression));
|
block.addStatement(new AssignNode(assignable, expression));
|
||||||
block.addStatement(new ReturnNode(null));
|
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(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||||
class1.addMember(constructor);
|
class1.addMember(constructor);
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
ProgramNode expected = new ProgramNode();
|
||||||
expected.addClass(class1);
|
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);
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Constructor This Dot Test")
|
@DisplayName("Constructor This Dot Test")
|
||||||
public void constructorThisDotTest(){
|
public void constructorThisDotTest() {
|
||||||
BlockNode block = new BlockNode();
|
BlockNode block = new BlockNode();
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||||
memberAccess.addIdentifier("a");
|
memberAccess.addIdentifier("a");
|
||||||
@@ -179,25 +181,25 @@ class AstBuilderTest {
|
|||||||
|
|
||||||
block.addStatement(new AssignNode(assignable, expression));
|
block.addStatement(new AssignNode(assignable, expression));
|
||||||
block.addStatement(new ReturnNode(null));
|
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"));
|
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(new FieldNode(new AccessModifierNode("private"), new BaseType(TypeEnum.INT), "a"));
|
||||||
class1.addMember(constructor);
|
class1.addMember(constructor);
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
ProgramNode expected = new ProgramNode();
|
||||||
expected.addClass(class1);
|
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);
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Void Methoden Test")
|
@DisplayName("Void Methoden Test")
|
||||||
public void voidMethodenTest(){
|
public void voidMethodenTest() {
|
||||||
ClassNode class1 = Helper.generateEmptyClass("TestClass");
|
ClassNode class1 = Helper.generateEmptyClass("VoidMethod");
|
||||||
BlockNode block = new BlockNode();
|
BlockNode block = new BlockNode();
|
||||||
block.addStatement(new ReturnNode(null));
|
block.addStatement(new ReturnNode(null));
|
||||||
class1.addMember(new MethodNode("public", null, true, "test", block));
|
class1.addMember(new MethodNode("public", null, true, "test", block));
|
||||||
@@ -205,14 +207,14 @@ class AstBuilderTest {
|
|||||||
ProgramNode expected = new ProgramNode();
|
ProgramNode expected = new ProgramNode();
|
||||||
expected.addClass(class1);
|
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);
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Constructor Method call Test")
|
@DisplayName("Constructor Method call Test")
|
||||||
public void constructorMethodCallTest(){
|
public void constructorMethodCallTest() {
|
||||||
BlockNode blockCon = new BlockNode();
|
BlockNode blockCon = new BlockNode();
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||||
memberAccess.addIdentifier("a");
|
memberAccess.addIdentifier("a");
|
||||||
@@ -223,13 +225,13 @@ class AstBuilderTest {
|
|||||||
|
|
||||||
blockCon.addStatement(new AssignNode(assignable, expression));
|
blockCon.addStatement(new AssignNode(assignable, expression));
|
||||||
blockCon.addStatement(new ReturnNode(null));
|
blockCon.addStatement(new ReturnNode(null));
|
||||||
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon);
|
ConstructorNode constructor = new ConstructorNode("public", "ConstructorMethodCall", blockCon);
|
||||||
|
|
||||||
BlockNode blockMethod = new BlockNode();
|
BlockNode blockMethod = new BlockNode();
|
||||||
blockMethod.addStatement(new ReturnNode(new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "1"))));
|
blockMethod.addStatement(new ReturnNode(new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "1"))));
|
||||||
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod", blockMethod);
|
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(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||||
class1.addMember(constructor);
|
class1.addMember(constructor);
|
||||||
class1.addMember(method);
|
class1.addMember(method);
|
||||||
@@ -237,14 +239,14 @@ class AstBuilderTest {
|
|||||||
ProgramNode expected = new ProgramNode();
|
ProgramNode expected = new ProgramNode();
|
||||||
expected.addClass(class1);
|
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);
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Constructor Method call Parameters Test")
|
@DisplayName("Constructor Method call Parameters Test")
|
||||||
public void constructorMethodCallParametersTest(){
|
public void constructorMethodCallParametersTest() {
|
||||||
BlockNode blockCon = new BlockNode();
|
BlockNode blockCon = new BlockNode();
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||||
memberAccess.addIdentifier("a");
|
memberAccess.addIdentifier("a");
|
||||||
@@ -257,7 +259,7 @@ class AstBuilderTest {
|
|||||||
|
|
||||||
blockCon.addStatement(new AssignNode(assignable, expression));
|
blockCon.addStatement(new AssignNode(assignable, expression));
|
||||||
blockCon.addStatement(new ReturnNode(null));
|
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"));
|
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
||||||
|
|
||||||
BlockNode blockMethod = new BlockNode();
|
BlockNode blockMethod = new BlockNode();
|
||||||
@@ -265,7 +267,7 @@ class AstBuilderTest {
|
|||||||
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod", blockMethod);
|
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod", blockMethod);
|
||||||
method.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
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(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||||
class1.addMember(constructor);
|
class1.addMember(constructor);
|
||||||
class1.addMember(method);
|
class1.addMember(method);
|
||||||
@@ -273,14 +275,14 @@ class AstBuilderTest {
|
|||||||
ProgramNode expected = new ProgramNode();
|
ProgramNode expected = new ProgramNode();
|
||||||
expected.addClass(class1);
|
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);
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Char Test")
|
@DisplayName("Char Test")
|
||||||
public void charTest(){
|
public void charTest() {
|
||||||
BlockNode blockCon = new BlockNode();
|
BlockNode blockCon = new BlockNode();
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||||
memberAccess.addIdentifier("a");
|
memberAccess.addIdentifier("a");
|
||||||
@@ -293,7 +295,7 @@ class AstBuilderTest {
|
|||||||
|
|
||||||
blockCon.addStatement(new AssignNode(assignable, expression));
|
blockCon.addStatement(new AssignNode(assignable, expression));
|
||||||
blockCon.addStatement(new ReturnNode(null));
|
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"));
|
constructor.addParameter(new ParameterNode(new BaseType(TypeEnum.CHAR), "a"));
|
||||||
|
|
||||||
BlockNode blockMethod = new BlockNode();
|
BlockNode blockMethod = new BlockNode();
|
||||||
@@ -301,7 +303,7 @@ class AstBuilderTest {
|
|||||||
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.CHAR), false, "testMethod", blockMethod);
|
MethodNode method = new MethodNode("public", new BaseType(TypeEnum.CHAR), false, "testMethod", blockMethod);
|
||||||
method.addParameter(new ParameterNode(new BaseType(TypeEnum.CHAR), "a"));
|
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(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.CHAR), "a"));
|
||||||
class1.addMember(constructor);
|
class1.addMember(constructor);
|
||||||
class1.addMember(method);
|
class1.addMember(method);
|
||||||
@@ -309,14 +311,14 @@ class AstBuilderTest {
|
|||||||
ProgramNode expected = new ProgramNode();
|
ProgramNode expected = new ProgramNode();
|
||||||
expected.addClass(class1);
|
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);
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Null Test")
|
@DisplayName("Null Test")
|
||||||
public void nullTest(){
|
public void nullTest() {
|
||||||
BlockNode blockCon = new BlockNode();
|
BlockNode blockCon = new BlockNode();
|
||||||
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
MemberAccessNode memberAccess = new MemberAccessNode(true);
|
||||||
memberAccess.addIdentifier("a");
|
memberAccess.addIdentifier("a");
|
||||||
@@ -325,65 +327,65 @@ class AstBuilderTest {
|
|||||||
|
|
||||||
blockCon.addStatement(new AssignNode(assignable, new UnaryNode(new ValueNode(EnumValueNode.NULL_VALUE, "null"))));
|
blockCon.addStatement(new AssignNode(assignable, new UnaryNode(new ValueNode(EnumValueNode.NULL_VALUE, "null"))));
|
||||||
blockCon.addStatement(new ReturnNode(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");
|
ClassNode class1 = new ClassNode("public", "Null");
|
||||||
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||||
class1.addMember(constructor);
|
class1.addMember(constructor);
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
ProgramNode expected = new ProgramNode();
|
||||||
expected.addClass(class1);
|
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);
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Self Reference Test")
|
@DisplayName("Self Reference Test")
|
||||||
public void selfReferneceTest(){
|
public void selfReferneceTest() {
|
||||||
|
|
||||||
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Variable Compare Test")
|
@DisplayName("Variable Compare Test")
|
||||||
public void variableCompareTest(){
|
public void variableCompareTest() {
|
||||||
|
|
||||||
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Variable Calculation Test")
|
@DisplayName("Variable Calculation Test")
|
||||||
public void variableCalculationTest(){
|
public void variableCalculationTest() {
|
||||||
|
|
||||||
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Main Method Test")
|
@DisplayName("Main Method Test")
|
||||||
public void mainMethodTest(){
|
public void mainMethodTest() {
|
||||||
|
|
||||||
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("While Test")
|
@DisplayName("While Test")
|
||||||
public void whileTest(){
|
public void whileTest() {
|
||||||
|
|
||||||
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Do While Test")
|
@DisplayName("Do While Test")
|
||||||
public void doWhileTest(){
|
public void doWhileTest() {
|
||||||
|
|
||||||
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("For Test")
|
@DisplayName("For Test")
|
||||||
public void forTest(){
|
public void forTest() {
|
||||||
|
|
||||||
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
//assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
@@ -391,26 +393,16 @@ class AstBuilderTest {
|
|||||||
//Noch nicht speziell Increment nur zum Development Testen per Debug
|
//Noch nicht speziell Increment nur zum Development Testen per Debug
|
||||||
@Test
|
@Test
|
||||||
@DisplayName("Increment Test")
|
@DisplayName("Increment Test")
|
||||||
public void incrementTest(){
|
public void incrementTest() {
|
||||||
ClassNode classNode = Helper.generateEmptyClass("TestClass");
|
ClassNode classNode = Helper.generateEmptyClass("Increment");
|
||||||
classNode.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
classNode.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
|
||||||
|
|
||||||
ProgramNode expected = new ProgramNode();
|
ProgramNode expected = new ProgramNode();
|
||||||
expected.addClass(classNode);
|
expected.addClass(classNode);
|
||||||
|
|
||||||
ASTNode actual = Helper.generateAST("src/test/resources/input/javaCases/Increment.java");
|
ASTNode actual = Helper.generateAST(directoryPath + "Increment.java");
|
||||||
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
45
src/test/java/semantic/BeginnToTAST.java
Normal file
45
src/test/java/semantic/BeginnToTAST.java
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -38,17 +38,14 @@ public class EndToTypedAstTest {
|
|||||||
|
|
||||||
CharStream codeCharStream = null;
|
CharStream codeCharStream = null;
|
||||||
try {
|
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) {
|
} catch (IOException e) {
|
||||||
throw new RuntimeException(e);
|
throw new RuntimeException(e);
|
||||||
}
|
}
|
||||||
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
|
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
|
||||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||||
|
|
||||||
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||||
ParseTree parseTree = parser.program(); // parse the input
|
ParseTree parseTree = parser.program();
|
||||||
|
|
||||||
/* ------------------------- AST builder -> AST ------------------------- */
|
|
||||||
ASTBuilder astBuilder = new ASTBuilder();
|
ASTBuilder astBuilder = new ASTBuilder();
|
||||||
ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree);
|
ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree);
|
||||||
|
|
||||||
@@ -115,7 +112,7 @@ public class EndToTypedAstTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void featureTest() {
|
public void featureTest() {
|
||||||
String directoryPath = "src/test/resources/input/typedAstFeaturesTests";
|
String directoryPath = "src/test/resources/input/typedAstFeatureTests";
|
||||||
File folder = new File(directoryPath);
|
File folder = new File(directoryPath);
|
||||||
if (folder.isDirectory()) {
|
if (folder.isDirectory()) {
|
||||||
File[] files = folder.listFiles((_, name) -> name.endsWith(".java"));
|
File[] files = folder.listFiles((_, name) -> name.endsWith(".java"));
|
||||||
|
@@ -2,112 +2,4 @@ package semantic;
|
|||||||
|
|
||||||
public class SemanticTest {
|
public class SemanticTest {
|
||||||
|
|
||||||
|
}
|
||||||
public void test(){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void test(int a, boolean b){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public void test(boolean b, int a){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// @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);
|
|
||||||
//}
|
|
||||||
}
|
|
@@ -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;
|
char c;
|
||||||
|
|
||||||
// Konstruktor
|
// Konstruktor
|
||||||
AllFeaturesClassExample(int a, boolean b, char c) {
|
public AllFeaturesClassExample(int a, boolean b, char c) {
|
||||||
this.a = a;
|
this.a = a;
|
||||||
this.b = b;
|
this.b = b;
|
||||||
this.c = c;
|
this.c = c;
|
||||||
@@ -14,47 +14,54 @@ public class AllFeaturesClassExample {
|
|||||||
void controlStructures() {
|
void controlStructures() {
|
||||||
// if-else Anweisung
|
// if-else Anweisung
|
||||||
if (a > 10) {
|
if (a > 10) {
|
||||||
// System.out.println("a ist größer als 10");
|
|
||||||
} else {
|
} else {
|
||||||
// System.out.println("a ist nicht größer als 10");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// while Schleife
|
// while Schleife
|
||||||
while (a > 0) {
|
while (a > 0) {
|
||||||
// System.out.println("a ist " + a);
|
|
||||||
a--;
|
a--;
|
||||||
}
|
}
|
||||||
|
int c = 0;
|
||||||
// for Schleife
|
// for Schleife
|
||||||
for (int i = 0; i < 5; i++) {
|
for (int i = 0; i < 5; i++) {
|
||||||
// System.out.println("for Schleife Iteration: " + i);
|
c++;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Methode zur Arbeit mit logischen Operatoren
|
// Methode zur Arbeit mit logischen Operatoren
|
||||||
void logicalOperations() {
|
void logicalOperations() {
|
||||||
// Logische UND-Operation
|
// Logische UND-Operation
|
||||||
if (b && a > 5) {
|
if (b && a > 5) {
|
||||||
// System.out.println("a ist größer als 5 und b ist wahr");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Logische ODER-Operation
|
// Logische ODER-Operation
|
||||||
if (b || a < 5) {
|
if (b || a < 5) {
|
||||||
// System.out.println("b ist wahr oder a ist kleiner als 5");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void mathOperations() {
|
int add(int a, int b) {
|
||||||
// Addition
|
return a + b;
|
||||||
int sum = a + 5;
|
}
|
||||||
// Subtraktion
|
|
||||||
int difference = a - 5;
|
int subtract(int a, int b) {
|
||||||
// Multiplikation
|
return a - b;
|
||||||
int product = a * 5;
|
}
|
||||||
// Division
|
|
||||||
int quotient = a / 5;
|
int multiply(int a, int b) {
|
||||||
// Modulo
|
return a * b;
|
||||||
int remainder = a % 5;
|
}
|
||||||
|
|
||||||
|
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) {
|
public static void main(String[] args) {
|
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 testMehtod3(){
|
|
||||||
TestClass testClass1 = new TestClass();
|
|
||||||
return testClass1.testClass.testMethod1();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,3 +0,0 @@
|
|||||||
class TestClass{
|
|
||||||
void test(){}
|
|
||||||
}
|
|
@@ -1,30 +0,0 @@
|
|||||||
class TestClass{
|
|
||||||
|
|
||||||
boolean true(){
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean false(){
|
|
||||||
return false();
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean trueAndTrue(){
|
|
||||||
return true && true;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean trueAndFalse(){
|
|
||||||
return true && true;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean falseAndFalse(){
|
|
||||||
return false && false;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean trueOrFalse(){
|
|
||||||
return true || false;
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean falseOrFalse(){
|
|
||||||
return false || false;
|
|
||||||
}
|
|
||||||
}
|
|
5
src/test/resources/input/johnsTests/FieldTests.java
Normal file
5
src/test/resources/input/johnsTests/FieldTests.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
public class Test{
|
||||||
|
|
||||||
|
int a = 10;
|
||||||
|
|
||||||
|
}
|
@@ -1,8 +1,8 @@
|
|||||||
class TestClass{
|
class Char {
|
||||||
|
|
||||||
char a;
|
char a;
|
||||||
|
|
||||||
public TestClass(char a){
|
public Char(char a){
|
||||||
this.a = testMethod(a);
|
this.a = testMethod(a);
|
||||||
}
|
}
|
||||||
|
|
@@ -3,6 +3,6 @@
|
|||||||
Mutliple Line Comment. Ignore
|
Mutliple Line Comment. Ignore
|
||||||
|
|
||||||
*/
|
*/
|
||||||
class TestClass{
|
class Comments{
|
||||||
private int a; // Ignore
|
private int a; // Ignore
|
||||||
}
|
}
|
@@ -1,8 +1,8 @@
|
|||||||
class TestClass {
|
public class ConstructorMethodCall {
|
||||||
|
|
||||||
int a;
|
int a;
|
||||||
|
|
||||||
public TestClass(){
|
public ConstructorMethodCall(){
|
||||||
this.a = testMethod();
|
this.a = testMethod();
|
||||||
}
|
}
|
||||||
|
|
@@ -1,8 +1,8 @@
|
|||||||
class TestClass {
|
class ConstructorMethodCallParameters {
|
||||||
|
|
||||||
int a;
|
int a;
|
||||||
|
|
||||||
public TestClass(int a){
|
public ConstructorMethodCallParameters(int a){
|
||||||
this.a = testMethod(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,10 +1,10 @@
|
|||||||
class TestClass{
|
class DoWhile{
|
||||||
|
|
||||||
public TestClass(){
|
public DoWhile(){
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
do{
|
do{
|
||||||
i++
|
i++;
|
||||||
}while(i < 10);
|
}while(i < 10);
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -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;
|
||||||
|
}
|
@@ -0,0 +1,3 @@
|
|||||||
|
public class FieldWithAccessModifier {
|
||||||
|
public int a;
|
||||||
|
}
|
@@ -1,6 +1,6 @@
|
|||||||
class TestClass{
|
class For{
|
||||||
|
|
||||||
public TestClass(){
|
public For(){
|
||||||
for(int i = 0; i < 10; i++){
|
for(int i = 0; i < 10; i++){
|
||||||
int a;
|
int a;
|
||||||
}
|
}
|
@@ -5,7 +5,7 @@ public class Increment {
|
|||||||
public void increment(int p) {
|
public void increment(int p) {
|
||||||
test = p++;
|
test = p++;
|
||||||
|
|
||||||
for(int i = 1; i<=10, i++) {
|
for(int i = 1; i<=10; i++) {
|
||||||
int a = 5;
|
int a = 5;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
class TestClass{
|
class MainMethod{
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
}
|
}
|
@@ -0,0 +1,3 @@
|
|||||||
|
class MultipleClasses {}
|
||||||
|
|
||||||
|
class TestClass2{}
|
8
src/test/resources/input/singleFeatureTests/Null.java
Normal file
8
src/test/resources/input/singleFeatureTests/Null.java
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
class Null{
|
||||||
|
|
||||||
|
int a;
|
||||||
|
|
||||||
|
public Null(){
|
||||||
|
// this.a = null;
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,18 @@
|
|||||||
|
class SelfReference{
|
||||||
|
|
||||||
|
SelfReference selfReference;
|
||||||
|
|
||||||
|
int testMethod1() {
|
||||||
|
return this.testMethod2();
|
||||||
|
}
|
||||||
|
|
||||||
|
int testMethod2() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int testMehtod3(){
|
||||||
|
SelfReference selfReference1 = new SelfReference();
|
||||||
|
return selfReference1.selfReference.testMethod1();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@@ -1,8 +1,8 @@
|
|||||||
class TestClass{
|
class ThisDot {
|
||||||
|
|
||||||
public int a;
|
public int a;
|
||||||
|
|
||||||
public TestClass() {
|
public ThisDot() {
|
||||||
this.a = 1;
|
this.a = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
class TestClass{
|
class VariableCalculationTest{
|
||||||
|
|
||||||
int aPlusB(int a, int b){
|
int aPlusB(int a, int b){
|
||||||
return a + b;
|
return a + b;
|
@@ -0,0 +1,30 @@
|
|||||||
|
class VariableCompareTest{
|
||||||
|
|
||||||
|
boolean trueMethod(){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean falseMethod(){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean trueAndTrue(){
|
||||||
|
return trueMethod() && trueMethod();
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean trueAndFalse(){
|
||||||
|
return trueMethod() && falseMethod();
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean falseAndFalse(){
|
||||||
|
return falseMethod() && falseMethod();
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean trueOrFalse(){
|
||||||
|
return trueMethod() || falseMethod();
|
||||||
|
}
|
||||||
|
|
||||||
|
boolean falseOrFalse(){
|
||||||
|
return falseMethod() || falseMethod();
|
||||||
|
}
|
||||||
|
}
|
@@ -0,0 +1,3 @@
|
|||||||
|
class VoidMethod{
|
||||||
|
void test(){}
|
||||||
|
}
|
@@ -1,6 +1,6 @@
|
|||||||
class TestClass{
|
class While{
|
||||||
|
|
||||||
public TestClass(){
|
public While(){
|
||||||
int i = 10;
|
int i = 10;
|
||||||
|
|
||||||
while ( i > 0){
|
while ( i > 0){
|
@@ -0,0 +1,12 @@
|
|||||||
|
// @expected: NotDeclaredException
|
||||||
|
public class Test {
|
||||||
|
|
||||||
|
public House1 h;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class House {
|
||||||
|
|
||||||
|
|
||||||
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
public class Test {
|
public class CallMethodFromObjekt {
|
||||||
|
|
||||||
public int firstInt;
|
public int firstInt;
|
||||||
public Car ca;
|
public Car ca;
|
||||||
@@ -7,7 +7,6 @@ public class Test {
|
|||||||
return ca.getSpeed();
|
return ca.getSpeed();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Car{
|
public class Car{
|
||||||
|
|
||||||
@@ -17,4 +16,5 @@ public class Car{
|
|||||||
return speed;
|
return speed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
public class Test{
|
public class CorrectMemberAccess{
|
||||||
|
|
||||||
public Car c;
|
public Car c;
|
||||||
|
|
||||||
@@ -6,9 +6,8 @@ public class Test{
|
|||||||
return c.getSpeed();
|
return c.getSpeed();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Car{
|
private class Car{
|
||||||
|
|
||||||
private int speed;
|
private int speed;
|
||||||
|
|
||||||
@@ -16,4 +15,5 @@ public class Car{
|
|||||||
return speed;
|
return speed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
public class Test{
|
public class CorrectMethodParameter{
|
||||||
|
|
||||||
public void test(int x){
|
public void test(int x){
|
||||||
|
|
@@ -1,4 +1,4 @@
|
|||||||
public class Test{
|
public class CorrectNonCalcTest{
|
||||||
|
|
||||||
public void test(boolean b){
|
public void test(boolean b){
|
||||||
if(b == true){
|
if(b == true){
|
@@ -1,4 +1,4 @@
|
|||||||
public class Example {
|
public class CorrectReturnType {
|
||||||
|
|
||||||
public static int testMethod(int x){
|
public static int testMethod(int x){
|
||||||
return x;
|
return x;
|
@@ -0,0 +1,21 @@
|
|||||||
|
public class CorrectTest {
|
||||||
|
int a;
|
||||||
|
boolean b;
|
||||||
|
char c;
|
||||||
|
|
||||||
|
public void controlStructures(int adf, boolean bool) {
|
||||||
|
if (a > (10 + 8)) {
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
while (a > adf) {
|
||||||
|
a--;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
public class AllFeaturesClassExample {
|
public class FullTest {
|
||||||
int a;
|
int a;
|
||||||
boolean b;
|
boolean b;
|
||||||
char c;
|
char c;
|
||||||
@@ -18,7 +18,7 @@ public class AllFeaturesClassExample {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// void logicalOperations() {
|
// void logicalOperations() {
|
||||||
// // Logische UND-Operation
|
// // Logische UND-Operation
|
||||||
@@ -62,3 +62,4 @@ public class Car {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
@@ -1,4 +1,4 @@
|
|||||||
public class Car{
|
public class IfExpressionBoolean{
|
||||||
|
|
||||||
public void test(boolean boo){
|
public void test(boolean boo){
|
||||||
|
|
@@ -1,4 +1,4 @@
|
|||||||
public class Car{
|
public class IfReturn{
|
||||||
|
|
||||||
public int getSpeed(boolean bool, int a, int b){
|
public int getSpeed(boolean bool, int a, int b){
|
||||||
|
|
@@ -1,4 +1,4 @@
|
|||||||
public class Test{
|
public class SelectRightOverloadedMethod{
|
||||||
|
|
||||||
public int i;
|
public int i;
|
||||||
public boolean b;
|
public boolean b;
|
@@ -1,4 +1,4 @@
|
|||||||
public class Car{
|
public class ThisDotMethod{
|
||||||
|
|
||||||
private int speed;
|
private int speed;
|
||||||
|
|
@@ -1,4 +1,4 @@
|
|||||||
public class Car{
|
public class VoidReturnTypeIF{
|
||||||
|
|
||||||
private int speed;
|
private int speed;
|
||||||
|
|
@@ -1,38 +0,0 @@
|
|||||||
public class AllFeaturesClassExample {
|
|
||||||
int a;
|
|
||||||
boolean b;
|
|
||||||
char c;
|
|
||||||
|
|
||||||
public void controlStructures(int adf, boolean bool) {
|
|
||||||
if (a > (10 + 8)) {
|
|
||||||
} else {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
while (a > adf) {
|
|
||||||
a--;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < 5; i++) {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
// 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");
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// public static void main(String[] args) {
|
|
||||||
// AllFeaturesClassExample obj = new AllFeaturesClassExample(12, true, 'a');
|
|
||||||
// obj.controlStructures();
|
|
||||||
// }
|
|
||||||
}
|
|
70
src/test/resources/trees/correctRefType.json
Normal file
70
src/test/resources/trees/correctRefType.json
Normal file
@@ -0,0 +1,70 @@
|
|||||||
|
{
|
||||||
|
"classes": [
|
||||||
|
{
|
||||||
|
"identifier": "testClass1",
|
||||||
|
"accessType": {
|
||||||
|
"enumAccessTypeNode": "PUBLIC"
|
||||||
|
},
|
||||||
|
"members": [
|
||||||
|
{
|
||||||
|
"@type": "Field",
|
||||||
|
"accessTypeNode": {
|
||||||
|
"enumAccessTypeNode": "PUBLIC"
|
||||||
|
},
|
||||||
|
"type": {
|
||||||
|
"@type": "Base",
|
||||||
|
"enumType": "INT"
|
||||||
|
},
|
||||||
|
"identifier": "testVar1"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"@type": "Method",
|
||||||
|
"visibility": {
|
||||||
|
"enumAccessTypeNode": "PUBLIC"
|
||||||
|
},
|
||||||
|
"type": {
|
||||||
|
"@type": "Base",
|
||||||
|
"enumType": "INT"
|
||||||
|
},
|
||||||
|
"identifier": "testMethod",
|
||||||
|
"parameters": {
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": {
|
||||||
|
"@type": "Base",
|
||||||
|
"enumType": "INT"
|
||||||
|
},
|
||||||
|
"identifier": "param1"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"statements": [
|
||||||
|
{
|
||||||
|
"@type": "Assignment",
|
||||||
|
"expressionLeft": {
|
||||||
|
"@type": "InstVar",
|
||||||
|
"identifier": "testVar1",
|
||||||
|
"expression": {
|
||||||
|
"@type": "This",
|
||||||
|
"type": {
|
||||||
|
"@type": "Reference",
|
||||||
|
"identifier": "testClass1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"type": null
|
||||||
|
},
|
||||||
|
"expressionRight": {
|
||||||
|
"@type": "Literal",
|
||||||
|
"type": {
|
||||||
|
"@type": "Base",
|
||||||
|
"enumType": "INT"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"hasConstructor": false
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
133
src/test/resources/trees/refTypeMismatch.json
Normal file
133
src/test/resources/trees/refTypeMismatch.json
Normal file
@@ -0,0 +1,133 @@
|
|||||||
|
{
|
||||||
|
"classes": [
|
||||||
|
{
|
||||||
|
"identifier": "testClass1",
|
||||||
|
"accessType": {
|
||||||
|
"enumAccessTypeNode": "PUBLIC"
|
||||||
|
},
|
||||||
|
"members": [
|
||||||
|
{
|
||||||
|
"@type": "Field",
|
||||||
|
"accessTypeNode": {
|
||||||
|
"enumAccessTypeNode": "PUBLIC"
|
||||||
|
},
|
||||||
|
"type": {
|
||||||
|
"@type": "Base",
|
||||||
|
"enumType": "INT"
|
||||||
|
},
|
||||||
|
"identifier": "testVar1"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"@type": "Method",
|
||||||
|
"visibility": {
|
||||||
|
"enumAccessTypeNode": "PUBLIC"
|
||||||
|
},
|
||||||
|
"type": {
|
||||||
|
"@type": "Base",
|
||||||
|
"enumType": "INT"
|
||||||
|
},
|
||||||
|
"identifier": "testMethod",
|
||||||
|
"parameters": {
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": {
|
||||||
|
"@type": "Base",
|
||||||
|
"enumType": "INT"
|
||||||
|
},
|
||||||
|
"identifier": "param1"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"statements": [
|
||||||
|
{
|
||||||
|
"@type": "Assignment",
|
||||||
|
"expressionLeft": {
|
||||||
|
"@type": "InstVar",
|
||||||
|
"identifier": "testVar1",
|
||||||
|
"expression": {
|
||||||
|
"@type": "This",
|
||||||
|
"type": {
|
||||||
|
"@type": "Reference",
|
||||||
|
"identifier": "testClass1"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"type": null
|
||||||
|
},
|
||||||
|
"expressionRight": {
|
||||||
|
"@type": "Literal",
|
||||||
|
"type": {
|
||||||
|
"@type": "Base",
|
||||||
|
"enumType": "BOOLEAN"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"hasConstructor": false,
|
||||||
|
"methods": [
|
||||||
|
{
|
||||||
|
"@type": "Method",
|
||||||
|
"visibility": {
|
||||||
|
"enumAccessTypeNode": "PUBLIC"
|
||||||
|
},
|
||||||
|
"type": {
|
||||||
|
"@type": "Base",
|
||||||
|
"enumType": "INT"
|
||||||
|
},
|
||||||
|
"identifier": "testMethod",
|
||||||
|
"parameters": {
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"type": {
|
||||||
|
"@type": "Base",
|
||||||
|
"enumType": "INT"
|
||||||
|
},
|
||||||
|
"identifier": "param1"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"statements": [
|
||||||
|
{
|
||||||
|
"@type": "Assignment",
|
||||||
|
"expressionLeft": {
|
||||||
|
"@type": "InstVar",
|
||||||
|
"identifier": "testVar",
|
||||||
|
"expression": {
|
||||||
|
"@type": "InstVar",
|
||||||
|
"identifier": "testVar",
|
||||||
|
"expression": {
|
||||||
|
"@type": "This",
|
||||||
|
"type": {
|
||||||
|
"@type": "Reference",
|
||||||
|
"identifier": "testClass2"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"type": null
|
||||||
|
},
|
||||||
|
"type": null
|
||||||
|
},
|
||||||
|
"expressionRight": {
|
||||||
|
"@type": "Literal",
|
||||||
|
"type": null
|
||||||
|
},
|
||||||
|
"type": null
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"@type": "VariableDeclaration",
|
||||||
|
"type": {
|
||||||
|
"@type": "Base",
|
||||||
|
"enumType": "CHAR"
|
||||||
|
},
|
||||||
|
"identifier": "objectVar",
|
||||||
|
"expression": {
|
||||||
|
"@type": "Literal",
|
||||||
|
"type": null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
1
src/test/resources/trees/test.json
Normal file
1
src/test/resources/trees/test.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"classes":[{"identifier":"testClass","accessType":{"enumAccessTypeNode":"PUBLIC"},"members":[{"@type":"Field","accessTypeNode":{"enumAccessTypeNode":"PUBLIC"},"type":{"@type":"Base","enumType":"INT"},"identifier":"testVar1"},{"@type":"Field","accessTypeNode":{"enumAccessTypeNode":"PUBLIC"},"type":{"@type":"Base","enumType":"INT"},"identifier":"objectVar"},{"@type":"Method","visibility":{"enumAccessTypeNode":"PUBLIC"},"type":{"@type":"Base","enumType":"INT"},"identifier":"testVar2","parameters":{"parameters":[{"type":{"@type":"Base","enumType":"INT"},"identifier":"param1"}]},"statements":[{"@type":"Assignment","expressionLeft":{"@type":"InstVar","identifier":"objectVar","expression":{"@type":"This","type":{"@type":"Reference","identifier":"testClass"}},"type":null},"expressionRight":{"@type":"Literal","type":{"@type":"Base","enumType":"INT"}}}]}],"hasConstructor":false,"methods":[{"@type":"Method","visibility":{"enumAccessTypeNode":"PUBLIC"},"type":{"@type":"Base","enumType":"INT"},"identifier":"testVar2","parameters":{"parameters":[{"type":{"@type":"Base","enumType":"INT"},"identifier":"param1"}]},"statements":[{"@type":"Assignment","expressionLeft":{"@type":"InstVar","identifier":"objectVar","expression":{"@type":"This","type":{"@type":"Reference","identifier":"testClass"}},"type":null},"expressionRight":{"@type":"Literal","type":{"@type":"Base","enumType":"INT"}}}]}]}]}
|
Reference in New Issue
Block a user