diff --git a/.idea/libraries/Maven__info_picocli_picocli_3_5_2.xml b/.idea/libraries/Maven__info_picocli_picocli_3_5_2.xml new file mode 100644 index 000000000..b0f17c8fc --- /dev/null +++ b/.idea/libraries/Maven__info_picocli_picocli_3_5_2.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/kickc.iml b/kickc.iml index 95ebcf0e8..26524d0bf 100644 --- a/kickc.iml +++ b/kickc.iml @@ -1,6 +1,6 @@ - + @@ -21,5 +21,6 @@ + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 85773e20a..8f6041617 100644 --- a/pom.xml +++ b/pom.xml @@ -38,6 +38,11 @@ kickassembler 4.19 + + info.picocli + picocli + 3.5.2 + diff --git a/src/main/java/dk/camelot64/kickc/KickC.java b/src/main/java/dk/camelot64/kickc/KickC.java new file mode 100644 index 000000000..0f04d3bea --- /dev/null +++ b/src/main/java/dk/camelot64/kickc/KickC.java @@ -0,0 +1,69 @@ +package dk.camelot64.kickc; + +import dk.camelot64.kickc.model.Program; +import picocli.CommandLine; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.OutputStreamWriter; +import java.util.concurrent.Callable; + +/** KickC Commandline */ +@CommandLine.Command(description = "Compiles KickC source files to KickAssembler.", + name = "kickc", mixinStandardHelpOptions = true, version = "KickC 0.7") +public class KickC implements Callable { + + @CommandLine.Parameters(index = "0", description = "The KickC file to compile.") + private File file = null; + + @CommandLine.Option(names = {"-libdir", "-I"}, description = "Path to a library folder, where the compiler looks for included files.") + private File libdir = null; + + @CommandLine.Option(names = {"-o"}, description = "Name of the output file. By default it is the same as the input file with extension .asm") + private String outname = null; + + public static void main(String[] args) throws Exception { + CommandLine.call(new KickC(), args); + } + + @Override + public Void call() throws Exception { + System.out.println("//------------------------------------"); + System.out.println("// KickC v0.7 by Jesper Gravgaard "); + System.out.println("//------------------------------------"); + Compiler compiler = new Compiler(); + if(libdir != null) { + compiler.addImportPath(libdir.getPath()); + } + if(outname == null) { + outname = getFileBaseName(file) + ".asm"; + } + System.out.println("Compiling "+file.getPath()); + Program program = compiler.compile(file.getName()); + System.out.println("Writing asm file "+outname); + FileOutputStream outputStream = new FileOutputStream(outname); + OutputStreamWriter writer = new OutputStreamWriter(outputStream); + String assembler = program.getAsm().toString(false); + writer.write(assembler); + writer.close(); + outputStream.close(); + + return null; + } + + String getFileBaseName(File file) { + String name = file.getName(); + int i = name.lastIndexOf('.'); + return i > 0 ? name.substring(0, i) : name; + } + + String getFileExtension(File file) { + if(file == null) { + return ""; + } + String name = file.getName(); + int i = name.lastIndexOf('.'); + return i > 0 ? name.substring(i + 1) : ""; + } + +}