Tests, Structure, More #10

Merged
i22005 merged 15 commits from Tests into main 2024-06-21 16:16:54 +00:00
3 changed files with 22 additions and 21 deletions
Showing only changes of commit 8f742191bb - Show all commits

View File

@ -1,3 +1,4 @@
import ast.ASTNode;
import ast.ProgramNode;
import parser.ASTBuilder;
import parser.generated.SimpleJavaLexer;
@ -60,7 +61,7 @@ public class Main {
*/
static void compileFile(CharStream inputCharStream, String outputDirectoryPath) {
// Initialize the logger
new MyLogger();
new RaupenLogger();
/* ------------------------- Scanner -> tokens ------------------------- */
// Use the SimpleJavaLexer to tokenize the input CharStream
@ -68,34 +69,34 @@ static void compileFile(CharStream inputCharStream, String outputDirectoryPath)
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
tokenStream.fill();
// Log the tokens
MyLogger.logScanner(tokenStream);
RaupenLogger.logScanner(tokenStream);
/*------------------------- Parser -> Parse tree -------------------------*/
/*------------------------- 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
MyLogger.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();
ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree);
ASTNode abstractSyntaxTree = astBuilder.visit(parseTree);
// Log the AST
MyLogger.logAST(abstractSyntaxTree);
RaupenLogger.logAST(abstractSyntaxTree);
/*------------------------- Semantic Analyzer -> typed AST -------------------------*/
// Use the SemanticAnalyzer to generate a typed AST
ProgramNode typedAst = (ProgramNode) SemanticAnalyzer.generateTast(abstractSyntaxTree);
ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
// Log the typed AST
MyLogger.logSemanticAnalyzer(typedAst);
RaupenLogger.logSemanticAnalyzer(typedAst);
/*------------------------- Bytecode Generator -> Bytecode -------------------------*/
// Use the ByteCodeGenerator to generate bytecode from the typed AST and output it to the specified directory
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(outputDirectoryPath);
assert typedAst != null;
byteCodeGenerator.visit(typedAst);
byteCodeGenerator.visit((ProgramNode) typedAst);
// Log the bytecode generation
MyLogger.logBytecodeGenerator();
RaupenLogger.logBytecodeGenerator();
}
}

View File

@ -27,11 +27,11 @@ import java.util.logging.*;
<code>consoleHandler.setLevel(Level.OFF);</code>
<code>fileHandler.setLevel(Level.ALL);</code>
*/
public class MyLogger {
public class RaupenLogger {
static Logger logger = Logger.getLogger("RaupenLogs");
public MyLogger() {
public RaupenLogger() {
// ------------------------- Logging -------------------------
logger.setLevel(Level.ALL);
logger.getParent().getHandlers()[0].setLevel(Level.ALL);
@ -97,18 +97,18 @@ public class MyLogger {
logger.info("\n");
}
public static void logAST(ASTNode node) {
public static void logAST(ASTNode abstractSyntaxTree) {
// Printing the AST
logger.info("-------------------- AST builder -> AST --------------------");
// logger.info("AST: " + ast.toString());
logAST(node, 0);
logAST(abstractSyntaxTree, 0);
logger.info("\n");
}
public static void logSemanticAnalyzer(ASTNode node) {
public static void logSemanticAnalyzer(ASTNode typedAst) {
// Printing the typed AST
logger.info("-------------------- Semantic Analyzer -> typed AST --------------------");
logAST(node, 0);
logAST(typedAst, 0);
logger.info("\n");
}
@ -163,13 +163,13 @@ public class MyLogger {
}
// TODO: Fix this method
public static void logAST(ASTNode node, int indent) {
if (node == null) {
System.out.println("null");
public static void logAST(ASTNode abstractSyntaxTree, int indent) {
if (abstractSyntaxTree == null) {
logger.severe("AST is null !!!");
return;
}
String indentString = " ".repeat(indent * 2);
logger.info(indentString + node.getClass().toString());
logger.info(indentString + abstractSyntaxTree.getClass());
// for (ASTNode child : node.) {
// printAST(child, indent + 1);

View File

@ -57,7 +57,7 @@ public class ClassCodeGen implements ClassVisitor {
private void printIntoClassFile(byte[] byteCode, String name) {
// String outputDirectoryPath = "src/main/resources/output";
System.out.println("Output directory path: " + outputDirectoryPath);
// System.out.println("Output directory path: " + outputDirectoryPath);
File directory = new File(outputDirectoryPath);
if (!directory.exists()) {
directory.mkdirs();