working on new ast

This commit is contained in:
Irmen de Jong 2022-03-06 20:35:15 +01:00
parent 48d782c69c
commit dc93691fd9
3 changed files with 16 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package prog8.codegen.experimental6502
import prog8.ast.Program
import prog8.ast.base.FatalAstException
import prog8.compilerinterface.*
class AsmGen(internal val program: Program,
@ -17,6 +18,8 @@ class AsmGen(internal val program: Program,
// TODO temporary location to do this:
val intermediateAst = IntermediateAstMaker.transform(program)
intermediateAst.print()
val entry = intermediateAst.entrypoint() ?: throw FatalAstException("no main.start() found")
println(entry)
println("..todo: create assembly code into ${options.outputDir.toAbsolutePath()}..")
return AssemblyProgram("dummy")

View File

@ -1,6 +1,8 @@
package prog8tests.ast
import io.kotest.assertions.fail
import io.kotest.core.spec.style.FunSpec
import io.kotest.matchers.ints.shouldBeGreaterThan
import io.kotest.matchers.shouldBe
import prog8.codegen.experimental6502.IntermediateAstMaker
import prog8.codegen.target.C64Target
@ -26,6 +28,11 @@ class TestIntermediateAst: FunSpec({
val ast = IntermediateAstMaker.transform(result.program)
ast.name shouldBe result.program.name
ast.builtinFunctions.names shouldBe result.program.builtinFunctions.names
val entry = ast.entrypoint() ?: fail("no main.start() found")
entry.name shouldBe "start"
val blocks = ast.allBlocks().toList()
blocks.size shouldBeGreaterThan 1
blocks[0].name shouldBe "main"
ast.print()
}

View File

@ -50,6 +50,12 @@ class PtProgram(
override fun printProperties() {
print("'$name'")
}
fun allBlocks(): Sequence<PtBlock> =
children.asSequence().flatMap { it.children }.filterIsInstance<PtBlock>()
fun entrypoint(): PtSub? =
allBlocks().firstOrNull { it.name == "main" }?.children?.firstOrNull { it is PtSub && it.name == "start" } as PtSub?
}