refactoring, readme

This commit is contained in:
Lucas 2024-07-04 16:49:14 +02:00
parent fb5372bc8f
commit 2f7b310254
7 changed files with 683 additions and 663 deletions

6
.gitignore vendored
View File

@ -77,10 +77,10 @@ fabric.properties
.idea/caches/build_file_checksums.ser
/target
src/main/resources/logs/RaupenLog.log
src/main/resources/logs/miniCompilerLog.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/raupenpiler/CompilerInput.class
src/test/resources/output/raupenpiler/CompilerInput$Test.class
src/test/resources/output/miniCompiler/CompilerInput.class
src/test/resources/output/miniCompiler/CompilerInput$Test.class
.idea/inspectionProfiles/Project_Default.xml

View File

@ -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 Suttgart (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 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.
@ -62,7 +62,39 @@ 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
```
## Download
### 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'
```
```bash
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

View File

@ -17,10 +17,10 @@ import java.util.Optional;
/**
* Start Raupenpiler using make:
* Start miniCompiler using make:
* <p> <code> cd .\src\test\ </code>
* <p> <code> make clean compile-raupenpiler </code>
* <p> Start Raupenpiler using jar:
* <p> <code> make clean compile-miniCompiler </code>
* <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> 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,16 +40,6 @@ 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());
}
}
*/
}
/**
@ -66,7 +56,7 @@ public class Main {
*/
static void compileFile(CharStream inputCharStream, String outputDirectoryPath) {
// Initialize the logger
new RaupenLogger();
new MiniCompilerLogger();
/* ------------------------- Scanner -> tokens ------------------------- */
// Use the SimpleJavaLexer to tokenize the input CharStream
@ -74,27 +64,27 @@ public class Main {
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
tokenStream.fill();
// Log the tokens
RaupenLogger.logScanner(tokenStream);
MiniCompilerLogger.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
RaupenLogger.logParser(parseTree, parser);
MiniCompilerLogger.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
RaupenLogger.logAST(abstractSyntaxTree);
MiniCompilerLogger.logAST(abstractSyntaxTree);
/*------------------------- Semantic Analyzer -> typed AST -------------------------*/
// Use the SemanticAnalyzer to generate a typed AST
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
// Log the typed AST
RaupenLogger.logSemanticAnalyzer(typedAst);
MiniCompilerLogger.logSemanticAnalyzer(typedAst);
if(SemanticAnalyzer.errors.isEmpty()){
/*------------------------- Bytecode Generator -> Bytecode -------------------------*/
@ -107,7 +97,7 @@ public class Main {
assert typedAst != null;
byteCodeGenerator.visit((ProgramNode) typedAst);
// Log the bytecode generation
RaupenLogger.logBytecodeGenerator();
MiniCompilerLogger.logBytecodeGenerator();
} else {
for(Exception exception : SemanticAnalyzer.errors){
exception.printStackTrace();

View File

@ -29,11 +29,11 @@ import java.util.logging.*;
* <code>consoleHandler.setLevel(Level.OFF);</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 -------------------------
logger.setLevel(Level.ALL);
logger.getParent().getHandlers()[0].setLevel(Level.ALL);
@ -66,7 +66,7 @@ public class RaupenLogger {
logger.addHandler(consoleHandler);
// 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
fileHandler.setLevel(Level.ALL);
fileHandler.setFormatter(new CustomFormatter());
@ -117,7 +117,7 @@ public class RaupenLogger {
public static void logBytecodeGenerator() {
// Printing the bytecode
logger.info("-------------------- Bytecode Generator -> Bytecode --------------------");
logger.info("Bytecode generated");
logger.info("Bytecode generated without errors.");
logger.info("\n");
}

View File

@ -2,17 +2,17 @@
### 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-raupenpiler
all: compile-javac compile-miniCompiler
compile-javac:
javac -d .\resources\output\javac .\resources\input\CompilerInput.java
compile-raupenpiler:
compile-miniCompiler:
cd ../.. ; mvn -DskipTests install
cd ../.. ; mvn exec:java -DgenJar=true -DgenClass=true -Dexec.mainClass="main.Main" -Dexec.args="'src/main/resources/input/CompilerInput.java' 'src/main/resources/output'"
# cp ../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
mv ../main/resources/output/CompilerInput.class .java/main/
# compile the test class
@ -28,8 +28,8 @@ clean:
rm -f ../main/resources/output/*.jar
# clean resources output folders
rm -f ./resources/output/javac/*.class
rm -f ./resources/output/raupenpiler/*.class
rm -f ./resources/output/raupenpiler/*.jar
rm -f ./resources/output/miniCompiler/*.class
rm -f ./resources/output/miniCompiler/*.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

View File

@ -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,
* 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) {