Skip to content

Commit

Permalink
Improve return type updates for bin expr
Browse files Browse the repository at this point in the history
  • Loading branch information
vicchig committed Jan 8, 2022
1 parent 8513d0f commit 256dd08
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 37 deletions.
3 changes: 1 addition & 2 deletions src/editor/action-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1111,8 +1111,7 @@ export class ActionExecutor {
if (newCode.rootNode instanceof BinaryOperatorExpr) {
newCode.rootNode.onInsertInto(newCode);
newCode.rootNode.validateTypes(this.module);
}
if (newCode.rootNode instanceof Statement) {
} else if (newCode.rootNode instanceof Statement) {
newCode.rootNode.onInsertInto(newCode);
}

Expand Down
113 changes: 78 additions & 35 deletions src/syntax-tree/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2797,43 +2797,75 @@ export class BinaryOperatorExpr extends Expression {
rootExpr.performReturnTypeUpdatesForAdditionOnInsertInto(rightOperand);
}

if (leftOperand && rightOperand && leftOperand instanceof Expression && rightOperand instanceof Expression) {
if (
leftOperand &&
rightOperand &&
leftOperand instanceof TypedEmptyExpr &&
rightOperand instanceof TypedEmptyExpr
) {
return;
} else if (rootExpr.operator === BinaryOperator.Add) {
if (
leftOperand.returns === rightOperand.returns &&
TypeChecker.getAllowedBinaryOperatorsForType(leftOperand.returns).indexOf(rootExpr.operator) > -1
leftOperand &&
rightOperand &&
leftOperand instanceof Expression &&
rightOperand instanceof Expression
) {
if (
leftOperand.returns === rightOperand.returns &&
TypeChecker.getAllowedBinaryOperatorsForType(leftOperand.returns).indexOf(rootExpr.operator) > -1
) {
rootExpr.returns = leftOperand.returns;
} else {
rootExpr.returns = DataType.Any;
}
} else if (
leftOperand &&
leftOperand instanceof Expression &&
(rootExpr.originalReturnType === leftOperand.returns || rootExpr.originalReturnType === DataType.Any)
) {
rootExpr.returns = leftOperand.returns;
} else if (
rightOperand &&
rightOperand instanceof Expression &&
(rootExpr.originalReturnType === rightOperand.returns || rootExpr.originalReturnType === DataType.Any)
) {
rootExpr.returns = rightOperand.returns;
} else if (
leftOperand &&
leftOperand instanceof TypedEmptyExpr &&
rightOperand &&
rightOperand instanceof Expression &&
(rootExpr.originalReturnType === rightOperand.returns || rootExpr.originalReturnType === DataType.Any)
) {
rootExpr.returns = rightOperand.returns;
} else if (
rightOperand &&
rightOperand instanceof TypedEmptyExpr &&
leftOperand &&
leftOperand instanceof Expression &&
(rootExpr.originalReturnType === leftOperand.returns || rootExpr.originalReturnType === DataType.Any)
) {
rootExpr.returns = leftOperand.returns;
}
} else if (rootExpr.isArithmetic()) {
if (
leftOperand &&
rightOperand &&
leftOperand instanceof Expression &&
rightOperand instanceof Expression
) {
if (
leftOperand.returns === rightOperand.returns &&
TypeChecker.getAllowedBinaryOperatorsForType(leftOperand.returns).indexOf(rootExpr.operator) > -1
) {
rootExpr.returns = leftOperand.returns;
} else {
rootExpr.returns = DataType.Any;
}
} else {
rootExpr.returns = DataType.Any;
}
} else if (
leftOperand &&
leftOperand instanceof Expression &&
(rootExpr.originalReturnType === leftOperand.returns || rootExpr.originalReturnType === DataType.Any)
) {
rootExpr.returns = leftOperand.returns;
} else if (
rightOperand &&
rightOperand instanceof Expression &&
(rootExpr.originalReturnType === rightOperand.returns || rootExpr.originalReturnType === DataType.Any)
) {
rootExpr.returns = rightOperand.returns;
} else if (
leftOperand &&
leftOperand instanceof TypedEmptyExpr &&
rightOperand &&
rightOperand instanceof Expression &&
(rootExpr.originalReturnType === rightOperand.returns || rootExpr.originalReturnType === DataType.Any)
) {
rootExpr.returns = rightOperand.returns;
} else if (
rightOperand &&
rightOperand instanceof TypedEmptyExpr &&
leftOperand &&
leftOperand instanceof Expression &&
(rootExpr.originalReturnType === leftOperand.returns || rootExpr.originalReturnType === DataType.Any)
) {
rootExpr.returns = leftOperand.returns;
}
}

Expand Down Expand Up @@ -2907,8 +2939,7 @@ export class BinaryOperatorExpr extends Expression {
}

//update return types in root
if (curr && this.isArithmetic() && this.operator === BinaryOperator.Add)
this.performReturnTypeUpdatesForAdditionOnInsertInto(curr);
if (curr && this.isArithmetic()) this.performReturnTypeUpdatesForAdditionOnInsertInto(curr);
}

//should only be used on nested binary ops
Expand Down Expand Up @@ -3113,7 +3144,13 @@ export class BinaryOperatorExpr extends Expression {
module.openDraftMode(
leftOperand,
TYPE_MISMATCH_ANY(this.typeOfHoles[this.leftOperandIndex], leftOperand.returns),
conversionActionsForLeft
[
new IgnoreConversionRecord("", null, null, "", null, Tooltip.IgnoreWarning).getConversionButton(
"",
module,
leftOperand
),
]
);
} else if (!operationDefinedBetweenTypes) {
if (conversionActionsForLeft.length > 0) {
Expand Down Expand Up @@ -3152,7 +3189,13 @@ export class BinaryOperatorExpr extends Expression {
module.openDraftMode(
rightOperand,
TYPE_MISMATCH_ANY(this.typeOfHoles[this.leftOperandIndex], rightOperand.returns),
conversionActionsForRight
[
new IgnoreConversionRecord("", null, null, "", null, Tooltip.IgnoreWarning).getConversionButton(
"",
module,
rightOperand
),
]
);
} else if (!operationDefinedBetweenTypes) {
if (conversionActionsForRight.length > 0) {
Expand Down

0 comments on commit 256dd08

Please sign in to comment.