mirror of
https://github.com/irmen/prog8.git
synced 2025-01-12 04:30:03 +00:00
add unit tests for AnonScope refactoring, cleaned up imports
This commit is contained in:
parent
f4186981fd
commit
9684f4e42a
@ -1,6 +1,9 @@
|
|||||||
package prog8.compiler
|
package prog8.compiler
|
||||||
|
|
||||||
import prog8.ast.*
|
import prog8.ast.IFunctionCall
|
||||||
|
import prog8.ast.IStatementContainer
|
||||||
|
import prog8.ast.Node
|
||||||
|
import prog8.ast.Program
|
||||||
import prog8.ast.base.*
|
import prog8.ast.base.*
|
||||||
import prog8.ast.expressions.*
|
import prog8.ast.expressions.*
|
||||||
import prog8.ast.statements.*
|
import prog8.ast.statements.*
|
||||||
|
@ -17,7 +17,7 @@ import prog8.compiler.target.ICompilationTarget
|
|||||||
import java.io.CharConversionException
|
import java.io.CharConversionException
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import kotlin.io.path.*
|
import kotlin.io.path.Path
|
||||||
|
|
||||||
internal class AstChecker(private val program: Program,
|
internal class AstChecker(private val program: Program,
|
||||||
private val compilerOptions: CompilationOptions,
|
private val compilerOptions: CompilationOptions,
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package prog8.compiler.astprocessing
|
package prog8.compiler.astprocessing
|
||||||
|
|
||||||
import prog8.ast.*
|
import prog8.ast.IFunctionCall
|
||||||
|
import prog8.ast.IStatementContainer
|
||||||
|
import prog8.ast.Node
|
||||||
|
import prog8.ast.Program
|
||||||
import prog8.ast.base.FatalAstException
|
import prog8.ast.base.FatalAstException
|
||||||
import prog8.ast.base.Position
|
import prog8.ast.base.Position
|
||||||
import prog8.ast.expressions.*
|
import prog8.ast.expressions.*
|
||||||
|
@ -3,7 +3,10 @@ package prog8.compiler.target
|
|||||||
import com.github.michaelbull.result.fold
|
import com.github.michaelbull.result.fold
|
||||||
import prog8.ast.IMemSizer
|
import prog8.ast.IMemSizer
|
||||||
import prog8.ast.Program
|
import prog8.ast.Program
|
||||||
import prog8.ast.base.*
|
import prog8.ast.base.ByteDatatypes
|
||||||
|
import prog8.ast.base.DataType
|
||||||
|
import prog8.ast.base.PassByReferenceDatatypes
|
||||||
|
import prog8.ast.base.WordDatatypes
|
||||||
import prog8.compiler.CompilationOptions
|
import prog8.compiler.CompilationOptions
|
||||||
import prog8.compiler.IErrorReporter
|
import prog8.compiler.IErrorReporter
|
||||||
import prog8.compiler.IStringEncoding
|
import prog8.compiler.IStringEncoding
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
package prog8.compiler.target.cpu6502.codegen
|
package prog8.compiler.target.cpu6502.codegen
|
||||||
|
|
||||||
import com.github.michaelbull.result.*
|
import com.github.michaelbull.result.fold
|
||||||
import prog8.ast.*
|
import prog8.ast.*
|
||||||
import prog8.ast.antlr.escape
|
import prog8.ast.antlr.escape
|
||||||
import prog8.ast.base.*
|
import prog8.ast.base.*
|
||||||
@ -19,6 +19,41 @@ import java.nio.file.Path
|
|||||||
import java.time.LocalDate
|
import java.time.LocalDate
|
||||||
import java.time.LocalDateTime
|
import java.time.LocalDateTime
|
||||||
import java.util.*
|
import java.util.*
|
||||||
|
import kotlin.collections.Iterable
|
||||||
|
import kotlin.collections.List
|
||||||
|
import kotlin.collections.MutableList
|
||||||
|
import kotlin.collections.MutableSet
|
||||||
|
import kotlin.collections.any
|
||||||
|
import kotlin.collections.chunked
|
||||||
|
import kotlin.collections.component1
|
||||||
|
import kotlin.collections.component2
|
||||||
|
import kotlin.collections.contains
|
||||||
|
import kotlin.collections.drop
|
||||||
|
import kotlin.collections.filter
|
||||||
|
import kotlin.collections.filterIsInstance
|
||||||
|
import kotlin.collections.first
|
||||||
|
import kotlin.collections.firstOrNull
|
||||||
|
import kotlin.collections.forEach
|
||||||
|
import kotlin.collections.getValue
|
||||||
|
import kotlin.collections.isNotEmpty
|
||||||
|
import kotlin.collections.iterator
|
||||||
|
import kotlin.collections.joinToString
|
||||||
|
import kotlin.collections.last
|
||||||
|
import kotlin.collections.listOf
|
||||||
|
import kotlin.collections.map
|
||||||
|
import kotlin.collections.mutableListOf
|
||||||
|
import kotlin.collections.mutableMapOf
|
||||||
|
import kotlin.collections.mutableSetOf
|
||||||
|
import kotlin.collections.partition
|
||||||
|
import kotlin.collections.plus
|
||||||
|
import kotlin.collections.removeLast
|
||||||
|
import kotlin.collections.set
|
||||||
|
import kotlin.collections.setOf
|
||||||
|
import kotlin.collections.single
|
||||||
|
import kotlin.collections.sortedBy
|
||||||
|
import kotlin.collections.toList
|
||||||
|
import kotlin.collections.toMutableList
|
||||||
|
import kotlin.collections.zip
|
||||||
import kotlin.io.path.Path
|
import kotlin.io.path.Path
|
||||||
import kotlin.math.absoluteValue
|
import kotlin.math.absoluteValue
|
||||||
|
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package prog8.optimizer
|
package prog8.optimizer
|
||||||
|
|
||||||
import prog8.ast.*
|
import prog8.ast.IBuiltinFunctions
|
||||||
|
import prog8.ast.IStatementContainer
|
||||||
|
import prog8.ast.Node
|
||||||
|
import prog8.ast.Program
|
||||||
import prog8.ast.base.*
|
import prog8.ast.base.*
|
||||||
import prog8.ast.expressions.*
|
import prog8.ast.expressions.*
|
||||||
import prog8.ast.statements.*
|
import prog8.ast.statements.*
|
||||||
|
@ -6,7 +6,10 @@ import org.junit.jupiter.api.Test
|
|||||||
import org.junit.jupiter.api.TestInstance
|
import org.junit.jupiter.api.TestInstance
|
||||||
import prog8.ast.Module
|
import prog8.ast.Module
|
||||||
import prog8.ast.Program
|
import prog8.ast.Program
|
||||||
import prog8.ast.base.*
|
import prog8.ast.base.DataType
|
||||||
|
import prog8.ast.base.Position
|
||||||
|
import prog8.ast.base.RegisterOrPair
|
||||||
|
import prog8.ast.base.VarDeclType
|
||||||
import prog8.ast.expressions.AddressOf
|
import prog8.ast.expressions.AddressOf
|
||||||
import prog8.ast.expressions.IdentifierReference
|
import prog8.ast.expressions.IdentifierReference
|
||||||
import prog8.ast.expressions.NumericLiteralValue
|
import prog8.ast.expressions.NumericLiteralValue
|
||||||
|
@ -1,16 +1,12 @@
|
|||||||
package prog8tests
|
package prog8tests
|
||||||
|
|
||||||
import kotlin.test.*
|
|
||||||
import com.github.michaelbull.result.getErrorOrElse
|
import com.github.michaelbull.result.getErrorOrElse
|
||||||
import com.github.michaelbull.result.getOrElse
|
import com.github.michaelbull.result.getOrElse
|
||||||
import org.hamcrest.MatcherAssert.assertThat
|
import org.hamcrest.MatcherAssert.assertThat
|
||||||
import org.hamcrest.Matchers.*
|
import org.hamcrest.Matchers.equalTo
|
||||||
|
import org.hamcrest.Matchers.nullValue
|
||||||
import org.hamcrest.core.Is
|
import org.hamcrest.core.Is
|
||||||
import org.junit.jupiter.api.BeforeEach
|
import org.junit.jupiter.api.*
|
||||||
import org.junit.jupiter.api.Nested
|
|
||||||
import org.junit.jupiter.api.Test
|
|
||||||
import org.junit.jupiter.api.TestInstance
|
|
||||||
import org.junit.jupiter.api.Disabled
|
|
||||||
import prog8.ast.Program
|
import prog8.ast.Program
|
||||||
import prog8.ast.internedStringsModuleName
|
import prog8.ast.internedStringsModuleName
|
||||||
import prog8.compiler.IErrorReporter
|
import prog8.compiler.IErrorReporter
|
||||||
@ -19,6 +15,10 @@ import prog8.parser.ParseError
|
|||||||
import prog8.parser.SourceCode
|
import prog8.parser.SourceCode
|
||||||
import prog8tests.helpers.*
|
import prog8tests.helpers.*
|
||||||
import kotlin.io.path.*
|
import kotlin.io.path.*
|
||||||
|
import kotlin.test.assertContains
|
||||||
|
import kotlin.test.assertFailsWith
|
||||||
|
import kotlin.test.assertFalse
|
||||||
|
import kotlin.test.fail
|
||||||
|
|
||||||
|
|
||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package prog8tests
|
package prog8tests
|
||||||
|
|
||||||
import org.junit.jupiter.api.Disabled
|
|
||||||
import org.junit.jupiter.api.DynamicTest
|
import org.junit.jupiter.api.DynamicTest
|
||||||
import org.junit.jupiter.api.DynamicTest.dynamicTest
|
import org.junit.jupiter.api.DynamicTest.dynamicTest
|
||||||
import org.junit.jupiter.api.TestFactory
|
import org.junit.jupiter.api.TestFactory
|
||||||
|
@ -14,9 +14,6 @@ import prog8.compiler.astprocessing.toConstantIntegerRange
|
|||||||
import prog8.compiler.target.C64Target
|
import prog8.compiler.target.C64Target
|
||||||
import prog8.compiler.target.Cx16Target
|
import prog8.compiler.target.Cx16Target
|
||||||
import prog8tests.helpers.*
|
import prog8tests.helpers.*
|
||||||
import prog8tests.helpers.assertFailure
|
|
||||||
import prog8tests.helpers.assertSuccess
|
|
||||||
import prog8tests.helpers.compileText
|
|
||||||
import kotlin.test.assertContains
|
import kotlin.test.assertContains
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
package prog8tests
|
package prog8tests
|
||||||
|
|
||||||
import org.hamcrest.CoreMatchers.instanceOf
|
|
||||||
import org.hamcrest.MatcherAssert.assertThat
|
|
||||||
import org.junit.jupiter.api.Test
|
import org.junit.jupiter.api.Test
|
||||||
import org.junit.jupiter.api.TestInstance
|
import org.junit.jupiter.api.TestInstance
|
||||||
import prog8.ast.statements.Block
|
import prog8.ast.statements.Block
|
||||||
|
@ -8,7 +8,7 @@ import org.junit.jupiter.api.Test
|
|||||||
import org.junit.jupiter.api.TestInstance
|
import org.junit.jupiter.api.TestInstance
|
||||||
import prog8.compiler.target.cbm.Petscii
|
import prog8.compiler.target.cbm.Petscii
|
||||||
import java.io.CharConversionException
|
import java.io.CharConversionException
|
||||||
import kotlin.test.*
|
import kotlin.test.assertFailsWith
|
||||||
|
|
||||||
|
|
||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
|
@ -5,6 +5,7 @@ import org.junit.jupiter.api.Test
|
|||||||
import org.junit.jupiter.api.TestInstance
|
import org.junit.jupiter.api.TestInstance
|
||||||
import prog8.ast.base.DataType
|
import prog8.ast.base.DataType
|
||||||
import prog8.ast.expressions.IdentifierReference
|
import prog8.ast.expressions.IdentifierReference
|
||||||
|
import prog8.ast.expressions.NumericLiteralValue
|
||||||
import prog8.ast.expressions.TypecastExpression
|
import prog8.ast.expressions.TypecastExpression
|
||||||
import prog8.ast.statements.*
|
import prog8.ast.statements.*
|
||||||
import prog8.compiler.target.C64Target
|
import prog8.compiler.target.C64Target
|
||||||
@ -12,7 +13,10 @@ import prog8tests.helpers.ErrorReporterForTests
|
|||||||
import prog8tests.helpers.assertFailure
|
import prog8tests.helpers.assertFailure
|
||||||
import prog8tests.helpers.assertSuccess
|
import prog8tests.helpers.assertSuccess
|
||||||
import prog8tests.helpers.compileText
|
import prog8tests.helpers.compileText
|
||||||
import kotlin.test.*
|
import kotlin.test.assertContains
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertFalse
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
|
|
||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
@ -178,4 +182,33 @@ class TestSubroutines {
|
|||||||
assertEquals(DataType.ARRAY_UB, func.parameters.single().type)
|
assertEquals(DataType.ARRAY_UB, func.parameters.single().type)
|
||||||
assertTrue(func.statements.isEmpty())
|
assertTrue(func.statements.isEmpty())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testAnonScopeVarsMovedIntoSubroutineScope() {
|
||||||
|
val src = """
|
||||||
|
main {
|
||||||
|
sub start() {
|
||||||
|
repeat {
|
||||||
|
ubyte xx = 99
|
||||||
|
xx++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
|
||||||
|
val result = compileText(C64Target, false, src, writeAssembly = false).assertSuccess()
|
||||||
|
val module = result.programAst.toplevelModule
|
||||||
|
val mainBlock = module.statements.single() as Block
|
||||||
|
val start = mainBlock.statements.single() as Subroutine
|
||||||
|
val repeatbody = start.statements.filterIsInstance<RepeatLoop>().single().body
|
||||||
|
assertFalse(mainBlock.statements.any { it is VarDecl }, "no vars moved to main block")
|
||||||
|
val subroutineVars = start.statements.filterIsInstance<VarDecl>()
|
||||||
|
assertEquals(1, subroutineVars.size, "var from repeat anonscope must be moved up to subroutine")
|
||||||
|
assertEquals("xx", subroutineVars[0].name)
|
||||||
|
assertFalse(repeatbody.statements.any { it is VarDecl }, "var should have been removed from repeat anonscope")
|
||||||
|
val initassign = repeatbody.statements[0] as? Assignment
|
||||||
|
assertEquals(listOf("xx"), initassign?.target?.identifier?.nameInSource, "vardecl in repeat should be replaced by init assignment")
|
||||||
|
assertEquals(99, (initassign?.value as? NumericLiteralValue)?.number?.toInt(), "vardecl in repeat should be replaced by init assignment")
|
||||||
|
assertTrue(repeatbody.statements[1] is PostIncrDecr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -607,4 +607,29 @@ class TestProg8Parser {
|
|||||||
assertTrue(abc!=abd)
|
assertTrue(abc!=abd)
|
||||||
assertFalse(abc!=abc)
|
assertFalse(abc!=abc)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testAnonScopeStillContainsVarsDirectlyAfterParse() {
|
||||||
|
val src = SourceCode.Text("""
|
||||||
|
main {
|
||||||
|
sub start() {
|
||||||
|
repeat {
|
||||||
|
ubyte xx = 99
|
||||||
|
xx++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
""")
|
||||||
|
val module = parseModule(src)
|
||||||
|
val mainBlock = module.statements.single() as Block
|
||||||
|
val start = mainBlock.statements.single() as Subroutine
|
||||||
|
val repeatbody = (start.statements.single() as RepeatLoop).body
|
||||||
|
assertFalse(mainBlock.statements.any { it is VarDecl }, "no vars moved to main block")
|
||||||
|
assertFalse(start.statements.any { it is VarDecl }, "no vars moved to start sub")
|
||||||
|
assertTrue(repeatbody.statements[0] is VarDecl, "var is still in repeat block (anonymousscope)")
|
||||||
|
val initvalue = (repeatbody.statements[0] as VarDecl).value as? NumericLiteralValue
|
||||||
|
assertEquals(99, initvalue?.number?.toInt())
|
||||||
|
assertTrue(repeatbody.statements[1] is PostIncrDecr)
|
||||||
|
// the ast processing steps used in the compiler, will eventually move the var up to the containing scope (subroutine).
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,7 +6,10 @@ import org.junit.jupiter.api.Test
|
|||||||
import org.junit.jupiter.api.TestInstance
|
import org.junit.jupiter.api.TestInstance
|
||||||
import prog8.parser.SourceCode
|
import prog8.parser.SourceCode
|
||||||
import prog8.parser.SourceCode.Companion.libraryFilePrefix
|
import prog8.parser.SourceCode.Companion.libraryFilePrefix
|
||||||
import prog8tests.helpers.*
|
import prog8tests.helpers.assumeNotExists
|
||||||
|
import prog8tests.helpers.assumeReadableFile
|
||||||
|
import prog8tests.helpers.fixturesDir
|
||||||
|
import prog8tests.helpers.resourcesDir
|
||||||
import kotlin.io.path.Path
|
import kotlin.io.path.Path
|
||||||
import kotlin.test.*
|
import kotlin.test.*
|
||||||
|
|
||||||
|
@ -7,7 +7,9 @@ import prog8.ast.statements.Block
|
|||||||
import prog8.ast.statements.Subroutine
|
import prog8.ast.statements.Subroutine
|
||||||
import prog8.parser.Prog8Parser.parseModule
|
import prog8.parser.Prog8Parser.parseModule
|
||||||
import prog8.parser.SourceCode
|
import prog8.parser.SourceCode
|
||||||
import kotlin.test.*
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertFalse
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
|
|
||||||
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user