ArnoldC
ArnoldC copied to clipboard
Comment source code
If the source was documented, then it would be easier for the community to create pull requests for their potential additions.
Better?
package org.arnoldc
import java.io.FileOutputStream
/**
* This is the entry class of this program.
*/
object ArnoldC {
/**
* Program main function which reads the source file and
* generates the bytecode. Can also run the compiled program
* if "-run" option is given.
*/
def main(args: Array[String]) {
// if there is no filename then print usage
if (args.length < 1) {
println("Usage: ArnoldC [-run] [FileToSourceCode]")
return
}
// get source code from filename
val filename = getFilNameFromArgs(args)
val sourceCode = scala.io.Source.fromFile(filename).mkString
val a = new ArnoldGenerator()
val classFilename = if (filename.contains('.')) {
filename.replaceAll("\\.[^.]*$", "")
}
else {
filename
}
// generate bytecode
val bytecode = a.generate(sourceCode, classFilename)
// write bytecode to class file
val out = new FileOutputStream(classFilename + ".class")
out.write(bytecode)
out.close()
// process options
processOption(getCommandFromArgs(args), classFilename)
}
/**
* Gets the filename from program arguments. If there is no arguments
* then throws an exception.
*/
def getFilNameFromArgs(args:Array[String]):String = args.length match {
case 1 => args(0)
case 2 => args(1)
case _ => throw new RuntimeException("WHAT THE FUCK DID I DO WRONG!")
}
/**
* Gets the additional command from program args. If there is 2 arguments
* then command is the first one, otherwise command is empty. If there is
* no arguments, then throws an exeption.
*/
def getCommandFromArgs(args:Array[String]):String = args.length match {
case 2 => args(0)
case 1 => ""
case _ => throw new RuntimeException("WHAT THE FUCK DID I DO WRONG!")
}
/**
* If "-run" command is given, then executes the given class file. Otherwise
* does nothing.
*/
def processOption(command:String, argFunc: => String):Unit = command match {
case "-run" => Executor.execute(argFunc)
case _ =>
}
}
Thanks, but I guess what I want is to know how to navigate through the many files of source code.