From 504c80cddff8469592b847630431a11e99cc4d93 Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Sun, 27 Oct 2024 15:57:27 +0100 Subject: [PATCH] fix parser rule for identifiers (void is a keyword, not an identifier) --- compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt | 16 ++++++++-------- parser/antlr/Prog8ANTLR.g4 | 5 ++++- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt index ad5ee34c3..543d3aa72 100644 --- a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt +++ b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt @@ -249,7 +249,7 @@ private fun Asmsub_paramsContext.toAst(): List val identifiers = vardecl.identifier() if(identifiers.size>1) throw SyntaxError("parameter name must be singular", identifiers[0].toPosition()) - val identifiername = identifiers[0].NAME() ?: identifiers[0].UNDERSCORENAME() ?: identifiers[0].VOID() + val identifiername = identifiers[0].NAME() ?: identifiers[0].UNDERSCORENAME() AsmSubroutineParameter(identifiername.text, datatype, registerorpair, statusregister, toPosition()) } @@ -324,7 +324,7 @@ private fun Sub_paramsContext.toAst(): List = val identifiers = it.identifier() if(identifiers.size>1) throw SyntaxError("parameter name must be singular", identifiers[0].toPosition()) - val identifiername = identifiers[0].NAME() ?: identifiers[0].UNDERSCORENAME() ?: identifiers[0].VOID() + val identifiername = identifiers[0].NAME() ?: identifiers[0].UNDERSCORENAME() SubroutineParameter(identifiername.text, datatype, zp, it.toPosition()) } @@ -343,10 +343,7 @@ private fun Assign_targetContext.toAst() : AssignTarget { return when(this) { is IdentifierTargetContext -> { val identifier = scoped_identifier().toAst() - if(identifier.nameInSource==listOf("void")) - AssignTarget(null, null, null, null, true, scoped_identifier().toPosition()) - else - AssignTarget(identifier, null, null, null, false, scoped_identifier().toPosition()) + AssignTarget(identifier, null, null, null, false, scoped_identifier().toPosition()) } is MemoryTargetContext -> AssignTarget(null, null, DirectMemoryWrite(directmemory().expression().toAst(), directmemory().toPosition()), null, false, toPosition()) @@ -357,6 +354,9 @@ private fun Assign_targetContext.toAst() : AssignTarget { val arrayindexed = ArrayIndexedExpression(arrayvar, index, ax.toPosition()) AssignTarget(null, arrayindexed, null, null, false, toPosition()) } + is VoidTargetContext -> { + AssignTarget(null, null, null, null, true, void_().toPosition()) + } else -> throw FatalAstException("weird assign target node $this") } } @@ -716,7 +716,7 @@ private fun VardeclContext.toAst(type: VarDeclType, value: Expression?): VarDecl val options = decloptions() val zp = getZpOption(options) val identifiers = identifier() - val identifiername = identifiers[0].NAME() ?: identifiers[0].UNDERSCORENAME() ?: identifiers[0].VOID() + val identifiername = identifiers[0].NAME() ?: identifiers[0].UNDERSCORENAME() val name = if(identifiers.size==1) identifiername.text else "" val isArray = ARRAYSIG() != null || arrayindex() != null val split = options.SPLIT().isNotEmpty() @@ -744,7 +744,7 @@ private fun VardeclContext.toAst(type: VarDeclType, value: Expression?): VarDecl arrayindex()?.toAst(), name, if(identifiers.size==1) emptyList() else identifiers.map { - val idname = it.NAME() ?: it.UNDERSCORENAME() ?: it.VOID() + val idname = it.NAME() ?: it.UNDERSCORENAME() idname.text }, value, diff --git a/parser/antlr/Prog8ANTLR.g4 b/parser/antlr/Prog8ANTLR.g4 index e1d9ba508..c92443e30 100644 --- a/parser/antlr/Prog8ANTLR.g4 +++ b/parser/antlr/Prog8ANTLR.g4 @@ -171,6 +171,7 @@ assign_target: scoped_identifier #IdentifierTarget | arrayindexed #ArrayindexedTarget | directmemory #MemoryTarget + | void #VoidTarget ; multi_assign_target: @@ -211,6 +212,8 @@ arrayindexed: ; +void : VOID ; + typecast : 'as' datatype; directmemory : '@' '(' expression ')'; @@ -231,7 +234,7 @@ breakstmt : 'break'; continuestmt: 'continue'; -identifier : NAME | UNDERSCORENAME | VOID; +identifier : NAME | UNDERSCORENAME ; scoped_identifier : identifier ('.' identifier)* ;