Merge remote-tracking branch 'origin/Endabgabe' into code-generator
This commit is contained in:
commit
721e1caa79
6
.gitignore
vendored
6
.gitignore
vendored
@ -77,10 +77,10 @@ fabric.properties
|
|||||||
.idea/caches/build_file_checksums.ser
|
.idea/caches/build_file_checksums.ser
|
||||||
|
|
||||||
/target
|
/target
|
||||||
src/main/resources/logs/RaupenLog.log
|
src/main/resources/logs/miniCompilerLog.log
|
||||||
src/main/resources/output/CompilerInput.class
|
src/main/resources/output/CompilerInput.class
|
||||||
src/test/resources/output/javac/CompilerInput$Test.class
|
src/test/resources/output/javac/CompilerInput$Test.class
|
||||||
src/test/resources/output/javac/CompilerInput.class
|
src/test/resources/output/javac/CompilerInput.class
|
||||||
src/test/resources/output/raupenpiler/CompilerInput.class
|
src/test/resources/output/miniCompiler/CompilerInput.class
|
||||||
src/test/resources/output/raupenpiler/CompilerInput$Test.class
|
src/test/resources/output/miniCompiler/CompilerInput$Test.class
|
||||||
.idea/inspectionProfiles/Project_Default.xml
|
.idea/inspectionProfiles/Project_Default.xml
|
||||||
|
149
README.md
Normal file
149
README.md
Normal file
@ -0,0 +1,149 @@
|
|||||||
|
# "Nicht Haskel 2.0" Java Compiler
|
||||||
|
|
||||||
|
Realisation of a subset of the Java Standard Compiler in the course Compiler Construction of the 4th semester Computer Science at the Duale Hochschule Stuttgart (Horb).
|
||||||
|
|
||||||
|
This project aims to provide a simplified version of the Java compiler, focusing on key language features and demonstrating the principles of compiler construction.
|
||||||
|
|
||||||
|
## Realised Java syntax
|
||||||
|
|
||||||
|
- **Data types**: `int`, `boolean`, `char`
|
||||||
|
- **Access modifier**: `public`, `protected`, `private`
|
||||||
|
- **Operators**: `=` `+` `-` `*` `%` `/` `>` `<` `>=` `<=` `==` `!=` `!` `&&` `||` `++` `--`
|
||||||
|
- **Keywords**: `class`, `this`, `while`, `do`, `if`, `else`, `for`, `return`, `new`, `switch`, `case`, `break`, `default`, `:`
|
||||||
|
- **Statements**:
|
||||||
|
- `if` ... `if else` ... `else`;
|
||||||
|
- `while` ... ;
|
||||||
|
- `do` ... `while`;
|
||||||
|
- `for`;
|
||||||
|
- `switch` ... `case` ... ;
|
||||||
|
- **Comments**:
|
||||||
|
- Single line: `// comment`
|
||||||
|
- Multi-line: `/* comment */`
|
||||||
|
- **Further functions**:
|
||||||
|
- All methods are overloadable
|
||||||
|
- High maintainability and expandability through implementation of the visitor pattern
|
||||||
|
- Logging Input and Outputs
|
||||||
|
- Error Handling in the Semantic Check
|
||||||
|
|
||||||
|
## Project Structure
|
||||||
|
|
||||||
|
```plain
|
||||||
|
src/
|
||||||
|
└── main/
|
||||||
|
├── java/
|
||||||
|
│ ├── ast/ -> Defining the structure of the AST
|
||||||
|
│ ├── bytecode/ -> Generate Java bytecode
|
||||||
|
│ ├── main/ -> Running the compiler
|
||||||
|
│ ├── parser/
|
||||||
|
│ │ ├── astBuilder/ -> Builder creating the AST
|
||||||
|
│ │ ├── generated/ -> Antlr generated grammar
|
||||||
|
│ │ └── grammar/ -> Antlr grammar
|
||||||
|
│ ├── semantic/ -> Running the semantic check
|
||||||
|
│ └── visitor/ -> Visitor interface
|
||||||
|
└── resources/
|
||||||
|
test/
|
||||||
|
└── java/
|
||||||
|
│ ├── main/ -> Running E2E tests
|
||||||
|
│ ├── parser/ -> Performs tests on the parser
|
||||||
|
│ ├── semantic/ -> Performs tests on the semantic check
|
||||||
|
└── resources/ -> Ressources for running the Tests
|
||||||
|
```
|
||||||
|
|
||||||
|
## Class-Diagramm AST
|
||||||
|
|
||||||
|
![AST Diagramm](ast.png)
|
||||||
|
|
||||||
|
## Distribution of the realisation
|
||||||
|
|
||||||
|
### i22030 & i22035
|
||||||
|
|
||||||
|
Parser:
|
||||||
|
- Grammar -> (src/main/java/parser/grammar)
|
||||||
|
- Scanner
|
||||||
|
- Parser
|
||||||
|
- Abstract Syntax Tree (AST) -> (src/main/java/ast)
|
||||||
|
- AstBuilder -> (src/main/java/parser/astBuilder)
|
||||||
|
|
||||||
|
Parser tests:
|
||||||
|
- ParserTests -> (src/test/java/parser)
|
||||||
|
- TestCases -> (src/test/resources/input/singeFeatureTests)
|
||||||
|
|
||||||
|
Other:
|
||||||
|
- Documentation -> (README.md)
|
||||||
|
- Ast Class-Diagramm -> (ast.png)
|
||||||
|
- PowerPoint
|
||||||
|
|
||||||
|
### i22005
|
||||||
|
Semantic check:
|
||||||
|
- Set all types and check whether types have been used correctly
|
||||||
|
- Contexts -> (src/main/java/semantic/context)
|
||||||
|
- Exceptions Handling -> (src/main/java/semantic/exceptions)
|
||||||
|
|
||||||
|
Semantic Tests:
|
||||||
|
- Typing and Type checking -> (src/test/java/semantic/EndToTypedAstTest)
|
||||||
|
- Exception and feature test -> (src/test/resources/input/typedAstExceptionsTests)
|
||||||
|
|
||||||
|
### i22007
|
||||||
|
Bytecode generation:
|
||||||
|
- Complete bytecode generation -> (src/mein/java/bytecode)
|
||||||
|
|
||||||
|
### i22011
|
||||||
|
Tests and execution:
|
||||||
|
- Makefile
|
||||||
|
- Running Compiler -> (src/main/main)
|
||||||
|
- Running E2E tests -> (src/test/main)
|
||||||
|
- 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
|
||||||
|
|
||||||
|
- [Maven 4.0](https://maven.apache.org/index.html)
|
||||||
|
- Used for automating the build process and managing dependencies.
|
||||||
|
- [ANTLR4 v.13.1](https://www.antlr.org/)
|
||||||
|
- Used to parse the input Java code into the Abstract Syntax Tree.
|
||||||
|
|
||||||
|
## How to run the compiler
|
||||||
|
### Possibilities
|
||||||
|
### 1. Start miniCompiler using make:
|
||||||
|
Make needs to be installed
|
||||||
|
```bash
|
||||||
|
cd .\src\test\ ; make clean compile-miniCompiler
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. Start miniCompiler using jar:
|
||||||
|
If you do not have the .jar, download it [here](https://gitea.hb.dhbw-stuttgart.de/i22005/NichtHaskell2.0/src/branch/Endabgabe/src) or compile it using mvn package or make first
|
||||||
|
```
|
||||||
|
java.exe -DgenJar=bool -DgenClass=bool -jar path_to_jar\jarName.jar 'path_to_input_file.java' 'path_to_output_directory'
|
||||||
|
```
|
||||||
|
|
||||||
|
Example (jar needs to be in the target directory)
|
||||||
|
```bash
|
||||||
|
java.exe -DgenJar=true -DgenClass=true -jar .\target\JavaCompiler-1.0-jar-with-dependencies.jar 'src/main/resources/input/CompilerInput.java' 'src/main/resources/output'
|
||||||
|
```
|
||||||
|
|
||||||
|
- set DgenJar true, to generate the jar, false for no jar
|
||||||
|
|
||||||
|
```
|
||||||
|
DgenJar=true
|
||||||
|
```
|
||||||
|
|
||||||
|
- set DgenClass true, to generate class files, false for no class files
|
||||||
|
|
||||||
|
```
|
||||||
|
DgenClass=true
|
||||||
|
```
|
||||||
|
|
||||||
|
## How to run tests
|
||||||
|
|
||||||
|
```bash
|
||||||
|
mvn test
|
||||||
|
```
|
||||||
|
Or start them manually in your IDE
|
68
readme.md
68
readme.md
@ -1,68 +0,0 @@
|
|||||||
# "Nicht Haskel 2.0" Java Compiler
|
|
||||||
|
|
||||||
Realisation of a subset of the Java Standard Compiler in the course Compiler Construction of the 4th semester Computer Science at the Duale Hochschule Suttgart (Horb).
|
|
||||||
|
|
||||||
This project aims to provide a simplified version of the Java compiler, focusing on key language features and demonstrating the principles of compiler construction.
|
|
||||||
|
|
||||||
## Realised Java syntax
|
|
||||||
|
|
||||||
- **Data types**: `int`, `boolean`, `char`
|
|
||||||
- **Access modifier**: `public`, `protected`, `private`
|
|
||||||
- **Operators**: `=` `+` `-` `*` `%` `/` `>` `<` `>=` `<=` `==` `!=` `!` `&&` `||` `++` `--`
|
|
||||||
- **Keywords**: `class`, `this`, `while`, `do`, `if`, `else`, `for`, `return`, `new`, `switch`, `case`, `break`, `default`, `:`
|
|
||||||
- **Statements**:
|
|
||||||
- `if` ... `if else` ... `else`;
|
|
||||||
- `while` ... ;
|
|
||||||
- `do` ... `while`;
|
|
||||||
- `for`;
|
|
||||||
- `switch` ... `case` ... ;
|
|
||||||
- **Comments**:
|
|
||||||
- Single line: `// comment`
|
|
||||||
- Multi-line: `/* comment */`
|
|
||||||
- **Further functions**:
|
|
||||||
- All methods are overloadable
|
|
||||||
- High maintainability and expandability through implementation of the visitor pattern
|
|
||||||
- Logging Input and Outputs
|
|
||||||
- Error Handling in the Semantic Check
|
|
||||||
|
|
||||||
## Project Structure
|
|
||||||
|
|
||||||
```plain
|
|
||||||
src/
|
|
||||||
└── main/
|
|
||||||
├── java/
|
|
||||||
│ ├── ast/ -> Defining the structure of the AST
|
|
||||||
│ ├── bytecode/ -> Generate Java bytecode
|
|
||||||
│ ├── main/ -> Running the compiler
|
|
||||||
│ ├── parser/
|
|
||||||
│ │ ├── astBuilder/ -> Builder creating the AST
|
|
||||||
│ │ ├── generated/ -> Antlr generated grammar
|
|
||||||
│ │ └── grammar/ -> Antlr grammar
|
|
||||||
│ ├── semantic/ -> Running the semantic check
|
|
||||||
│ └── visitor/ -> Visitor interface
|
|
||||||
└── resources/
|
|
||||||
test/
|
|
||||||
└── java/
|
|
||||||
│ ├── main/ -> MainTest, E2ETests, UtilityTests
|
|
||||||
│ ├── parser/ -> Performs tests on the parser
|
|
||||||
│ └── semantic/ -> Performs tests on the semantic check
|
|
||||||
└── resources/ -> Ressources for running the Tests
|
|
||||||
```
|
|
||||||
|
|
||||||
## Class-Diagramm AST
|
|
||||||
|
|
||||||
![AST Diagramm](ast.png)
|
|
||||||
|
|
||||||
## Used Tools
|
|
||||||
|
|
||||||
- [Maven 4.0](https://maven.apache.org/index.html)
|
|
||||||
- Used for automating the build process and managing dependencies.
|
|
||||||
- [ANTLR4 v.13.1](https://www.antlr.org/)
|
|
||||||
- Used to parse the input Java code into the Abstract Syntax Tree.
|
|
||||||
|
|
||||||
|
|
||||||
## How to run the compiler
|
|
||||||
|
|
||||||
## Download
|
|
||||||
|
|
||||||
```bash
|
|
@ -1,20 +1,6 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
import bytecode.visitor.ClassVisitor;
|
|
||||||
import semantic.SemanticVisitor;
|
|
||||||
import typechecker.TypeCheckResult;
|
|
||||||
|
|
||||||
public interface ASTNode {
|
public interface ASTNode {
|
||||||
|
|
||||||
//Todo: @BruderJohn & @i22007 Interface anwenden + geeignetetn Methodename.
|
|
||||||
|
|
||||||
/*
|
|
||||||
Typecheck:
|
|
||||||
public TypeCheckResult acceptType(SemanticVisitor visitor);
|
|
||||||
|
|
||||||
Bytecode:
|
|
||||||
public void accepByteCode(ClassVisitor classVisitor);
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
package ast;
|
package ast;
|
||||||
|
|
||||||
import ast.type.AccessModifierNode;
|
import ast.type.AccessModifierNode;
|
||||||
import ast.members.ConstructorNode;
|
|
||||||
import ast.members.MemberNode;
|
import ast.members.MemberNode;
|
||||||
import ast.members.MethodNode;
|
import ast.members.MethodNode;
|
||||||
import bytecode.visitor.ClassVisitor;
|
import bytecode.visitor.ClassVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
import visitor.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -17,11 +16,11 @@ public class ClassNode implements ASTNode, Visitable {
|
|||||||
public String identifier;
|
public String identifier;
|
||||||
public List<MemberNode> members = new ArrayList<>();
|
public List<MemberNode> members = new ArrayList<>();
|
||||||
|
|
||||||
public ClassNode(){
|
public ClassNode() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public ClassNode(String accessType, String identifier){
|
public ClassNode(String accessType, String identifier) {
|
||||||
this.accessType = new AccessModifierNode(accessType);
|
this.accessType = new AccessModifierNode(accessType);
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
}
|
}
|
||||||
@ -30,7 +29,7 @@ public class ClassNode implements ASTNode, Visitable {
|
|||||||
members.add(member);
|
members.add(member);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<MethodNode> getMethods(){
|
public List<MethodNode> getMethods() {
|
||||||
List<MethodNode> methods = new ArrayList<>();
|
List<MethodNode> methods = new ArrayList<>();
|
||||||
for (MemberNode member : members) {
|
for (MemberNode member : members) {
|
||||||
if (member instanceof MethodNode methodNode) {
|
if (member instanceof MethodNode methodNode) {
|
||||||
|
@ -2,7 +2,7 @@ package ast;
|
|||||||
|
|
||||||
import bytecode.visitor.ProgramVisitor;
|
import bytecode.visitor.ProgramVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
import visitor.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -4,12 +4,12 @@ import ast.expressions.IExpressionNode;
|
|||||||
import ast.type.type.*;
|
import ast.type.type.*;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
import visitor.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class BinaryNode implements IExpressionNode, Visitable {
|
public class BinaryNode implements IExpressionNode, Visitable {
|
||||||
|
|
||||||
private ITypeNode typeNode;
|
public ITypeNode typeNode;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
package ast.expressions.binaryexpressions;
|
package ast.expressions.binaryexpressions;
|
||||||
|
|
||||||
import ast.type.type.*;
|
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
|
|
||||||
public class CalculationNode extends BinaryNode {
|
public class CalculationNode extends BinaryNode {
|
||||||
public CalculationNode calculationExpression;
|
public CalculationNode calculationExpression;
|
||||||
@ -20,11 +19,11 @@ public class CalculationNode extends BinaryNode {
|
|||||||
this.dotExpression = dotExpression;
|
this.dotExpression = dotExpression;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setOperator(String operator) {
|
public void setOperator(String operator) {
|
||||||
if(operator != null) {
|
if (operator != null) {
|
||||||
if(operator.equals("+")) {
|
if (operator.equals("+")) {
|
||||||
this.operator = EnumLineOperator.PLUS;
|
this.operator = EnumLineOperator.PLUS;
|
||||||
} else if(operator.equals("-")) {
|
} else if (operator.equals("-")) {
|
||||||
this.operator = EnumLineOperator.MINUS;
|
this.operator = EnumLineOperator.MINUS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,30 +2,28 @@ package ast.expressions.binaryexpressions;
|
|||||||
|
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
|
|
||||||
public class DotNode extends BinaryNode {
|
public class DotNode extends BinaryNode {
|
||||||
public DotNode dotExpression;
|
public DotNode dotExpression;
|
||||||
public EnumDotOperator operator;
|
public EnumDotOperator operator;
|
||||||
public DotSubstractionNode dotSubstractionExpression;
|
public DotSubtractionNode dotSubtractionExpression;
|
||||||
|
|
||||||
public DotNode(DotNode dotExpression, String operator, DotSubstractionNode dotSubstractionExpression) {
|
public DotNode(DotNode dotExpression, String operator, DotSubtractionNode dotSubtractionExpression) {
|
||||||
this.dotExpression = dotExpression;
|
this.dotExpression = dotExpression;
|
||||||
setOperator(operator);
|
setOperator(operator);
|
||||||
this.dotSubstractionExpression = dotSubstractionExpression;
|
this.dotSubtractionExpression = dotSubtractionExpression;
|
||||||
}
|
}
|
||||||
|
|
||||||
public DotNode(DotSubstractionNode dotSubstractionExpression) {
|
public DotNode(DotSubtractionNode dotSubtractionExpression) {
|
||||||
this.dotSubstractionExpression = dotSubstractionExpression;
|
this.dotSubtractionExpression = dotSubtractionExpression;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setOperator(String operator) {
|
public void setOperator(String operator) {
|
||||||
if(operator.equals("*")) {
|
switch (operator) {
|
||||||
this.operator = EnumDotOperator.MULT;
|
case "*" -> this.operator = EnumDotOperator.MULT;
|
||||||
} else if(operator.equals("/")) {
|
case "/" -> this.operator = EnumDotOperator.DIV;
|
||||||
this.operator = EnumDotOperator.DIV;
|
case "%" -> this.operator = EnumDotOperator.MOD;
|
||||||
} else if(operator.equals("%")) {
|
|
||||||
this.operator = EnumDotOperator.MOD;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,45 +1,44 @@
|
|||||||
package ast.expressions.binaryexpressions;
|
package ast.expressions.binaryexpressions;
|
||||||
|
|
||||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
import ast.expressions.unaryexpressions.MemberAccessNode;
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
||||||
import ast.type.type.*;
|
import ast.type.ValueNode;
|
||||||
import ast.type.ValueNode;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.TypeCheckResult;
|
||||||
import typechecker.TypeCheckResult;
|
|
||||||
|
public class DotSubtractionNode extends BinaryNode {
|
||||||
public class DotSubstractionNode extends BinaryNode {
|
public ValueNode value;
|
||||||
public ValueNode value;
|
public String identifier;
|
||||||
public String identifier;
|
public MemberAccessNode memberAccess;
|
||||||
public MemberAccessNode memberAccess;
|
public MethodCallNode methodCall;
|
||||||
public MethodCallNode methodCall;
|
public CalculationNode calculationExpression;
|
||||||
public CalculationNode calculationExpression;
|
|
||||||
|
public DotSubtractionNode(ValueNode value) {
|
||||||
public DotSubstractionNode(ValueNode value) {
|
this.value = value;
|
||||||
this.value = value;
|
}
|
||||||
}
|
|
||||||
|
public DotSubtractionNode(String identifier) {
|
||||||
public DotSubstractionNode(String identifier) {
|
this.identifier = identifier;
|
||||||
this.identifier = identifier;
|
}
|
||||||
}
|
|
||||||
|
public DotSubtractionNode(MemberAccessNode memberAccess) {
|
||||||
public DotSubstractionNode(MemberAccessNode memberAccess) {
|
this.memberAccess = memberAccess;
|
||||||
this.memberAccess = memberAccess;
|
}
|
||||||
}
|
|
||||||
|
public DotSubtractionNode(MethodCallNode methodCall, CalculationNode calculationExpression) {
|
||||||
public DotSubstractionNode(MethodCallNode methodCall, CalculationNode calculationExpression) {
|
this.methodCall = methodCall;
|
||||||
this.methodCall = methodCall;
|
this.calculationExpression = calculationExpression;
|
||||||
this.calculationExpression = calculationExpression;
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
@Override
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
return visitor.analyze(this);
|
||||||
return visitor.analyze(this);
|
}
|
||||||
}
|
|
||||||
|
@Override
|
||||||
@Override
|
public void accept(MethodVisitor methodVisitor) {
|
||||||
public void accept(MethodVisitor methodVisitor) {
|
methodVisitor.visit(this);
|
||||||
methodVisitor.visit(this);
|
}
|
||||||
}
|
|
||||||
|
}
|
||||||
}
|
|
@ -2,10 +2,9 @@ package ast.expressions.binaryexpressions;
|
|||||||
|
|
||||||
import ast.expressions.IExpressionNode;
|
import ast.expressions.IExpressionNode;
|
||||||
import ast.expressions.unaryexpressions.UnaryNode;
|
import ast.expressions.unaryexpressions.UnaryNode;
|
||||||
import ast.type.type.*;
|
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
|
|
||||||
public class NonCalculationNode extends BinaryNode {
|
public class NonCalculationNode extends BinaryNode {
|
||||||
public UnaryNode unaryExpression;
|
public UnaryNode unaryExpression;
|
||||||
@ -18,23 +17,16 @@ public class NonCalculationNode extends BinaryNode {
|
|||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setOperator(String operator) {
|
public void setOperator(String operator) {
|
||||||
if(operator.equals("&&")) {
|
switch (operator) {
|
||||||
this.operator = EnumNonCalculationOperator.AND;
|
case "&&" -> this.operator = EnumNonCalculationOperator.AND;
|
||||||
} else if(operator.equals("||")) {
|
case "||" -> this.operator = EnumNonCalculationOperator.OR;
|
||||||
this.operator = EnumNonCalculationOperator.OR;
|
case ">" -> this.operator = EnumNonCalculationOperator.GREATER;
|
||||||
} else if(operator.equals(">")) {
|
case "<" -> this.operator = EnumNonCalculationOperator.LESS;
|
||||||
this.operator = EnumNonCalculationOperator.GREATER;
|
case ">=" -> this.operator = EnumNonCalculationOperator.GREATER_EQUAL;
|
||||||
} else if(operator.equals("<")) {
|
case "<=" -> this.operator = EnumNonCalculationOperator.LESS_EQUAL;
|
||||||
this.operator = EnumNonCalculationOperator.LESS;
|
case "==" -> this.operator = EnumNonCalculationOperator.EQUAL;
|
||||||
} else if(operator.equals(">=")) {
|
case "!=" -> this.operator = EnumNonCalculationOperator.NOT_EQUAL;
|
||||||
this.operator = EnumNonCalculationOperator.GREATER_EQUAL;
|
|
||||||
} else if(operator.equals("<=")) {
|
|
||||||
this.operator = EnumNonCalculationOperator.LESS_EQUAL;
|
|
||||||
} else if(operator.equals("==")) {
|
|
||||||
this.operator = EnumNonCalculationOperator.EQUAL;
|
|
||||||
} else if(operator.equals("!=")) {
|
|
||||||
this.operator = EnumNonCalculationOperator.NOT_EQUAL;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,9 +2,9 @@ package ast.expressions.unaryexpressions;
|
|||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import ast.type.type.ITypeNode;
|
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
import visitor.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -13,7 +13,6 @@ import java.util.List;
|
|||||||
public class MemberAccessNode implements ASTNode, Visitable {
|
public class MemberAccessNode implements ASTNode, Visitable {
|
||||||
public Boolean thisExpr;
|
public Boolean thisExpr;
|
||||||
public List<String> identifiers = new ArrayList<>();
|
public List<String> identifiers = new ArrayList<>();
|
||||||
private ITypeNode typeNode;
|
|
||||||
|
|
||||||
public MemberAccessNode(Boolean thisExpr) {
|
public MemberAccessNode(Boolean thisExpr) {
|
||||||
this.thisExpr = thisExpr;
|
this.thisExpr = thisExpr;
|
||||||
@ -27,17 +26,10 @@ public class MemberAccessNode implements ASTNode, Visitable {
|
|||||||
public void accept(MethodVisitor methodVisitor) {
|
public void accept(MethodVisitor methodVisitor) {
|
||||||
methodVisitor.visit(this);
|
methodVisitor.visit(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
return visitor.analyze(this);
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ITypeNode getTypeNode() {
|
|
||||||
return typeNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTypeNode(ITypeNode typeNode) {
|
|
||||||
this.typeNode = typeNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
package ast.expressions.unaryexpressions;
|
package ast.expressions.unaryexpressions;
|
||||||
|
|
||||||
import ast.ASTNode;
|
|
||||||
import ast.expressions.IExpressionNode;
|
import ast.expressions.IExpressionNode;
|
||||||
|
import ast.type.type.ITypeNode;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
import visitor.Visitable;
|
|
||||||
|
|
||||||
public class NotNode implements ASTNode, Visitable {
|
public class NotNode implements IExpressionNode {
|
||||||
public IExpressionNode expression;
|
public IExpressionNode expression;
|
||||||
|
|
||||||
public NotNode(IExpressionNode expression) {
|
public NotNode(IExpressionNode expression) {
|
||||||
@ -24,4 +23,13 @@ public class NotNode implements ASTNode, Visitable {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
@Override
|
||||||
|
public ITypeNode getType() {
|
||||||
|
return expression.getType();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setType(ITypeNode type) {
|
||||||
|
this.expression.setType(type);
|
||||||
|
}
|
||||||
|
}
|
@ -6,7 +6,7 @@ import ast.type.type.*;
|
|||||||
import ast.type.ValueNode;
|
import ast.type.ValueNode;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
|
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
@ -18,10 +18,10 @@ public class UnaryNode implements IExpressionNode {
|
|||||||
public NotNode notExpression;
|
public NotNode notExpression;
|
||||||
public IStatementNode statement;
|
public IStatementNode statement;
|
||||||
public IExpressionNode expression;
|
public IExpressionNode expression;
|
||||||
private ITypeNode type;
|
public ITypeNode type;
|
||||||
|
|
||||||
public UnaryNode(String value) {
|
public UnaryNode(String value) {
|
||||||
if(Objects.equals(value, "this")) {
|
if (Objects.equals(value, "this")) {
|
||||||
this.thisExp = "this";
|
this.thisExp = "this";
|
||||||
} else {
|
} else {
|
||||||
this.identifier = value;
|
this.identifier = value;
|
||||||
|
@ -1,11 +0,0 @@
|
|||||||
package ast.literal;
|
|
||||||
|
|
||||||
public class BooleanLiteralNode {
|
|
||||||
private String value;
|
|
||||||
|
|
||||||
public BooleanLiteralNode(String value) {this.value = value;}
|
|
||||||
|
|
||||||
public String getValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,11 +0,0 @@
|
|||||||
package ast.literal;
|
|
||||||
|
|
||||||
public class CharLiteralNode {
|
|
||||||
public String value;
|
|
||||||
|
|
||||||
public CharLiteralNode(String value) {this.value = value;}
|
|
||||||
|
|
||||||
public String getValue() {
|
|
||||||
return value;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,31 +0,0 @@
|
|||||||
package ast.literal;
|
|
||||||
|
|
||||||
import ast.expressions.IExpressionNode;
|
|
||||||
import ast.type.type.ITypeNode;
|
|
||||||
import semantic.SemanticVisitor;
|
|
||||||
import typechecker.TypeCheckResult;
|
|
||||||
|
|
||||||
public class LiteralNode implements IExpressionNode {
|
|
||||||
|
|
||||||
public String value;
|
|
||||||
private ITypeNode type;
|
|
||||||
|
|
||||||
public LiteralNode(String value, ITypeNode type) {
|
|
||||||
this.value = value;
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public ITypeNode getType() {
|
|
||||||
return type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setType(ITypeNode type) {
|
|
||||||
this.type = type;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public TypeCheckResult accept(SemanticVisitor visitor) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
|
@ -32,8 +32,7 @@ public class ConstructorNode extends MethodNode implements Visitable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSame(MethodNode methodNode) {
|
public boolean isSame(MethodNode methodNode) {
|
||||||
if (!(Objects.equals(this.identifier, methodNode.getIdentifier()))
|
if (!(Objects.equals(this.identifier, methodNode.getIdentifier())) || getParameters().size() != methodNode.getParameters().size()) {
|
||||||
|| getParameters().size() != methodNode.getParameters().size()) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,9 +3,8 @@ package ast.members;
|
|||||||
import ast.type.AccessModifierNode;
|
import ast.type.AccessModifierNode;
|
||||||
import ast.type.type.ITypeNode;
|
import ast.type.type.ITypeNode;
|
||||||
import bytecode.visitor.ClassVisitor;
|
import bytecode.visitor.ClassVisitor;
|
||||||
import bytecode.visitor.MethodVisitor;
|
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
import visitor.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class FieldNode implements MemberNode, Visitable {
|
public class FieldNode implements MemberNode, Visitable {
|
||||||
@ -13,7 +12,7 @@ public class FieldNode implements MemberNode, Visitable {
|
|||||||
public ITypeNode type;
|
public ITypeNode type;
|
||||||
public String identifier;
|
public String identifier;
|
||||||
|
|
||||||
public FieldNode(AccessModifierNode accessTypeNode, ITypeNode type, String name){
|
public FieldNode(AccessModifierNode accessTypeNode, ITypeNode type, String name) {
|
||||||
this.accessTypeNode = accessTypeNode;
|
this.accessTypeNode = accessTypeNode;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.identifier = name;
|
this.identifier = name;
|
||||||
|
@ -10,7 +10,8 @@ import ast.ASTNode;
|
|||||||
@JsonSubTypes({
|
@JsonSubTypes({
|
||||||
@JsonSubTypes.Type(value = MethodNode.class, name = "Method"),
|
@JsonSubTypes.Type(value = MethodNode.class, name = "Method"),
|
||||||
|
|
||||||
@JsonSubTypes.Type(value = FieldNode.class, name = "Field") }
|
@JsonSubTypes.Type(value = FieldNode.class, name = "Field")}
|
||||||
)
|
)
|
||||||
|
|
||||||
public interface MemberNode extends ASTNode {}
|
public interface MemberNode extends ASTNode {
|
||||||
|
}
|
||||||
|
@ -6,7 +6,7 @@ import ast.type.AccessModifierNode;
|
|||||||
import ast.type.type.*;
|
import ast.type.type.*;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
import visitor.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -15,16 +15,16 @@ import java.util.Objects;
|
|||||||
|
|
||||||
public class MethodNode implements MemberNode, Visitable {
|
public class MethodNode implements MemberNode, Visitable {
|
||||||
public AccessModifierNode accesModifier;
|
public AccessModifierNode accesModifier;
|
||||||
private ITypeNode type;
|
public ITypeNode type;
|
||||||
public Boolean voidType;
|
public Boolean voidType;
|
||||||
private String identifier;
|
public String identifier;
|
||||||
public List<ParameterNode> parameters = new ArrayList<>();
|
public List<ParameterNode> parameters = new ArrayList<>();
|
||||||
public BlockNode block;
|
public BlockNode block;
|
||||||
|
|
||||||
public MethodNode() {
|
public MethodNode() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public MethodNode(String accessModifier, ITypeNode type, Boolean voidType, String identifier, BlockNode block){
|
public MethodNode(String accessModifier, ITypeNode type, Boolean voidType, String identifier, BlockNode block) {
|
||||||
this.accesModifier = new AccessModifierNode(accessModifier);
|
this.accesModifier = new AccessModifierNode(accessModifier);
|
||||||
this.type = type;
|
this.type = type;
|
||||||
this.voidType = voidType;
|
this.voidType = voidType;
|
||||||
@ -40,7 +40,7 @@ public class MethodNode implements MemberNode, Visitable {
|
|||||||
return parameters;
|
return parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isSame(MethodNode methodNode){
|
public boolean isSame(MethodNode methodNode) {
|
||||||
if (!(Objects.equals(this.identifier, methodNode.getIdentifier())) || type.equals(methodNode.type)
|
if (!(Objects.equals(this.identifier, methodNode.getIdentifier())) || type.equals(methodNode.type)
|
||||||
|| getParameters().size() != methodNode.getParameters().size()) {
|
|| getParameters().size() != methodNode.getParameters().size()) {
|
||||||
return false;
|
return false;
|
||||||
|
@ -3,7 +3,7 @@ package ast.parameters;
|
|||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.type.type.*;
|
import ast.type.type.*;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
import visitor.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class ParameterNode implements ASTNode, Visitable {
|
public class ParameterNode implements ASTNode, Visitable {
|
||||||
|
@ -3,7 +3,7 @@ package ast.statementexpressions;
|
|||||||
import ast.expressions.IExpressionNode;
|
import ast.expressions.IExpressionNode;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
|
|
||||||
public class AssignNode implements IStatementExpressionNode {
|
public class AssignNode implements IStatementExpressionNode {
|
||||||
public AssignableNode assignable;
|
public AssignableNode assignable;
|
||||||
|
@ -3,11 +3,11 @@ package ast.statementexpressions;
|
|||||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
import ast.expressions.unaryexpressions.MemberAccessNode;
|
||||||
import ast.type.type.ITypeNode;
|
import ast.type.type.ITypeNode;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
|
|
||||||
public class AssignableNode implements IStatementExpressionNode {
|
public class AssignableNode implements IStatementExpressionNode {
|
||||||
public String identifier;
|
public String identifier;
|
||||||
private ITypeNode typeNode;
|
public ITypeNode typeNode;
|
||||||
|
|
||||||
public MemberAccessNode memberAccess;
|
public MemberAccessNode memberAccess;
|
||||||
|
|
||||||
@ -24,10 +24,6 @@ public class AssignableNode implements IStatementExpressionNode {
|
|||||||
return visitor.analyze(this);
|
return visitor.analyze(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ITypeNode getTypeNode() {
|
|
||||||
return typeNode;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setTypeNode(ITypeNode typeNode) {
|
public void setTypeNode(ITypeNode typeNode) {
|
||||||
this.typeNode = typeNode;
|
this.typeNode = typeNode;
|
||||||
}
|
}
|
||||||
|
@ -2,4 +2,5 @@ package ast.statementexpressions;
|
|||||||
|
|
||||||
import ast.statements.IStatementNode;
|
import ast.statements.IStatementNode;
|
||||||
|
|
||||||
public interface IStatementExpressionNode extends IStatementNode {}
|
public interface IStatementExpressionNode extends IStatementNode {
|
||||||
|
}
|
||||||
|
@ -3,7 +3,7 @@ package ast.statementexpressions;
|
|||||||
import ast.expressions.IExpressionNode;
|
import ast.expressions.IExpressionNode;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -2,10 +2,8 @@ package ast.statementexpressions.crementexpressions;
|
|||||||
|
|
||||||
import ast.statementexpressions.AssignableNode;
|
import ast.statementexpressions.AssignableNode;
|
||||||
import ast.statementexpressions.IStatementExpressionNode;
|
import ast.statementexpressions.IStatementExpressionNode;
|
||||||
import bytecode.visitor.MethodVisitor;
|
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
import visitor.Visitable;
|
|
||||||
|
|
||||||
public class DecrementNode implements IStatementExpressionNode, Visitable {
|
public class DecrementNode implements IStatementExpressionNode, Visitable {
|
||||||
public CrementType crementType;
|
public CrementType crementType;
|
||||||
|
@ -4,8 +4,8 @@ import ast.statementexpressions.AssignableNode;
|
|||||||
import ast.statementexpressions.IStatementExpressionNode;
|
import ast.statementexpressions.IStatementExpressionNode;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
|
||||||
import visitor.Visitable;
|
import visitor.Visitable;
|
||||||
|
import semantic.TypeCheckResult;
|
||||||
|
|
||||||
public class IncrementNode implements IStatementExpressionNode, Visitable {
|
public class IncrementNode implements IStatementExpressionNode, Visitable {
|
||||||
public CrementType crementType;
|
public CrementType crementType;
|
||||||
|
@ -4,7 +4,7 @@ import ast.ASTNode;
|
|||||||
import ast.expressions.IExpressionNode;
|
import ast.expressions.IExpressionNode;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
import visitor.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -5,7 +5,7 @@ import ast.statements.IStatementNode;
|
|||||||
import ast.type.type.ITypeNode;
|
import ast.type.type.ITypeNode;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -3,9 +3,8 @@ package ast.statementexpressions.methodcallstatementnexpressions;
|
|||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
import ast.expressions.unaryexpressions.MemberAccessNode;
|
||||||
import ast.statementexpressions.NewDeclarationNode;
|
import ast.statementexpressions.NewDeclarationNode;
|
||||||
import bytecode.visitor.MethodVisitor;
|
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
import visitor.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class TargetNode implements ASTNode, Visitable {
|
public class TargetNode implements ASTNode, Visitable {
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
package ast.statements;
|
package ast.statements;
|
||||||
|
|
||||||
import ast.ASTNode;
|
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
import visitor.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
@ -11,7 +10,8 @@ import java.util.List;
|
|||||||
public class BlockNode implements IStatementNode, Visitable {
|
public class BlockNode implements IStatementNode, Visitable {
|
||||||
public List<IStatementNode> statements = new ArrayList<>();
|
public List<IStatementNode> statements = new ArrayList<>();
|
||||||
|
|
||||||
public BlockNode() {}
|
public BlockNode() {
|
||||||
|
}
|
||||||
|
|
||||||
public void addStatement(IStatementNode statement) {
|
public void addStatement(IStatementNode statement) {
|
||||||
statements.add(statement);
|
statements.add(statement);
|
||||||
|
@ -1,10 +0,0 @@
|
|||||||
package ast.statements;
|
|
||||||
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class BlockStatementNode {
|
|
||||||
List<IStatementNode> statements;
|
|
||||||
|
|
||||||
public BlockStatementNode(List<IStatementNode> statements) {this.statements = statements;}
|
|
||||||
}
|
|
@ -1,7 +1,7 @@
|
|||||||
package ast.statements;
|
package ast.statements;
|
||||||
|
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
|
|
||||||
public class ElseNode implements IStatementNode {
|
public class ElseNode implements IStatementNode {
|
||||||
public BlockNode block;
|
public BlockNode block;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package ast.statements;
|
package ast.statements;
|
||||||
|
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
@ -2,7 +2,7 @@ package ast.statements;
|
|||||||
|
|
||||||
import ast.expressions.IExpressionNode;
|
import ast.expressions.IExpressionNode;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
|
|
||||||
public class IfNode implements IStatementNode {
|
public class IfNode implements IStatementNode {
|
||||||
public IExpressionNode expression;
|
public IExpressionNode expression;
|
||||||
|
@ -4,7 +4,7 @@ import ast.expressions.IExpressionNode;
|
|||||||
import ast.type.type.*;
|
import ast.type.type.*;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
|
|
||||||
public class LocalVariableDeclarationNode implements IStatementNode {
|
public class LocalVariableDeclarationNode implements IStatementNode {
|
||||||
public ITypeNode type;
|
public ITypeNode type;
|
||||||
|
@ -3,14 +3,14 @@ package ast.statements;
|
|||||||
import ast.expressions.IExpressionNode;
|
import ast.expressions.IExpressionNode;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
|
|
||||||
public class ReturnNode implements IStatementNode {
|
public class ReturnNode implements IStatementNode {
|
||||||
public IExpressionNode expression;
|
public IExpressionNode expression;
|
||||||
public Boolean voidReturn = false;
|
public Boolean voidReturn = false;
|
||||||
|
|
||||||
public ReturnNode(IExpressionNode expression) {
|
public ReturnNode(IExpressionNode expression) {
|
||||||
if(expression != null) {
|
if (expression != null) {
|
||||||
this.expression = expression;
|
this.expression = expression;
|
||||||
} else {
|
} else {
|
||||||
voidReturn = true;
|
voidReturn = true;
|
||||||
|
@ -2,7 +2,7 @@ package ast.statements;
|
|||||||
|
|
||||||
import ast.expressions.IExpressionNode;
|
import ast.expressions.IExpressionNode;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
|
|
||||||
public class WhileNode implements IStatementNode {
|
public class WhileNode implements IStatementNode {
|
||||||
public IExpressionNode expression;
|
public IExpressionNode expression;
|
||||||
@ -14,7 +14,6 @@ public class WhileNode implements IStatementNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void test() {
|
public void test() {
|
||||||
return;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@ public class AccessModifierNode {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void setModifier(String accessType) {
|
private void setModifier(String accessType) {
|
||||||
switch(accessType) {
|
switch (accessType) {
|
||||||
case "public":
|
case "public":
|
||||||
this.accessType = EnumAccessModifierNode.PUBLIC;
|
this.accessType = EnumAccessModifierNode.PUBLIC;
|
||||||
break;
|
break;
|
||||||
|
@ -3,7 +3,7 @@ package ast.type;
|
|||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
import visitor.Visitable;
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class ValueNode implements ASTNode, Visitable {
|
public class ValueNode implements ASTNode, Visitable {
|
||||||
|
@ -2,7 +2,7 @@ package ast.type.type;
|
|||||||
|
|
||||||
public class BaseType implements ITypeNode {
|
public class BaseType implements ITypeNode {
|
||||||
|
|
||||||
private TypeEnum typeEnum;
|
public final TypeEnum typeEnum;
|
||||||
|
|
||||||
public BaseType(TypeEnum typeEnum) {
|
public BaseType(TypeEnum typeEnum) {
|
||||||
this.typeEnum = typeEnum;
|
this.typeEnum = typeEnum;
|
||||||
@ -21,8 +21,6 @@ public class BaseType implements ITypeNode {
|
|||||||
if (getClass() != obj.getClass())
|
if (getClass() != obj.getClass())
|
||||||
return false;
|
return false;
|
||||||
BaseType other = (BaseType) obj;
|
BaseType other = (BaseType) obj;
|
||||||
if (typeEnum != other.typeEnum)
|
return typeEnum == other.typeEnum;
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,5 +1,4 @@
|
|||||||
package ast.type.type;
|
package ast.type.type;
|
||||||
|
|
||||||
public interface ITypeNode {
|
public interface ITypeNode {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
package ast.type.type;
|
package ast.type.type;
|
||||||
|
|
||||||
public class ReferenceType implements ITypeNode{
|
public class ReferenceType implements ITypeNode {
|
||||||
|
|
||||||
private String identifier;
|
public final String identifier;
|
||||||
|
|
||||||
public ReferenceType(String identifier) {
|
public ReferenceType(String identifier) {
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
@ -22,11 +22,8 @@ public class ReferenceType implements ITypeNode{
|
|||||||
return false;
|
return false;
|
||||||
ReferenceType other = (ReferenceType) obj;
|
ReferenceType other = (ReferenceType) obj;
|
||||||
if (identifier == null) {
|
if (identifier == null) {
|
||||||
if (other.identifier != null)
|
return other.identifier == null;
|
||||||
return false;
|
} else return identifier.equals(other.identifier);
|
||||||
} else if (!identifier.equals(other.identifier))
|
|
||||||
return false;
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -4,6 +4,5 @@ public enum TypeEnum {
|
|||||||
VOID,
|
VOID,
|
||||||
INT,
|
INT,
|
||||||
CHAR,
|
CHAR,
|
||||||
BOOL;
|
BOOL
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,9 +17,9 @@ public class ByteCodeGenerator implements ProgramVisitor {
|
|||||||
|
|
||||||
private JarOutputStream jarOutputStream;
|
private JarOutputStream jarOutputStream;
|
||||||
private ByteArrayOutputStream byteArrayOutputStream;
|
private ByteArrayOutputStream byteArrayOutputStream;
|
||||||
private String outputDirectory;
|
private final String outputDirectory;
|
||||||
private boolean generateJar;
|
private final boolean generateJar;
|
||||||
private boolean generateClassFiles;
|
private final boolean generateClassFiles;
|
||||||
|
|
||||||
public ByteCodeGenerator(String outputDirectory, boolean generateJar, boolean generateClassFiles) {
|
public ByteCodeGenerator(String outputDirectory, boolean generateJar, boolean generateClassFiles) {
|
||||||
this.outputDirectory = outputDirectory;
|
this.outputDirectory = outputDirectory;
|
||||||
|
@ -19,12 +19,12 @@ import java.util.jar.JarOutputStream;
|
|||||||
|
|
||||||
|
|
||||||
public class ClassCodeGen implements ClassVisitor {
|
public class ClassCodeGen implements ClassVisitor {
|
||||||
private Mapper mapper;
|
private final Mapper mapper;
|
||||||
private ClassWriter classWriter;
|
private ClassWriter classWriter;
|
||||||
private JarOutputStream jarOutputStream;
|
private final JarOutputStream jarOutputStream;
|
||||||
private String outputDirectory;
|
private final String outputDirectory;
|
||||||
private boolean generateJar;
|
private final boolean generateJar;
|
||||||
private boolean generateClassFiles;
|
private final boolean generateClassFiles;
|
||||||
|
|
||||||
public ClassCodeGen(JarOutputStream jarOutputStream, String outputDirectory, boolean generateJar, boolean generateClassFiles) {
|
public ClassCodeGen(JarOutputStream jarOutputStream, String outputDirectory, boolean generateJar, boolean generateClassFiles) {
|
||||||
this.mapper = new Mapper();
|
this.mapper = new Mapper();
|
||||||
|
@ -33,11 +33,11 @@ import static org.objectweb.asm.Opcodes.*;
|
|||||||
|
|
||||||
public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
||||||
|
|
||||||
private ClassWriter classWriter;
|
private final ClassWriter classWriter;
|
||||||
private Mapper mapper;
|
private final Mapper mapper;
|
||||||
private MethodVisitor methodVisitor;
|
private MethodVisitor methodVisitor;
|
||||||
|
|
||||||
private List<String> localVariables;
|
private final List<String> localVariables;
|
||||||
|
|
||||||
public MethodCodeGen(ClassWriter classWriter) {
|
public MethodCodeGen(ClassWriter classWriter) {
|
||||||
this.classWriter = classWriter;
|
this.classWriter = classWriter;
|
||||||
@ -154,8 +154,8 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
if (dotNode.dotExpression != null) {
|
if (dotNode.dotExpression != null) {
|
||||||
dotNode.dotExpression.accept(this);
|
dotNode.dotExpression.accept(this);
|
||||||
}
|
}
|
||||||
if (dotNode.dotSubstractionExpression != null) {
|
if (dotNode.dotSubtractionExpression != null) {
|
||||||
dotNode.dotSubstractionExpression.accept(this);
|
dotNode.dotSubtractionExpression.accept(this);
|
||||||
}
|
}
|
||||||
if (dotNode.operator != null) {
|
if (dotNode.operator != null) {
|
||||||
switch (dotNode.operator) {
|
switch (dotNode.operator) {
|
||||||
@ -173,17 +173,17 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void visit(DotSubstractionNode dotSubstractionNode) {
|
public void visit(DotSubtractionNode dotSubtractionNode) {
|
||||||
if (dotSubstractionNode.value != null) {
|
if (dotSubtractionNode.value != null) {
|
||||||
dotSubstractionNode.value.accept(this);
|
dotSubtractionNode.value.accept(this);
|
||||||
} else if (dotSubstractionNode.identifier != null) {
|
} else if (dotSubtractionNode.identifier != null) {
|
||||||
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(dotSubstractionNode.identifier));
|
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(dotSubtractionNode.identifier));
|
||||||
} else if (dotSubstractionNode.memberAccess != null) {
|
} else if (dotSubtractionNode.memberAccess != null) {
|
||||||
dotSubstractionNode.memberAccess.accept(this);
|
dotSubtractionNode.memberAccess.accept(this);
|
||||||
} else if (dotSubstractionNode.methodCall != null) {
|
} else if (dotSubtractionNode.methodCall != null) {
|
||||||
dotSubstractionNode.methodCall.accept(this);
|
dotSubtractionNode.methodCall.accept(this);
|
||||||
} else if (dotSubstractionNode.calculationExpression != null) {
|
} else if (dotSubtractionNode.calculationExpression != null) {
|
||||||
dotSubstractionNode.calculationExpression.accept(this);
|
dotSubtractionNode.calculationExpression.accept(this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -423,8 +423,7 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
assignLocalVar(assignNode);
|
assignLocalVar(assignNode);
|
||||||
incrementNode.accept(this);
|
incrementNode.accept(this);
|
||||||
}
|
}
|
||||||
} else if (assignNode.expression instanceof DecrementNode) {
|
} else if (assignNode.expression instanceof DecrementNode decrementNode) {
|
||||||
DecrementNode decrementNode = (DecrementNode) assignNode.expression;
|
|
||||||
if (decrementNode.crementType.equals(CrementType.PREFIX)) {
|
if (decrementNode.crementType.equals(CrementType.PREFIX)) {
|
||||||
decrementNode.accept(this);
|
decrementNode.accept(this);
|
||||||
assignLocalVar(assignNode);
|
assignLocalVar(assignNode);
|
||||||
@ -521,9 +520,9 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
if(incrementNode.assignableExpression.memberAccess != null) {
|
if(incrementNode.assignableExpression.memberAccess != null) {
|
||||||
incrementNode.assignableExpression.memberAccess.accept(this);
|
incrementNode.assignableExpression.memberAccess.accept(this);
|
||||||
} else {
|
} else {
|
||||||
if(assignNode.assignable.getTypeNode() instanceof BaseType) {
|
if(assignNode.assignable.typeNode instanceof BaseType) {
|
||||||
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignNode.assignable.identifier));
|
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignNode.assignable.identifier));
|
||||||
} else if(assignNode.assignable.getTypeNode() instanceof ReferenceType) {
|
} else if(assignNode.assignable.typeNode instanceof ReferenceType) {
|
||||||
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
|
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -532,9 +531,9 @@ public class MethodCodeGen implements bytecode.visitor.MethodVisitor {
|
|||||||
if(decrementNode.assignableExpression.memberAccess != null) {
|
if(decrementNode.assignableExpression.memberAccess != null) {
|
||||||
decrementNode.assignableExpression.memberAccess.accept(this);
|
decrementNode.assignableExpression.memberAccess.accept(this);
|
||||||
} else {
|
} else {
|
||||||
if(assignNode.assignable.getTypeNode() instanceof BaseType) {
|
if(assignNode.assignable.typeNode instanceof BaseType) {
|
||||||
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignNode.assignable.identifier));
|
methodVisitor.visitVarInsn(ILOAD, localVariables.indexOf(assignNode.assignable.identifier));
|
||||||
} else if(assignNode.assignable.getTypeNode() instanceof ReferenceType) {
|
} else if(assignNode.assignable.typeNode instanceof ReferenceType) {
|
||||||
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
|
methodVisitor.visitVarInsn(ALOAD, localVariables.indexOf(assignNode.assignable.identifier));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -8,32 +8,38 @@ import ast.members.ConstructorNode;
|
|||||||
import ast.members.MainMethodNode;
|
import ast.members.MainMethodNode;
|
||||||
import ast.members.MethodNode;
|
import ast.members.MethodNode;
|
||||||
import ast.statementexpressions.AssignNode;
|
import ast.statementexpressions.AssignNode;
|
||||||
import ast.statementexpressions.AssignableNode;
|
|
||||||
import ast.statementexpressions.NewDeclarationNode;
|
import ast.statementexpressions.NewDeclarationNode;
|
||||||
import ast.statementexpressions.crementexpressions.DecrementNode;
|
import ast.statementexpressions.crementexpressions.DecrementNode;
|
||||||
import ast.statementexpressions.crementexpressions.IncrementNode;
|
import ast.statementexpressions.crementexpressions.IncrementNode;
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNode;
|
import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNode;
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
|
||||||
import ast.statements.*;
|
import ast.statements.*;
|
||||||
import ast.type.ValueNode;
|
import ast.type.ValueNode;
|
||||||
|
|
||||||
public interface MethodVisitor {
|
public interface MethodVisitor {
|
||||||
// members
|
// members
|
||||||
void visit(ConstructorNode constructorNode);
|
void visit(ConstructorNode constructorNode);
|
||||||
|
|
||||||
void visit(MethodNode methodNode);
|
void visit(MethodNode methodNode);
|
||||||
|
|
||||||
void visit(MainMethodNode mainMethodNode);
|
void visit(MainMethodNode mainMethodNode);
|
||||||
|
|
||||||
// Binary expressions
|
// Binary expressions
|
||||||
void visit(BinaryNode binaryNode);
|
void visit(BinaryNode binaryNode);
|
||||||
|
|
||||||
void visit(CalculationNode calculationNode);
|
void visit(CalculationNode calculationNode);
|
||||||
|
|
||||||
void visit(DotNode dotNode);
|
void visit(DotNode dotNode);
|
||||||
void visit(DotSubstractionNode dotSubstractionNode);
|
|
||||||
|
void visit(DotSubtractionNode dotSubtractionNode);
|
||||||
|
|
||||||
void visit(NonCalculationNode nonCalculationNode);
|
void visit(NonCalculationNode nonCalculationNode);
|
||||||
|
|
||||||
// Unary expressions
|
// Unary expressions
|
||||||
void visit(MemberAccessNode memberAccessNode);
|
void visit(MemberAccessNode memberAccessNode);
|
||||||
|
|
||||||
void visit(NotNode notExpressionNode);
|
void visit(NotNode notExpressionNode);
|
||||||
|
|
||||||
void visit(UnaryNode unaryExpressionNode);
|
void visit(UnaryNode unaryExpressionNode);
|
||||||
|
|
||||||
// statements
|
// statements
|
||||||
@ -43,13 +49,16 @@ public interface MethodVisitor {
|
|||||||
void visit(DecrementNode decrementNode);
|
void visit(DecrementNode decrementNode);
|
||||||
|
|
||||||
void visit(LocalVariableDeclarationNode localVariableDeclarationNode);
|
void visit(LocalVariableDeclarationNode localVariableDeclarationNode);
|
||||||
|
|
||||||
void visit(ReturnNode returnNode);
|
void visit(ReturnNode returnNode);
|
||||||
|
|
||||||
void visit(WhileNode whileNode);
|
void visit(WhileNode whileNode);
|
||||||
|
|
||||||
// statement expression
|
// statement expression
|
||||||
void visit(MethodCallNode methodCallNode);
|
void visit(MethodCallNode methodCallNode);
|
||||||
|
|
||||||
void visit(AssignNode assignNode);
|
void visit(AssignNode assignNode);
|
||||||
|
|
||||||
void visit(NewDeclarationNode newDeclarationNode);
|
void visit(NewDeclarationNode newDeclarationNode);
|
||||||
|
|
||||||
// type
|
// type
|
||||||
|
@ -3,5 +3,5 @@ package bytecode.visitor;
|
|||||||
import ast.ProgramNode;
|
import ast.ProgramNode;
|
||||||
|
|
||||||
public interface ProgramVisitor {
|
public interface ProgramVisitor {
|
||||||
void visit(ProgramNode programNode);
|
void visit(ProgramNode programNode);
|
||||||
}
|
}
|
||||||
|
@ -17,10 +17,10 @@ import java.util.Optional;
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Start Raupenpiler using make:
|
* Start miniCompiler using make:
|
||||||
* <p> <code> cd .\src\test\ </code>
|
* <p> <code> cd .\src\test\ </code>
|
||||||
* <p> <code> make clean compile-raupenpiler </code>
|
* <p> <code> make clean compile-miniCompiler </code>
|
||||||
* <p> Start Raupenpiler using jar:
|
* <p> Start miniCompiler using jar:
|
||||||
* <p> <code> java.exe -DgenJar=true_OR_false -DgenClass=true_OR_false -jar path_to_jar\JavaCompiler-1.0-jar-with-dependencies.jar 'path_to_input_file.java' 'path_to_output_directory' </code>
|
* <p> <code> java.exe -DgenJar=true_OR_false -DgenClass=true_OR_false -jar path_to_jar\JavaCompiler-1.0-jar-with-dependencies.jar 'path_to_input_file.java' 'path_to_output_directory' </code>
|
||||||
* <p> Example (jar needs to be in the target directory, compile with make or mvn package first):
|
* <p> Example (jar needs to be in the target directory, compile with make or mvn package first):
|
||||||
* <code> java.exe -DgenJar=true -DgenClass=true -jar .\target\JavaCompiler-1.0-jar-with-dependencies.jar 'src/main/resources/input/CompilerInput.java' 'src/main/resources/output' </code>
|
* <code> java.exe -DgenJar=true -DgenClass=true -jar .\target\JavaCompiler-1.0-jar-with-dependencies.jar 'src/main/resources/input/CompilerInput.java' 'src/main/resources/output' </code>
|
||||||
@ -40,16 +40,6 @@ public class Main {
|
|||||||
System.err.println("Error reading the file: " + e.getMessage());
|
System.err.println("Error reading the file: " + e.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* !!! Else Branch (main ohne args starten) ist nicht zur Verwendung vorgesehen, immer mit args starten !!!
|
|
||||||
else {
|
|
||||||
try {
|
|
||||||
CharStream codeCharStream = CharStreams.fromPath(Paths.get("src/main/resources/input/CompilerInput.java"));
|
|
||||||
compileFile(codeCharStream);
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.err.println("Error reading the file: " + e.getMessage());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,7 +56,7 @@ public class Main {
|
|||||||
*/
|
*/
|
||||||
static void compileFile(CharStream inputCharStream, String outputDirectoryPath) {
|
static void compileFile(CharStream inputCharStream, String outputDirectoryPath) {
|
||||||
// Initialize the logger
|
// Initialize the logger
|
||||||
new RaupenLogger();
|
new MiniCompilerLogger();
|
||||||
|
|
||||||
/* ------------------------- Scanner -> tokens ------------------------- */
|
/* ------------------------- Scanner -> tokens ------------------------- */
|
||||||
// Use the SimpleJavaLexer to tokenize the input CharStream
|
// Use the SimpleJavaLexer to tokenize the input CharStream
|
||||||
@ -74,27 +64,27 @@ public class Main {
|
|||||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||||
tokenStream.fill();
|
tokenStream.fill();
|
||||||
// Log the tokens
|
// Log the tokens
|
||||||
RaupenLogger.logScanner(tokenStream);
|
MiniCompilerLogger.logScanner(tokenStream);
|
||||||
|
|
||||||
/*------------------------- Parser -> Parsetree -------------------------*/
|
/*------------------------- Parser -> Parsetree -------------------------*/
|
||||||
// Use the SimpleJavaParser to parse the tokens and generate a ParseTree
|
// Use the SimpleJavaParser to parse the tokens and generate a ParseTree
|
||||||
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||||
ParseTree parseTree = parser.program(); // parse the input
|
ParseTree parseTree = parser.program(); // parse the input
|
||||||
// Log the ParseTree
|
// Log the ParseTree
|
||||||
RaupenLogger.logParser(parseTree, parser);
|
MiniCompilerLogger.logParser(parseTree, parser);
|
||||||
|
|
||||||
/*------------------------- AST builder -> AST -------------------------*/
|
/*------------------------- AST builder -> AST -------------------------*/
|
||||||
// Use the ASTBuilder to visit the ParseTree and generate an Abstract Syntax Tree (AST)
|
// Use the ASTBuilder to visit the ParseTree and generate an Abstract Syntax Tree (AST)
|
||||||
ASTBuilder astBuilder = new ASTBuilder();
|
ASTBuilder astBuilder = new ASTBuilder();
|
||||||
ASTNode abstractSyntaxTree = astBuilder.visit(parseTree);
|
ASTNode abstractSyntaxTree = astBuilder.visit(parseTree);
|
||||||
// Log the AST
|
// Log the AST
|
||||||
RaupenLogger.logAST(abstractSyntaxTree);
|
MiniCompilerLogger.logAST(abstractSyntaxTree);
|
||||||
|
|
||||||
/*------------------------- Semantic Analyzer -> typed AST -------------------------*/
|
/*------------------------- Semantic Analyzer -> typed AST -------------------------*/
|
||||||
// Use the SemanticAnalyzer to generate a typed AST
|
// Use the SemanticAnalyzer to generate a typed AST
|
||||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||||
// Log the typed AST
|
// Log the typed AST
|
||||||
RaupenLogger.logSemanticAnalyzer(typedAst);
|
MiniCompilerLogger.logSemanticAnalyzer(typedAst);
|
||||||
|
|
||||||
if(SemanticAnalyzer.errors.isEmpty()){
|
if(SemanticAnalyzer.errors.isEmpty()){
|
||||||
/*------------------------- Bytecode Generator -> Bytecode -------------------------*/
|
/*------------------------- Bytecode Generator -> Bytecode -------------------------*/
|
||||||
@ -107,7 +97,7 @@ public class Main {
|
|||||||
assert typedAst != null;
|
assert typedAst != null;
|
||||||
byteCodeGenerator.visit((ProgramNode) typedAst);
|
byteCodeGenerator.visit((ProgramNode) typedAst);
|
||||||
// Log the bytecode generation
|
// Log the bytecode generation
|
||||||
RaupenLogger.logBytecodeGenerator();
|
MiniCompilerLogger.logBytecodeGenerator();
|
||||||
} else {
|
} else {
|
||||||
for(Exception exception : SemanticAnalyzer.errors){
|
for(Exception exception : SemanticAnalyzer.errors){
|
||||||
exception.printStackTrace();
|
exception.printStackTrace();
|
||||||
|
@ -29,11 +29,11 @@ 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 RaupenLogger {
|
public class MiniCompilerLogger {
|
||||||
|
|
||||||
static Logger logger = Logger.getLogger("RaupenLogs");
|
static Logger logger = Logger.getLogger("miniCompilerLogs");
|
||||||
|
|
||||||
public RaupenLogger() {
|
public MiniCompilerLogger() {
|
||||||
// ------------------------- Logging -------------------------
|
// ------------------------- Logging -------------------------
|
||||||
logger.setLevel(Level.ALL);
|
logger.setLevel(Level.ALL);
|
||||||
logger.getParent().getHandlers()[0].setLevel(Level.ALL);
|
logger.getParent().getHandlers()[0].setLevel(Level.ALL);
|
||||||
@ -66,7 +66,7 @@ public class RaupenLogger {
|
|||||||
logger.addHandler(consoleHandler);
|
logger.addHandler(consoleHandler);
|
||||||
|
|
||||||
// Configure file handler
|
// Configure file handler
|
||||||
Handler fileHandler = new FileHandler("src/main/resources/logs/RaupenLog.log");
|
Handler fileHandler = new FileHandler("src/main/resources/logs/miniCompiler.log");
|
||||||
// Toggle file logging on/off
|
// Toggle file logging on/off
|
||||||
fileHandler.setLevel(Level.ALL);
|
fileHandler.setLevel(Level.ALL);
|
||||||
fileHandler.setFormatter(new CustomFormatter());
|
fileHandler.setFormatter(new CustomFormatter());
|
||||||
@ -117,7 +117,7 @@ public class RaupenLogger {
|
|||||||
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");
|
logger.info("Bytecode generated without errors.");
|
||||||
logger.info("\n");
|
logger.info("\n");
|
||||||
}
|
}
|
||||||
|
|
@ -211,8 +211,8 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
|
|
||||||
WhileNode While = new WhileNode(condition, doBlock);
|
WhileNode While = new WhileNode(condition, doBlock);
|
||||||
BlockNode resultBlock = new BlockNode();
|
BlockNode resultBlock = new BlockNode();
|
||||||
resultBlock.addStatement((IStatementNode) doBlock);
|
resultBlock.addStatement(doBlock);
|
||||||
resultBlock.addStatement((IStatementNode) While);
|
resultBlock.addStatement(While);
|
||||||
|
|
||||||
return resultBlock;
|
return resultBlock;
|
||||||
}
|
}
|
||||||
@ -253,7 +253,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
|
|
||||||
// Prä-Inkrement: Das Inkrement kommt vor dem Block
|
// Prä-Inkrement: Das Inkrement kommt vor dem Block
|
||||||
if (crement != null && isPrefix) {
|
if (crement != null && isPrefix) {
|
||||||
whileBody.addStatement((IStatementNode) crement);
|
whileBody.addStatement(crement);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Block Statements der For-Schleife in den While-Block kopieren
|
// Block Statements der For-Schleife in den While-Block kopieren
|
||||||
@ -263,7 +263,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
|
|
||||||
// Post-Inkrement: Das Inkrement kommt nach dem Block
|
// Post-Inkrement: Das Inkrement kommt nach dem Block
|
||||||
if (crement != null && !isPrefix) {
|
if (crement != null && !isPrefix) {
|
||||||
whileBody.addStatement((IStatementNode) crement);
|
whileBody.addStatement(crement);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Bedingung der While-Schleife
|
// Bedingung der While-Schleife
|
||||||
@ -273,7 +273,7 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
|
|
||||||
BlockNode resultBlock = new BlockNode();
|
BlockNode resultBlock = new BlockNode();
|
||||||
for (IStatementNode statement : statements) {
|
for (IStatementNode statement : statements) {
|
||||||
resultBlock.addStatement((IStatementNode) statement);
|
resultBlock.addStatement(statement);
|
||||||
}
|
}
|
||||||
|
|
||||||
return resultBlock;
|
return resultBlock;
|
||||||
@ -559,9 +559,9 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
@Override
|
@Override
|
||||||
public ASTNode visitDotExpression(SimpleJavaParser.DotExpressionContext ctx) {
|
public ASTNode visitDotExpression(SimpleJavaParser.DotExpressionContext ctx) {
|
||||||
if(ctx.dotExpression() != null) {
|
if(ctx.dotExpression() != null) {
|
||||||
return new DotNode((DotNode) visit(ctx.dotExpression()), ctx.DotOperator().getText(), (DotSubstractionNode) visit(ctx.dotSubtractionExpression()));
|
return new DotNode((DotNode) visit(ctx.dotExpression()), ctx.DotOperator().getText(), (DotSubtractionNode) visit(ctx.dotSubtractionExpression()));
|
||||||
} else if(ctx.dotSubtractionExpression() != null) {
|
} else if(ctx.dotSubtractionExpression() != null) {
|
||||||
return new DotNode((DotSubstractionNode) visit(ctx.dotSubtractionExpression()));
|
return new DotNode((DotSubtractionNode) visit(ctx.dotSubtractionExpression()));
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -569,13 +569,13 @@ public class ASTBuilder extends SimpleJavaBaseVisitor<ASTNode> {
|
|||||||
@Override
|
@Override
|
||||||
public ASTNode visitDotSubtractionExpression(SimpleJavaParser.DotSubtractionExpressionContext ctx) {
|
public ASTNode visitDotSubtractionExpression(SimpleJavaParser.DotSubtractionExpressionContext ctx) {
|
||||||
if(ctx.IntValue() != null) {
|
if(ctx.IntValue() != null) {
|
||||||
return new DotSubstractionNode(new ValueNode(EnumValueNode.INT_VALUE, ctx.IntValue().getText()));
|
return new DotSubtractionNode(new ValueNode(EnumValueNode.INT_VALUE, ctx.IntValue().getText()));
|
||||||
} else if(ctx.Identifier() != null) {
|
} else if(ctx.Identifier() != null) {
|
||||||
return new DotSubstractionNode(ctx.Identifier().getText());
|
return new DotSubtractionNode(ctx.Identifier().getText());
|
||||||
} else if(ctx.memberAccess() != null) {
|
} else if(ctx.memberAccess() != null) {
|
||||||
return new DotSubstractionNode((MemberAccessNode) visit(ctx.memberAccess()));
|
return new DotSubtractionNode((MemberAccessNode) visit(ctx.memberAccess()));
|
||||||
} else if(ctx.methodCall() != null && ctx.calculationExpression() != null) {
|
} else if(ctx.methodCall() != null && ctx.calculationExpression() != null) {
|
||||||
return new DotSubstractionNode((MethodCallNode) visit(ctx.methodCall()), (CalculationNode) visit(ctx.calculationExpression()));
|
return new DotSubtractionNode((MethodCallNode) visit(ctx.methodCall()), (CalculationNode) visit(ctx.calculationExpression()));
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -8,7 +8,7 @@ import java.util.Stack;
|
|||||||
|
|
||||||
public class Scope {
|
public class Scope {
|
||||||
|
|
||||||
private Stack<HashMap<String, ITypeNode>> localVars;
|
private final Stack<HashMap<String, ITypeNode>> localVars;
|
||||||
|
|
||||||
public Scope() {
|
public Scope() {
|
||||||
localVars = new Stack<HashMap<String, ITypeNode>>();
|
localVars = new Stack<HashMap<String, ITypeNode>>();
|
||||||
|
@ -33,7 +33,7 @@ import com.sun.jdi.IntegerType;
|
|||||||
import semantic.context.ClassContext;
|
import semantic.context.ClassContext;
|
||||||
import semantic.context.Context;
|
import semantic.context.Context;
|
||||||
import semantic.exceptions.*;
|
import semantic.exceptions.*;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
|
|
||||||
public class SemanticAnalyzer implements SemanticVisitor {
|
public class SemanticAnalyzer implements SemanticVisitor {
|
||||||
|
|
||||||
@ -480,8 +480,8 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(DotNode toCheck) {
|
public TypeCheckResult analyze(DotNode toCheck) {
|
||||||
if (toCheck.dotSubstractionExpression != null) {
|
if (toCheck.dotSubtractionExpression != null) {
|
||||||
var result = toCheck.dotSubstractionExpression.accept(this);
|
var result = toCheck.dotSubtractionExpression.accept(this);
|
||||||
toCheck.setType(result.getType());
|
toCheck.setType(result.getType());
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@ -489,7 +489,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(DotSubstractionNode toCheck) {
|
public TypeCheckResult analyze(DotSubtractionNode toCheck) {
|
||||||
if (toCheck.value != null) {
|
if (toCheck.value != null) {
|
||||||
var result = toCheck.value.accept(this);
|
var result = toCheck.value.accept(this);
|
||||||
toCheck.setType(result.getType());
|
toCheck.setType(result.getType());
|
||||||
|
@ -15,7 +15,6 @@ import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
|||||||
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
||||||
import ast.statements.*;
|
import ast.statements.*;
|
||||||
import ast.type.ValueNode;
|
import ast.type.ValueNode;
|
||||||
import typechecker.TypeCheckResult;
|
|
||||||
|
|
||||||
public interface SemanticVisitor {
|
public interface SemanticVisitor {
|
||||||
|
|
||||||
@ -61,7 +60,7 @@ public interface SemanticVisitor {
|
|||||||
|
|
||||||
TypeCheckResult analyze(DotNode toCheck);
|
TypeCheckResult analyze(DotNode toCheck);
|
||||||
|
|
||||||
TypeCheckResult analyze(DotSubstractionNode toCheck);
|
TypeCheckResult analyze(DotSubtractionNode toCheck);
|
||||||
|
|
||||||
TypeCheckResult analyze(NonCalculationNode toCheck);
|
TypeCheckResult analyze(NonCalculationNode toCheck);
|
||||||
|
|
||||||
|
@ -1,23 +1,23 @@
|
|||||||
package typechecker;
|
package semantic;
|
||||||
|
|
||||||
|
|
||||||
import ast.type.type.ITypeNode;
|
import ast.type.type.ITypeNode;
|
||||||
|
|
||||||
public class TypeCheckResult {
|
public class TypeCheckResult {
|
||||||
|
|
||||||
private boolean valid;
|
private final boolean valid;
|
||||||
private ITypeNode type;
|
private final ITypeNode type;
|
||||||
|
|
||||||
public TypeCheckResult(boolean valid, ITypeNode type) {
|
public TypeCheckResult(boolean valid, ITypeNode type) {
|
||||||
this.valid = valid;
|
this.valid = valid;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isValid() {
|
public boolean isValid() {
|
||||||
return valid;
|
return valid;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ITypeNode getType() {
|
public ITypeNode getType() {
|
||||||
return type;
|
return type;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -9,8 +9,8 @@ import java.util.HashMap;
|
|||||||
|
|
||||||
public class ClassContext {
|
public class ClassContext {
|
||||||
|
|
||||||
private HashMap<String, FieldContext> fields;
|
private final HashMap<String, FieldContext> fields;
|
||||||
private ArrayList<MethodNode> methods = new ArrayList<>();
|
private final ArrayList<MethodNode> methods = new ArrayList<>();
|
||||||
|
|
||||||
public ClassContext(ClassNode classNode) {
|
public ClassContext(ClassNode classNode) {
|
||||||
|
|
||||||
|
@ -5,7 +5,7 @@ import java.util.HashMap;
|
|||||||
|
|
||||||
public class Context {
|
public class Context {
|
||||||
|
|
||||||
private HashMap<String, ClassContext> classes;
|
private final HashMap<String, ClassContext> classes;
|
||||||
|
|
||||||
public Context(ProgramNode programNode) {
|
public Context(ProgramNode programNode) {
|
||||||
classes = new HashMap<>();
|
classes = new HashMap<>();
|
||||||
|
@ -6,8 +6,8 @@ import ast.type.type.*;
|
|||||||
|
|
||||||
public class FieldContext {
|
public class FieldContext {
|
||||||
|
|
||||||
private AccessModifierNode accessModifier;
|
private final AccessModifierNode accessModifier;
|
||||||
private ITypeNode type;
|
private final ITypeNode type;
|
||||||
|
|
||||||
public FieldContext(FieldNode field) {
|
public FieldContext(FieldNode field) {
|
||||||
accessModifier = field.accessTypeNode;
|
accessModifier = field.accessTypeNode;
|
||||||
|
@ -4,7 +4,7 @@ import bytecode.visitor.ClassVisitor;
|
|||||||
import bytecode.visitor.MethodVisitor;
|
import bytecode.visitor.MethodVisitor;
|
||||||
import bytecode.visitor.ProgramVisitor;
|
import bytecode.visitor.ProgramVisitor;
|
||||||
import semantic.SemanticVisitor;
|
import semantic.SemanticVisitor;
|
||||||
import typechecker.TypeCheckResult;
|
import semantic.TypeCheckResult;
|
||||||
|
|
||||||
public interface Visitable {
|
public interface Visitable {
|
||||||
default void accept(ProgramVisitor programVisitor) {
|
default void accept(ProgramVisitor programVisitor) {
|
||||||
|
@ -2,17 +2,17 @@
|
|||||||
### IntelliJs play buttons do not work. Run in "src/test" folder with "make" command to run all
|
### 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.
|
### Or run only parts with "make compile-javac", "make clean" etc.
|
||||||
|
|
||||||
all: compile-javac compile-raupenpiler
|
all: compile-javac compile-miniCompiler
|
||||||
|
|
||||||
compile-javac:
|
compile-javac:
|
||||||
javac -d .\resources\output\javac .\resources\input\CompilerInput.java
|
javac -d .\resources\output\javac .\resources\input\CompilerInput.java
|
||||||
|
|
||||||
compile-raupenpiler:
|
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/raupenpiler
|
# cp ../main/resources/output/CompilerInput.class .java/resources/output/miniCompiler
|
||||||
|
|
||||||
test-raupenpiler:
|
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/CompilerInput.class .java/main/
|
||||||
# compile the test class
|
# compile the test class
|
||||||
@ -28,8 +28,8 @@ clean:
|
|||||||
rm -f ../main/resources/output/*.jar
|
rm -f ../main/resources/output/*.jar
|
||||||
# clean resources output folders
|
# clean resources output folders
|
||||||
rm -f ./resources/output/javac/*.class
|
rm -f ./resources/output/javac/*.class
|
||||||
rm -f ./resources/output/raupenpiler/*.class
|
rm -f ./resources/output/miniCompiler/*.class
|
||||||
rm -f ./resources/output/raupenpiler/*.jar
|
rm -f ./resources/output/miniCompiler/*.jar
|
||||||
# clean logs
|
# clean logs
|
||||||
rm -f ../main/resources/logs/*
|
rm -f ../main/resources/logs/*
|
||||||
# clean test/java/main folders from .class files for End-to-End tests
|
# clean test/java/main folders from .class files for End-to-End tests
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -11,8 +11,6 @@ package main;
|
|||||||
* Wenn unser Compiler funktioniert, sollten keine Errors kommen (sondern nur die Ausgaben, die wir in der CompilerInput.java Datei gemacht haben,
|
* Wenn unser Compiler funktioniert, sollten keine Errors kommen (sondern nur die Ausgaben, die wir in der CompilerInput.java Datei gemacht haben,
|
||||||
* oder Methoden, die wir hier aufrufen).</p>
|
* oder Methoden, die wir hier aufrufen).</p>
|
||||||
*
|
*
|
||||||
* <p><strong>PROBLEM:</strong> Hier kommen Errors, was eigentlich heißt, dass der Compiler nicht funktioniert, der Test sollte eigentlich passen.
|
|
||||||
* <br><strong>DENN:</strong> Wenn ich statt unserem CompilerInput.class die CompilerInput.class von javac verwende (aus <code> src/test/resources/output/javac </code>), dann funktioniert es.</p>
|
|
||||||
*/
|
*/
|
||||||
public class EndToEndTester {
|
public class EndToEndTester {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
@ -2,8 +2,6 @@ package main;
|
|||||||
|
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.junit.runners.Suite;
|
import org.junit.runners.Suite;
|
||||||
import parser.ParserTest;
|
|
||||||
import parser.ScannerTest;
|
|
||||||
import semantic.EndToTypedAstTest;
|
import semantic.EndToTypedAstTest;
|
||||||
import semantic.SemanticTest;
|
import semantic.SemanticTest;
|
||||||
|
|
||||||
@ -15,8 +13,6 @@ import semantic.SemanticTest;
|
|||||||
@RunWith(Suite.class)
|
@RunWith(Suite.class)
|
||||||
@Suite.SuiteClasses({
|
@Suite.SuiteClasses({
|
||||||
InputFilesTest.class,
|
InputFilesTest.class,
|
||||||
ScannerTest.class,
|
|
||||||
ParserTest.class,
|
|
||||||
SemanticTest.class,
|
SemanticTest.class,
|
||||||
EndToTypedAstTest.class
|
EndToTypedAstTest.class
|
||||||
})
|
})
|
||||||
|
@ -6,7 +6,7 @@ import ast.ProgramNode;
|
|||||||
import ast.expressions.IExpressionNode;
|
import ast.expressions.IExpressionNode;
|
||||||
import ast.expressions.binaryexpressions.CalculationNode;
|
import ast.expressions.binaryexpressions.CalculationNode;
|
||||||
import ast.expressions.binaryexpressions.DotNode;
|
import ast.expressions.binaryexpressions.DotNode;
|
||||||
import ast.expressions.binaryexpressions.DotSubstractionNode;
|
import ast.expressions.binaryexpressions.DotSubtractionNode;
|
||||||
import ast.expressions.binaryexpressions.NonCalculationNode;
|
import ast.expressions.binaryexpressions.NonCalculationNode;
|
||||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
import ast.expressions.unaryexpressions.MemberAccessNode;
|
||||||
import ast.expressions.unaryexpressions.UnaryNode;
|
import ast.expressions.unaryexpressions.UnaryNode;
|
||||||
@ -514,31 +514,31 @@ class AstBuilderTest {
|
|||||||
ClassNode class1 = Helper.generateEmptyClass("VariableCalculation");
|
ClassNode class1 = Helper.generateEmptyClass("VariableCalculation");
|
||||||
|
|
||||||
BlockNode aPlusBBlock = new BlockNode();
|
BlockNode aPlusBBlock = new BlockNode();
|
||||||
aPlusBBlock.addStatement(new ReturnNode(new CalculationNode(new CalculationNode(new DotNode(new DotSubstractionNode("a"))), "+", new DotNode(new DotSubstractionNode("b")))));
|
aPlusBBlock.addStatement(new ReturnNode(new CalculationNode(new CalculationNode(new DotNode(new DotSubtractionNode("a"))), "+", new DotNode(new DotSubtractionNode("b")))));
|
||||||
MethodNode aPlusBMethod = new MethodNode("public", new BaseType(TypeEnum.INT), false, "aPlusB", aPlusBBlock);
|
MethodNode aPlusBMethod = new MethodNode("public", new BaseType(TypeEnum.INT), false, "aPlusB", aPlusBBlock);
|
||||||
aPlusBMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
aPlusBMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
||||||
aPlusBMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b"));
|
aPlusBMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b"));
|
||||||
|
|
||||||
BlockNode aMinusBBlock = new BlockNode();
|
BlockNode aMinusBBlock = new BlockNode();
|
||||||
aMinusBBlock.addStatement(new ReturnNode(new CalculationNode(new CalculationNode(new DotNode(new DotSubstractionNode("a"))), "-", new DotNode(new DotSubstractionNode("b")))));
|
aMinusBBlock.addStatement(new ReturnNode(new CalculationNode(new CalculationNode(new DotNode(new DotSubtractionNode("a"))), "-", new DotNode(new DotSubtractionNode("b")))));
|
||||||
MethodNode aMinusBMethod = new MethodNode("public", new BaseType(TypeEnum.INT), false, "aMinusB", aMinusBBlock);
|
MethodNode aMinusBMethod = new MethodNode("public", new BaseType(TypeEnum.INT), false, "aMinusB", aMinusBBlock);
|
||||||
aMinusBMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
aMinusBMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
||||||
aMinusBMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b"));
|
aMinusBMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b"));
|
||||||
|
|
||||||
BlockNode aTimeBBlock = new BlockNode();
|
BlockNode aTimeBBlock = new BlockNode();
|
||||||
aTimeBBlock.addStatement(new ReturnNode(new CalculationNode(new DotNode(new DotNode(new DotSubstractionNode("a")), "*", new DotSubstractionNode("b")))));
|
aTimeBBlock.addStatement(new ReturnNode(new CalculationNode(new DotNode(new DotNode(new DotSubtractionNode("a")), "*", new DotSubtractionNode("b")))));
|
||||||
MethodNode aTimeBMethod = new MethodNode("public", new BaseType(TypeEnum.INT), false, "aTimeB", aTimeBBlock);
|
MethodNode aTimeBMethod = new MethodNode("public", new BaseType(TypeEnum.INT), false, "aTimeB", aTimeBBlock);
|
||||||
aTimeBMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
aTimeBMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
||||||
aTimeBMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b"));
|
aTimeBMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b"));
|
||||||
|
|
||||||
BlockNode aDivBBlock = new BlockNode();
|
BlockNode aDivBBlock = new BlockNode();
|
||||||
aDivBBlock.addStatement(new ReturnNode(new CalculationNode(new DotNode(new DotNode(new DotSubstractionNode("a")), "/", new DotSubstractionNode("b")))));
|
aDivBBlock.addStatement(new ReturnNode(new CalculationNode(new DotNode(new DotNode(new DotSubtractionNode("a")), "/", new DotSubtractionNode("b")))));
|
||||||
MethodNode aDivBMethod = new MethodNode("public", new BaseType(TypeEnum.INT), false, "aDivB", aDivBBlock);
|
MethodNode aDivBMethod = new MethodNode("public", new BaseType(TypeEnum.INT), false, "aDivB", aDivBBlock);
|
||||||
aDivBMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
aDivBMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
||||||
aDivBMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b"));
|
aDivBMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b"));
|
||||||
|
|
||||||
BlockNode complexCalcBlock = new BlockNode();
|
BlockNode complexCalcBlock = new BlockNode();
|
||||||
complexCalcBlock.addStatement(new ReturnNode(new CalculationNode(null, null, new DotNode(new DotNode(new DotNode(new DotNode(new DotSubstractionNode("a")), "*", new DotSubstractionNode("b")), "/", new DotSubstractionNode(new ValueNode(EnumValueNode.INT_VALUE, "1"))), "*", new DotSubstractionNode(new ValueNode(EnumValueNode.INT_VALUE, "3"))))));
|
complexCalcBlock.addStatement(new ReturnNode(new CalculationNode(null, null, new DotNode(new DotNode(new DotNode(new DotNode(new DotSubtractionNode("a")), "*", new DotSubtractionNode("b")), "/", new DotSubtractionNode(new ValueNode(EnumValueNode.INT_VALUE, "1"))), "*", new DotSubtractionNode(new ValueNode(EnumValueNode.INT_VALUE, "3"))))));
|
||||||
MethodNode complexCalcMethod = new MethodNode("public", new BaseType(TypeEnum.INT), false, "complexCalc", complexCalcBlock);
|
MethodNode complexCalcMethod = new MethodNode("public", new BaseType(TypeEnum.INT), false, "complexCalc", complexCalcBlock);
|
||||||
complexCalcMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
complexCalcMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "a"));
|
||||||
complexCalcMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b"));
|
complexCalcMethod.addParameter(new ParameterNode(new BaseType(TypeEnum.INT), "b"));
|
||||||
|
@ -2,12 +2,10 @@ package parser;
|
|||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.ClassNode;
|
import ast.ClassNode;
|
||||||
import ast.ProgramNode;
|
|
||||||
import ast.members.ConstructorNode;
|
import ast.members.ConstructorNode;
|
||||||
import ast.members.MemberNode;
|
import ast.members.MemberNode;
|
||||||
import ast.statements.BlockNode;
|
import ast.statements.BlockNode;
|
||||||
import ast.statements.ReturnNode;
|
import ast.statements.ReturnNode;
|
||||||
import ast.type.AccessModifierNode;
|
|
||||||
import org.antlr.v4.runtime.CharStream;
|
import org.antlr.v4.runtime.CharStream;
|
||||||
import org.antlr.v4.runtime.CharStreams;
|
import org.antlr.v4.runtime.CharStreams;
|
||||||
import org.antlr.v4.runtime.CommonTokenStream;
|
import org.antlr.v4.runtime.CommonTokenStream;
|
||||||
@ -39,7 +37,7 @@ public class Helper {
|
|||||||
public static ClassNode generateEmptyClass(String className) {
|
public static ClassNode generateEmptyClass(String className) {
|
||||||
BlockNode blockNode = new BlockNode();
|
BlockNode blockNode = new BlockNode();
|
||||||
blockNode.addStatement(new ReturnNode(null));
|
blockNode.addStatement(new ReturnNode(null));
|
||||||
MemberNode constructor = new ConstructorNode("public",className, blockNode);
|
MemberNode constructor = new ConstructorNode("public", className, blockNode);
|
||||||
ClassNode classNode = new ClassNode("public", className);
|
ClassNode classNode = new ClassNode("public", className);
|
||||||
classNode.addMember(constructor);
|
classNode.addMember(constructor);
|
||||||
return classNode;
|
return classNode;
|
||||||
|
@ -1,138 +0,0 @@
|
|||||||
package parser;
|
|
||||||
|
|
||||||
import org.antlr.v4.runtime.*;
|
|
||||||
import org.antlr.v4.runtime.tree.ParseTree;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import parser.generated.SimpleJavaLexer;
|
|
||||||
import parser.generated.SimpleJavaParser;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
|
||||||
|
|
||||||
import java.util.*;
|
|
||||||
|
|
||||||
public class ParserTest {
|
|
||||||
/*
|
|
||||||
@BeforeEach
|
|
||||||
public void init() { // noch nicht benötigt
|
|
||||||
String inputFilePath = "src/main/resources/input/CompilerInput.java";
|
|
||||||
String outputDirectoryPath = "src/main/resources/output";
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
@Test
|
|
||||||
public void parserTest() {
|
|
||||||
// init
|
|
||||||
CharStream inputCharStream = CharStreams.fromString("public class Name {}");
|
|
||||||
SimpleJavaLexer lexer = new SimpleJavaLexer(inputCharStream);
|
|
||||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
|
||||||
tokenStream.fill();
|
|
||||||
|
|
||||||
|
|
||||||
/* Parser -> Parsetree */
|
|
||||||
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
|
||||||
ParseTree parseTree = parser.program(); // parse the input
|
|
||||||
|
|
||||||
//Variante 1 (geht)
|
|
||||||
String expectedParseTreeAsString = "(program (classDeclaration public class Name { }))";
|
|
||||||
String actualParseTreeAsString = parseTree.toStringTree(parser);
|
|
||||||
|
|
||||||
assertEquals(expectedParseTreeAsString, actualParseTreeAsString);
|
|
||||||
|
|
||||||
// Variante 2 (geht nicht)
|
|
||||||
// - Sollte es gehen und es liegt am Parser? (keine Ahnung) -> Bitte Fehler (actual und expected) durchlesen
|
|
||||||
// ist die Methode parseStringToTree() korrekt? -> (glaub nicht)
|
|
||||||
Map<String, Object> expectedTreeStructure = parseStringToTree(expectedParseTreeAsString);
|
|
||||||
Map<String, Object> actualTreeStructure = buildTreeStructure(parseTree, parser);
|
|
||||||
|
|
||||||
// assertEquals(expectedTreeStructure, actualTreeStructure);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Helpers Variante 2.1
|
|
||||||
|
|
||||||
public static Map<String, Object> buildTreeStructure(ParseTree tree, Parser parser) {
|
|
||||||
return buildTree(tree, parser, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Map<String, Object> buildTree(ParseTree tree, Parser parser, int indent) {
|
|
||||||
Map<String, Object> node = new HashMap<>();
|
|
||||||
|
|
||||||
if (tree instanceof RuleContext) {
|
|
||||||
int ruleIndex = ((RuleContext) tree).getRuleIndex();
|
|
||||||
String ruleName = parser.getRuleNames()[ruleIndex];
|
|
||||||
node.put("rule", ruleName);
|
|
||||||
} else {
|
|
||||||
node.put("text", tree.getText());
|
|
||||||
}
|
|
||||||
|
|
||||||
List<Map<String, Object>> children = new ArrayList<>();
|
|
||||||
for (int i = 0; i < tree.getChildCount(); i++) {
|
|
||||||
children.add(buildTree(tree.getChild(i), parser, indent + 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!children.isEmpty()) {
|
|
||||||
node.put("children", children);
|
|
||||||
}
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Helpers Variante 2.2
|
|
||||||
|
|
||||||
public static Map<String, Object> parseStringToTree(String input) {
|
|
||||||
input = input.trim();
|
|
||||||
if (input.startsWith("(") && input.endsWith(")")) {
|
|
||||||
input = input.substring(1, input.length() - 1).trim();
|
|
||||||
}
|
|
||||||
return parse(input);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static Map<String, Object> parse(String input) {
|
|
||||||
Map<String, Object> node = new HashMap<>();
|
|
||||||
StringBuilder currentToken = new StringBuilder();
|
|
||||||
List<Map<String, Object>> children = new ArrayList<>();
|
|
||||||
|
|
||||||
int depth = 0;
|
|
||||||
boolean inToken = false;
|
|
||||||
for (char ch : input.toCharArray()) {
|
|
||||||
if (ch == '(') {
|
|
||||||
if (depth == 0) {
|
|
||||||
if (!currentToken.isEmpty()) {
|
|
||||||
node.put("node", currentToken.toString().trim());
|
|
||||||
currentToken.setLength(0);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
currentToken.append(ch);
|
|
||||||
}
|
|
||||||
depth++;
|
|
||||||
} else if (ch == ')') {
|
|
||||||
depth--;
|
|
||||||
if (depth == 0) {
|
|
||||||
children.add(parse(currentToken.toString().trim()));
|
|
||||||
currentToken.setLength(0);
|
|
||||||
} else {
|
|
||||||
currentToken.append(ch);
|
|
||||||
}
|
|
||||||
} else if (Character.isWhitespace(ch) && depth == 0) {
|
|
||||||
if (!currentToken.isEmpty()) {
|
|
||||||
node.put("node", currentToken.toString().trim());
|
|
||||||
currentToken.setLength(0);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
currentToken.append(ch);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!currentToken.isEmpty()) {
|
|
||||||
node.put("node", currentToken.toString().trim());
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!children.isEmpty()) {
|
|
||||||
node.put("children", children);
|
|
||||||
}
|
|
||||||
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,45 +0,0 @@
|
|||||||
package parser;
|
|
||||||
|
|
||||||
import org.antlr.v4.runtime.CharStream;
|
|
||||||
import org.antlr.v4.runtime.CharStreams;
|
|
||||||
import org.antlr.v4.runtime.CommonTokenStream;
|
|
||||||
import org.antlr.v4.runtime.Token;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
import parser.generated.SimpleJavaLexer;
|
|
||||||
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
||||||
|
|
||||||
public class ScannerTest {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This test method is used to test the scanner functionality of the SimpleJavaLexer.
|
|
||||||
* It creates a CharStream from a string representing a simple Java class declaration,
|
|
||||||
* and uses the SimpleJavaLexer to tokenize this input.
|
|
||||||
* It then compares the actual tokens and their types produced by the lexer to the expected tokens and their types.
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void scannerTest() {
|
|
||||||
// Create a CharStream from a string representing a simple Java class declaration
|
|
||||||
CharStream inputCharStream = CharStreams.fromString("public class Name {}");
|
|
||||||
|
|
||||||
// Use the SimpleJavaLexer to tokenize the input
|
|
||||||
SimpleJavaLexer lexer = new SimpleJavaLexer(inputCharStream);
|
|
||||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
|
||||||
tokenStream.fill();
|
|
||||||
|
|
||||||
// Prepare the expected results
|
|
||||||
List<String> expectedTokens = Arrays.asList("public", "class", "Name", "{", "}", "<EOF>");
|
|
||||||
List<String> expectedTokenTypes = Arrays.asList("AccessModifier", "Class", "Identifier", "OpenCurlyBracket", "ClosedCurlyBracket", "EOF");
|
|
||||||
List<Token> actualTokens = tokenStream.getTokens();
|
|
||||||
|
|
||||||
// Compare the actual tokens and their types to the expected tokens and their types
|
|
||||||
assertEquals(expectedTokens.size(), actualTokens.size());
|
|
||||||
for (int i = 0; i < expectedTokens.size(); i++) {
|
|
||||||
assertEquals(expectedTokens.get(i), actualTokens.get(i).getText());
|
|
||||||
assertEquals(expectedTokenTypes.get(i), SimpleJavaLexer.VOCABULARY.getSymbolicName(actualTokens.get(i).getType()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -30,6 +30,6 @@ public class SemanticHelper {
|
|||||||
ParseTree parseTree = parser.program();
|
ParseTree parseTree = parser.program();
|
||||||
ASTBuilder astBuilder = new ASTBuilder();
|
ASTBuilder astBuilder = new ASTBuilder();
|
||||||
|
|
||||||
return (ProgramNode) astBuilder.visit(parseTree);
|
return astBuilder.visit(parseTree);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user