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 ast.ProgramNode;
import parser.ASTBuilder; import parser.ASTBuilder;
import parser.generated.SimpleJavaLexer; import parser.generated.SimpleJavaLexer;
@ -60,7 +61,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 MyLogger(); new RaupenLogger();
/* ------------------------- Scanner -> tokens ------------------------- */ /* ------------------------- Scanner -> tokens ------------------------- */
// Use the SimpleJavaLexer to tokenize the input CharStream // Use the SimpleJavaLexer to tokenize the input CharStream
@ -68,34 +69,34 @@ static void compileFile(CharStream inputCharStream, String outputDirectoryPath)
CommonTokenStream tokenStream = new CommonTokenStream(lexer); CommonTokenStream tokenStream = new CommonTokenStream(lexer);
tokenStream.fill(); tokenStream.fill();
// Log the tokens // Log the tokens
i22011 marked this conversation as resolved Outdated

Same Here

Same Here
MyLogger.logScanner(tokenStream); RaupenLogger.logScanner(tokenStream);
/*------------------------- Parser -> Parse tree -------------------------*/ /*------------------------- 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
MyLogger.logParser(parseTree, parser); RaupenLogger.logParser(parseTree, parser);
/*------------------------- AST builder -> AST -------------------------*/ /*------------------------- AST builder -> AST -------------------------*/
i22011 marked this conversation as resolved Outdated

Same Here

Same Here
// 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();
ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree); ASTNode abstractSyntaxTree = astBuilder.visit(parseTree);
// Log the AST // Log the AST
MyLogger.logAST(abstractSyntaxTree); RaupenLogger.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
i22011 marked this conversation as resolved Outdated

Same Here

Same Here
ProgramNode typedAst = (ProgramNode) SemanticAnalyzer.generateTast(abstractSyntaxTree); ASTNode typedAst = SemanticAnalyzer.generateTast(abstractSyntaxTree);
// Log the typed AST // Log the typed AST
MyLogger.logSemanticAnalyzer(typedAst); RaupenLogger.logSemanticAnalyzer(typedAst);
/*------------------------- Bytecode Generator -> Bytecode -------------------------*/ /*------------------------- Bytecode Generator -> Bytecode -------------------------*/
// Use the ByteCodeGenerator to generate bytecode from the typed AST and output it to the specified directory // Use the ByteCodeGenerator to generate bytecode from the typed AST and output it to the specified directory
ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(outputDirectoryPath); ByteCodeGenerator byteCodeGenerator = new ByteCodeGenerator(outputDirectoryPath);
assert typedAst != null; assert typedAst != null;
byteCodeGenerator.visit(typedAst); byteCodeGenerator.visit((ProgramNode) typedAst);
// Log the bytecode generation // 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>consoleHandler.setLevel(Level.OFF);</code>
<code>fileHandler.setLevel(Level.ALL);</code> <code>fileHandler.setLevel(Level.ALL);</code>
*/ */
public class MyLogger { public class RaupenLogger {
static Logger logger = Logger.getLogger("RaupenLogs"); static Logger logger = Logger.getLogger("RaupenLogs");
public MyLogger() { public RaupenLogger() {
// ------------------------- Logging ------------------------- // ------------------------- Logging -------------------------
logger.setLevel(Level.ALL); logger.setLevel(Level.ALL);
logger.getParent().getHandlers()[0].setLevel(Level.ALL); logger.getParent().getHandlers()[0].setLevel(Level.ALL);
@ -97,18 +97,18 @@ public class MyLogger {
logger.info("\n"); logger.info("\n");
} }
public static void logAST(ASTNode node) { public static void logAST(ASTNode abstractSyntaxTree) {
// Printing the AST // Printing the AST
logger.info("-------------------- AST builder -> AST --------------------"); logger.info("-------------------- AST builder -> AST --------------------");
// logger.info("AST: " + ast.toString()); // logger.info("AST: " + ast.toString());
logAST(node, 0); logAST(abstractSyntaxTree, 0);
logger.info("\n"); logger.info("\n");
} }
public static void logSemanticAnalyzer(ASTNode node) { public static void logSemanticAnalyzer(ASTNode typedAst) {
// Printing the typed AST // Printing the typed AST
logger.info("-------------------- Semantic Analyzer -> typed AST --------------------"); logger.info("-------------------- Semantic Analyzer -> typed AST --------------------");
logAST(node, 0); logAST(typedAst, 0);
logger.info("\n"); logger.info("\n");
} }
@ -163,13 +163,13 @@ public class MyLogger {
} }
// TODO: Fix this method // TODO: Fix this method
public static void logAST(ASTNode node, int indent) { public static void logAST(ASTNode abstractSyntaxTree, int indent) {
if (node == null) { if (abstractSyntaxTree == null) {
System.out.println("null"); logger.severe("AST is null !!!");
return; return;
} }
String indentString = " ".repeat(indent * 2); String indentString = " ".repeat(indent * 2);
logger.info(indentString + node.getClass().toString()); logger.info(indentString + abstractSyntaxTree.getClass());
// for (ASTNode child : node.) { // for (ASTNode child : node.) {
// printAST(child, indent + 1); // printAST(child, indent + 1);

View File

@ -57,7 +57,7 @@ public class ClassCodeGen implements ClassVisitor {
private void printIntoClassFile(byte[] byteCode, String name) { private void printIntoClassFile(byte[] byteCode, String name) {
// String outputDirectoryPath = "src/main/resources/output"; // 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); File directory = new File(outputDirectoryPath);
if (!directory.exists()) { if (!directory.exists()) {
directory.mkdirs(); directory.mkdirs();