Class AbstractCompiler

java.lang.Object
net.emustudio.emulib.plugin.AbstractPlugin
net.emustudio.emulib.plugin.compiler.AbstractCompiler
All Implemented Interfaces:
Compiler, Plugin

public abstract class AbstractCompiler extends AbstractPlugin implements Compiler
Compiler base. Implements listeners and the compilation workflow:
  1. listeners are notified about the start; title and version are reported
  2. output path is resolved (replacing source file extension by getOutputExtension())
  3. compile(Path, Path) is called; CompileException or any other exception is reported as an error
  4. listeners are notified about the finish
  • Constructor Details

    • AbstractCompiler

      public AbstractCompiler(long pluginID, ApplicationApi applicationApi, PluginSettings settings)
      Creates new instance.
      Parameters:
      pluginID - plugin ID
      applicationApi - emuStudio API
      settings - plugin custom settings
  • Method Details

    • compile

      protected abstract void compile(Path input, Path output) throws Exception
      Compiles the input file into the output file.

      Errors can be reported either by error(SourceCodePosition, String) or by throwing an exception.

      Parameters:
      input - input file path (source code)
      output - output file path
      Throws:
      Exception - on compilation error
    • getOutputExtension

      protected abstract String getOutputExtension()
      Output file extension (without dot), used when output path is not given.
      Returns:
      output file extension, e.g. "hex"
    • compile

      public final void compile(Path inputPath, Optional<Path> outputPath)
      Description copied from interface: Compiler
      Compile given input file.

      If output path is not given, it is derived by replacing the source file extension of the input path. Usually a successfully compiled program is loaded in memory.

      Specified by:
      compile in interface Compiler
      Parameters:
      inputPath - input file path (source code)
      outputPath - optional output file path
    • addCompilerListener

      public void addCompilerListener(CompilerListener listener)
      Description copied from interface: Compiler
      Adds compiler listener.
      Specified by:
      addCompilerListener in interface Compiler
      Parameters:
      listener - compiler listener
    • removeCompilerListener

      public void removeCompilerListener(CompilerListener listener)
      Description copied from interface: Compiler
      Removes compiler listener.
      Specified by:
      removeCompilerListener in interface Compiler
      Parameters:
      listener - compiler listener
    • readSource

      protected final String readSource(Path path) throws IOException
      Reads UTF-8 source and reports the exact text to the compilation listeners.
      Throws:
      IOException
    • error

      protected void error(String message)
    • error

      protected void error(SourceCodePosition position, String message)
      Parameters:
      position - source position, or null
      message - message
    • warning

      protected void warning(String message)
    • info

      protected void info(String message)
    • runCommandLine

      public static int runCommandLine(String jarName, String[] args, AbstractCompiler.Factory factory)
      Command-line runner for compilers. Usage: [-o outputFile] inputFile, --version, --help. Messages are printed to standard error output.
      Parameters:
      jarName - jar file name used in help
      args - command-line arguments
      factory - compiler constructor, e.g. MyCompiler::new
      Returns:
      exit code: 0 if successful, 1 if there were errors