2021-07-17 10:37:58 +02:00
|
|
|
package prog8tests.helpers
|
|
|
|
|
2022-09-27 16:32:44 +02:00
|
|
|
import prog8.code.core.ICompilationTarget
|
|
|
|
import prog8.code.core.IErrorReporter
|
2021-10-29 05:28:02 +02:00
|
|
|
import prog8.compiler.CompilationResult
|
2021-11-30 01:40:21 +01:00
|
|
|
import prog8.compiler.CompilerArguments
|
2021-10-29 05:28:02 +02:00
|
|
|
import prog8.compiler.compileProgram
|
2021-10-11 00:22:04 +02:00
|
|
|
import java.nio.file.Path
|
|
|
|
import kotlin.io.path.name
|
2021-07-17 10:37:58 +02:00
|
|
|
|
2021-07-30 17:39:43 +02:00
|
|
|
|
2021-07-17 10:37:58 +02:00
|
|
|
internal fun compileFile(
|
|
|
|
platform: ICompilationTarget,
|
|
|
|
optimize: Boolean,
|
|
|
|
fileDir: Path,
|
|
|
|
fileName: String,
|
2022-06-26 18:51:03 +02:00
|
|
|
outputDir: Path = prog8tests.helpers.outputDir,
|
2021-10-22 01:25:26 +02:00
|
|
|
errors: IErrorReporter? = null,
|
2021-11-09 03:45:07 +01:00
|
|
|
writeAssembly: Boolean = true,
|
2023-03-19 00:24:05 +01:00
|
|
|
optFloatExpr: Boolean = true,
|
2022-03-07 21:41:12 +01:00
|
|
|
) : CompilationResult? {
|
2021-07-17 10:37:58 +02:00
|
|
|
val filepath = fileDir.resolve(fileName)
|
2022-06-26 18:51:03 +02:00
|
|
|
assumeReadableFile(filepath)
|
2021-11-30 01:40:21 +01:00
|
|
|
val args = CompilerArguments(
|
2021-07-17 10:37:58 +02:00
|
|
|
filepath,
|
|
|
|
optimize,
|
2021-11-09 03:45:07 +01:00
|
|
|
optimizeFloatExpressions = optFloatExpr,
|
2021-10-22 01:25:26 +02:00
|
|
|
writeAssembly = writeAssembly,
|
2021-07-17 10:37:58 +02:00
|
|
|
slowCodegenWarnings = false,
|
2021-10-30 15:26:40 +02:00
|
|
|
quietAssembler = true,
|
2021-12-30 14:38:40 +01:00
|
|
|
asmListfile = false,
|
2022-02-05 21:25:19 +01:00
|
|
|
experimentalCodegen = false,
|
2023-06-27 00:27:34 +02:00
|
|
|
varsHighBank = null,
|
2021-07-17 10:37:58 +02:00
|
|
|
platform.name,
|
2022-07-11 18:36:20 +02:00
|
|
|
evalStackBaseAddress = null,
|
2022-07-06 23:40:36 +02:00
|
|
|
symbolDefs = emptyMap(),
|
2021-11-30 01:40:21 +01:00
|
|
|
outputDir = outputDir,
|
2023-05-30 19:08:34 +02:00
|
|
|
errors = errors ?: ErrorReporterForTests(),
|
|
|
|
splitWordArrays = false
|
2021-07-17 10:37:58 +02:00
|
|
|
)
|
2021-11-30 01:40:21 +01:00
|
|
|
return compileProgram(args)
|
2021-07-17 10:37:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Takes a [sourceText] as a String, writes it to a temporary
|
|
|
|
* file and then runs the compiler on that.
|
|
|
|
* @see compileFile
|
|
|
|
*/
|
|
|
|
internal fun compileText(
|
|
|
|
platform: ICompilationTarget,
|
|
|
|
optimize: Boolean,
|
2021-10-22 01:25:26 +02:00
|
|
|
sourceText: String,
|
|
|
|
errors: IErrorReporter? = null,
|
2021-11-09 03:45:07 +01:00
|
|
|
writeAssembly: Boolean = true,
|
2023-03-19 00:24:05 +01:00
|
|
|
optFloatExpr: Boolean = true,
|
2022-03-07 21:41:12 +01:00
|
|
|
) : CompilationResult? {
|
2022-06-26 18:51:03 +02:00
|
|
|
val filePath = outputDir.resolve("on_the_fly_test_" + sourceText.hashCode().toUInt().toString(16) + ".p8")
|
2021-07-17 10:37:58 +02:00
|
|
|
// we don't assumeNotExists(filePath) - should be ok to just overwrite it
|
|
|
|
filePath.toFile().writeText(sourceText)
|
2022-09-24 13:54:00 +02:00
|
|
|
return compileFile(platform, optimize, filePath.parent, filePath.name,
|
2023-07-02 15:26:04 +02:00
|
|
|
errors=errors, writeAssembly=writeAssembly, optFloatExpr = optFloatExpr)
|
2021-11-09 03:45:07 +01:00
|
|
|
}
|