From cd295228ef3de6d52017346219cc1c586f3e42c3 Mon Sep 17 00:00:00 2001 From: meisl Date: Sun, 11 Jul 2021 15:19:20 +0200 Subject: [PATCH] + TestCompilerOnImportsAndIncludes.kt: 2 tests, both passing (but see FIXME in asmIncludeFromSameFolder.p8) --- .../compiler/target/cpu6502/codegen/AsmGen.kt | 3 + .../test/TestCompilerOnImportsAndIncludes.kt | 103 ++++++++++++++++++ .../test/fixtures/asmIncludeFromSameFolder.p8 | 13 +++ compiler/test/fixtures/foo_bar.asm | 2 + compiler/test/fixtures/foo_bar.p8 | 3 + .../test/fixtures/importFromSameFolder.p8 | 9 ++ 6 files changed, 133 insertions(+) create mode 100644 compiler/test/TestCompilerOnImportsAndIncludes.kt create mode 100644 compiler/test/fixtures/asmIncludeFromSameFolder.p8 create mode 100644 compiler/test/fixtures/foo_bar.asm create mode 100644 compiler/test/fixtures/foo_bar.p8 create mode 100644 compiler/test/fixtures/importFromSameFolder.p8 diff --git a/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt b/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt index cc55baed0..95c3ae71a 100644 --- a/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt +++ b/compiler/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt @@ -1308,6 +1308,9 @@ $repeatLabel lda $counterVar } } + /** + * TODO: %asminclude and %asmbinary should be done earlier than code gen (-> put content into AST) + */ private fun translate(stmt: Directive) { when(stmt.directive) { "%asminclude" -> { diff --git a/compiler/test/TestCompilerOnImportsAndIncludes.kt b/compiler/test/TestCompilerOnImportsAndIncludes.kt new file mode 100644 index 000000000..57d3cf960 --- /dev/null +++ b/compiler/test/TestCompilerOnImportsAndIncludes.kt @@ -0,0 +1,103 @@ +package prog8tests + +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.TestInstance +import kotlin.io.path.* +import kotlin.test.* + +import prog8.ast.expressions.AddressOf +import prog8.ast.expressions.IdentifierReference +import prog8.ast.expressions.StringLiteralValue +import prog8.ast.statements.FunctionCallStatement +import prog8.ast.statements.Label +import prog8.compiler.compileProgram +import prog8.compiler.target.Cx16Target + + +/** + * ATTENTION: this is just kludge! + * They are not really unit tests, but rather tests of the whole process, + * from source file loading all the way through to running 64tass. + */ +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class TestCompilerOnImportsAndIncludes { + val workingDir = Path("").absolute() // Note: Path(".") does NOT work..! + val fixturesDir = workingDir.resolve("test/fixtures") + val outputDir = workingDir.resolve("build/tmp/test") + + @Test + fun sanityCheckDirectories() { + assertEquals("compiler", workingDir.fileName.toString()) + assertTrue(fixturesDir.isDirectory(), "sanity check; should be directory: $fixturesDir") + assertTrue(outputDir.isDirectory(), "sanity check; should be directory: $outputDir") + } + + @Test + fun testImportFromSameFolder() { + val filepath = fixturesDir.resolve("importFromSameFolder.p8") + val imported = fixturesDir.resolve("foo_bar.p8") + + assertTrue(filepath.isRegularFile(), "sanity check; should be regular file: $filepath") + assertTrue(imported.isRegularFile(), "sanity check; should be regular file: $imported") + + val compilationTarget = Cx16Target + val result = compileProgram( + filepath, + optimize = false, + writeAssembly = true, + slowCodegenWarnings = false, + compilationTarget.name, + libdirs = listOf(), + outputDir + ) + assertTrue(result.success, "compilation should succeed") + + val program = result.programAst + val startSub = program.entrypoint() + val strLits = startSub.statements + .filterIsInstance() + .map { it.args[0] as IdentifierReference } + .map { it.targetVarDecl(program)!!.value as StringLiteralValue } + + assertEquals("main.bar", strLits[0].value) + assertEquals("foo.bar", strLits[1].value) + assertEquals("main", strLits[0].definingScope().name) + assertEquals("foo", strLits[1].definingScope().name) + } + + @Test + fun testAsmIncludeFromSameFolder() { + val filepath = fixturesDir.resolve("asmIncludeFromSameFolder.p8") + val included = fixturesDir.resolve("foo_bar.asm") + + assertTrue(filepath.isRegularFile(), "sanity check; should be regular file: $filepath") + assertTrue(included.isRegularFile(), "sanity check; should be regular file: $included") + + val compilationTarget = Cx16Target + val result = compileProgram( + filepath, + optimize = false, + writeAssembly = true, + slowCodegenWarnings = false, + compilationTarget.name, + libdirs = listOf(), + outputDir + ) + assertTrue(result.success, "compilation should succeed") + + val program = result.programAst + val startSub = program.entrypoint() + val args = startSub.statements + .filterIsInstance() + .map { it.args[0] } + + val str0 = (args[0] as IdentifierReference).targetVarDecl(program)!!.value as StringLiteralValue + assertEquals("main.bar", str0.value) + assertEquals("main", str0.definingScope().name) + + val id1 = (args[1] as AddressOf).identifier + val lbl1 = id1.targetStatement(program) as Label + assertEquals("foo_bar", lbl1.name) + assertEquals("start", lbl1.definingScope().name) + } +} diff --git a/compiler/test/fixtures/asmIncludeFromSameFolder.p8 b/compiler/test/fixtures/asmIncludeFromSameFolder.p8 new file mode 100644 index 000000000..df25f6e78 --- /dev/null +++ b/compiler/test/fixtures/asmIncludeFromSameFolder.p8 @@ -0,0 +1,13 @@ +%import textio +main { + str myBar = "main.bar" +;foo_bar: +; %asminclude "foo_bar.asm" ; FIXME: should be accessible from inside start() but give assembler error + sub start() { + txt.print(myBar) + txt.print(&foo_bar) + return +foo_bar: + %asminclude "foo_bar.asm" + } +} diff --git a/compiler/test/fixtures/foo_bar.asm b/compiler/test/fixtures/foo_bar.asm new file mode 100644 index 000000000..d415b6923 --- /dev/null +++ b/compiler/test/fixtures/foo_bar.asm @@ -0,0 +1,2 @@ +bar .text "foo.bar",0 + diff --git a/compiler/test/fixtures/foo_bar.p8 b/compiler/test/fixtures/foo_bar.p8 new file mode 100644 index 000000000..e60fa8199 --- /dev/null +++ b/compiler/test/fixtures/foo_bar.p8 @@ -0,0 +1,3 @@ +foo { + str bar = "foo.bar" +} diff --git a/compiler/test/fixtures/importFromSameFolder.p8 b/compiler/test/fixtures/importFromSameFolder.p8 new file mode 100644 index 000000000..3f233b10d --- /dev/null +++ b/compiler/test/fixtures/importFromSameFolder.p8 @@ -0,0 +1,9 @@ +%import textio +%import foo_bar +main { + str myBar = "main.bar" + sub start() { + txt.print(myBar) + txt.print(foo.bar) + } +}