2021-10-29 03:00:30 +00:00
|
|
|
package prog8tests.ast
|
2021-10-21 20:06:21 +00:00
|
|
|
|
2021-11-07 16:25:53 +00:00
|
|
|
import io.kotest.core.spec.style.AnnotationSpec
|
2021-10-21 20:06:21 +00:00
|
|
|
import prog8.ast.base.DataType
|
|
|
|
import prog8.ast.statements.Block
|
|
|
|
import prog8.ast.statements.Subroutine
|
|
|
|
import prog8.parser.Prog8Parser.parseModule
|
|
|
|
import prog8.parser.SourceCode
|
2021-10-26 22:05:46 +00:00
|
|
|
import kotlin.test.assertEquals
|
|
|
|
import kotlin.test.assertFalse
|
|
|
|
import kotlin.test.assertTrue
|
2021-10-21 20:06:21 +00:00
|
|
|
|
|
|
|
|
2021-11-07 16:25:53 +00:00
|
|
|
class TestSubroutines: AnnotationSpec() {
|
2021-10-21 20:06:21 +00:00
|
|
|
|
|
|
|
@Test
|
|
|
|
fun stringParameterAcceptedInParser() {
|
|
|
|
// note: the *parser* should accept this as it is valid *syntax*,
|
|
|
|
// however, the compiler itself may or may not complain about it later.
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
asmsub asmfunc(str thing @AY) {
|
|
|
|
}
|
|
|
|
|
|
|
|
sub func(str thing) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
val src = SourceCode.Text(text)
|
|
|
|
val module = parseModule(src)
|
|
|
|
val mainBlock = module.statements.single() as Block
|
|
|
|
val asmfunc = mainBlock.statements.filterIsInstance<Subroutine>().single { it.name=="asmfunc"}
|
|
|
|
val func = mainBlock.statements.filterIsInstance<Subroutine>().single { it.name=="func"}
|
|
|
|
assertTrue(asmfunc.isAsmSubroutine)
|
|
|
|
assertEquals(DataType.STR, asmfunc.parameters.single().type)
|
|
|
|
assertTrue(asmfunc.statements.isEmpty())
|
|
|
|
assertFalse(func.isAsmSubroutine)
|
|
|
|
assertEquals(DataType.STR, func.parameters.single().type)
|
|
|
|
assertTrue(func.statements.isEmpty())
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
fun arrayParameterAcceptedInParser() {
|
|
|
|
// note: the *parser* should accept this as it is valid *syntax*,
|
|
|
|
// however, the compiler itself may or may not complain about it later.
|
|
|
|
val text = """
|
|
|
|
main {
|
|
|
|
asmsub asmfunc(ubyte[] thing @AY) {
|
|
|
|
}
|
|
|
|
|
|
|
|
sub func(ubyte[22] thing) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
val src = SourceCode.Text(text)
|
|
|
|
val module = parseModule(src)
|
|
|
|
val mainBlock = module.statements.single() as Block
|
|
|
|
val asmfunc = mainBlock.statements.filterIsInstance<Subroutine>().single { it.name=="asmfunc"}
|
|
|
|
val func = mainBlock.statements.filterIsInstance<Subroutine>().single { it.name=="func"}
|
|
|
|
assertTrue(asmfunc.isAsmSubroutine)
|
|
|
|
assertEquals(DataType.ARRAY_UB, asmfunc.parameters.single().type)
|
|
|
|
assertTrue(asmfunc.statements.isEmpty())
|
|
|
|
assertFalse(func.isAsmSubroutine)
|
|
|
|
assertEquals(DataType.ARRAY_UB, func.parameters.single().type)
|
|
|
|
assertTrue(func.statements.isEmpty())
|
|
|
|
}
|
|
|
|
}
|