Compare commits
6 Commits
ea97f34398
...
Tests
Author | SHA1 | Date | |
---|---|---|---|
|
729f4f23d6 | ||
|
a1a9cce511 | ||
|
f7a4e65093 | ||
|
2934872457 | ||
|
1c327705d8 | ||
|
49195c754c |
6
.gitignore
vendored
6
.gitignore
vendored
@@ -77,10 +77,10 @@ fabric.properties
|
||||
.idea/caches/build_file_checksums.ser
|
||||
|
||||
/target
|
||||
src/main/resources/logs/miniCompilerLog.log
|
||||
src/main/resources/logs/RaupenLog.log
|
||||
src/main/resources/output/CompilerInput.class
|
||||
src/test/resources/output/javac/CompilerInput$Test.class
|
||||
src/test/resources/output/javac/CompilerInput.class
|
||||
src/test/resources/output/miniCompiler/CompilerInput.class
|
||||
src/test/resources/output/miniCompiler/CompilerInput$Test.class
|
||||
src/test/resources/output/raupenpiler/CompilerInput.class
|
||||
src/test/resources/output/raupenpiler/CompilerInput$Test.class
|
||||
.idea/inspectionProfiles/Project_Default.xml
|
||||
|
57
readme.md
57
readme.md
@@ -1,6 +1,6 @@
|
||||
# "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).
|
||||
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.
|
||||
|
||||
@@ -43,10 +43,21 @@ src/
|
||||
└── resources/
|
||||
test/
|
||||
└── java/
|
||||
│ ├── main/ -> MainTest, E2ETests, UtilityTests
|
||||
│ ├── main/
|
||||
│ ├── parser/ -> Performs tests on the parser
|
||||
│ └── semantic/ -> Performs tests on the semantic check
|
||||
└── resources/ -> Ressources for running the Tests
|
||||
│ └── semantic/
|
||||
└── resources/ -> Ressources for running the Tests
|
||||
├──input
|
||||
│ ├── combinedFeatureTests
|
||||
│ ├── endabgabeTests
|
||||
│ ├── failureTests
|
||||
│ ├── singleFeatureSemanticTests
|
||||
│ ├── singleFeatureTests
|
||||
│ ├── typedAstExceptionsTests
|
||||
│ └── typedAstFeatureTests
|
||||
└──output
|
||||
├── javac
|
||||
└── raupenpiler
|
||||
```
|
||||
|
||||
## Class-Diagramm AST
|
||||
@@ -62,39 +73,5 @@ test/
|
||||
|
||||
|
||||
## 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
|
||||
## Download
|
||||
```bash
|
@@ -17,10 +17,10 @@ import java.util.Optional;
|
||||
|
||||
|
||||
/**
|
||||
* Start miniCompiler using make:
|
||||
* Start Raupenpiler using make:
|
||||
* <p> <code> cd .\src\test\ </code>
|
||||
* <p> <code> make clean compile-miniCompiler </code>
|
||||
* <p> Start miniCompiler using jar:
|
||||
* <p> <code> make clean compile-raupenpiler </code>
|
||||
* <p> Start Raupenpiler 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> 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>
|
||||
@@ -40,6 +40,16 @@ public class Main {
|
||||
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());
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -56,7 +66,7 @@ public class Main {
|
||||
*/
|
||||
static void compileFile(CharStream inputCharStream, String outputDirectoryPath) {
|
||||
// Initialize the logger
|
||||
new MiniCompilerLogger();
|
||||
new RaupenLogger();
|
||||
|
||||
/* ------------------------- Scanner -> tokens ------------------------- */
|
||||
// Use the SimpleJavaLexer to tokenize the input CharStream
|
||||
@@ -64,27 +74,27 @@ public class Main {
|
||||
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||
tokenStream.fill();
|
||||
// Log the tokens
|
||||
MiniCompilerLogger.logScanner(tokenStream);
|
||||
RaupenLogger.logScanner(tokenStream);
|
||||
|
||||
/*------------------------- Parser -> Parsetree -------------------------*/
|
||||
// Use the SimpleJavaParser to parse the tokens and generate a ParseTree
|
||||
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||
ParseTree parseTree = parser.program(); // parse the input
|
||||
// Log the ParseTree
|
||||
MiniCompilerLogger.logParser(parseTree, parser);
|
||||
RaupenLogger.logParser(parseTree, parser);
|
||||
|
||||
/*------------------------- AST builder -> AST -------------------------*/
|
||||
// Use the ASTBuilder to visit the ParseTree and generate an Abstract Syntax Tree (AST)
|
||||
ASTBuilder astBuilder = new ASTBuilder();
|
||||
ASTNode abstractSyntaxTree = astBuilder.visit(parseTree);
|
||||
// Log the AST
|
||||
MiniCompilerLogger.logAST(abstractSyntaxTree);
|
||||
RaupenLogger.logAST(abstractSyntaxTree);
|
||||
|
||||
/*------------------------- Semantic Analyzer -> typed AST -------------------------*/
|
||||
// Use the SemanticAnalyzer to generate a typed AST
|
||||
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||
// Log the typed AST
|
||||
MiniCompilerLogger.logSemanticAnalyzer(typedAst);
|
||||
RaupenLogger.logSemanticAnalyzer(typedAst);
|
||||
|
||||
if(SemanticAnalyzer.errors.isEmpty()){
|
||||
/*------------------------- Bytecode Generator -> Bytecode -------------------------*/
|
||||
@@ -97,7 +107,7 @@ public class Main {
|
||||
assert typedAst != null;
|
||||
byteCodeGenerator.visit((ProgramNode) typedAst);
|
||||
// Log the bytecode generation
|
||||
MiniCompilerLogger.logBytecodeGenerator();
|
||||
RaupenLogger.logBytecodeGenerator();
|
||||
} else {
|
||||
for(Exception exception : SemanticAnalyzer.errors){
|
||||
exception.printStackTrace();
|
||||
|
@@ -29,11 +29,11 @@ import java.util.logging.*;
|
||||
* <code>consoleHandler.setLevel(Level.OFF);</code>
|
||||
* <code>fileHandler.setLevel(Level.ALL);</code>
|
||||
*/
|
||||
public class MiniCompilerLogger {
|
||||
public class RaupenLogger {
|
||||
|
||||
static Logger logger = Logger.getLogger("miniCompilerLogs");
|
||||
static Logger logger = Logger.getLogger("RaupenLogs");
|
||||
|
||||
public MiniCompilerLogger() {
|
||||
public RaupenLogger() {
|
||||
// ------------------------- Logging -------------------------
|
||||
logger.setLevel(Level.ALL);
|
||||
logger.getParent().getHandlers()[0].setLevel(Level.ALL);
|
||||
@@ -66,7 +66,7 @@ public class MiniCompilerLogger {
|
||||
logger.addHandler(consoleHandler);
|
||||
|
||||
// Configure file handler
|
||||
Handler fileHandler = new FileHandler("src/main/resources/logs/miniCompiler.log");
|
||||
Handler fileHandler = new FileHandler("src/main/resources/logs/RaupenLog.log");
|
||||
// Toggle file logging on/off
|
||||
fileHandler.setLevel(Level.ALL);
|
||||
fileHandler.setFormatter(new CustomFormatter());
|
||||
@@ -117,7 +117,7 @@ public class MiniCompilerLogger {
|
||||
public static void logBytecodeGenerator() {
|
||||
// Printing the bytecode
|
||||
logger.info("-------------------- Bytecode Generator -> Bytecode --------------------");
|
||||
logger.info("Bytecode generated without errors.");
|
||||
logger.info("Bytecode generated");
|
||||
logger.info("\n");
|
||||
}
|
||||
|
@@ -253,7 +253,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
|
||||
if (toCheck.memberAccess != null) {
|
||||
var result = toCheck.memberAccess.accept(this);
|
||||
toCheck.identifier = toCheck.memberAccess.identifiers.getLast();
|
||||
toCheck.setTypeNode(result.getType());
|
||||
return result;
|
||||
} else {
|
||||
@@ -317,13 +316,9 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
@Override
|
||||
public TypeCheckResult analyze(IfElseNode toCheck) {
|
||||
var resultIf = toCheck.ifStatement.accept(this);
|
||||
if(toCheck.elseStatement != null){
|
||||
var resultElse = toCheck.elseStatement.accept(this);
|
||||
return new TypeCheckResult(resultIf.isValid() && resultElse.isValid(), new BaseType(TypeEnum.VOID));
|
||||
}
|
||||
var resultElse = toCheck.elseStatement.accept(this);
|
||||
|
||||
|
||||
return new TypeCheckResult(resultIf.isValid(), new BaseType(TypeEnum.VOID));
|
||||
return new TypeCheckResult(resultIf.isValid() && resultElse.isValid(), new BaseType(TypeEnum.VOID));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -609,15 +604,11 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
ITypeNode currentType = null;
|
||||
int start = 0;
|
||||
if(!memberAccessNode.identifiers.isEmpty()){
|
||||
if(currentFields.get(memberAccessNode.identifiers.getFirst()) != null){
|
||||
memberAccessNode.identifiers.addFirst(currentClass.identifier);
|
||||
start++;
|
||||
if(currentFields.get(memberAccessNode.identifiers.get(0)) != null){
|
||||
memberAccessNode.identifiers.add(0, currentClass.identifier);
|
||||
start = 1;
|
||||
}
|
||||
}
|
||||
if(context.getClasses().get(memberAccessNode.identifiers.getFirst()) == null){
|
||||
memberAccessNode.identifiers.addFirst(currentClass.identifier);
|
||||
start++;
|
||||
}
|
||||
for (int i = start; i < memberAccessNode.identifiers.size(); i++) {
|
||||
|
||||
String s = memberAccessNode.identifiers.get(i);
|
||||
@@ -635,8 +626,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
||||
} else {
|
||||
if (currentType instanceof ReferenceType reference) {
|
||||
var currentTypeClass = context.getClass(reference.getIdentifier());
|
||||
memberAccessNode.identifiers.add(i, reference.getIdentifier());
|
||||
i++;
|
||||
|
||||
var currentField = currentTypeClass.getField(s);
|
||||
if (currentField.getAccessModifier().accessType == EnumAccessModifierNode.PUBLIC) {
|
||||
currentType = currentField.getType();
|
||||
|
@@ -2,17 +2,23 @@
|
||||
### IntelliJs play buttons do not work. Run in "src/test" folder with "make" command to run all
|
||||
### Or run only parts with "make compile-javac", "make clean" etc.
|
||||
|
||||
all: compile-javac compile-miniCompiler
|
||||
all: compile-javac compile-raupenpiler
|
||||
|
||||
compile-javac:
|
||||
javac -d .\resources\output\javac .\resources\input\CompilerInput.java
|
||||
|
||||
compile-miniCompiler:
|
||||
compile-raupenpiler:
|
||||
cd ../.. ; mvn -DskipTests install
|
||||
cd ../.. ; mvn exec:java -DgenJar=true -DgenClass=true -Dexec.mainClass="main.Main" -Dexec.args="'src/main/resources/input/CompilerInput.java' 'src/main/resources/output'"
|
||||
# cp ../main/resources/output/CompilerInput.class .java/resources/output/miniCompiler
|
||||
# cp ../main/resources/output/CompilerInput.class .java/resources/output/raupenpiler
|
||||
|
||||
test-miniCompiler:
|
||||
test: compile-javac compile-raupenpiler test-javac test-raupenpiler
|
||||
|
||||
test-javac:
|
||||
# gleich wie bei raupenpiler, kann ich ohne funktionierenden Compiler nicht testen
|
||||
|
||||
|
||||
test-raupenpiler:
|
||||
# move the compiled class to the test/main folder
|
||||
mv ../main/resources/output/CompilerInput.class .java/main/
|
||||
# compile the test class
|
||||
@@ -28,8 +34,8 @@ clean:
|
||||
rm -f ../main/resources/output/*.jar
|
||||
# clean resources output folders
|
||||
rm -f ./resources/output/javac/*.class
|
||||
rm -f ./resources/output/miniCompiler/*.class
|
||||
rm -f ./resources/output/miniCompiler/*.jar
|
||||
rm -f ./resources/output/raupenpiler/*.class
|
||||
rm -f ./resources/output/raupenpiler/*.jar
|
||||
# clean logs
|
||||
rm -f ../main/resources/logs/*
|
||||
# clean test/java/main folders from .class files for End-to-End tests
|
||||
|
File diff suppressed because it is too large
Load Diff
@@ -11,6 +11,8 @@ package main;
|
||||
* 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>
|
||||
*
|
||||
* <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 static void main(String[] args) {
|
||||
|
@@ -338,17 +338,6 @@ public class EndToTypedAstTest {
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void Expression(){
|
||||
|
||||
ASTNode tast = SemanticHelper.generateTypedASTFrom("src/test/resources/input/singleFeatureSemanticTests/Expression.java");
|
||||
|
||||
SemanticAnalyzer.generateTast(tast);
|
||||
|
||||
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||
|
||||
}
|
||||
|
||||
// ------------------ Helpers ------------------
|
||||
|
||||
/**
|
||||
|
@@ -1,10 +0,0 @@
|
||||
class VariableCompare{
|
||||
|
||||
void trueMethod(boolean a, int c) {
|
||||
if(a && c == 10){
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -1,16 +1,13 @@
|
||||
public class Compiler {
|
||||
Node node;
|
||||
public int add(int i, int j) {
|
||||
node = new Node();
|
||||
node.x = 1;
|
||||
return i+j;
|
||||
}
|
||||
}
|
||||
public class Klasse1 {
|
||||
public int test;
|
||||
|
||||
public class Node {
|
||||
public int x;
|
||||
public void main() {
|
||||
Compiler compiler = new Compiler();
|
||||
int i = compiler.add(5, 8);
|
||||
public int test1() {
|
||||
test = 5;
|
||||
return 1;
|
||||
}
|
||||
|
||||
public void test2() {
|
||||
int testInt;
|
||||
testInt = this.test1();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user