Skip to content

Commit

Permalink
[string] The "+" and "+=" operators no longer concatenate strings wit…
Browse files Browse the repository at this point in the history
…h numbers or objects.

Use string interpolation instead.
  • Loading branch information
pfusik committed Jul 24, 2023
1 parent db855b9 commit b29f67c
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
7 changes: 3 additions & 4 deletions Sema.ci
Original file line number Diff line number Diff line change
Expand Up @@ -868,9 +868,8 @@ public class CiSema
SaturatedAdd(leftAdd.Min, rightAdd.Min),
SaturatedAdd(leftAdd.Max, rightAdd.Max));
}
else if (left.Type is CiStringType || right.Type is CiStringType) {
Coerce(left, this.Program.System.PrintableType);
Coerce(right, this.Program.System.PrintableType);
else if (left.Type is CiStringType) {
Coerce(right, this.Program.System.StringPtrType);
if (left is CiLiteral leftLiteral && right is CiLiteral rightLiteral)
return this.Program.System.NewLiteralString(leftLiteral.GetLiteralString() + rightLiteral.GetLiteralString(), expr.Line);
if (left is CiInterpolatedString || right is CiInterpolatedString)
Expand Down Expand Up @@ -1035,7 +1034,7 @@ public class CiSema
case CiToken.AddAssign:
CheckLValue(left);
if (left.Type.Id == CiId.StringStorageType)
Coerce(right, this.Program.System.PrintableType);
Coerce(right, this.Program.System.StringPtrType);
else {
Coerce(left, this.Program.System.DoubleType);
Coerce(right, left.Type);
Expand Down
7 changes: 3 additions & 4 deletions Transpiled.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4868,9 +4868,8 @@ std::shared_ptr<CiExpr> CiSema::visitBinaryExpr(std::shared_ptr<CiBinaryExpr> ex
if ((leftAdd = dynamic_cast<const CiRangeType *>(left->type.get())) && (rightAdd = dynamic_cast<const CiRangeType *>(right->type.get()))) {
type = CiRangeType::new_(saturatedAdd(leftAdd->min, rightAdd->min), saturatedAdd(leftAdd->max, rightAdd->max));
}
else if (dynamic_cast<const CiStringType *>(left->type.get()) || dynamic_cast<const CiStringType *>(right->type.get())) {
coerce(left.get(), this->program->system->printableType.get());
coerce(right.get(), this->program->system->printableType.get());
else if (dynamic_cast<const CiStringType *>(left->type.get())) {
coerce(right.get(), this->program->system->stringPtrType.get());
const CiLiteral * leftLiteral;
const CiLiteral * rightLiteral;
if ((leftLiteral = dynamic_cast<const CiLiteral *>(left.get())) && (rightLiteral = dynamic_cast<const CiLiteral *>(right.get())))
Expand Down Expand Up @@ -5057,7 +5056,7 @@ std::shared_ptr<CiExpr> CiSema::visitBinaryExpr(std::shared_ptr<CiBinaryExpr> ex
case CiToken::addAssign:
checkLValue(left.get());
if (left->type->id == CiId::stringStorageType)
coerce(right.get(), this->program->system->printableType.get());
coerce(right.get(), this->program->system->stringPtrType.get());
else {
coerce(left.get(), this->program->system->doubleType.get());
coerce(right.get(), left->type.get());
Expand Down
7 changes: 3 additions & 4 deletions Transpiled.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5112,9 +5112,8 @@ CiExpr VisitBinaryExpr(CiBinaryExpr expr)
if (left.Type is CiRangeType leftAdd && right.Type is CiRangeType rightAdd) {
type = CiRangeType.New(SaturatedAdd(leftAdd.Min, rightAdd.Min), SaturatedAdd(leftAdd.Max, rightAdd.Max));
}
else if (left.Type is CiStringType || right.Type is CiStringType) {
Coerce(left, this.Program.System.PrintableType);
Coerce(right, this.Program.System.PrintableType);
else if (left.Type is CiStringType) {
Coerce(right, this.Program.System.StringPtrType);
if (left is CiLiteral leftLiteral && right is CiLiteral rightLiteral)
return this.Program.System.NewLiteralString(leftLiteral.GetLiteralString() + rightLiteral.GetLiteralString(), expr.Line);
if (left is CiInterpolatedString || right is CiInterpolatedString)
Expand Down Expand Up @@ -5258,7 +5257,7 @@ CiExpr VisitBinaryExpr(CiBinaryExpr expr)
case CiToken.AddAssign:
CheckLValue(left);
if (left.Type.Id == CiId.StringStorageType)
Coerce(right, this.Program.System.PrintableType);
Coerce(right, this.Program.System.StringPtrType);
else {
Coerce(left, this.Program.System.DoubleType);
Coerce(right, left.Type);
Expand Down
7 changes: 3 additions & 4 deletions Transpiled.js
Original file line number Diff line number Diff line change
Expand Up @@ -5288,9 +5288,8 @@ export class CiSema
if ((leftAdd = left.type) instanceof CiRangeType && (rightAdd = right.type) instanceof CiRangeType) {
type = CiRangeType.new(CiSema.#saturatedAdd(leftAdd.min, rightAdd.min), CiSema.#saturatedAdd(leftAdd.max, rightAdd.max));
}
else if (left.type instanceof CiStringType || right.type instanceof CiStringType) {
this.#coerce(left, this.program.system.printableType);
this.#coerce(right, this.program.system.printableType);
else if (left.type instanceof CiStringType) {
this.#coerce(right, this.program.system.stringPtrType);
let leftLiteral;
let rightLiteral;
if ((leftLiteral = left) instanceof CiLiteral && (rightLiteral = right) instanceof CiLiteral)
Expand Down Expand Up @@ -5456,7 +5455,7 @@ export class CiSema
case CiToken.ADD_ASSIGN:
this.#checkLValue(left);
if (left.type.id == CiId.STRING_STORAGE_TYPE)
this.#coerce(right, this.program.system.printableType);
this.#coerce(right, this.program.system.stringPtrType);
else {
this.#coerce(left, this.program.system.doubleType);
this.#coerce(right, left.type);
Expand Down

0 comments on commit b29f67c

Please sign in to comment.