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;
import bytecode.visitor.MethodVisitor;
import bytecode.visitor.ProgramVisitor;
import semantic.SemanticVisitor;
import typechecker.TypeCheckResult;

View File

@ -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;

View File

@ -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);
}

View File

@ -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);