refactor compiler arguments passing

This commit is contained in:
Irmen de Jong 2021-11-30 01:40:21 +01:00
parent 83f893f50b
commit 3d1d0696b9
6 changed files with 77 additions and 49 deletions

View File

@ -3,6 +3,7 @@ package prog8
import kotlinx.cli.* import kotlinx.cli.*
import prog8.ast.base.AstException import prog8.ast.base.AstException
import prog8.compiler.CompilationResult import prog8.compiler.CompilationResult
import prog8.compiler.CompilerArguments
import prog8.compiler.compileProgram import prog8.compiler.compileProgram
import prog8.compiler.target.C64Target import prog8.compiler.target.C64Target
import prog8.compiler.target.Cx16Target import prog8.compiler.target.Cx16Target
@ -80,10 +81,18 @@ private fun compileMain(args: Array<String>): Boolean {
val results = mutableListOf<CompilationResult>() val results = mutableListOf<CompilationResult>()
for(filepathRaw in moduleFiles) { for(filepathRaw in moduleFiles) {
val filepath = pathFrom(filepathRaw).normalize() val filepath = pathFrom(filepathRaw).normalize()
val compilationResult = compileProgram(filepath, val args = CompilerArguments(
dontOptimize!=true, optimizeFloatExpressions==true, filepath,
dontWriteAssembly!=true, slowCodegenWarnings==true, quietAssembler==true, dontOptimize != true,
compilationTarget, srcdirs, outputPath) optimizeFloatExpressions == true,
dontWriteAssembly != true,
slowCodegenWarnings == true,
quietAssembler == true,
compilationTarget,
srcdirs,
outputPath
)
val compilationResult = compileProgram(args)
results.add(compilationResult) results.add(compilationResult)
} }
@ -120,11 +129,19 @@ private fun compileMain(args: Array<String>): Boolean {
val filepath = pathFrom(filepathRaw).normalize() val filepath = pathFrom(filepathRaw).normalize()
val compilationResult: CompilationResult val compilationResult: CompilationResult
try { try {
compilationResult = compileProgram(filepath, val args = CompilerArguments(
dontOptimize!=true, optimizeFloatExpressions==true, filepath,
dontWriteAssembly!=true, slowCodegenWarnings==true, quietAssembler==true, dontOptimize != true,
compilationTarget, srcdirs, outputPath) optimizeFloatExpressions == true,
if(!compilationResult.success) dontWriteAssembly != true,
slowCodegenWarnings == true,
quietAssembler == true,
compilationTarget,
srcdirs,
outputPath
)
compilationResult = compileProgram(args)
if (!compilationResult.success)
return false return false
} catch (x: AstException) { } catch (x: AstException) {
return false return false

View File

@ -28,26 +28,27 @@ class CompilationResult(val success: Boolean,
val compTarget: ICompilationTarget, val compTarget: ICompilationTarget,
val importedFiles: List<Path>) val importedFiles: List<Path>)
class CompilerArguments(val filepath: Path,
val optimize: Boolean,
val optimizeFloatExpressions: Boolean,
val writeAssembly: Boolean,
val slowCodegenWarnings: Boolean,
val quietAssembler: Boolean,
val compilationTarget: String,
val sourceDirs: List<String> = emptyList(),
val outputDir: Path = Path(""),
val errors: IErrorReporter = ErrorReporter())
// TODO refactor the gigantic list of parameters
fun compileProgram(filepath: Path, fun compileProgram(args: CompilerArguments): CompilationResult {
optimize: Boolean,
optimizeFloatExpressions: Boolean,
writeAssembly: Boolean,
slowCodegenWarnings: Boolean,
quietAssembler: Boolean,
compilationTarget: String,
sourceDirs: List<String>,
outputDir: Path,
errors: IErrorReporter = ErrorReporter()): CompilationResult {
var programName = "" var programName = ""
lateinit var program: Program lateinit var program: Program
lateinit var importedFiles: List<Path> lateinit var importedFiles: List<Path>
val optimizeFloatExpr = if(optimize) optimizeFloatExpressions else false val optimizeFloatExpr = if(args.optimize) args.optimizeFloatExpressions else false
val compTarget = val compTarget =
when(compilationTarget) { when(args.compilationTarget) {
C64Target.name -> C64Target C64Target.name -> C64Target
Cx16Target.name -> Cx16Target Cx16Target.name -> Cx16Target
else -> throw IllegalArgumentException("invalid compilation target") else -> throw IllegalArgumentException("invalid compilation target")
@ -56,30 +57,30 @@ fun compileProgram(filepath: Path,
try { try {
val totalTime = measureTimeMillis { val totalTime = measureTimeMillis {
// import main module and everything it needs // import main module and everything it needs
val (programresult, compilationOptions, imported) = parseImports(filepath, errors, compTarget, sourceDirs) val (programresult, compilationOptions, imported) = parseImports(args.filepath, args.errors, compTarget, args.sourceDirs)
with(compilationOptions) { with(compilationOptions) {
this.slowCodegenWarnings = slowCodegenWarnings this.slowCodegenWarnings = args.slowCodegenWarnings
this.optimize = optimize this.optimize = args.optimize
this.optimizeFloatExpressions = optimizeFloatExpr this.optimizeFloatExpressions = optimizeFloatExpr
} }
program = programresult program = programresult
importedFiles = imported importedFiles = imported
processAst(program, errors, compilationOptions) processAst(program, args.errors, compilationOptions)
if (compilationOptions.optimize) if (compilationOptions.optimize)
optimizeAst( optimizeAst(
program, program,
compilationOptions, compilationOptions,
errors, args.errors,
BuiltinFunctionsFacade(BuiltinFunctions), BuiltinFunctionsFacade(BuiltinFunctions),
compTarget compTarget
) )
postprocessAst(program, errors, compilationOptions) postprocessAst(program, args.errors, compilationOptions)
// println("*********** AST BEFORE ASSEMBLYGEN *************") // println("*********** AST BEFORE ASSEMBLYGEN *************")
// printProgram(program) // printProgram(program)
if (writeAssembly) { if (args.writeAssembly) {
val result = writeAssembly(program, errors, outputDir, quietAssembler, compilationOptions) val result = writeAssembly(program, args.errors, args.outputDir, args.quietAssembler, compilationOptions)
when (result) { when (result) {
is WriteAssemblyResult.Ok -> programName = result.filename is WriteAssemblyResult.Ok -> programName = result.filename
is WriteAssemblyResult.Fail -> { is WriteAssemblyResult.Fail -> {

View File

@ -1,6 +1,8 @@
package prog8tests package prog8tests
import io.kotest.core.spec.style.FunSpec import io.kotest.core.spec.style.FunSpec
import prog8.compiler.CompilationResult
import prog8.compiler.CompilerArguments
import prog8.compiler.compileProgram import prog8.compiler.compileProgram
import prog8.compiler.target.C64Target import prog8.compiler.target.C64Target
import prog8.compiler.target.Cx16Target import prog8.compiler.target.Cx16Target
@ -20,17 +22,19 @@ import kotlin.io.path.exists
private val examplesDir = assumeDirectory(workingDir, "../examples") private val examplesDir = assumeDirectory(workingDir, "../examples")
private fun compileTheThing(filepath: Path, optimize: Boolean, target: ICompilationTarget) = compileProgram( private fun compileTheThing(filepath: Path, optimize: Boolean, target: ICompilationTarget): CompilationResult {
filepath, val args = CompilerArguments(
optimize, filepath,
optimizeFloatExpressions = true, optimize,
writeAssembly = true, optimizeFloatExpressions = true,
slowCodegenWarnings = false, writeAssembly = true,
quietAssembler = true, slowCodegenWarnings = false,
compilationTarget = target.name, quietAssembler = true,
sourceDirs = listOf(), compilationTarget = target.name,
outputDir outputDir = outputDir
) )
return compileProgram(args)
}
private fun prepareTestFiles(source: String, optimize: Boolean, target: ICompilationTarget): Pair<String, Path> { private fun prepareTestFiles(source: String, optimize: Boolean, target: ICompilationTarget): Pair<String, Path> {
val searchIn = mutableListOf(examplesDir) val searchIn = mutableListOf(examplesDir)

View File

@ -1,6 +1,8 @@
package prog8tests package prog8tests
import io.kotest.core.spec.style.FunSpec import io.kotest.core.spec.style.FunSpec
import prog8.compiler.CompilationResult
import prog8.compiler.CompilerArguments
import prog8.compiler.compileProgram import prog8.compiler.compileProgram
import prog8.compiler.target.Cx16Target import prog8.compiler.target.Cx16Target
import prog8tests.ast.helpers.assumeReadableFile import prog8tests.ast.helpers.assumeReadableFile
@ -37,8 +39,8 @@ class TestCompilerOptionSourcedirs: FunSpec({
tempFileInWorkingDir.deleteExisting() tempFileInWorkingDir.deleteExisting()
} }
fun compileFile(filePath: Path, sourceDirs: List<String>) = fun compileFile(filePath: Path, sourceDirs: List<String>): CompilationResult {
compileProgram( val args = CompilerArguments(
filepath = filePath, filepath = filePath,
optimize = false, optimize = false,
optimizeFloatExpressions = false, optimizeFloatExpressions = false,
@ -49,6 +51,8 @@ class TestCompilerOptionSourcedirs: FunSpec({
sourceDirs, sourceDirs,
outputDir outputDir
) )
return compileProgram(args)
}
test("testAbsoluteFilePathInWorkingDir") { test("testAbsoluteFilePathInWorkingDir") {
val filepath = assumeReadableFile(tempFileInWorkingDir.absolute()) val filepath = assumeReadableFile(tempFileInWorkingDir.absolute())

View File

@ -4,6 +4,7 @@ import io.kotest.assertions.withClue
import io.kotest.matchers.shouldBe import io.kotest.matchers.shouldBe
import prog8.ast.Program import prog8.ast.Program
import prog8.compiler.CompilationResult import prog8.compiler.CompilationResult
import prog8.compiler.CompilerArguments
import prog8.compiler.compileProgram import prog8.compiler.compileProgram
import prog8.compiler.target.C64Target import prog8.compiler.target.C64Target
import prog8.compiler.target.c64.C64MachineDefinition import prog8.compiler.target.c64.C64MachineDefinition
@ -45,7 +46,7 @@ internal fun compileFile(
) : CompilationResult { ) : CompilationResult {
val filepath = fileDir.resolve(fileName) val filepath = fileDir.resolve(fileName)
assumeReadableFile(filepath) assumeReadableFile(filepath)
return compileProgram( val args = CompilerArguments(
filepath, filepath,
optimize, optimize,
optimizeFloatExpressions = optFloatExpr, optimizeFloatExpressions = optFloatExpr,
@ -53,10 +54,10 @@ internal fun compileFile(
slowCodegenWarnings = false, slowCodegenWarnings = false,
quietAssembler = true, quietAssembler = true,
platform.name, platform.name,
sourceDirs = listOf(), outputDir = outputDir,
outputDir,
errors = errors ?: ErrorReporterForTests() errors = errors ?: ErrorReporterForTests()
) )
return compileProgram(args)
} }
/** /**

View File

@ -11,6 +11,7 @@ import org.takes.facets.fork.TkFork
import org.takes.rq.form.RqFormBase import org.takes.rq.form.RqFormBase
import org.takes.rs.RsJson import org.takes.rs.RsJson
import org.takes.tk.TkSlf4j import org.takes.tk.TkSlf4j
import prog8.compiler.CompilerArguments
import javax.json.Json import javax.json.Json
import prog8.compiler.compileProgram import prog8.compiler.compileProgram
import java.nio.file.Path import java.nio.file.Path
@ -29,16 +30,16 @@ class RequestParser : Take {
val form = RqFormBase(request) val form = RqFormBase(request)
val names = form.names() val names = form.names()
val a = form.param("a").single() val a = form.param("a").single()
val compilationResult = compileProgram(Path.of(a), val args = CompilerArguments(
Path.of(a),
optimize = true, optimize = true,
optimizeFloatExpressions = false, optimizeFloatExpressions = false,
writeAssembly = true, writeAssembly = true,
slowCodegenWarnings = true, slowCodegenWarnings = true,
compilationTarget = "c64", compilationTarget = "c64",
sourceDirs = emptyList(),
outputDir = Path.of(""),
quietAssembler = false quietAssembler = false
) )
val compilationResult = compileProgram(args)
return RsJson(Jsonding()) return RsJson(Jsonding())
} }
} }