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

View File

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

View File

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

View File

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

View File

@ -4,6 +4,7 @@ import io.kotest.assertions.withClue
import io.kotest.matchers.shouldBe
import prog8.ast.Program
import prog8.compiler.CompilationResult
import prog8.compiler.CompilerArguments
import prog8.compiler.compileProgram
import prog8.compiler.target.C64Target
import prog8.compiler.target.c64.C64MachineDefinition
@ -45,7 +46,7 @@ internal fun compileFile(
) : CompilationResult {
val filepath = fileDir.resolve(fileName)
assumeReadableFile(filepath)
return compileProgram(
val args = CompilerArguments(
filepath,
optimize,
optimizeFloatExpressions = optFloatExpr,
@ -53,10 +54,10 @@ internal fun compileFile(
slowCodegenWarnings = false,
quietAssembler = true,
platform.name,
sourceDirs = listOf(),
outputDir,
outputDir = outputDir,
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.rs.RsJson
import org.takes.tk.TkSlf4j
import prog8.compiler.CompilerArguments
import javax.json.Json
import prog8.compiler.compileProgram
import java.nio.file.Path
@ -29,16 +30,16 @@ class RequestParser : Take {
val form = RqFormBase(request)
val names = form.names()
val a = form.param("a").single()
val compilationResult = compileProgram(Path.of(a),
val args = CompilerArguments(
Path.of(a),
optimize = true,
optimizeFloatExpressions = false,
writeAssembly = true,
slowCodegenWarnings = true,
compilationTarget = "c64",
sourceDirs = emptyList(),
outputDir = Path.of(""),
quietAssembler = false
)
val compilationResult = compileProgram(args)
return RsJson(Jsonding())
}
}