All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.community.flock.wirespec.plugin.CompilerUtil.kt Maven / Gradle / Ivy

There is a newer version: 0.10.17
Show newest version
package community.flock.wirespec.plugin

import arrow.core.Either
import community.flock.wirespec.compiler.core.WirespecSpec
import community.flock.wirespec.compiler.core.emit.JavaEmitter
import community.flock.wirespec.compiler.core.emit.KotlinEmitter
import community.flock.wirespec.compiler.core.emit.ScalaEmitter
import community.flock.wirespec.compiler.core.emit.TypeScriptEmitter
import community.flock.wirespec.compiler.core.emit.WirespecEmitter
import community.flock.wirespec.compiler.core.emit.common.Emitted
import community.flock.wirespec.compiler.core.emit.common.Emitter
import community.flock.wirespec.compiler.core.emit.shared.JavaShared
import community.flock.wirespec.compiler.core.emit.shared.KotlinShared
import community.flock.wirespec.compiler.core.emit.shared.ScalaShared
import community.flock.wirespec.compiler.core.emit.shared.Shared
import community.flock.wirespec.compiler.core.parse
import community.flock.wirespec.compiler.core.parse.Node
import community.flock.wirespec.compiler.utils.Logger

typealias FilesContent = List>

fun FilesContent.parse(logger: Logger): List>> =
    map { (name, source) -> name to WirespecSpec.parse(source)(logger) }
        .map { (name, result) ->
            name to when (result) {
                is Either.Right -> result.value
                is Either.Left -> error("compile error: ${result.value}")
            }
        }

fun FilesContent.compile(logger: Logger, emitter: Emitter) =
    parse(logger)
        .map { (name, ast) -> name to emitter.emit(ast) }
        .flatMap { (name, results) ->
            if (emitter.split) results
            else listOf(Emitted(name, results.first().result))
        }

data class LanguageEmitter(val emitter: Emitter, val extension: FileExtension, val shared: Shared? = null)

fun Language.mapEmitter(packageName: PackageName, logger: Logger) =
    when (this) {
        Language.Java -> LanguageEmitter(JavaEmitter(packageName.value, logger), FileExtension.Java, JavaShared)
        Language.Kotlin -> LanguageEmitter(KotlinEmitter(packageName.value, logger), FileExtension.Kotlin, KotlinShared)
        Language.Scala -> LanguageEmitter(ScalaEmitter(packageName.value, logger), FileExtension.Scala, ScalaShared)
        Language.TypeScript -> LanguageEmitter(TypeScriptEmitter(logger), FileExtension.TypeScript)
        Language.Wirespec -> LanguageEmitter(WirespecEmitter(logger), FileExtension.Wirespec)
    }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy