make compiler accessible from commandline

This commit is contained in:
simon 2024-06-23 20:30:16 +02:00
parent 64c3ed6db3
commit 849ca1257b
3 changed files with 32 additions and 15 deletions

20
pom.xml
View File

@ -82,17 +82,27 @@
<finalName>compilerUltimate</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>de.maishai.Main</mainClass>
<mainClass>de.maishai.Compiler</mainClass>
</manifest>
</archive>
<outputDirectory>${project.build.directory}</outputDirectory>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

View File

@ -90,15 +90,25 @@ public class Compiler {
return byteCode;
}
public static void generateByteCodeFileFromFile(List<String> sourcePath, List<String> classname) {
List<byte[]> bytes = generateByteCodeArrayFromFile(sourcePath);
public static void generateByteCodeFileFromFile(List<String> sourcePaths) {
List<byte[]> bytes = generateByteCodeArrayFromFile(sourcePaths);
for (int i = 0; i < bytes.size(); i++) {
CodeGenUtils.writeClassfile(bytes.get(i), classname.get(i));
String targetPath = sourcePaths.get(i).replace(".java", ".class");
CodeGenUtils.writeClassfile(bytes.get(i), targetPath);
}
}
public static void main(String[] args) {
generateByteCodeFileFromFile(List.of("src/test/testFiles/JavaTestfilesMore/ClassCanBeBytecoded.java"),
List.of("ClassCanBeBytecoded"));
if (args.length < 1) {
System.out.println("Please provide the Java files to compile as command line arguments.");
return;
}
List<String> sourcePaths = new ArrayList<>();
for (String arg : args) {
String sourcePath = arg.endsWith(".java") ? arg : arg + ".java";
sourcePaths.add(sourcePath);
}
generateByteCodeFileFromFile(sourcePaths);
}
}

View File

@ -1,8 +1,5 @@
package de.maishai.typedast;
import de.maishai.typedast.typedclass.TypedParameter;
import org.objectweb.asm.ClassWriter;
import java.io.FileOutputStream;
import java.util.List;
@ -21,9 +18,9 @@ public class CodeGenUtils {
return builder.toString();
}
public static void writeClassfile(byte[] code, String name) {
public static void writeClassfile(byte[] code, String targetPath) {
try {
FileOutputStream fos = new FileOutputStream("output/" + name + ".class");
FileOutputStream fos = new FileOutputStream(targetPath);
fos.write(code);
fos.close();
} catch (java.io.IOException e) {