OutputDirectory
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled

This commit is contained in:
i22007 2024-06-27 19:56:54 -04:00
parent 612c967023
commit 90f91d58d1
4 changed files with 26 additions and 12 deletions

View File

@ -1,6 +1,5 @@
package ast; package ast;
import bytecode.visitor.MethodVisitor;
import bytecode.visitor.ProgramVisitor; import bytecode.visitor.ProgramVisitor;
import semantic.SemanticVisitor; import semantic.SemanticVisitor;
import typechecker.TypeCheckResult; import typechecker.TypeCheckResult;

View File

@ -1,13 +1,12 @@
package ast.statements; package ast.statements;
import ast.ASTNode;
import ast.expressions.IExpressionNode; import ast.expressions.IExpressionNode;
import semantic.SemanticVisitor; import semantic.SemanticVisitor;
import typechecker.TypeCheckResult; import typechecker.TypeCheckResult;
public class WhileNode implements IStatementNode { public class WhileNode implements IStatementNode {
IExpressionNode expression; public IExpressionNode expression;
BlockNode block; public BlockNode block;
public WhileNode(IExpressionNode expression, BlockNode block) { public WhileNode(IExpressionNode expression, BlockNode block) {
this.expression = expression; this.expression = expression;

View File

@ -2,6 +2,8 @@ package bytecode;
import ast.ProgramNode; import ast.ProgramNode;
import ast.ClassNode; import ast.ClassNode;
import ast.members.MainMethodNode;
import ast.members.MemberNode;
import bytecode.visitor.ProgramVisitor; import bytecode.visitor.ProgramVisitor;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -27,7 +29,20 @@ public class ByteCodeGenerator implements ProgramVisitor {
try { try {
Manifest manifest = new Manifest(); Manifest manifest = new Manifest();
manifest.getMainAttributes().putValue("Manifest-Version", "1.0"); 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); jarOutputStream = new JarOutputStream(byteArrayOutputStream, manifest);
} catch (IOException e) { } catch (IOException e) {
@ -35,7 +50,7 @@ public class ByteCodeGenerator implements ProgramVisitor {
} }
for (ClassNode classDeclarationNode : programNode.classes) { for (ClassNode classDeclarationNode : programNode.classes) {
ClassCodeGen classCodeGen = new ClassCodeGen(jarOutputStream); ClassCodeGen classCodeGen = new ClassCodeGen(jarOutputStream, outputDirectory);
classDeclarationNode.accept(classCodeGen); classDeclarationNode.accept(classCodeGen);
} }

View File

@ -22,10 +22,12 @@ public class ClassCodeGen implements ClassVisitor {
private Mapper mapper; private Mapper mapper;
private ClassWriter classWriter; private ClassWriter classWriter;
private JarOutputStream jarOutputStream; private JarOutputStream jarOutputStream;
private String outputDirectory;
public ClassCodeGen(JarOutputStream jarOutputStream) { public ClassCodeGen(JarOutputStream jarOutputStream, String outputDirectory) {
mapper = new Mapper(); mapper = new Mapper();
this.jarOutputStream = jarOutputStream; this.jarOutputStream = jarOutputStream;
this.outputDirectory = outputDirectory;
} }
@Override @Override
@ -45,7 +47,7 @@ public class ClassCodeGen implements ClassVisitor {
classWriter.visitEnd(); classWriter.visitEnd();
writeToJar(classWriter.toByteArray(), classNode.identifier); writeToJar(classWriter.toByteArray(), classNode.identifier);
printIntoClassFile(classWriter.toByteArray(), classNode.identifier); printIntoClassFile(classWriter.toByteArray(), classNode.identifier, outputDirectory);
classWriter.visitEnd(); classWriter.visitEnd();
} }
@ -58,14 +60,13 @@ public class ClassCodeGen implements ClassVisitor {
classWriter.visitEnd(); classWriter.visitEnd();
} }
private void printIntoClassFile(byte[] byteCode, String name) { private void printIntoClassFile(byte[] byteCode, String name, String outputDirectory) {
String directoryPath = "src/main/resources/classFileOutput"; File directory = new File(outputDirectory);
File directory = new File(directoryPath);
if (!directory.exists()) { if (!directory.exists()) {
directory.mkdirs(); directory.mkdirs();
} }
String filePath = directoryPath + "/" + name + ".class"; String filePath = outputDirectory + "/" + name + ".class";
try { try {
FileOutputStream fileOutputStream = new FileOutputStream(filePath); FileOutputStream fileOutputStream = new FileOutputStream(filePath);
fileOutputStream.write(byteCode); fileOutputStream.write(byteCode);