From e227cc92ffe6601442216e33316981d1bdc76e32 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sat, 12 Mar 2022 14:12:06 +0100 Subject: [PATCH] new ast: regular subroutine has just 0 or 1 return type --- codeAst/src/prog8/code/ast/AstStatements.kt | 2 +- compiler/src/prog8/compiler/IntermediateAstMaker.kt | 2 +- compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt | 9 ++------- examples/test.p8 | 3 +++ parser/antlr/Prog8ANTLR.g4 | 4 +--- 5 files changed, 8 insertions(+), 12 deletions(-) diff --git a/codeAst/src/prog8/code/ast/AstStatements.kt b/codeAst/src/prog8/code/ast/AstStatements.kt index 51f501c70..63083b07b 100644 --- a/codeAst/src/prog8/code/ast/AstStatements.kt +++ b/codeAst/src/prog8/code/ast/AstStatements.kt @@ -21,7 +21,7 @@ class PtAsmSub( class PtSub( name: String, val parameters: List, - val returntypes: List, + val returntype: DataType?, val inline: Boolean, position: Position ) : PtNamedNode(name, position) { diff --git a/compiler/src/prog8/compiler/IntermediateAstMaker.kt b/compiler/src/prog8/compiler/IntermediateAstMaker.kt index 094989292..d7fc6d3bd 100644 --- a/compiler/src/prog8/compiler/IntermediateAstMaker.kt +++ b/compiler/src/prog8/compiler/IntermediateAstMaker.kt @@ -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) diff --git a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt index eace01d50..9fbfb87bb 100644 --- a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt +++ b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt @@ -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 { - val returns = sub_returns() ?: return emptyList() - return returns.datatype().map { it.toAst() } -} - private fun Prog8ANTLRParser.Sub_paramsContext.toAst(): List = vardecl().map { var datatype = it.datatype()?.toAst() ?: DataType.UNDEFINED diff --git a/examples/test.p8 b/examples/test.p8 index 377a16819..ee3022edd 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -31,6 +31,9 @@ main { ; test_stack.test() } + sub derp() -> ubyte, ubyte { + return 0, 1 + } sub find_next_prime() -> ubyte { diff --git a/parser/antlr/Prog8ANTLR.g4 b/parser/antlr/Prog8ANTLR.g4 index aad9b8f08..aa4985ec0 100644 --- a/parser/antlr/Prog8ANTLR.g4 +++ b/parser/antlr/Prog8ANTLR.g4 @@ -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 ;