new ast: regular subroutine has just 0 or 1 return type

This commit is contained in:
Irmen de Jong 2022-03-12 14:12:06 +01:00
parent 73dbdbcbe6
commit e227cc92ff
5 changed files with 8 additions and 12 deletions

View File

@ -21,7 +21,7 @@ class PtAsmSub(
class PtSub(
name: String,
val parameters: List<PtSubroutineParameter>,
val returntypes: List<DataType>,
val returntype: DataType?,
val inline: Boolean,
position: Position
) : PtNamedNode(name, position) {

View File

@ -284,7 +284,7 @@ class IntermediateAstMaker(val srcProgram: Program) {
private fun transformSub(srcSub: Subroutine): PtSub {
val sub = PtSub(srcSub.name,
srcSub.parameters.map { PtSubroutineParameter(it.name, it.type, it.position) },
srcSub.returntypes,
srcSub.returntypes.singleOrNull(),
srcSub.inline,
srcSub.position)

View File

@ -275,20 +275,15 @@ private fun Prog8ANTLRParser.LabeldefContext.toAst(): Statement =
private fun Prog8ANTLRParser.SubroutineContext.toAst() : Subroutine {
// non-asm subroutine
val inline = inline()!=null
val returntypes = sub_return_part()?.toAst() ?: emptyList()
val returntype = sub_return_part()?.datatype()?.toAst()
return Subroutine(identifier().text,
sub_params()?.toAst()?.toMutableList() ?: mutableListOf(),
returntypes,
if(returntype==null) emptyList() else listOf(returntype),
statement_block()?.toAst() ?: mutableListOf(),
inline,
toPosition())
}
private fun Prog8ANTLRParser.Sub_return_partContext.toAst(): List<DataType> {
val returns = sub_returns() ?: return emptyList()
return returns.datatype().map { it.toAst() }
}
private fun Prog8ANTLRParser.Sub_paramsContext.toAst(): List<SubroutineParameter> =
vardecl().map {
var datatype = it.datatype()?.toAst() ?: DataType.UNDEFINED

View File

@ -31,6 +31,9 @@ main {
; test_stack.test()
}
sub derp() -> ubyte, ubyte {
return 0, 1
}
sub find_next_prime() -> ubyte {

View File

@ -245,7 +245,7 @@ subroutine :
inline? 'sub' identifier '(' sub_params? ')' sub_return_part? (statement_block EOL)
;
sub_return_part : '->' sub_returns ;
sub_return_part : '->' datatype ;
statement_block :
'{' EOL
@ -256,8 +256,6 @@ statement_block :
sub_params : vardecl (',' EOL? vardecl)* ;
sub_returns : datatype (',' EOL? datatype)* ;
asmsubroutine :
inline? 'asmsub' asmsub_decl statement_block
;