From 90f91d58d1ad2618f66fcf89bb2c064f715f57ea Mon Sep 17 00:00:00 2001 From: i22007 Date: Thu, 27 Jun 2024 19:56:54 -0400 Subject: [PATCH] OutputDirectory --- src/main/java/ast/ProgramNode.java | 1 - src/main/java/ast/statements/WhileNode.java | 5 ++--- src/main/java/bytecode/ByteCodeGenerator.java | 19 +++++++++++++++++-- src/main/java/bytecode/ClassCodeGen.java | 13 +++++++------ 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/src/main/java/ast/ProgramNode.java b/src/main/java/ast/ProgramNode.java index b13d78e..f8ad19a 100644 --- a/src/main/java/ast/ProgramNode.java +++ b/src/main/java/ast/ProgramNode.java @@ -1,6 +1,5 @@ package ast; -import bytecode.visitor.MethodVisitor; import bytecode.visitor.ProgramVisitor; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; diff --git a/src/main/java/ast/statements/WhileNode.java b/src/main/java/ast/statements/WhileNode.java index 01c1160..35a7590 100644 --- a/src/main/java/ast/statements/WhileNode.java +++ b/src/main/java/ast/statements/WhileNode.java @@ -1,13 +1,12 @@ package ast.statements; -import ast.ASTNode; import ast.expressions.IExpressionNode; import semantic.SemanticVisitor; import typechecker.TypeCheckResult; public class WhileNode implements IStatementNode { - IExpressionNode expression; - BlockNode block; + public IExpressionNode expression; + public BlockNode block; public WhileNode(IExpressionNode expression, BlockNode block) { this.expression = expression; diff --git a/src/main/java/bytecode/ByteCodeGenerator.java b/src/main/java/bytecode/ByteCodeGenerator.java index 1fb4ec4..280bda1 100644 --- a/src/main/java/bytecode/ByteCodeGenerator.java +++ b/src/main/java/bytecode/ByteCodeGenerator.java @@ -2,6 +2,8 @@ package bytecode; import ast.ProgramNode; import ast.ClassNode; +import ast.members.MainMethodNode; +import ast.members.MemberNode; import bytecode.visitor.ProgramVisitor; import java.io.ByteArrayOutputStream; @@ -27,7 +29,20 @@ public class ByteCodeGenerator implements ProgramVisitor { try { Manifest manifest = new Manifest(); manifest.getMainAttributes().putValue("Manifest-Version", "1.0"); - manifest.getMainAttributes().putValue("Main-Class", programNode.classes.get(0).identifier); + boolean foundMainClass = false; + for (ClassNode classNode : programNode.classes) { + if (foundMainClass) { + break; + } + for (MemberNode memberNode : classNode.members) { + if (memberNode instanceof MainMethodNode) { + manifest.getMainAttributes().putValue("Main-Class", classNode.identifier); + foundMainClass = true; + break; + } + } + } + jarOutputStream = new JarOutputStream(byteArrayOutputStream, manifest); } catch (IOException e) { @@ -35,7 +50,7 @@ public class ByteCodeGenerator implements ProgramVisitor { } for (ClassNode classDeclarationNode : programNode.classes) { - ClassCodeGen classCodeGen = new ClassCodeGen(jarOutputStream); + ClassCodeGen classCodeGen = new ClassCodeGen(jarOutputStream, outputDirectory); classDeclarationNode.accept(classCodeGen); } diff --git a/src/main/java/bytecode/ClassCodeGen.java b/src/main/java/bytecode/ClassCodeGen.java index f58608c..3fec11a 100644 --- a/src/main/java/bytecode/ClassCodeGen.java +++ b/src/main/java/bytecode/ClassCodeGen.java @@ -22,10 +22,12 @@ public class ClassCodeGen implements ClassVisitor { private Mapper mapper; private ClassWriter classWriter; private JarOutputStream jarOutputStream; + private String outputDirectory; - public ClassCodeGen(JarOutputStream jarOutputStream) { + public ClassCodeGen(JarOutputStream jarOutputStream, String outputDirectory) { mapper = new Mapper(); this.jarOutputStream = jarOutputStream; + this.outputDirectory = outputDirectory; } @Override @@ -45,7 +47,7 @@ public class ClassCodeGen implements ClassVisitor { classWriter.visitEnd(); writeToJar(classWriter.toByteArray(), classNode.identifier); - printIntoClassFile(classWriter.toByteArray(), classNode.identifier); + printIntoClassFile(classWriter.toByteArray(), classNode.identifier, outputDirectory); classWriter.visitEnd(); } @@ -58,14 +60,13 @@ public class ClassCodeGen implements ClassVisitor { classWriter.visitEnd(); } - private void printIntoClassFile(byte[] byteCode, String name) { - String directoryPath = "src/main/resources/classFileOutput"; - File directory = new File(directoryPath); + private void printIntoClassFile(byte[] byteCode, String name, String outputDirectory) { + File directory = new File(outputDirectory); if (!directory.exists()) { directory.mkdirs(); } - String filePath = directoryPath + "/" + name + ".class"; + String filePath = outputDirectory + "/" + name + ".class"; try { FileOutputStream fileOutputStream = new FileOutputStream(filePath); fileOutputStream.write(byteCode);