simplify result handling of assembly phase

This commit is contained in:
Irmen de Jong 2022-03-08 18:51:07 +01:00
parent 9872f43cbf
commit a58e5a3399

View File

@ -47,7 +47,6 @@ class CompilerArguments(val filepath: Path,
fun compileProgram(args: CompilerArguments): CompilationResult? { fun compileProgram(args: CompilerArguments): CompilationResult? {
var programName = ""
lateinit var program: Program lateinit var program: Program
lateinit var importedFiles: List<Path> lateinit var importedFiles: List<Path>
@ -103,12 +102,9 @@ fun compileProgram(args: CompilerArguments): CompilationResult? {
// printProgram(program) // printProgram(program)
if (args.writeAssembly) { if (args.writeAssembly) {
when (val result = writeAssembly(program, args.errors, compilationOptions)) { if(!createAssemblyAndAssemble(program, args.errors, compilationOptions)) {
is WriteAssemblyResult.Ok -> programName = result.filename System.err.println("Error in codegeneration or assembler")
is WriteAssemblyResult.Fail -> { return null
System.err.println(result.error)
return null
}
} }
} }
} }
@ -342,15 +338,10 @@ private fun postprocessAst(program: Program, errors: IErrorReporter, compilerOpt
errors.report() errors.report()
} }
private sealed class WriteAssemblyResult { private fun createAssemblyAndAssemble(program: Program,
class Ok(val filename: String): WriteAssemblyResult() errors: IErrorReporter,
class Fail(val error: String): WriteAssemblyResult() compilerOptions: CompilationOptions
} ): Boolean {
private fun writeAssembly(program: Program,
errors: IErrorReporter,
compilerOptions: CompilationOptions
): WriteAssemblyResult {
compilerOptions.compTarget.machine.initializeZeropage(compilerOptions) compilerOptions.compTarget.machine.initializeZeropage(compilerOptions)
program.processAstBeforeAsmGeneration(compilerOptions, errors) program.processAstBeforeAsmGeneration(compilerOptions, errors)
errors.report() errors.report()
@ -369,12 +360,9 @@ private fun writeAssembly(program: Program,
errors.report() errors.report()
return if(assembly!=null && errors.noErrors()) { return if(assembly!=null && errors.noErrors()) {
if(assembly.assemble(compilerOptions)) { assembly.assemble(compilerOptions)
WriteAssemblyResult.Ok(assembly.name)
} else
WriteAssemblyResult.Fail("assembler step failed")
} else { } else {
WriteAssemblyResult.Fail("compiler failed with errors") false
} }
} }