allow multi var declarations for floats too

This commit is contained in:
Irmen de Jong 2023-12-08 23:00:23 +01:00
parent 1bdc427d73
commit ae3b2ddf5f
3 changed files with 25 additions and 17 deletions

View File

@ -113,19 +113,24 @@ class AstPreprocessor(val program: Program,
movements.add(IAstModification.InsertFirst(decl, parentscope)) movements.add(IAstModification.InsertFirst(decl, parentscope))
replacements.add(IAstModification.Remove(decl, scope)) replacements.add(IAstModification.Remove(decl, scope))
} else { } else {
val declToInsert: VarDecl if(decl.names.size>1) {
if(decl.value!=null && decl.datatype in NumericDatatypes) { // we need to handle multi-decl here too, the desugarer maybe has not processed it here yet...
val target = AssignTarget(IdentifierReference(listOf(decl.name), decl.position), null, null, decl.position) TODO("handle multi-decl movement")
val assign = Assignment(target, decl.value!!, AssignmentOrigin.VARINIT, decl.position)
replacements.add(IAstModification.ReplaceNode(decl, assign, scope))
decl.value = null
decl.allowInitializeWithZero = false
declToInsert = decl.copy()
} else { } else {
replacements.add(IAstModification.Remove(decl, scope)) val declToInsert: VarDecl
declToInsert = decl if(decl.value!=null && decl.datatype in NumericDatatypes) {
val target = AssignTarget(IdentifierReference(listOf(decl.name), decl.position), null, null, decl.position)
val assign = Assignment(target, decl.value!!, AssignmentOrigin.VARINIT, decl.position)
replacements.add(IAstModification.ReplaceNode(decl, assign, scope))
decl.value = null
decl.allowInitializeWithZero = false
declToInsert = decl.copy()
} else {
replacements.add(IAstModification.Remove(decl, scope))
declToInsert = decl
}
movements.add(IAstModification.InsertFirst(declToInsert, parentscope))
} }
movements.add(IAstModification.InsertFirst(declToInsert, parentscope))
} }
} }
return movements + replacements return movements + replacements
@ -150,14 +155,15 @@ class AstPreprocessor(val program: Program,
if(decl.names.size>1) { if(decl.names.size>1) {
// note: the desugaring of a multi-variable vardecl has to be done here // note: the desugaring of a multi-variable vardecl has to be done here
// and not in CodeDesugarer, that one is too late (identifiers can't be found otherwise) // and not in CodeDesugarer, that one is too late (identifiers can't be found otherwise)
if(decl.datatype !in IntegerDatatypes) if(decl.datatype !in NumericDatatypes)
errors.err("can only multi declare integer variables", decl.position) errors.err("can only multi declare numeric variables", decl.position)
if(errors.noErrors()) { return if(errors.noErrors()) {
// desugar into individual vardecl per name. // desugar into individual vardecl per name.
return decl.desugarMultiDecl().map { decl.desugarMultiDecl().map {
IAstModification.InsertAfter(decl, it, parent as IStatementContainer) IAstModification.InsertAfter(decl, it, parent as IStatementContainer)
} + IAstModification.Remove(decl, parent as IStatementContainer) } + IAstModification.Remove(decl, parent as IStatementContainer)
} } else
noModifications
} }
val nextAssignment = decl.nextSibling() as? Assignment val nextAssignment = decl.nextSibling() as? Assignment

View File

@ -300,7 +300,7 @@ class VarDecl(val type: VarDeclType,
override fun copy(): VarDecl { override fun copy(): VarDecl {
if(names.size>1) if(names.size>1)
throw FatalAstException("should not copy a vardecl that still has multiple names") throw FatalAstException("should not copy a vardecl that still has multiple names")
val copy = VarDecl(type, origin, declaredDatatype, zeropage, arraysize?.copy(), name, emptyList(), value?.copy(), val copy = VarDecl(type, origin, declaredDatatype, zeropage, arraysize?.copy(), name, names, value?.copy(),
isArray, sharedWithAsm, splitArray, position) isArray, sharedWithAsm, splitArray, position)
copy.allowInitializeWithZero = this.allowInitializeWithZero copy.allowInitializeWithZero = this.allowInitializeWithZero
return copy return copy

View File

@ -2,6 +2,8 @@
TODO TODO
==== ====
- fix multi-decl in for loops, see AstPreprocessor TODO("handle multi-decl movement")
- [on branch: shortcircuit] investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 .... - [on branch: shortcircuit] investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 ....
... ...