type check tuning

This commit is contained in:
Irmen de Jong
2025-05-05 00:11:37 +02:00
parent d11386ef26
commit 38949b82c3
12 changed files with 193 additions and 43 deletions

View File

@@ -353,12 +353,13 @@ class AstToSourceTextConverter(val output: (text: String) -> Unit, val program:
override fun visit(assignment: Assignment) {
val binExpr = assignment.value as? BinaryExpression
if(binExpr!=null && assignment.isAugmentable) {
if(binExpr!=null && binExpr.left isSameAs assignment.target && binExpr.operator !in ComparisonOperators) {
// we only support the inplace assignments of the form A = A <operator> <value>
// don't use assignment.isAugmentable here! That one is a more general check, and not suitable for printing the AST like here
assignment.target.accept(this)
output(" ${binExpr.operator}= ")
binExpr.right.accept(this)
} else {
val whyNot = assignment.isAugmentable
assignment.target.accept(this)
output(" = ")
assignment.value.accept(this)

View File

@@ -386,7 +386,7 @@ class StructDecl(override val name: String, val fields: List<Pair<DataType, Stri
override fun copy() = StructDecl(name, fields.toList(), position)
override fun accept(visitor: IAstVisitor) = visitor.visit(this)
override fun accept(visitor: AstWalker, parent: Node) = visitor.visit(this, parent)
fun memsize(sizer: IMemSizer): Int = fields.sumOf { sizer.memorySize(it.first, 1) }
override fun memsize(sizer: IMemSizer): Int = fields.sumOf { sizer.memorySize(it.first, 1) }
fun getFieldType(name: String): DataType? = fields.firstOrNull { it.second==name }?.first
override val scopedNameString by lazy { scopedName.joinToString(".") }
}
@@ -503,6 +503,7 @@ class Assignment(var target: AssignTarget, var value: Expression, var origin: As
/**
* Is the assigment value an expression that references the assignment target itself?
* Note it doesn't have to be the first term in the expression!
* The expression can be a BinaryExpression, PrefixExpression or TypecastExpression (possibly with one sub-cast).
*/
val isAugmentable: Boolean