Exit Full View

Feather2 / documentation / Compiling.md

Compiling

Here's the basic outline to compile a single script :

val config = FeatherConfiguration()

val compiler = FeatherCompiler( config ) 

val script = StringFeatherScript( """
    class HelloWorld {
        func run() {
            println( "Hello World" )
        }
""" )

val results = compiler.compile( script )

Scripts must be compiled in one batch, e.g. if your have 3 scripts which interact with each other, you must compile all three together.

Here's a more typical example, which compiles all scripts in a directory :

fun compileDirectory(dir: File) {
    val config = FeatherConfiguration()
    val compiler = FeatherCompiler(config)

    val scripts = dir.absoluteFile.listFiles()?.filter {
        it.isFile && it.extension == "feather"
    }?.map {
        FileFeatherScript(it)
    }

    if (scripts.isNullOrEmpty()) {
        println( "No feather scripts found in `$dir`" )
    } else {
        try {
            val results = compiler.compile(scripts)
        } catch (e: FeatherException) {
            // Report the error to the user - using something better than println!
            println("${e.message} @ ${e.position.row} , ${e.position.column}")
        }
    }
}

The code starting with val scripts =... looks complicated, but when you break it down, it is quite simple. It uses Java's File API to list all files (in one directory) with an extension of .feather. The resulting array of Files, are convert (mapped) to a list of FileFeatherScript.

The next step is to Run the script.

Back to Contents