45 Commits

Author SHA1 Message Date
f673612f54 Merge pull request 'Endabgabe' (#20) from Endabgabe into main
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
Reviewed-on: #20
Reviewed-by: i22030 <i22030@hb.dhbw-stuttgart.de>
2024-07-05 11:59:46 +00:00
Maximilian Stahl
9760e39a29 Test updated
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled
2024-07-05 13:57:07 +02:00
Maximilian Stahl
93e17e5415 Merge remote-tracking branch 'origin/Endabgabe' into Endabgabe
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-05 13:51:29 +02:00
Maximilian Stahl
b151edc2fd Updated README.md 2024-07-05 13:51:14 +02:00
Lucas
c0d8ab8399 Merge remote-tracking branch 'origin/Endabgabe' into Endabgabe
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-05 13:50:11 +02:00
Lucas
f50bb7efd7 refactoring 2024-07-05 13:49:43 +02:00
Maximilian Stahl
51a390752b Merge remote-tracking branch 'origin/Endabgabe' into Endabgabe
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-05 13:46:31 +02:00
Maximilian Stahl
3114064038 Fixed Generation 2024-07-05 13:46:18 +02:00
Lucas
745db43b34 make and input
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-05 13:44:11 +02:00
Lucas
e0286e0840 Merge branch 'Endabgabe' of https://gitea.hb.dhbw-stuttgart.de/i22005/JavaCompiler into Endabgabe
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-05 13:25:01 +02:00
Lucas
ebef717141 refactoring 2024-07-05 13:24:48 +02:00
Maximilian Stahl
3de54afa93 Merge remote-tracking branch 'origin/Endabgabe' into Endabgabe
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
# Conflicts:
#	src/main/java/bytecode/MethodCodeGen.java
2024-07-05 13:23:57 +02:00
Maximilian Stahl
30d3cfa44a Fix and Final Test File 2024-07-05 13:23:09 +02:00
efa1a21655 Fix for MethodCallNode
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-05 06:30:55 -04:00
3fb11e5d7e Fixed methotcall with params
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-05 06:20:49 -04:00
Bruder John
c6e61defce Revert "Some small changes for SemanticAnalyzer"
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
This reverts commit dbde4e8047.
2024-07-05 09:34:06 +02:00
Bruder John
dbde4e8047 Some small changes for SemanticAnalyzer
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-05 09:13:27 +02:00
5a12d61623 Changed testfiles
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 21:59:37 -04:00
f5bccab651 Small change
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 21:40:21 -04:00
0de5c3e993 Fixed MethodCall
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 21:03:52 -04:00
3628a0a4d8 Merge branch 'Endabgabe' of https://gitea.hb.dhbw-stuttgart.de/i22005/NichtHaskell2.0 into Endabgabe
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 20:04:23 -04:00
36f0683240 Fixed crement assign 2024-07-04 20:04:10 -04:00
Purplumbi504
97aadb9ba8 Merge remote-tracking branch 'origin/Endabgabe' into Endabgabe
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 23:41:17 +02:00
Purplumbi504
e5dcbb8f99 Fixing AstBuilder For
pp script
2024-07-04 23:41:04 +02:00
e96f30fe19 Updated Loop Tests
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 23:39:54 +02:00
64b15af6ef Null Test Deleted
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 23:15:22 +02:00
4ff6854094 NonCalculation Type added
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 23:14:00 +02:00
0a9cc7655a Changed For Structure
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 23:07:54 +02:00
7e66b5b8e3 Fixed Target = null
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 22:53:33 +02:00
d835a98e6f Some fixes
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 16:41:44 -04:00
240913d422 Fixed MemberAccess Type
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 22:38:39 +02:00
4bee432294 Merge remote-tracking branch 'origin/Endabgabe' into Endabgabe
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 22:24:59 +02:00
0cf4715782 Fixes of Constructor 2024-07-04 22:24:50 +02:00
Purplumbi504
3c0f7e857a Adding comments to ASTBuilder und ANTL Grammar
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 21:57:53 +02:00
Purplumbi504
2b7e0d0d42 Changing location of Makefile
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 21:13:38 +02:00
Purplumbi504
bd61a0e595 Merge remote-tracking branch 'origin/Endabgabe' into Endabgabe
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 21:11:35 +02:00
Purplumbi504
24407a5c1c Revert test case directory: singleFeatureTests 2024-07-04 21:11:21 +02:00
195440e9d9 Added SemanticAnalyzer ElseIfStatement
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 20:49:54 +02:00
6fa57cf319 Type Error Change
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 20:36:36 +02:00
1de6add080 Fixe Type Error
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 20:23:43 +02:00
c315966219 Merge remote-tracking branch 'origin/Endabgabe' into Endabgabe
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 19:45:09 +02:00
c2c4974c76 More Abgabe Tests working 2024-07-04 19:44:59 +02:00
Purplumbi504
85e0cf3807 Revert test case directory: singleFeatureTests
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 19:10:21 +02:00
Purplumbi504
6f3fb02666 Fixing test case directory: singleFeatureTests
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 19:03:44 +02:00
Purplumbi504
435697053a Fixing ReflectionTests
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
2024-07-04 18:57:36 +02:00
35 changed files with 883 additions and 2645 deletions

37
Makefile Normal file
View File

@@ -0,0 +1,37 @@
### IntelliJs play buttons do not work. Run in "src/test" folder with "make" command to run all
### Or run only parts with "make compile-javac", "make clean" etc.
all: compile-javac compile-miniCompiler
compile-javac:
#javac -d src/test/resources/output/javac src/test/resources/input/CompilerInput.java
compile-miniCompiler:
cd ../.. ; mvn -DskipTests install
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 src/main/resources/output/CompilerInput.class src/test/resources/output/miniCompiler
test-miniCompiler:
# move the compiled class to the test/main folder
mv src/main/resources/output/CompilerInput.class src/test/java/main/
# compile the test class
javac src/test/java/main/EndToEndTester.java
# run the test class
java -cp src/test/java/main EndToEndTester
clean:
# clean main output folders
rm -f src/main/resources/output/*.class
rm -f src/main/resources/output/*.jar
# clean resources output folders
rm -f src/test/resources/output/javac/*.class
rm -f src/test/resources/output/miniCompiler/*.class
rm -f src/test/resources/output/miniCompiler/*.jar
# clean logs
rm -f src/main/resources/logs/*
# clean test/java/main folders from .class files for End-to-End tests
rm -f src/test/java/main/*.class
# clean javac output from every folder
rm -f src/test/resources/input/*/*.class
# clean test results from maven surefire plugin
rm -f ../../target/surefire-reports/*.txt

BIN
Parser_Dokumentation.pdf Normal file

Binary file not shown.

View File

@@ -15,7 +15,6 @@ This project aims to provide a simplified version of the Java compiler, focusing
- `while` ... ; - `while` ... ;
- `do` ... `while`; - `do` ... `while`;
- `for`; - `for`;
- `switch` ... `case` ... ;
- **Comments**: - **Comments**:
- Single line: `// comment` - Single line: `// comment`
- Multi-line: `/* comment */` - Multi-line: `/* comment */`
@@ -56,7 +55,7 @@ test/
## Distribution of the realisation ## Distribution of the realisation
### i22030 & i22035 ### i22030 & i22035
Ausführliche Beschreibung der Parser Umsetzung: ![Parserumsetzung](Parser_Dokumentation.pdf)
Parser: Parser:
- Grammar -> (src/main/java/parser/grammar) - Grammar -> (src/main/java/parser/grammar)
- Scanner - Scanner
@@ -95,14 +94,6 @@ Tests and execution:
- Typing and Type checking -> (src/test/java/semantic/EndToTypedAstTest) - Typing and Type checking -> (src/test/java/semantic/EndToTypedAstTest)
## Used Tools
- [Maven 4.0](https://maven.apache.org/index.html)
- Used for automating the build process and managing dependencies.
- [ANTLR4 v.13.1](https://www.antlr.org/)
- Used to parse the input Java code into the Abstract Syntax Tree.
## Used Tools ## Used Tools
- [Maven 4.0](https://maven.apache.org/index.html) - [Maven 4.0](https://maven.apache.org/index.html)
@@ -140,10 +131,3 @@ Example (jar needs to be in the target directory)
``` ```
DgenClass=true DgenClass=true
``` ```
## How to run tests
```bash
mvn test
```
Or start them manually in your IDE

BIN
ast.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 MiB

View File

@@ -11,13 +11,8 @@ import java.util.List;
import java.util.Objects; import java.util.Objects;
public class ConstructorNode extends MethodNode implements Visitable { public class ConstructorNode extends MethodNode implements Visitable {
public AccessModifierNode accessType;
public String identifier;
public List<ParameterNode> parameters = new ArrayList<>();
public BlockNode block;
public ConstructorNode(String accessType, String identifier, BlockNode block) { public ConstructorNode(String accessType, String identifier, BlockNode block) {
this.accessType = new AccessModifierNode(accessType); this.accesModifier = new AccessModifierNode(accessType);
this.identifier = identifier; this.identifier = identifier;
this.block = block; this.block = block;
} }

View File

@@ -4,5 +4,6 @@ public enum TypeEnum {
VOID, VOID,
INT, INT,
CHAR, CHAR,
BOOL BOOL,
NULL
} }

View File

@@ -10,6 +10,7 @@ import ast.members.MainMethodNode;
import ast.members.MethodNode; import ast.members.MethodNode;
import ast.parameters.ParameterNode; import ast.parameters.ParameterNode;
import ast.statementexpressions.AssignNode; import ast.statementexpressions.AssignNode;
import ast.statementexpressions.AssignableNode;
import ast.statementexpressions.NewDeclarationNode; import ast.statementexpressions.NewDeclarationNode;
import ast.statementexpressions.crementexpressions.CrementType; import ast.statementexpressions.crementexpressions.CrementType;
import ast.statementexpressions.crementexpressions.DecrementNode; import ast.statementexpressions.crementexpressions.DecrementNode;
@@ -53,7 +54,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override @Override
public void visit(ConstructorNode constructorNode) { public void visit(ConstructorNode constructorNode) {
methodVisitor = methodVisitor =
classWriter.visitMethod(mapper.mapAccessTypeToOpcode(constructorNode.accessType), classWriter.visitMethod(mapper.mapAccessTypeToOpcode(constructorNode.accesModifier),
"<init>", "<init>",
mapper.generateMethodDescriptor(new BaseType(TypeEnum.VOID), constructorNode.parameters), mapper.generateMethodDescriptor(new BaseType(TypeEnum.VOID), constructorNode.parameters),
null, null,
@@ -135,12 +136,12 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override @Override
public void visit(CalculationNode calculationNode) { public void visit(CalculationNode calculationNode) {
if (calculationNode.dotExpression != null) {
calculationNode.dotExpression.accept(this);
}
if (calculationNode.calculationExpression != null) { if (calculationNode.calculationExpression != null) {
calculationNode.calculationExpression.accept(this); calculationNode.calculationExpression.accept(this);
} }
if (calculationNode.dotExpression != null) {
calculationNode.dotExpression.accept(this);
}
if (calculationNode.operator != null) { if (calculationNode.operator != null) {
switch (calculationNode.operator) { switch (calculationNode.operator) {
case PLUS: case PLUS:
@@ -304,9 +305,8 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
public void visit(UnaryNode unaryNode) { public void visit(UnaryNode unaryNode) {
if (unaryNode.thisExp != null) { if (unaryNode.thisExp != null) {
methodVisitor.visitVarInsn(ALOAD, 0); // this methodVisitor.visitVarInsn(ALOAD, 0); // this
} else if (unaryNode.identifier != null) {
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(unaryNode.identifier));
} else if (unaryNode.memberAccess != null) { } else if (unaryNode.memberAccess != null) {
unaryNode.memberAccess.setTypeNode(unaryNode.getType());
unaryNode.memberAccess.accept(this); unaryNode.memberAccess.accept(this);
} else if (unaryNode.value != null) { } else if (unaryNode.value != null) {
unaryNode.value.accept(this); unaryNode.value.accept(this);
@@ -316,6 +316,8 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
unaryNode.statement.accept(this); unaryNode.statement.accept(this);
} else if (unaryNode.expression != null) { } else if (unaryNode.expression != null) {
unaryNode.expression.accept(this); unaryNode.expression.accept(this);
} else if (unaryNode.identifier != null) {
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(unaryNode.identifier));
} }
} }
@@ -324,7 +326,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override @Override
public void visit(BlockNode blockNode) { public void visit(BlockNode blockNode) {
for(IStatementNode statementNode : blockNode.statements) { for (IStatementNode statementNode : blockNode.statements) {
statementNode.accept(this); statementNode.accept(this);
} }
} }
@@ -380,6 +382,20 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
if (localVariableDeclarationNode.expression != null) { if (localVariableDeclarationNode.expression != null) {
// Process expression // Process expression
localVariableDeclarationNode.expression.accept(this); localVariableDeclarationNode.expression.accept(this);
if (localVariableDeclarationNode.expression instanceof UnaryNode) {
UnaryNode unaryNode = (UnaryNode) localVariableDeclarationNode.expression;
if (unaryNode.statement instanceof IncrementNode) {
IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
incrementNode.assignableExpression.typeNode = unaryNode.type;
loadIncrement(incrementNode);
} else if (unaryNode.statement instanceof DecrementNode) {
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
decrementNode.assignableExpression.typeNode = unaryNode.type;
loadDecrement(decrementNode);
}
}
// add local var to list if not in list // add local var to list if not in list
if (!localVariables.contains(localVariableDeclarationNode.identifier)) { if (!localVariables.contains(localVariableDeclarationNode.identifier)) {
localVariables.add(localVariableDeclarationNode.identifier); localVariables.add(localVariableDeclarationNode.identifier);
@@ -400,24 +416,37 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override @Override
public void visit(AssignNode assignNode) { public void visit(AssignNode assignNode) {
if (assignNode.assignable.memberAccess != null) { // this / object if (assignNode.assignable.memberAccess != null) { // this / object
if (assignNode.expression instanceof IncrementNode) { assignNode.assignable.memberAccess.setTypeNode(assignNode.assignable.typeNode);
if (((IncrementNode) assignNode.expression).crementType.equals(CrementType.PREFIX)) { // ++i if (assignNode.expression instanceof UnaryNode) {
fieldOrObjectVarPrefixCrementAssign(assignNode); UnaryNode unaryNode = (UnaryNode) assignNode.expression;
} else { // i++ if (unaryNode.statement instanceof IncrementNode) {
fieldOrObjectVarSuffixCrementAssign(assignNode); IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
} if (incrementNode.crementType.equals(CrementType.PREFIX)) { // ++i
} else if (assignNode.expression instanceof DecrementNode) { incrementNode.accept(this); // crement
if (((DecrementNode) assignNode.expression).crementType.equals(CrementType.PREFIX)) { fieldOrObjectVarCrementAssign(assignNode); // assign
fieldOrObjectVarPrefixCrementAssign(assignNode); } else { // i++
} else { fieldOrObjectVarCrementAssign(assignNode); // assign
fieldOrObjectVarSuffixCrementAssign(assignNode); incrementNode.accept(this); // crement
}
} else if (unaryNode.statement instanceof DecrementNode) {
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
if (decrementNode.crementType.equals(CrementType.PREFIX)) {
decrementNode.accept(this); // crement
fieldOrObjectVarCrementAssign(assignNode); // assign
} else {
fieldOrObjectVarCrementAssign(assignNode); // assign
decrementNode.accept(this); // crement
}
} }
} else { } else {
assignFieldOrObjectVar(assignNode); assignFieldOrObjectVar(assignNode);
} }
} else { // local var } else { // local var
if (assignNode.expression instanceof IncrementNode || assignNode.expression instanceof DecrementNode) { if (assignNode.expression instanceof UnaryNode) {
localVarCrementAssign(assignNode); UnaryNode unaryNode = (UnaryNode) assignNode.expression;
if (unaryNode.statement instanceof IncrementNode || unaryNode.statement instanceof DecrementNode) {
localVarCrementAssign(assignNode);
}
} else { } else {
assignNode.expression.accept(this); assignNode.expression.accept(this);
assignLocalVar(assignNode); assignLocalVar(assignNode);
@@ -425,82 +454,40 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
} }
} }
private void localVarCrementAssign(AssignNode assignNode) { private void loadIncrement(IncrementNode incrementNode) {
if (assignNode.expression instanceof IncrementNode) { AssignableNode assignableNode = incrementNode.assignableExpression;
IncrementNode incrementNode = (IncrementNode) assignNode.expression; if (assignableNode.memberAccess != null) {
if (incrementNode.crementType.equals(CrementType.PREFIX)) { // ++i assignableNode.memberAccess.accept(this);
incrementNode.accept(this); } else {
assignLocalVar(assignNode); if (assignableNode.typeNode instanceof BaseType) {
} else { // i++ methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignableNode.identifier));
loadBeforeCrement(assignNode); } else if (assignableNode.typeNode instanceof ReferenceType) {
assignLocalVar(assignNode); methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignableNode.identifier));
incrementNode.accept(this);
}
} else if (assignNode.expression instanceof DecrementNode decrementNode) {
if (decrementNode.crementType.equals(CrementType.PREFIX)) {
decrementNode.accept(this);
assignLocalVar(assignNode);
} else {
loadBeforeCrement(assignNode);
assignLocalVar(assignNode);
decrementNode.accept(this);
} }
} }
} }
private void fieldOrObjectVarPrefixCrementAssign(AssignNode assignNode) { private void loadDecrement(DecrementNode decrementNode) {
int localVarIndex = localVariables.indexOf(assignNode.assignable.identifier); AssignableNode assignableNode = decrementNode.assignableExpression;
if(localVarIndex >= 0) { // object if (assignableNode.memberAccess != null) {
methodVisitor.visitVarInsn(ALOAD, localVarIndex); assignableNode.memberAccess.accept(this);
} else if(assignNode.assignable.memberAccess.thisExpr) { // field
methodVisitor.visitVarInsn(ALOAD, 0);
} else { } else {
localVariables.add(assignNode.assignable.identifier); if (assignableNode.typeNode instanceof BaseType) {
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier)); methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignableNode.identifier));
} else if (assignableNode.typeNode instanceof ReferenceType) {
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignableNode.identifier));
}
} }
assignNode.expression.accept(this);
methodVisitor.visitInsn(DUP_X1);
if (assignNode.expression instanceof BaseType) {
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) assignNode.expression.getType()));
} else if (assignNode.expression instanceof ReferenceType) {
ReferenceType referenceType = (ReferenceType) assignNode.expression.getType();
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), "L" + referenceType.getIdentifier() + ";");
}
}
private void fieldOrObjectVarSuffixCrementAssign(AssignNode assignNode) {
int localVarIndex = localVariables.indexOf(assignNode.assignable.identifier);
if(localVarIndex >= 0) { // object
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
} else if(assignNode.assignable.memberAccess.thisExpr) { // field
methodVisitor.visitVarInsn(ALOAD, 0);
} else {
localVariables.add(assignNode.assignable.identifier);
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
}
loadBeforeCrement(assignNode);
if (assignNode.expression instanceof BaseType) {
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) assignNode.expression.getType()));
} else if (assignNode.expression instanceof ReferenceType) {
ReferenceType referenceType = (ReferenceType) assignNode.expression.getType();
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), "L" + referenceType.getIdentifier() + ";");
}
assignNode.expression.accept(this);
} }
private void assignLocalVar(AssignNode assignNode) { private void assignLocalVar(AssignNode assignNode) {
methodVisitor.visitInsn(DUP);
if (!localVariables.contains(assignNode.assignable.identifier)) { if (!localVariables.contains(assignNode.assignable.identifier)) {
localVariables.add(assignNode.assignable.identifier); localVariables.add(assignNode.assignable.identifier);
} }
if (assignNode.expression instanceof BaseType) {
if (assignNode.expression.getType() instanceof BaseType) {
methodVisitor.visitVarInsn(ISTORE, localVariables.indexOf(assignNode.assignable.identifier)); methodVisitor.visitVarInsn(ISTORE, localVariables.indexOf(assignNode.assignable.identifier));
} else if (assignNode.expression instanceof ReferenceType) { } else if (assignNode.expression.getType() instanceof ReferenceType) {
methodVisitor.visitVarInsn(ASTORE, localVariables.indexOf(assignNode.assignable.identifier)); methodVisitor.visitVarInsn(ASTORE, localVariables.indexOf(assignNode.assignable.identifier));
} }
} }
@@ -513,46 +500,78 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitVarInsn(ALOAD, 0);
} else { } else {
localVariables.add(assignNode.assignable.identifier); localVariables.add(assignNode.assignable.identifier);
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
} }
assignNode.expression.accept(this); assignNode.expression.accept(this);
methodVisitor.visitInsn(DUP_X1);
if (assignNode.expression instanceof BaseType) { if (assignNode.expression.getType() instanceof BaseType) {
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) assignNode.expression.getType())); 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) { } else if (assignNode.expression.getType() instanceof ReferenceType) {
ReferenceType referenceType = (ReferenceType) assignNode.expression.getType(); ReferenceType referenceType = (ReferenceType) assignNode.expression.getType();
methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), "L" + referenceType.getIdentifier() + ";"); methodVisitor.visitFieldInsn(PUTFIELD, assignNode.assignable.memberAccess.identifiers.get(0), assignNode.assignable.memberAccess.identifiers.get(1), "L" + referenceType.getIdentifier() + ";");
} }
} }
private void loadBeforeCrement(AssignNode assignNode) { private void localVarCrementAssign(AssignNode assignNode) {
if(assignNode.expression instanceof IncrementNode) { UnaryNode unaryNode = (UnaryNode) assignNode.expression;
IncrementNode incrementNode = (IncrementNode) assignNode.expression; if (unaryNode.statement instanceof IncrementNode) {
if(incrementNode.assignableExpression.memberAccess != null) { IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
incrementNode.assignableExpression.memberAccess.accept(this); if (incrementNode.crementType.equals(CrementType.PREFIX)) { // ++i
} else { incrementNode.accept(this);
if(assignNode.assignable.typeNode instanceof BaseType) { incrementNode.assignableExpression.typeNode = unaryNode.getType();
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignNode.assignable.identifier)); loadIncrement(incrementNode);
} else if(assignNode.assignable.typeNode instanceof ReferenceType) { assignLocalVar(assignNode);
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier)); } else { // i++
} incrementNode.assignableExpression.typeNode = unaryNode.getType();
loadIncrement(incrementNode);
assignLocalVar(assignNode);
incrementNode.accept(this);
} }
} else if(assignNode.expression instanceof DecrementNode) { } else if (unaryNode.statement instanceof DecrementNode) {
DecrementNode decrementNode = (DecrementNode) assignNode.expression; DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
if(decrementNode.assignableExpression.memberAccess != null) { if (decrementNode.crementType.equals(CrementType.PREFIX)) {
decrementNode.assignableExpression.memberAccess.accept(this); decrementNode.accept(this);
decrementNode.assignableExpression.typeNode = unaryNode.getType();
loadDecrement(decrementNode);
assignLocalVar(assignNode);
} else { } else {
if(assignNode.assignable.typeNode instanceof BaseType) { decrementNode.assignableExpression.typeNode = unaryNode.getType();
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignNode.assignable.identifier)); loadDecrement(decrementNode);
} else if(assignNode.assignable.typeNode instanceof ReferenceType) { assignLocalVar(assignNode);
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier)); decrementNode.accept(this);
}
} }
} }
} }
private void fieldOrObjectVarCrementAssign(AssignNode assignNode) {
int localVarIndex = localVariables.indexOf(assignNode.assignable.identifier);
if (localVarIndex >= 0) { // object
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
} else if (assignNode.assignable.memberAccess.thisExpr) { // field
methodVisitor.visitVarInsn(ALOAD, 0);
} else {
localVariables.add(assignNode.assignable.identifier);
}
UnaryNode unaryNode = (UnaryNode) assignNode.expression;
if (unaryNode.statement instanceof IncrementNode) {
IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
incrementNode.assignableExpression.typeNode = unaryNode.getType();
loadIncrement((incrementNode));
} else if (unaryNode.statement instanceof DecrementNode) {
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
decrementNode.assignableExpression.typeNode = unaryNode.getType();
loadDecrement(decrementNode);
}
if (assignNode.expression.getType() 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.getType() 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(IncrementNode incrementNode) { public void visit(IncrementNode incrementNode) {
if (incrementNode.assignableExpression.memberAccess != null) { // Object var / field if (incrementNode.assignableExpression.memberAccess != null) { // Object var / field
@@ -567,7 +586,6 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
} else if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) { } else if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) {
methodVisitor.visitFieldInsn(GETFIELD, incrementNode.assignableExpression.memberAccess.identifiers.get(0), incrementNode.assignableExpression.memberAccess.identifiers.get(1), "L" + (((ReferenceType) incrementNode.assignableExpression.memberAccess.getTypeNode()).getIdentifier() + ";")); methodVisitor.visitFieldInsn(GETFIELD, incrementNode.assignableExpression.memberAccess.identifiers.get(0), incrementNode.assignableExpression.memberAccess.identifiers.get(1), "L" + (((ReferenceType) incrementNode.assignableExpression.memberAccess.getTypeNode()).getIdentifier() + ";"));
} }
methodVisitor.visitInsn(DUP);
methodVisitor.visitInsn(ICONST_1); methodVisitor.visitInsn(ICONST_1);
methodVisitor.visitInsn(IADD); methodVisitor.visitInsn(IADD);
if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) { if (incrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) {
@@ -589,7 +607,6 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
} else { // this } else { // this
methodVisitor.visitVarInsn(ALOAD, 0); methodVisitor.visitVarInsn(ALOAD, 0);
} }
methodVisitor.visitInsn(DUP);
if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) { if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof BaseType) {
methodVisitor.visitFieldInsn(GETFIELD, decrementNode.assignableExpression.memberAccess.identifiers.get(0), decrementNode.assignableExpression.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) decrementNode.assignableExpression.memberAccess.getTypeNode())); methodVisitor.visitFieldInsn(GETFIELD, decrementNode.assignableExpression.memberAccess.identifiers.get(0), decrementNode.assignableExpression.memberAccess.identifiers.get(1), mapper.getTypeChar((BaseType) decrementNode.assignableExpression.memberAccess.getTypeNode()));
} else if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) { } else if (decrementNode.assignableExpression.memberAccess.getTypeNode() instanceof ReferenceType) {
@@ -662,6 +679,21 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
} else { // Return something } else { // Return something
// Process expression // Process expression
returnNode.expression.accept(this); returnNode.expression.accept(this);
if (returnNode.expression instanceof UnaryNode) {
UnaryNode unaryNode = (UnaryNode) returnNode.expression;
if (unaryNode.statement instanceof IncrementNode) {
IncrementNode incrementNode = (IncrementNode) unaryNode.statement;
incrementNode.assignableExpression.typeNode = unaryNode.getType();
loadIncrement(incrementNode);
}
if (unaryNode.statement instanceof DecrementNode) {
DecrementNode decrementNode = (DecrementNode) unaryNode.statement;
decrementNode.assignableExpression.typeNode = unaryNode.getType();
loadDecrement(decrementNode);
}
}
// Return result of expression // Return result of expression
if (returnNode.expression.getType() instanceof BaseType) { if (returnNode.expression.getType() instanceof BaseType) {
methodVisitor.visitInsn(IRETURN); methodVisitor.visitInsn(IRETURN);
@@ -691,10 +723,22 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
@Override @Override
public void visit(MethodCallNode methodCallNode) { public void visit(MethodCallNode methodCallNode) {
List<ParameterNode> parameterNodes = new ArrayList<>(); List<ParameterNode> parameterNodes = new ArrayList<>();
int localVarIndex = -1;
if (methodCallNode.target.memberAccess.identifiers.size() > 1) {
localVarIndex = localVariables.indexOf(methodCallNode.target.memberAccess.identifiers.get(1));
}
if (localVarIndex >= 0) { // local var object
methodVisitor.visitVarInsn(ALOAD, localVarIndex);
} else { // this field
methodVisitor.visitVarInsn(ALOAD, 0);
}
for (IExpressionNode expressionNode : methodCallNode.parameters) { for (IExpressionNode expressionNode : methodCallNode.parameters) {
expressionNode.accept(this); expressionNode.accept(this);
parameterNodes.add(new ParameterNode(expressionNode.getType(), "")); parameterNodes.add(new ParameterNode(expressionNode.getType(), ""));
} }
if(methodCallNode.type == null) {
methodCallNode.type = new BaseType(TypeEnum.INT);
}
methodVisitor.visitMethodInsn(INVOKEVIRTUAL, methodCallNode.target.memberAccess.identifiers.get(0), methodCallNode.identifier, mapper.generateMethodDescriptor(methodCallNode.type, parameterNodes), false); methodVisitor.visitMethodInsn(INVOKEVIRTUAL, methodCallNode.target.memberAccess.identifiers.get(0), methodCallNode.identifier, mapper.generateMethodDescriptor(methodCallNode.type, parameterNodes), false);
} }
} }

View File

@@ -15,7 +15,6 @@ import java.io.IOException;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Optional; import java.util.Optional;
/** /**
* Start miniCompiler using make: * Start miniCompiler using make:
* <p> <code> cd .\src\test\ </code> * <p> <code> cd .\src\test\ </code>
@@ -40,6 +39,14 @@ public class Main {
System.err.println("Error reading the file: " + e.getMessage()); System.err.println("Error reading the file: " + e.getMessage());
} }
} }
else {
try {
CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/finalTest/Car.java"));
compileFile(codeCharStream, "src/test/resources/input/finalTest");
} catch (IOException e) {
System.err.println("Error reading the file: " + e.getMessage());
}
}
} }
/** /**

View File

@@ -29,6 +29,10 @@ import java.util.logging.*;
* <code>consoleHandler.setLevel(Level.OFF);</code> * <code>consoleHandler.setLevel(Level.OFF);</code>
* <code>fileHandler.setLevel(Level.ALL);</code> * <code>fileHandler.setLevel(Level.ALL);</code>
*/ */
public class MiniCompilerLogger { public class MiniCompilerLogger {
static Logger logger = Logger.getLogger("miniCompilerLogs"); static Logger logger = Logger.getLogger("miniCompilerLogs");
@@ -72,8 +76,10 @@ public class MiniCompilerLogger {
fileHandler.setFormatter(new CustomFormatter()); fileHandler.setFormatter(new CustomFormatter());
logger.addHandler(fileHandler); logger.addHandler(fileHandler);
} catch (SecurityException | IOException e) { } catch (SecurityException e) {
e.printStackTrace(); e.printStackTrace();
} catch (IOException e) {
throw new RuntimeException(e);
} }
} }
@@ -102,7 +108,7 @@ public class MiniCompilerLogger {
public static void logAST(ASTNode abstractSyntaxTree) { public static void logAST(ASTNode abstractSyntaxTree) {
// Printing the AST // Printing the AST
logger.info("-------------------- AST builder -> AST --------------------"); logger.info("-------------------- AST builder -> AST --------------------");
// logger.info("AST: " + ast.toString()); logger.info("Abstract Syntax Tree generated, Startnode:");
logAST(abstractSyntaxTree, 0); logAST(abstractSyntaxTree, 0);
logger.info("\n"); logger.info("\n");
} }
@@ -110,14 +116,15 @@ public class MiniCompilerLogger {
public static void logSemanticAnalyzer(ASTNode typedAst) { public static void logSemanticAnalyzer(ASTNode typedAst) {
// Printing the typed AST // Printing the typed AST
logger.info("-------------------- Semantic Analyzer -> typed AST --------------------"); logger.info("-------------------- Semantic Analyzer -> typed AST --------------------");
logAST(typedAst, 0); // logAST(typedAst, 0);
logger.info("Typed Abstract Syntax Tree generated without errors");
logger.info("\n"); logger.info("\n");
} }
public static void logBytecodeGenerator() { public static void logBytecodeGenerator() {
// Printing the bytecode // Printing the bytecode
logger.info("-------------------- Bytecode Generator -> Bytecode --------------------"); logger.info("-------------------- Bytecode Generator -> Bytecode --------------------");
logger.info("Bytecode generated without errors."); logger.info("Bytecode generated without errors");
logger.info("\n"); logger.info("\n");
} }
@@ -164,7 +171,6 @@ public class MiniCompilerLogger {
} }
} }
// TODO: Fix this method
public static void logAST(ASTNode abstractSyntaxTree, int indent) { public static void logAST(ASTNode abstractSyntaxTree, int indent) {
if (abstractSyntaxTree == null) { if (abstractSyntaxTree == null) {
logger.severe("AST is null !!!"); logger.severe("AST is null !!!");
@@ -172,9 +178,9 @@ public class MiniCompilerLogger {
} }
String indentString = " ".repeat(indent * 2); String indentString = " ".repeat(indent * 2);
logger.info(indentString + abstractSyntaxTree.getClass()); logger.info(indentString + abstractSyntaxTree.getClass());
//for (ASTNode child : abstractSyntaxTree.getChildren()) { // for (ASTNode child : abstractSyntaxTree.getChildren()) {
// logAST(child, indent + 1); // logAST(child, indent + 1);
// } // }
} }
} }

View File

@@ -30,7 +30,18 @@ import parser.generated.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
/**
* ASTBuilder is a visitor that converts the parse tree into an abstract syntax tree (AST).
*/
public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> { public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
/**
* Visits a program node and creates a ProgramNode.
* It iterates through all class declarations in the context and adds them to the ProgramNode.
*
* @param ctx the program context
* @return the AST node for the program
*/
@Override @Override
public ASTNode visitProgram(SimpleJavaParser.ProgramContext ctx) { public ASTNode visitProgram(SimpleJavaParser.ProgramContext ctx) {
ProgramNode program = new ProgramNode(); ProgramNode program = new ProgramNode();
@@ -40,6 +51,14 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return program; return program;
} }
/**
* Visits a class declaration and creates a ClassNode.
* It checks for an access modifier, processes member declarations, and ensures the class has a constructor.
*
* @param ctx the class declaration context
* @return the AST node for the class
*/
@Override @Override
public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) { public ASTNode visitClassDeclaration(SimpleJavaParser.ClassDeclarationContext ctx) {
ClassNode classNode; ClassNode classNode;
@@ -71,6 +90,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return classNode; return classNode;
} }
/**
* Visits a constructor declaration and creates a ConstructorNode.
* It processes access modifiers, block statements, and parameters.
*
* @param ctx the constructor declaration context
* @return the AST node for the constructor
*/
@Override @Override
public ASTNode visitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) { public ASTNode visitConstructorDeclaration(SimpleJavaParser.ConstructorDeclarationContext ctx) {
ConstructorNode constructorNode; ConstructorNode constructorNode;
@@ -88,6 +114,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return constructorNode; return constructorNode;
} }
/**
* Visits a method declaration and creates a MethodNode or MainMethodNode.
* It handles method types, access modifiers, block statements, and parameters.
*
* @param ctx the method declaration context
* @return the AST node for the method
*/
@Override @Override
public ASTNode visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) { public ASTNode visitMethodDeclaration(SimpleJavaParser.MethodDeclarationContext ctx) {
if(ctx.MainMethodDeclaration() != null) { if(ctx.MainMethodDeclaration() != null) {
@@ -137,6 +170,14 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
} }
} }
/**
* Visits a field declaration and creates a FieldNode.
* It handles access modifiers and types.
*
* @param ctx the field declaration context
* @return the AST node for the field
*/
@Override @Override
public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) { public ASTNode visitFieldDeclaration(SimpleJavaParser.FieldDeclarationContext ctx) {
if(ctx.AccessModifier() != null) { if(ctx.AccessModifier() != null) {
@@ -146,11 +187,25 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
} }
} }
/**
* Visits a parameter and creates a ParameterNode.
* It handles the type and identifier of the parameter.
*
* @param ctx the parameter context
* @return the AST node for the parameter
*/
@Override @Override
public ASTNode visitParameter(SimpleJavaParser.ParameterContext ctx) { public ASTNode visitParameter(SimpleJavaParser.ParameterContext ctx) {
return new ParameterNode(createTypeNode(ctx.type().getText()), ctx.Identifier().getText()); return new ParameterNode(createTypeNode(ctx.type().getText()), ctx.Identifier().getText());
} }
/**
* Visits a statement and creates the corresponding AST node.
* It determines the specific type of statement and processes it accordingly.
*
* @param ctx the statement context
* @return the AST node for the statement
*/
@Override @Override
public ASTNode visitStatement(SimpleJavaParser.StatementContext ctx) { public ASTNode visitStatement(SimpleJavaParser.StatementContext ctx) {
if(ctx.returnStatement() != null) { if(ctx.returnStatement() != null) {
@@ -175,11 +230,25 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a return statement and creates a ReturnNode.
* It processes the expression in the return statement.
*
* @param ctx the return statement context
* @return the AST node for the return statement
*/
@Override @Override
public ASTNode visitReturnStatement(SimpleJavaParser.ReturnStatementContext ctx) { public ASTNode visitReturnStatement(SimpleJavaParser.ReturnStatementContext ctx) {
return new ReturnNode((IExpressionNode) visit(ctx.expression())); return new ReturnNode((IExpressionNode) visit(ctx.expression()));
} }
/**
* Visits a return statement and creates a ReturnNode.
* It processes the expression in the return statement.
*
* @param ctx the return statement context
* @return the AST node for the return statement
*/
@Override @Override
public ASTNode visitLocalVariableDeclaration(SimpleJavaParser.LocalVariableDeclarationContext ctx) { public ASTNode visitLocalVariableDeclaration(SimpleJavaParser.LocalVariableDeclarationContext ctx) {
if(ctx.Assign() != null) { if(ctx.Assign() != null) {
@@ -190,6 +259,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
} }
/**
* Visits a block statement and creates a BlockNode.
* It processes all statements within the block.
*
* @param ctx the block statement context
* @return the AST node for the block statement
*/
@Override @Override
public ASTNode visitBlockStatement(SimpleJavaParser.BlockStatementContext ctx) { public ASTNode visitBlockStatement(SimpleJavaParser.BlockStatementContext ctx) {
BlockNode blockNode = new BlockNode(); BlockNode blockNode = new BlockNode();
@@ -199,11 +275,25 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return blockNode; return blockNode;
} }
/**
* Visits a while statement and creates a WhileNode.
* It processes the condition expression and the block statement.
*
* @param ctx the while statement context
* @return the AST node for the while statement
*/
@Override @Override
public ASTNode visitWhileStatement(SimpleJavaParser.WhileStatementContext ctx) { public ASTNode visitWhileStatement(SimpleJavaParser.WhileStatementContext ctx) {
return new WhileNode((IExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.blockStatement())); return new WhileNode((IExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.blockStatement()));
} }
/**
* Visits a do-while statement and creates a BlockNode containing a WhileNode.
* It processes the condition expression and the block statement.
*
* @param ctx the do-while statement context
* @return the AST node for the do-while statement
*/
@Override @Override
public ASTNode visitDoWhileStatement(SimpleJavaParser.DoWhileStatementContext ctx) { public ASTNode visitDoWhileStatement(SimpleJavaParser.DoWhileStatementContext ctx) {
IExpressionNode condition = (IExpressionNode) visit(ctx.expression()); IExpressionNode condition = (IExpressionNode) visit(ctx.expression());
@@ -217,6 +307,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return resultBlock; return resultBlock;
} }
/**
* Visits a for statement and creates a BlockNode that contains a WhileNode.
* It processes the initialization, condition, and increment parts of the for loop.
*
* @param ctx the for statement context
* @return the AST node for the for statement
*/
@Override @Override
public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) { public ASTNode visitForStatement(SimpleJavaParser.ForStatementContext ctx) {
List<IStatementNode> statements = new ArrayList<>(); List<IStatementNode> statements = new ArrayList<>();
@@ -235,15 +332,8 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
// Inkrement // Inkrement
IStatementExpressionNode crement = null; IStatementExpressionNode crement = null;
boolean isPrefix = false;
if (ctx.statementExpression(i) != null) { if (ctx.statementExpression(i) != null) {
crement = (IStatementExpressionNode) visit(ctx.statementExpression(i)); crement = (IStatementExpressionNode) visit(ctx.statementExpression(i));
if (crement instanceof IncrementNode) {
isPrefix = ((IncrementNode) crement).crementType == CrementType.PREFIX;
} else if (crement instanceof DecrementNode) {
isPrefix = ((DecrementNode) crement).crementType == CrementType.PREFIX;
}
} }
BlockNode forBlock = (BlockNode) visit(ctx.blockStatement()); BlockNode forBlock = (BlockNode) visit(ctx.blockStatement());
@@ -251,18 +341,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
// While-Schleife // While-Schleife
BlockNode whileBody = new BlockNode(); BlockNode whileBody = new BlockNode();
// Prä-Inkrement: Das Inkrement kommt vor dem Block
if (crement != null && isPrefix) {
whileBody.addStatement(crement);
}
// Block Statements der For-Schleife in den While-Block kopieren // Block Statements der For-Schleife in den While-Block kopieren
for (IStatementNode statement : forBlock.statements) { for (IStatementNode statement : forBlock.statements) {
whileBody.addStatement(statement); whileBody.addStatement(statement);
} }
// Post-Inkrement: Das Inkrement kommt nach dem Block // Post-Inkrement: Das Inkrement kommt nach dem Block
if (crement != null && !isPrefix) { if (crement != null) {
whileBody.addStatement(crement); whileBody.addStatement(crement);
} }
@@ -279,6 +364,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return resultBlock; return resultBlock;
} }
/**
* Visits an if-else statement and creates an IfElseNode.
* It processes the if, else-if, and else parts of the statement.
*
* @param ctx the if-else statement context
* @return the AST node for the if-else statement
*/
@Override @Override
public ASTNode visitIfElseStatement(SimpleJavaParser.IfElseStatementContext ctx) { public ASTNode visitIfElseStatement(SimpleJavaParser.IfElseStatementContext ctx) {
IfElseNode ifElseStatementNode; IfElseNode ifElseStatementNode;
@@ -296,21 +388,49 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return ifElseStatementNode; return ifElseStatementNode;
} }
/**
* Visits an if statement and creates an IfNode.
* It processes the condition expression and the block statement.
*
* @param ctx the if statement context
* @return the AST node for the if statement
*/
@Override @Override
public ASTNode visitIfStatement(SimpleJavaParser.IfStatementContext ctx) { public ASTNode visitIfStatement(SimpleJavaParser.IfStatementContext ctx) {
return new IfNode((IExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.blockStatement())); return new IfNode((IExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.blockStatement()));
} }
/**
* Visits an else-if statement and creates an IfNode.
* It processes the condition expression and the block statement.
*
* @param ctx the else-if statement context
* @return the AST node for the else-if statement
*/
@Override @Override
public ASTNode visitElseIfStatement(SimpleJavaParser.ElseIfStatementContext ctx) { public ASTNode visitElseIfStatement(SimpleJavaParser.ElseIfStatementContext ctx) {
return new IfNode((IExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.blockStatement())); return new IfNode((IExpressionNode) visit(ctx.expression()), (BlockNode) visit(ctx.blockStatement()));
} }
/**
* Visits an else statement and creates an ElseNode.
* It processes the block statement of the else part.
*
* @param ctx the else statement context
* @return the AST node for the else statement
*/
@Override @Override
public ASTNode visitElseStatement(SimpleJavaParser.ElseStatementContext ctx) { public ASTNode visitElseStatement(SimpleJavaParser.ElseStatementContext ctx) {
return new ElseNode((BlockNode) visit(ctx.blockStatement())); return new ElseNode((BlockNode) visit(ctx.blockStatement()));
} }
/**
* Visits a statement expression and creates the corresponding AST node.
* It handles assignments, new declarations, method calls, and increment/decrement expressions.
*
* @param ctx the statement expression context
* @return the AST node for the statement expression
*/
@Override @Override
public ASTNode visitStatementExpression(SimpleJavaParser.StatementExpressionContext ctx) { public ASTNode visitStatementExpression(SimpleJavaParser.StatementExpressionContext ctx) {
if(ctx.assign() != null) { if(ctx.assign() != null) {
@@ -325,6 +445,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a switch statement and creates an IfElseNode representing the switch cases.
* It processes the switch expression, case statements, and default statement.
*
* @param ctx the switch statement context
* @return the AST node for the switch statement
*/
@Override @Override
public ASTNode visitSwitchStatement(SimpleJavaParser.SwitchStatementContext ctx) { public ASTNode visitSwitchStatement(SimpleJavaParser.SwitchStatementContext ctx) {
UnaryNode switchExpression = (UnaryNode) visit(ctx.expression()); UnaryNode switchExpression = (UnaryNode) visit(ctx.expression());
@@ -368,11 +495,25 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return ifElseNode; return ifElseNode;
} }
/**
* Visits an assignment expression and creates an AssignNode.
* It handles the assignable expression and the assigned expression.
*
* @param ctx the assignment context
* @return the AST node for the assignment
*/
@Override @Override
public ASTNode visitAssign(SimpleJavaParser.AssignContext ctx) { public ASTNode visitAssign(SimpleJavaParser.AssignContext ctx) {
return new AssignNode((AssignableNode) visit(ctx.assignableExpression()), (IExpressionNode) visit(ctx.expression())); return new AssignNode((AssignableNode) visit(ctx.assignableExpression()), (IExpressionNode) visit(ctx.expression()));
} }
/**
* Visits a new declaration and creates a NewDeclarationNode.
* It processes the identifier and the argument list.
*
* @param ctx the new declaration context
* @return the AST node for the new declaration
*/
@Override @Override
public ASTNode visitNewDeclaration(SimpleJavaParser.NewDeclarationContext ctx) { public ASTNode visitNewDeclaration(SimpleJavaParser.NewDeclarationContext ctx) {
NewDeclarationNode newDeclarationNode = new NewDeclarationNode(ctx.Identifier().getText()); NewDeclarationNode newDeclarationNode = new NewDeclarationNode(ctx.Identifier().getText());
@@ -382,6 +523,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return newDeclarationNode; return newDeclarationNode;
} }
/**
* Visits a method call and creates a MethodCallNode.
* It processes the target, chained methods, and argument list.
*
* @param ctx the method call context
* @return the AST node for the method call
*/
@Override @Override
public ASTNode visitMethodCall(SimpleJavaParser.MethodCallContext ctx) { public ASTNode visitMethodCall(SimpleJavaParser.MethodCallContext ctx) {
MethodCallNode methodCallStatementExpressionNode; MethodCallNode methodCallStatementExpressionNode;
@@ -399,6 +547,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return methodCallStatementExpressionNode; return methodCallStatementExpressionNode;
} }
/**
* Visits the target of a method call and creates a TargetNode.
* It handles various types of targets including 'this', member access, new declarations, and identifiers.
*
* @param ctx the target context
* @return the AST node for the target
*/
@Override @Override
public ASTNode visitTarget(SimpleJavaParser.TargetContext ctx) { public ASTNode visitTarget(SimpleJavaParser.TargetContext ctx) {
if(ctx.This() != null) { if(ctx.This() != null) {
@@ -413,6 +568,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a chained method and creates a ChainedMethodNode.
* It processes the identifier and the argument list.
*
* @param ctx the chained method context
* @return the AST node for the chained method
*/
@Override @Override
public ASTNode visitChainedMethod(SimpleJavaParser.ChainedMethodContext ctx) { public ASTNode visitChainedMethod(SimpleJavaParser.ChainedMethodContext ctx) {
ChainedMethodNode chainedMethodNode = new ChainedMethodNode(ctx.Identifier().getText()); ChainedMethodNode chainedMethodNode = new ChainedMethodNode(ctx.Identifier().getText());
@@ -422,6 +584,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return chainedMethodNode; return chainedMethodNode;
} }
/**
* Visits an increment or decrement expression and creates the corresponding AST node.
* It handles both prefix and suffix forms of increment and decrement.
*
* @param ctx the crement expression context
* @return the AST node for the crement expression
*/
@Override @Override
public ASTNode visitCrementExpression(SimpleJavaParser.CrementExpressionContext ctx) { public ASTNode visitCrementExpression(SimpleJavaParser.CrementExpressionContext ctx) {
if(ctx.incrementExpression() != null) { if(ctx.incrementExpression() != null) {
@@ -432,6 +601,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits an increment expression and creates an IncrementNode.
* It handles both prefix and suffix forms.
*
* @param ctx the increment expression context
* @return the AST node for the increment expression
*/
@Override @Override
public ASTNode visitIncrementExpression(SimpleJavaParser.IncrementExpressionContext ctx) { public ASTNode visitIncrementExpression(SimpleJavaParser.IncrementExpressionContext ctx) {
if(ctx.prefixIncrementExpression() != null) { if(ctx.prefixIncrementExpression() != null) {
@@ -442,16 +618,35 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a prefix increment expression and creates an IncrementNode.
*
* @param ctx the prefix increment expression context
* @return the AST node for the prefix increment expression
*/
@Override @Override
public ASTNode visitPrefixIncrementExpression(SimpleJavaParser.PrefixIncrementExpressionContext ctx) { public ASTNode visitPrefixIncrementExpression(SimpleJavaParser.PrefixIncrementExpressionContext ctx) {
return new IncrementNode(CrementType.PREFIX, (AssignableNode) visit(ctx.assignableExpression())); return new IncrementNode(CrementType.PREFIX, (AssignableNode) visit(ctx.assignableExpression()));
} }
/**
* Visits a suffix increment expression and creates an IncrementNode.
*
* @param ctx the suffix increment expression context
* @return the AST node for the suffix increment expression
*/
@Override @Override
public ASTNode visitSuffixIncrementExpression(SimpleJavaParser.SuffixIncrementExpressionContext ctx) { public ASTNode visitSuffixIncrementExpression(SimpleJavaParser.SuffixIncrementExpressionContext ctx) {
return new IncrementNode(CrementType.SUFFIX, (AssignableNode) visit(ctx.assignableExpression())); return new IncrementNode(CrementType.SUFFIX, (AssignableNode) visit(ctx.assignableExpression()));
} }
/**
* Visits a decrement expression and creates a DecrementNode.
* It handles both prefix and suffix forms.
*
* @param ctx the decrement expression context
* @return the AST node for the decrement expression
*/
@Override @Override
public ASTNode visitDecrementExpression(SimpleJavaParser.DecrementExpressionContext ctx) { public ASTNode visitDecrementExpression(SimpleJavaParser.DecrementExpressionContext ctx) {
if(ctx.prefixDecrementExpression() != null) { if(ctx.prefixDecrementExpression() != null) {
@@ -462,16 +657,35 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a prefix decrement expression and creates a DecrementNode.
*
* @param ctx the prefix decrement expression context
* @return the AST node for the prefix decrement expression
*/
@Override @Override
public ASTNode visitPrefixDecrementExpression(SimpleJavaParser.PrefixDecrementExpressionContext ctx) { public ASTNode visitPrefixDecrementExpression(SimpleJavaParser.PrefixDecrementExpressionContext ctx) {
return new DecrementNode(CrementType.PREFIX, (AssignableNode) visit(ctx.assignableExpression())); return new DecrementNode(CrementType.PREFIX, (AssignableNode) visit(ctx.assignableExpression()));
} }
/**
* Visits a suffix decrement expression and creates a DecrementNode.
*
* @param ctx the suffix decrement expression context
* @return the AST node for the suffix decrement expression
*/
@Override @Override
public ASTNode visitSuffixDecrementExpression(SimpleJavaParser.SuffixDecrementExpressionContext ctx) { public ASTNode visitSuffixDecrementExpression(SimpleJavaParser.SuffixDecrementExpressionContext ctx) {
return new DecrementNode(CrementType.SUFFIX, (AssignableNode) visit(ctx.assignableExpression())); return new DecrementNode(CrementType.SUFFIX, (AssignableNode) visit(ctx.assignableExpression()));
} }
/**
* Visits an expression and creates the corresponding AST node.
* It handles both unary and binary expressions.
*
* @param ctx the expression context
* @return the AST node for the expression
*/
@Override @Override
public ASTNode visitExpression(SimpleJavaParser.ExpressionContext ctx) { public ASTNode visitExpression(SimpleJavaParser.ExpressionContext ctx) {
if(ctx.unaryExpression() != null) { if(ctx.unaryExpression() != null) {
@@ -482,6 +696,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a unary expression and creates a UnaryNode.
* It handles various forms of unary expressions including 'this', identifiers, member access, values, and more.
*
* @param ctx the unary expression context
* @return the AST node for the unary expression
*/
@Override @Override
public ASTNode visitUnaryExpression(SimpleJavaParser.UnaryExpressionContext ctx) { public ASTNode visitUnaryExpression(SimpleJavaParser.UnaryExpressionContext ctx) {
if(ctx.This() != null) { if(ctx.This() != null) {
@@ -502,6 +723,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a member access expression and creates a MemberAccessNode.
* It processes the 'this' keyword and identifiers for member access.
*
* @param ctx the member access context
* @return the AST node for the member access
*/
@Override @Override
public ASTNode visitMemberAccess(SimpleJavaParser.MemberAccessContext ctx) { public ASTNode visitMemberAccess(SimpleJavaParser.MemberAccessContext ctx) {
MemberAccessNode memberAccessNode; MemberAccessNode memberAccessNode;
@@ -516,6 +744,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return memberAccessNode; return memberAccessNode;
} }
/**
* Visits a value and creates a ValueNode.
* It handles integer, boolean, char, and null values.
*
* @param ctx the value context
* @return the AST node for the value
*/
@Override @Override
public ASTNode visitValue(SimpleJavaParser.ValueContext ctx) { public ASTNode visitValue(SimpleJavaParser.ValueContext ctx) {
if(ctx.IntValue() != null) { if(ctx.IntValue() != null) {
@@ -530,12 +765,25 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a not expression and creates a NotNode.
* It processes the expression to which the 'not' operator is applied.
*
* @param ctx the not expression context
* @return the AST node for the not expression
*/
@Override @Override
public ASTNode visitNotExpression(SimpleJavaParser.NotExpressionContext ctx) { public ASTNode visitNotExpression(SimpleJavaParser.NotExpressionContext ctx) {
return new NotNode((IExpressionNode) visitExpression(ctx.expression())); return new NotNode((IExpressionNode) visitExpression(ctx.expression()));
} }
/**
* Visits a binary expression and creates the corresponding AST node.
* It handles both calculation and non-calculation expressions.
*
* @param ctx the binary expression context
* @return the AST node for the binary expression
*/
@Override @Override
public ASTNode visitBinaryExpression(SimpleJavaParser.BinaryExpressionContext ctx) { public ASTNode visitBinaryExpression(SimpleJavaParser.BinaryExpressionContext ctx) {
if(ctx.calculationExpression() != null) { if(ctx.calculationExpression() != null) {
@@ -546,6 +794,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a calculation expression and creates a CalculationNode.
* It handles line operators and dot expressions.
*
* @param ctx the calculation expression context
* @return the AST node for the calculation expression
*/
@Override @Override
public ASTNode visitCalculationExpression(SimpleJavaParser.CalculationExpressionContext ctx) { public ASTNode visitCalculationExpression(SimpleJavaParser.CalculationExpressionContext ctx) {
if(ctx.calculationExpression() != null) { if(ctx.calculationExpression() != null) {
@@ -556,6 +811,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a dot expression and creates a DotNode.
* It handles dot operators and dot subtraction expressions.
*
* @param ctx the dot expression context
* @return the AST node for the dot expression
*/
@Override @Override
public ASTNode visitDotExpression(SimpleJavaParser.DotExpressionContext ctx) { public ASTNode visitDotExpression(SimpleJavaParser.DotExpressionContext ctx) {
if(ctx.dotExpression() != null) { if(ctx.dotExpression() != null) {
@@ -566,6 +828,19 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a dot subtraction expression and creates a DotSubtractionNode.
* It handles integer values, identifiers, member access, and method calls.
*
* @param ctx the dot subtraction expression context
* @return the AST node for the dot subtraction expression
*//**
* Creates an ITypeNode based on the type identifier.
* It handles basic types and reference types.
*
* @param identifier the type identifier
* @return the type node
*/
@Override @Override
public ASTNode visitDotSubtractionExpression(SimpleJavaParser.DotSubtractionExpressionContext ctx) { public ASTNode visitDotSubtractionExpression(SimpleJavaParser.DotSubtractionExpressionContext ctx) {
if(ctx.IntValue() != null) { if(ctx.IntValue() != null) {
@@ -580,11 +855,25 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Visits a non-calculation expression and creates a NonCalculationNode.
* It handles unary expressions combined with non-calculation operators.
*
* @param ctx the non-calculation expression context
* @return the AST node for the non-calculation expression
*/
@Override @Override
public ASTNode visitNonCalculationExpression(SimpleJavaParser.NonCalculationExpressionContext ctx) { public ASTNode visitNonCalculationExpression(SimpleJavaParser.NonCalculationExpressionContext ctx) {
return new NonCalculationNode((UnaryNode) visit(ctx.unaryExpression()), ctx.nonCalculationOperator().getText(), (IExpressionNode) visit(ctx.expression())); return new NonCalculationNode((UnaryNode) visit(ctx.unaryExpression()), ctx.nonCalculationOperator().getText(), (IExpressionNode) visit(ctx.expression()));
} }
/**
* Visits an assignable expression and creates an AssignableNode.
* It handles identifiers and member access.
*
* @param ctx the assignable expression context
* @return the AST node for the assignable expression
*/
@Override @Override
public ASTNode visitAssignableExpression(SimpleJavaParser.AssignableExpressionContext ctx) { public ASTNode visitAssignableExpression(SimpleJavaParser.AssignableExpressionContext ctx) {
if(ctx.Identifier() != null) { if(ctx.Identifier() != null) {
@@ -595,6 +884,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
return null; return null;
} }
/**
* Creates an ITypeNode based on the type identifier.
* It handles basic types and reference types.
*
* @param identifier the type identifier
* @return the type node
*/
public ITypeNode createTypeNode(String identifier){ public ITypeNode createTypeNode(String identifier){
return switch (identifier) { return switch (identifier) {
case "int" -> new BaseType(TypeEnum.INT); case "int" -> new BaseType(TypeEnum.INT);

View File

@@ -1,18 +1,18 @@
grammar SimpleJava; grammar SimpleJava;
// Programm-Konstrukte // Programm-Konstrukte
program: classDeclaration+; program: classDeclaration+; // Ein Programm besteht aus einer oder mehreren Klassendeklarationen
classDeclaration: AccessModifier? 'class' Identifier OpenCurlyBracket memberDeclaration* ClosedCurlyBracket; classDeclaration: AccessModifier? 'class' Identifier OpenCurlyBracket memberDeclaration* ClosedCurlyBracket; // Deklariert eine Klasse mit optionalem Zugriffsmodifikator und mehreren Mitgliedern
memberDeclaration: constructorDeclaration | fieldDeclaration | methodDeclaration; memberDeclaration: constructorDeclaration | fieldDeclaration | methodDeclaration; // Ein Mitglied kann ein Konstruktor, ein Feld oder eine Methode sein
constructorDeclaration: AccessModifier? Identifier OpenRoundBracket parameterList? ClosedRoundBracket blockStatement; constructorDeclaration: AccessModifier? Identifier OpenRoundBracket parameterList? ClosedRoundBracket blockStatement; // Deklariert einen Konstruktor
fieldDeclaration: AccessModifier? type Identifier (Assign expression)? Semicolon; fieldDeclaration: AccessModifier? type Identifier (Assign expression)? Semicolon; // Deklariert ein Feld mit optionaler Initialisierung
methodDeclaration: MainMethodDeclaration blockStatement | AccessModifier? (type | Void) Identifier OpenRoundBracket parameterList? ClosedRoundBracket blockStatement; methodDeclaration: MainMethodDeclaration blockStatement | AccessModifier? (type | Void) Identifier OpenRoundBracket parameterList? ClosedRoundBracket blockStatement; // Deklariert eine Methode
parameterList: parameter (Comma parameter)*; parameterList: parameter (Comma parameter)*; // Parameterliste für Methoden und Konstruktoren
parameter: type Identifier; parameter: type Identifier; // Ein einzelner Parameter mit Typ und Bezeichner
argumentList: (expression (Comma expression)*)?; argumentList: (expression (Comma expression)*)?; // Argumentliste für Methodenaufrufe
// Anweisungen // Anweisungen
statement: returnStatement Semicolon statement: returnStatement Semicolon
@@ -23,32 +23,32 @@ statement: returnStatement Semicolon
| forStatement | forStatement
| ifElseStatement | ifElseStatement
| switchStatement | switchStatement
| statementExpression Semicolon; | statementExpression Semicolon; // Verschiedene Arten von Anweisungen
blockStatement: OpenCurlyBracket statement* ClosedCurlyBracket; blockStatement: OpenCurlyBracket statement* ClosedCurlyBracket; // Ein Block von Anweisungen
returnStatement: Return (expression)?; returnStatement: Return (expression)?; // Rückgabe-Anweisung
localVariableDeclaration: type Identifier (Assign expression)?; localVariableDeclaration: type Identifier (Assign expression)?; // Deklaration einer lokalen Variable
whileStatement: While OpenRoundBracket expression ClosedRoundBracket blockStatement; whileStatement: While OpenRoundBracket expression ClosedRoundBracket blockStatement; // While-Schleife
doWhileStatement: Do blockStatement While OpenRoundBracket expression ClosedRoundBracket Semicolon; doWhileStatement: Do blockStatement While OpenRoundBracket expression ClosedRoundBracket Semicolon; // Do-While-Schleife
forStatement: For OpenRoundBracket (statementExpression | localVariableDeclaration) Semicolon (expression)? Semicolon (statementExpression)? ClosedRoundBracket blockStatement; forStatement: For OpenRoundBracket (statementExpression | localVariableDeclaration) Semicolon (expression)? Semicolon (statementExpression)? ClosedRoundBracket blockStatement; // For-Schleife
ifElseStatement: ifStatement elseIfStatement* elseStatement?; ifElseStatement: ifStatement elseIfStatement* elseStatement?; // If-Else-Bedingung
ifStatement: If OpenRoundBracket expression ClosedRoundBracket blockStatement; ifStatement: If OpenRoundBracket expression ClosedRoundBracket blockStatement; // If-Teil der Bedingung
elseIfStatement: Else If OpenRoundBracket expression ClosedRoundBracket blockStatement; elseIfStatement: Else If OpenRoundBracket expression ClosedRoundBracket blockStatement; // Else-If-Teil der Bedingung
elseStatement: Else blockStatement; elseStatement: Else blockStatement; // Else-Teil der Bedingung
switchStatement: Switch OpenRoundBracket expression ClosedRoundBracket OpenCurlyBracket caseStatement+ defaultStatement? ClosedCurlyBracket; switchStatement: Switch OpenRoundBracket expression ClosedRoundBracket OpenCurlyBracket caseStatement+ defaultStatement? ClosedCurlyBracket; // Switch-Anweisung
caseStatement: Case value Colon statement*; caseStatement: Case value Colon statement*; // Ein Case-Teil einer Switch-Anweisung
defaultStatement: Default Colon statement*; defaultStatement: Default Colon statement*; // Default-Teil einer Switch-Anweisung
statementExpression: assign | newDeclaration | methodCall | crementExpression; statementExpression: assign | newDeclaration | methodCall | crementExpression; // Ausdruck innerhalb einer Anweisung
assign: assignableExpression Assign expression; assign: assignableExpression Assign expression; // Zuweisung
newDeclaration: New Identifier OpenRoundBracket argumentList ClosedRoundBracket; newDeclaration: New Identifier OpenRoundBracket argumentList ClosedRoundBracket; // Instanziierung eines neuen Objekts
// Ausdrücke // Ausdrücke
expression: unaryExpression | binaryExpression; expression: unaryExpression | binaryExpression; // Ein Ausdruck kann unär oder binär sein
unaryExpression: This unaryExpression: This
| Identifier | Identifier
@@ -56,97 +56,97 @@ unaryExpression: This
| value | value
| notExpression | notExpression
| statementExpression | statementExpression
| OpenRoundBracket expression ClosedRoundBracket; | OpenRoundBracket expression ClosedRoundBracket; // Unäre Ausdrücke
notExpression: Not expression; notExpression: Not expression; // Nicht-Ausdruck
crementExpression: incrementExpression | decrementExpression; crementExpression: incrementExpression | decrementExpression; // Inkrement-/Dekrement-Ausdrücke
incrementExpression: prefixIncrementExpression | suffixIncrementExpression; incrementExpression: prefixIncrementExpression | suffixIncrementExpression;
prefixIncrementExpression: '++' assignableExpression; prefixIncrementExpression: '++' assignableExpression; // Präfix-Inkrement
suffixIncrementExpression: assignableExpression '++'; suffixIncrementExpression: assignableExpression '++'; // Suffix-Inkrement
decrementExpression: prefixDecrementExpression | suffixDecrementExpression; decrementExpression: prefixDecrementExpression | suffixDecrementExpression; // Dekrement-Ausdrücke
prefixDecrementExpression: '--' assignableExpression; prefixDecrementExpression: '--' assignableExpression; // Präfix-Dekrement
suffixDecrementExpression: assignableExpression '--'; suffixDecrementExpression: assignableExpression '--'; // Suffix-Dekrement
assignableExpression: Identifier | memberAccess; assignableExpression: Identifier | memberAccess; // Zuweisbarer Ausdruck
memberAccess: This Dot Identifier memberAccess: This Dot Identifier
| (This Dot)? (Identifier Dot)+ Identifier; | (This Dot)? (Identifier Dot)+ Identifier; // Mitgliedszugriff
binaryExpression: calculationExpression | nonCalculationExpression; binaryExpression: calculationExpression | nonCalculationExpression; // Binäre Ausdrücke
calculationExpression: calculationExpression LineOperator dotExpression calculationExpression: calculationExpression LineOperator dotExpression
| dotExpression; | dotExpression; // Berechnungsausdrücke
dotExpression: dotExpression DotOperator dotSubtractionExpression dotExpression: dotExpression DotOperator dotSubtractionExpression
| dotSubtractionExpression; | dotSubtractionExpression; // Punktoperationen
dotSubtractionExpression: IntValue dotSubtractionExpression: IntValue
| Identifier | Identifier
| memberAccess | memberAccess
| methodCall OpenRoundBracket calculationExpression ClosedRoundBracket; | methodCall OpenRoundBracket calculationExpression ClosedRoundBracket; // Punkt-Subtraktionsausdrücke
nonCalculationExpression: unaryExpression nonCalculationOperator expression; nonCalculationExpression: unaryExpression nonCalculationOperator expression; // Nicht-Berechnungsausdrücke
// Methodenaufrufe // Methodenaufrufe
methodCall: target? chainedMethod* Identifier OpenRoundBracket argumentList ClosedRoundBracket; methodCall: target? chainedMethod* Identifier OpenRoundBracket argumentList ClosedRoundBracket; // Methodenaufruf
target: (This | memberAccess | newDeclaration | Identifier) Dot; target: (This | memberAccess | newDeclaration | Identifier) Dot; // Ziel eines Methodenaufrufs
chainedMethod: Identifier OpenRoundBracket argumentList ClosedRoundBracket Dot; chainedMethod: Identifier OpenRoundBracket argumentList ClosedRoundBracket Dot; // Verkettete Methode
// Typen // Typen
type: Int type: Int
| Boolean | Boolean
| Char | Char
| Identifier; | Identifier; // Datentypen
Void: 'void'; Void: 'void'; // Void-Typ
Boolean: 'boolean'; Boolean: 'boolean'; // Boolean-Typ
Char: 'char'; Char: 'char'; // Char-Typ
Int: 'int'; Int: 'int'; // Integer-Typ
value: IntValue value: IntValue
| BooleanValue | BooleanValue
| CharValue | CharValue
| NullValue; | NullValue; // Werte
// Zugriffsmodifikatoren // Zugriffsmodifikatoren
AccessModifier: 'public' | 'private' | 'public static' | 'private static'; AccessModifier: 'public' | 'private' | 'public static' | 'private static'; // Zugriffsmodifikatoren
MainMethodDeclaration: 'public static void main(String[] args)'; MainMethodDeclaration: 'public static void main(String[] args)'; // Hauptmethoden-Deklaration
// Operatoren // Operatoren
nonCalculationOperator: LogicalOperator | ComparisonOperator; nonCalculationOperator: LogicalOperator | ComparisonOperator; // Nicht-Berechnungsoperatoren
DotOperator: Mult | Div | Modulo; DotOperator: Mult | Div | Modulo; // Punktoperatoren
LineOperator: Plus | Minus; LineOperator: Plus | Minus; // Linienoperatoren
ComparisonOperator: Greater | Less | GreaterEqual | LessEqual | Equal | NotEqual; ComparisonOperator: Greater | Less | GreaterEqual | LessEqual | Equal | NotEqual; // Vergleichsoperatoren
LogicalOperator: And | Or; LogicalOperator: And | Or; // Logische Operatoren
Assign: '='; Assign: '='; // Zuweisungsoperator
Plus: '+'; Plus: '+'; // Plus-Operator
Minus: '-'; Minus: '-'; // Minus-Operator
Mult: '*'; Mult: '*'; // Multiplikationsoperator
Modulo: '%'; Modulo: '%'; // Modulo-Operator
Div: '/'; Div: '/'; // Divisionsoperator
Greater: '>'; Greater: '>'; // Größer-Operator
Less: '<'; Less: '<'; // Kleiner-Operator
GreaterEqual: '>='; GreaterEqual: '>='; // Größer-Gleich-Operator
LessEqual: '<='; LessEqual: '<='; // Kleiner-Gleich-Operator
Equal: '=='; Equal: '=='; // Gleichheitsoperator
NotEqual: '!='; NotEqual: '!='; // Ungleichheitsoperator
Not: '!'; Not: '!'; // Nicht-Operator
And: '&&'; And: '&&'; // Und-Operator
Or: '||'; Or: '||'; // Oder-Operator
// Symbole // Symbole
Dot: '.'; Dot: '.'; // Punkt-Symbol
OpenRoundBracket: '('; OpenRoundBracket: '('; // Öffnende runde Klammer
ClosedRoundBracket: ')'; ClosedRoundBracket: ')'; // Schließende runde Klammer
OpenCurlyBracket: '{'; OpenCurlyBracket: '{'; // Öffnende geschweifte Klammer
ClosedCurlyBracket: '}'; ClosedCurlyBracket: '}'; // Schließende geschweifte Klammer
Semicolon: ';'; Semicolon: ';'; // Semikolon
Comma: ','; Comma: ','; // Komma
// Schlüsselwörter // Schlüsselwörter
Class: 'class'; Class: 'class';
@@ -161,21 +161,21 @@ New: 'new';
Switch: 'switch'; Switch: 'switch';
Case: 'case'; Case: 'case';
Default: 'default'; Default: 'default';
Colon: ':'; Colon: ':'; // Doppelpunkt
// Werte // Werte
CharValue: '\'' ~[\r\n]* '\''; CharValue: '\'' ~[\r\n]* '\''; // Zeichenwert
IntValue: Minus? Numeric+; IntValue: Minus? Numeric+; // Ganzzahlwert
BooleanValue: 'true' | 'false'; BooleanValue: 'true' | 'false'; // Boolean-Wert
NullValue: 'null'; NullValue: 'null'; // Null-Wert
// Bezeichner // Bezeichner
fragment Alphabetic: [a-zA-Z]; fragment Alphabetic: [a-zA-Z];
fragment Numeric: [0-9]; fragment Numeric: [0-9];
fragment ValidIdentSymbols: Alphabetic | Numeric | '$' | '_'; fragment ValidIdentSymbols: Alphabetic | Numeric | '$' | '_';
Identifier: Alphabetic ValidIdentSymbols*; Identifier: Alphabetic ValidIdentSymbols*; // Bezeichner
// Whitespaces und Kommentare ignorieren // Whitespaces und Kommentare ignorieren
WS: [ \t\r\n]+ -> skip; WS: [ \t\r\n]+ -> skip; // Leerzeichen ignorieren
InlineComment: '//' ~[\r\n]* -> skip; InlineComment: '//' ~[\r\n]* -> skip; // Einzeilige Kommentare ignorieren
MultilineComment: '/*' .*? '*/' -> skip; MultilineComment: '/*' .*? '*/' -> skip; // Mehrzeilige Kommentare ignorieren

View File

View File

@@ -1,6 +1,5 @@
package semantic; package semantic;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@@ -25,15 +24,11 @@ import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNod
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.*;
import ast.type.AccessModifierNode;
import ast.type.EnumAccessModifierNode; 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 semantic.context.ClassContext;
import semantic.context.Context; import semantic.context.Context;
import semantic.exceptions.*; import semantic.exceptions.*;
import semantic.TypeCheckResult;
public class SemanticAnalyzer implements SemanticVisitor { public class SemanticAnalyzer implements SemanticVisitor {
@@ -114,10 +109,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override @Override
public TypeCheckResult analyze(MethodNode methodNode) { public TypeCheckResult analyze(MethodNode methodNode) {
if (methodNode instanceof ConstructorNode) {
return new TypeCheckResult(true, new BaseType(TypeEnum.VOID));
} else {
var valid = true; var valid = true;
for (var otherMethod : currentClass.getMethods()) { for (var otherMethod : currentClass.getMethods()) {
@@ -159,8 +150,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
} }
return new TypeCheckResult(valid, resultType); return new TypeCheckResult(valid, resultType);
}
} }
@Override @Override
@@ -177,8 +166,6 @@ 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
@@ -267,12 +254,11 @@ public class SemanticAnalyzer implements SemanticVisitor {
return new TypeCheckResult(true, type); return new TypeCheckResult(true, type);
} else if (currentScope.getLocalVar(toCheck.identifier) != null) { } else if (currentScope.getLocalVar(toCheck.identifier) != null) {
var type = currentScope.getLocalVar(toCheck.identifier); var type = currentScope.getLocalVar(toCheck.identifier);
toCheck.setTypeNode(type);
return new TypeCheckResult(true, type); return new TypeCheckResult(true, type);
} }
} }
return new TypeCheckResult(true, null); return new TypeCheckResult(true, null);
} }
@Override @Override
@@ -317,9 +303,18 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override @Override
public TypeCheckResult analyze(IfElseNode toCheck) { public TypeCheckResult analyze(IfElseNode toCheck) {
var resultIf = toCheck.ifStatement.accept(this); var resultIf = toCheck.ifStatement.accept(this);
var validElseIf = true;
if(!toCheck.elseIfStatements.isEmpty()) {
for(IfNode ifNode : toCheck.elseIfStatements) {
var resultIfFor = ifNode.accept(this);
validElseIf = validElseIf && resultIfFor.isValid();
}
}
if(toCheck.elseStatement != null){ if(toCheck.elseStatement != null){
var resultElse = toCheck.elseStatement.accept(this); var resultElse = toCheck.elseStatement.accept(this);
return new TypeCheckResult(resultIf.isValid() && resultElse.isValid(), new BaseType(TypeEnum.VOID)); return new TypeCheckResult(resultIf.isValid() && resultElse.isValid() && validElseIf, new BaseType(TypeEnum.VOID));
} }
@@ -364,6 +359,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
if (toCheck.target.thisTar) { if (toCheck.target.thisTar) {
var type = getTypeFromMethod(toCheck, new ReferenceType(currentClass.identifier)); var type = getTypeFromMethod(toCheck, new ReferenceType(currentClass.identifier));
if (type != null) { if (type != null) {
toCheck.type = type;
return new TypeCheckResult(true, type); return new TypeCheckResult(true, type);
} }
} }
@@ -375,6 +371,10 @@ public class SemanticAnalyzer implements SemanticVisitor {
} }
} }
} else { } else {
if(toCheck.identifier != null) {
toCheck.target = new TargetNode(new MemberAccessNode(false));
toCheck.target.memberAccess.identifiers.add(currentClass.identifier);
}
ReferenceType reference = new ReferenceType(currentClass.identifier); ReferenceType reference = new ReferenceType(currentClass.identifier);
if (!toCheck.chainedMethods.isEmpty()) { if (!toCheck.chainedMethods.isEmpty()) {
@@ -392,7 +392,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
} else { } else {
return new TypeCheckResult(false, null); return new TypeCheckResult(false, null);
} }
} }
return new TypeCheckResult(false, null); return new TypeCheckResult(false, null);
@@ -430,8 +429,10 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override @Override
public TypeCheckResult analyze(NewDeclarationNode toCheck) { public TypeCheckResult analyze(NewDeclarationNode toCheck) {
if (context.containsClass(toCheck.identifier)) { if (context.containsClass(toCheck.identifier)) {
for(var t : toCheck.expressions) {
t.accept(this);
}
return new TypeCheckResult(true, new ReferenceType(toCheck.identifier)); return new TypeCheckResult(true, new ReferenceType(toCheck.identifier));
} else { } else {
throw new RuntimeException("Cannot find class " + toCheck.identifier); throw new RuntimeException("Cannot find class " + toCheck.identifier);
@@ -533,7 +534,9 @@ public class SemanticAnalyzer implements SemanticVisitor {
case LESS, LESS_EQUAL, GREATER, GREATER_EQUAL: case LESS, LESS_EQUAL, GREATER, GREATER_EQUAL:
if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.INT) && if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.INT) &&
unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.INT)) { unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.INT)) {
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL)); ITypeNode type = new BaseType(TypeEnum.BOOL);
nonCalculationNode.setType(type);
return new TypeCheckResult(true, type);
} else { } else {
errors.add(new TypeMismatchException("Both types must be Integer")); errors.add(new TypeMismatchException("Both types must be Integer"));
} }
@@ -541,7 +544,9 @@ public class SemanticAnalyzer implements SemanticVisitor {
case OR, AND: case OR, AND:
if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.BOOL) && if (expResult.getType() instanceof BaseType expResultType && expResultType.getTypeEnum().equals(TypeEnum.BOOL) &&
unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.BOOL)) { unaryResult.getType() instanceof BaseType unaryResultType && unaryResultType.getTypeEnum().equals(TypeEnum.BOOL)) {
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL)); ITypeNode type = new BaseType(TypeEnum.BOOL);
nonCalculationNode.setType(type);
return new TypeCheckResult(true, type);
} else { } else {
errors.add(new TypeMismatchException("Both types must be Boolean")); errors.add(new TypeMismatchException("Both types must be Boolean"));
} }
@@ -549,7 +554,9 @@ public class SemanticAnalyzer implements SemanticVisitor {
case EQUAL, NOT_EQUAL: case EQUAL, NOT_EQUAL:
if (expResult.getType() instanceof BaseType expResultType && unaryResult.getType() instanceof BaseType unaryResultType if (expResult.getType() instanceof BaseType expResultType && unaryResult.getType() instanceof BaseType unaryResultType
&& Objects.equals(expResultType, unaryResultType)) { && Objects.equals(expResultType, unaryResultType)) {
return new TypeCheckResult(true, new BaseType(TypeEnum.BOOL)); ITypeNode type = new BaseType(TypeEnum.BOOL);
nonCalculationNode.setType(type);
return new TypeCheckResult(true, type);
} else { } else {
errors.add(new TypeMismatchException("Both types must be the same")); errors.add(new TypeMismatchException("Both types must be the same"));
} }
@@ -575,6 +582,10 @@ public class SemanticAnalyzer implements SemanticVisitor {
var type = currentFields.get(unary.identifier); var type = currentFields.get(unary.identifier);
unary.setType(type); unary.setType(type);
return new TypeCheckResult(valid,type ); return new TypeCheckResult(valid,type );
} else if (unary.value != null) {
var result = unary.value.accept(this);
unary.setType(result.getType());
return new TypeCheckResult(result.isValid(), result.getType());
} else if (unary.statement != null) { } else if (unary.statement != null) {
var result = unary.statement.accept(this); var result = unary.statement.accept(this);
unary.setType(result.getType()); unary.setType(result.getType());
@@ -624,7 +635,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
if (currentType == null) { if (currentType == null) {
if (currentScope.getLocalVar(s) != null) { if (currentScope.getLocalVar(s) != null) {
currentType = currentScope.getLocalVar(s); currentType = currentScope.getLocalVar(s);
} else if (currentFields.get(s) != null) { } else if (currentFields.get(s) != null) {
currentType = currentFields.get(s); currentType = currentFields.get(s);
} else { } else {
@@ -648,7 +658,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
} }
} }
memberAccessNode.setTypeNode(currentType);
return new TypeCheckResult(true, currentType); return new TypeCheckResult(true, currentType);
} }
@@ -674,6 +684,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
return new TypeCheckResult(true, new BaseType(TypeEnum.INT)); return new TypeCheckResult(true, new BaseType(TypeEnum.INT));
} }
case CHAR_VALUE -> { case CHAR_VALUE -> {
valueNode.value = valueNode.value.replace("'", "");
return new TypeCheckResult(true, new BaseType(TypeEnum.CHAR)); return new TypeCheckResult(true, new BaseType(TypeEnum.CHAR));
} }
case BOOLEAN_VALUE -> { case BOOLEAN_VALUE -> {

View File

@@ -1,13 +1,63 @@
public class Compiler { public class CompilerInput {
public int add(int i, int j) { public int ps;
return i+j; public char brand;
public int tires;
public int drivenKilometers;
public CompilerInput(int horsePower) {
this.ps = horsePower;
this.tires = 4;
}
public int getTires() {
return this.tires;
}
public boolean isSuperCar() {
if(this.ps > 300) {
return true;
} else {
return false;
}
}
public void tune(int horsePower) {
this.ps = this.ps + horsePower;
}
public void tune() {
this.ps = this.ps + 50;
}
public void driveCircels(int circels) {
for(int i = 0; i < circels; i++) {
this.drivenKilometers = this.drivenKilometers + 5;
}
}
public void isTunable(boolean hasEngine, boolean hasTires) {
if(hasEngine && hasTires) {
this.tune(5);
}
}
public char race() {
int enemyHorsePower = 200;
char win = 'W';
char lose = 'L';
CompilerInput enemy = new CompilerInput(ps);
if(this.ps > enemyHorsePower) {
return win;
} else {
return lose;
}
}
public int refuel(int currentTank, int maxTank){
int tank = currentTank;
do{
tank++;
}while(tank<maxTank);
return tank;
} }
} }
public class Node {
public void main() {
Compiler compiler = new Compiler();
int i = compiler.add(5, 8);
}
}

View File

@@ -10,11 +10,12 @@ compile-javac:
compile-miniCompiler: compile-miniCompiler:
cd ../.. ; mvn -DskipTests install cd ../.. ; mvn -DskipTests install
cd ../.. ; mvn exec:java -DgenJar=true -DgenClass=true -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/miniCompiler
test: compile-miniCompiler test-miniCompiler
test-miniCompiler: test-miniCompiler:
# move the compiled class to the test/main folder # move the compiled class to the test/main folder
mv ../main/resources/output/CompilerInput.class .java/main/ mv ../main/resources/output/Compiler.class .java/
# compile the test class # compile the test class
javac .java/main.EndToEndTester.java javac .java/main.EndToEndTester.java
# run the test class # run the test class

View File

@@ -1,93 +0,0 @@
# Scanner
## Scanner Input
### Beispiel 1: Empty Class
String empty class = "public class Name {}";
### Beispiel 2: Filled Class
String filled class =
"class javaFileInput.Example {" +
"if (x < 5) {" +
"for (int i = 0; i < 10; i++) {" +
"while (true) {" +
"x = 5;" +
"}"
## Scanner Output
CommonTokenStream
### Beispiel 1: Empty Class
Token Type; Token Text
Type gibts nur bei Terminalen, Text bei allen
[null "public", null "class", IDENTIFIER "Name", null "{", null "}", EOF "<EOF>"]
Bsp von Ihm mal:
[TokPublic,TokClass,TokIdentifier "Name",TokLeftBrace,TokRightBrace]
# Parser
## Parser Input
CommonTokenStream
(Scanner Output)
## Parser Output (AST)
(program (classDeclaration (accessType public) class Name { }))
ParseTree
### Beispiel 1: Empty Class
# Semantische Analyse / Typcheck
## Typcheck Input
(Parser Output = AST)
## Typcheck Output
### Beispiel 1: Empty Class
# Bytecodegenerierung
## Bytecodegenerierung Input
(Typcheck Output = vom Typcheck eventuell manipulierter AST)
## Bytecodegenerierung Output
### Beispiel 1: Empty Class
Compiled Classfile
public class javaFileInput.Example {
}
## E2E Tests:
- Testdatei mit Main ausführen/kompilieren
- Testdatei mit "javac -d output .\CompilerInput.java" kompilieren
- -> Dateien mit javap vergleichen
wenn beides erfolgreich
- Ergebnis vom eigenen Compiler mithilfe von main.EndToEndTester ausführen
- (Ergebnis von javac mithilfe von main.EndToEndTester ausführen)
### Andis Tipps:
- cp mitgeben
- makefile
- java -jar pfadtocompiler.jar EmptyClass.java
- mvn package
- javac tester // tester compilen
- java tester // tester ausführen
- -> tester ist in unserem Fall main.EndToEndTester.java
- -> Hab ich alles umgesetzt

File diff suppressed because it is too large Load Diff

View File

@@ -16,7 +16,7 @@ public class EndToEndTester {
public static void main(String[] args) { public static void main(String[] args) {
try { try {
// Try to load the class named "CompilerInput" // Try to load the class named "CompilerInput"
Class<?> cls = Class.forName("CompilerInput"); Class<?> cls = Class.forName("Compiler");
// Print a success message if the class is loaded successfully // Print a success message if the class is loaded successfully
System.out.println("Class loaded successfully: " + cls.getName()); System.out.println("Class loaded successfully: " + cls.getName());

View File

@@ -32,12 +32,14 @@ public class InputFilesTest {
File singleFeatureSemanticTests = new File("src/test/resources/input/singleFeatureSemanticTests"); File singleFeatureSemanticTests = new File("src/test/resources/input/singleFeatureSemanticTests");
File singleFeatureTests = new File("src/test/resources/input/singleFeatureTests"); File singleFeatureTests = new File("src/test/resources/input/singleFeatureTests");
File typedAstFeatureTests = new File("src/test/resources/input/typedAstFeatureTests"); File typedAstFeatureTests = new File("src/test/resources/input/typedAstFeatureTests");
File finalTest = new File("src/test/resources/input/finalTest");
List<File> files = getJavaFilesFromDirectory(combinedFeatureTests); List<File> files = getJavaFilesFromDirectory(combinedFeatureTests);
// files.addAll(getJavaFilesFromDirectory(endabgabeTests)); // files.addAll(getJavaFilesFromDirectory(endabgabeTests));
// files.addAll(getJavaFilesFromDirectory(singleFeatureSemanticTests)); // files.addAll(getJavaFilesFromDirectory(singleFeatureSemanticTests));
files.addAll(getJavaFilesFromDirectory(singleFeatureTests)); files.addAll(getJavaFilesFromDirectory(singleFeatureTests));
// files.addAll(getJavaFilesFromDirectory(typedAstFeatureTests)); // files.addAll(getJavaFilesFromDirectory(typedAstFeatureTests));
//files.addAll(getJavaFilesFromDirectory(finalTest));
if (!files.isEmpty()) { if (!files.isEmpty()) {
for (File file : files) { for (File file : files) {

View File

@@ -308,31 +308,6 @@ class AstBuilderTest {
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected); assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
} }
@Test
@DisplayName("Null Test")
public void nullTest() {
BlockNode blockCon = new BlockNode();
MemberAccessNode memberAccess = new MemberAccessNode(true);
memberAccess.addIdentifier("a");
AssignableNode assignable = new AssignableNode(memberAccess);
blockCon.addStatement(new AssignNode(assignable, new UnaryNode(new ValueNode(EnumValueNode.NULL_VALUE, "null"))));
blockCon.addStatement(new ReturnNode(null));
ConstructorNode constructor = new ConstructorNode("public", "Null", blockCon);
ClassNode class1 = new ClassNode("public", "Null");
class1.addMember(new FieldNode(new AccessModifierNode("public"), new ReferenceType("Null"), "a"));
class1.addMember(constructor);
ProgramNode expected = new ProgramNode();
expected.addClass(class1);
ASTNode actual = Helper.generateAST(directoryPath + "Null.java");
assertThat(actual).isEqualToComparingFieldByFieldRecursively(expected);
}
@Test @Test
@DisplayName("If Test") @DisplayName("If Test")
public void ifTest() { public void ifTest() {
@@ -428,7 +403,7 @@ class AstBuilderTest {
MemberNode testClassObject = new FieldNode(new AccessModifierNode("public"), new ReferenceType("SelfReference"),"selfReference"); MemberNode testClassObject = new FieldNode(new AccessModifierNode("public"), new ReferenceType("SelfReference"),"selfReference");
BlockNode testMethod1Block = new BlockNode(); BlockNode testMethod1Block = new BlockNode();
testMethod1Block.addStatement(new ReturnNode(new UnaryNode(new MethodCallNode(new TargetNode(true), "testMethod2")))); testMethod1Block.addStatement(new ReturnNode(new UnaryNode(new MethodCallNode(null, "testMethod2"))));
MethodNode testMethod1 = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod1", testMethod1Block); MethodNode testMethod1 = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod1", testMethod1Block);
BlockNode testMethod2Block = new BlockNode(); BlockNode testMethod2Block = new BlockNode();
@@ -438,9 +413,7 @@ class AstBuilderTest {
BlockNode testMethod3Block = new BlockNode(); BlockNode testMethod3Block = new BlockNode();
testMethod3Block.addStatement(new LocalVariableDeclarationNode(new ReferenceType("SelfReference"),"selfReference1", "=", new UnaryNode(new NewDeclarationNode("SelfReference")))); // Assing einfach "=" ? testMethod3Block.addStatement(new LocalVariableDeclarationNode(new ReferenceType("SelfReference"),"selfReference1", "=", new UnaryNode(new NewDeclarationNode("SelfReference")))); // Assing einfach "=" ?
MemberAccessNode methodAccess = new MemberAccessNode(false); MemberAccessNode methodAccess = new MemberAccessNode(false);
methodAccess.addIdentifier("selfReference1"); TargetNode methodTarget = new TargetNode("selfReference1");
methodAccess.addIdentifier("selfReference");
TargetNode methodTarget = new TargetNode(methodAccess);
testMethod3Block.addStatement(new ReturnNode(new UnaryNode(new MethodCallNode(methodTarget,"testMethod1")))); testMethod3Block.addStatement(new ReturnNode(new UnaryNode(new MethodCallNode(methodTarget,"testMethod1"))));
MethodNode testMethod3 = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod3", testMethod3Block); MethodNode testMethod3 = new MethodNode("public", new BaseType(TypeEnum.INT), false, "testMethod3", testMethod3Block);
@@ -610,7 +583,7 @@ class AstBuilderTest {
blockCon.addStatement(new LocalVariableDeclarationNode(new BaseType(TypeEnum.INT), "i", "=", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "10")))); blockCon.addStatement(new LocalVariableDeclarationNode(new BaseType(TypeEnum.INT), "i", "=", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "10"))));
blockCon.addStatement(whileStatement); blockCon.addStatement(whileStatement);
blockCon.addStatement(new ReturnNode(null)); blockCon.addStatement(new ReturnNode(null));
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon); ConstructorNode constructor = new ConstructorNode("public", "While", blockCon);
ClassNode class1 = new ClassNode("public", "While"); ClassNode class1 = new ClassNode("public", "While");
class1.addMember(constructor); class1.addMember(constructor);
@@ -641,7 +614,7 @@ class AstBuilderTest {
blockCon.addStatement(new LocalVariableDeclarationNode(new BaseType(TypeEnum.INT), "i", "=", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "0")))); blockCon.addStatement(new LocalVariableDeclarationNode(new BaseType(TypeEnum.INT), "i", "=", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "0"))));
blockCon.addStatement(blockDoWhile); blockCon.addStatement(blockDoWhile);
blockCon.addStatement(new ReturnNode(null)); blockCon.addStatement(new ReturnNode(null));
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon); ConstructorNode constructor = new ConstructorNode("public", "DoWhile", blockCon);
ClassNode class1 = new ClassNode("public", "DoWhile"); ClassNode class1 = new ClassNode("public", "DoWhile");
class1.addMember(constructor); class1.addMember(constructor);
@@ -660,7 +633,7 @@ class AstBuilderTest {
LocalVariableDeclarationNode forDeclaration = new LocalVariableDeclarationNode(new BaseType(TypeEnum.INT), "i", "=", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "0"))); LocalVariableDeclarationNode forDeclaration = new LocalVariableDeclarationNode(new BaseType(TypeEnum.INT), "i", "=", new UnaryNode(new ValueNode(EnumValueNode.INT_VALUE, "0")));
AssignableNode assignable = new AssignableNode("i"); AssignableNode assignable = new AssignableNode("i");
IncrementNode increment = new IncrementNode(CrementType.SUFFIX, assignable); IncrementNode increment = new IncrementNode(CrementType.PREFIX, assignable);
LocalVariableDeclarationNode declaration = new LocalVariableDeclarationNode(new BaseType(TypeEnum.INT), "a", null, null); LocalVariableDeclarationNode declaration = new LocalVariableDeclarationNode(new BaseType(TypeEnum.INT), "a", null, null);
@@ -677,7 +650,7 @@ class AstBuilderTest {
BlockNode blockCon = new BlockNode(); BlockNode blockCon = new BlockNode();
blockCon.addStatement(forStatement); blockCon.addStatement(forStatement);
blockCon.addStatement(new ReturnNode(null)); blockCon.addStatement(new ReturnNode(null));
ConstructorNode constructor = new ConstructorNode("public", "TestClass", blockCon); ConstructorNode constructor = new ConstructorNode("public", "For", blockCon);
ClassNode class1 = new ClassNode("public", "For"); ClassNode class1 = new ClassNode("public", "For");
class1.addMember(constructor); class1.addMember(constructor);

View File

@@ -352,34 +352,6 @@ public class SemanticTest {
assertNotNull(typedAst); assertNotNull(typedAst);
} }
@Test
@DisplayName("Null Test")
public void nullTest() {
BlockNode blockCon = new BlockNode();
MemberAccessNode memberAccess = new MemberAccessNode(true);
memberAccess.addIdentifier("a");
AssignableNode assignable = new AssignableNode(memberAccess);
blockCon.addStatement(new AssignNode(assignable, new UnaryNode(new ValueNode(EnumValueNode.NULL_VALUE, "null"))));
blockCon.addStatement(new ReturnNode(null));
ConstructorNode constructor = new ConstructorNode("public", "Null", blockCon);
ClassNode class1 = new ClassNode("public", "Null");
class1.addMember(new FieldNode(new AccessModifierNode("public"), new BaseType(TypeEnum.INT), "a"));
class1.addMember(constructor);
ProgramNode abstractSyntaxTree = new ProgramNode();
abstractSyntaxTree.addClass(class1);
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
for (Exception runtimeException : SemanticAnalyzer.errors) {
runtimeException.printStackTrace();
}
assertTrue(SemanticAnalyzer.errors.isEmpty());
assertNotNull(typedAst);
}
@Test @Test
@DisplayName("Self Reference Test") @DisplayName("Self Reference Test")
public void selfReferenceTest() { public void selfReferenceTest() {

View File

@@ -1,25 +0,0 @@
public class Main {
public static void main(String[] args) {
Car myCar = new Car(2020);
int tires = 0;
int year = myCar.getYear();
if (year == 2020) {
tires = 4;
} else {
tires = 2;
}
}
}
class Car {
private int year;
public Car(int year) {
this.year = year;
}
public int getYear() {
return this.year;
}
}

View File

@@ -1,36 +0,0 @@
public class ControlStructures {
public int sum(int a, int b) {
return a + b;
}
public cahr checkNumber(int num) {
if (num > 0) {
return "p";
} else if (num < 0) {
return "n";
} else {
return "z";
}
}
public void printNumbersUpTo(int limit) {
int even = 0;
int uneven = 0;
int i = 0;
while (i < limit) {
if (i % 2 == 0) {
even++;
} else {
uneven = uneven + 1;
}
i++;
}
}
public static void main(String[] args) {
ControlStructures cs = new ControlStructures();
cs.printNumbersUpTo(5);
int result = cs.sum(5, 5);
}
}

View File

@@ -1,11 +0,0 @@
public class Person {
private int age;
public Person(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
}

View File

@@ -0,0 +1,63 @@
public class Car {
public int ps;
public char brand;
public int tires;
public int drivenKilometers;
public Car(int horsePower) {
this.ps = horsePower;
this.tires = 4;
}
public int getTires() {
return this.tires;
}
public boolean isSuperCar() {
if(this.ps > 300) {
return true;
} else {
return false;
}
}
public void tune(int horsePower) {
this.ps = this.ps + horsePower;
}
public void tune() {
this.ps = this.ps + 50;
}
public void driveCircels(int circels) {
for(int i = 0; i < circels; i++) {
this.drivenKilometers = this.drivenKilometers + 5;
}
}
public void isTunable(boolean hasEngine, boolean hasTires) {
if(hasEngine && hasTires) {
this.tune(5);
}
}
public char race() {
int enemyHorsePower = 200;
char win = 'W';
char lose = 'L';
Car enemy = new Car(ps);
if(this.ps > enemyHorsePower) {
return win;
} else {
return lose;
}
}
public int refuel(int currentTank, int maxTank){
int tank = currentTank;
do{
tank++;
}while(tank<maxTank);
return tank;
}
}

View File

@@ -0,0 +1,32 @@
public class Main {
public static void main(String[] args) {
Car vw = new Car(50);
System.out.println(vw.getTires());
System.out.println(vw.isSuperCar());
System.out.println("--------------------------------------");
vw.tune(300);
System.out.println(vw.ps);
System.out.println(vw.isSuperCar());
System.out.println("--------------------------------------");
System.out.println(vw.drivenKilometers);
vw.driveCircels(25);
System.out.println(vw.drivenKilometers);
System.out.println("--------------------------------------");
vw.isTunable(true, true);
System.out.println(vw.ps);
System.out.println("--------------------------------------");
System.out.println(vw.race());
System.out.println("--------------------------------------");
System.out.println(vw.refuel(10, 20));
}
}

View File

@@ -1,7 +1,7 @@
class For{ class For{
public For(){ public For(){
for(int i = 0; i < 10; i++){ for(int i = 0; i < 10; ++i){
int a; int a;
} }
} }

View File

@@ -3,7 +3,7 @@ public class Increment {
public int test; public int test;
public void increment(int p) { public void increment(int p) {
test = p++; this.test = p++;
for(int i = 1; i<=10; i++) { for(int i = 1; i<=10; i++) {
int a = 5; int a = 5;

View File

@@ -1,8 +0,0 @@
class Null{
Null a;
public Null(){
this.a = null;
}
}

View File

@@ -1,18 +1,18 @@
class SelfReference{ class SelfReference {
SelfReference selfReference; SelfReference selfReference;
int testMethod1() { int testMethod1() {
return this.testMethod2(); return testMethod2();
} }
int testMethod2() { int testMethod2() {
return 1; return 1;
} }
int testMethod3(){ int testMethod3() {
SelfReference selfReference1 = new SelfReference(); SelfReference selfReference1 = new SelfReference();
return selfReference1.selfReference.testMethod1(); return selfReference1.testMethod1();
} }
} }

View File

@@ -1,16 +1,8 @@
public class Compiler { public class If {
Node node; public If() {
public int add(int i, int j) { int intValue = 5;
node = new Node(); if(intValue == 5) {
node.x = 1; intValue--;
return i+j; }
}
}
public class Node {
public int x;
public void main() {
Compiler compiler = new Compiler();
int i = compiler.add(5, 8);
} }
} }

View File

View File

@@ -1,70 +0,0 @@
{
"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
}
]
}

View File

@@ -1,133 +0,0 @@
{
"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
}
}
]
}
]
}
]
}

View File

@@ -1 +0,0 @@
{"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"}}}]}]}]}