Skip to content

Commit

Permalink
[c] Implement string concatenation.
Browse files Browse the repository at this point in the history
Close #36
  • Loading branch information
pfusik committed Aug 9, 2023
1 parent ae5f5fc commit 25a0675
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
13 changes: 11 additions & 2 deletions GenC.fu
Original file line number Diff line number Diff line change
Expand Up @@ -2390,8 +2390,17 @@ public class GenC : GenCCpp
{
switch (expr.Op) {
case FuToken.Plus:
if (expr.Type.Id == FuId.StringStorageType)
NotSupported(expr, "String concatenation");
if (expr.Type.Id == FuId.StringStorageType) {
this.StringFormat = true;
Include("stdarg.h");
Include("stdio.h");
Write("FuString_Format(\"%s%s\", ");
expr.Left.Accept(this, FuPriority.Argument);
Write(", ");
expr.Right.Accept(this, FuPriority.Argument);
WriteChar(')');
return;
}
break;
case FuToken.Equal:
case FuToken.NotEqual:
Expand Down
13 changes: 11 additions & 2 deletions libfut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10933,8 +10933,17 @@ void GenC::visitBinaryExpr(const FuBinaryExpr * expr, FuPriority parent)
{
switch (expr->op) {
case FuToken::plus:
if (expr->type->id == FuId::stringStorageType)
notSupported(expr, "String concatenation");
if (expr->type->id == FuId::stringStorageType) {
this->stringFormat = true;
include("stdarg.h");
include("stdio.h");
write("FuString_Format(\"%s%s\", ");
expr->left->accept(this, FuPriority::argument);
write(", ");
expr->right->accept(this, FuPriority::argument);
writeChar(')');
return;
}
break;
case FuToken::equal:
case FuToken::notEqual:
Expand Down
13 changes: 11 additions & 2 deletions libfut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11238,8 +11238,17 @@ internal override void VisitBinaryExpr(FuBinaryExpr expr, FuPriority parent)
{
switch (expr.Op) {
case FuToken.Plus:
if (expr.Type.Id == FuId.StringStorageType)
NotSupported(expr, "String concatenation");
if (expr.Type.Id == FuId.StringStorageType) {
this.StringFormat = true;
Include("stdarg.h");
Include("stdio.h");
Write("FuString_Format(\"%s%s\", ");
expr.Left.Accept(this, FuPriority.Argument);
Write(", ");
expr.Right.Accept(this, FuPriority.Argument);
WriteChar(')');
return;
}
break;
case FuToken.Equal:
case FuToken.NotEqual:
Expand Down
13 changes: 11 additions & 2 deletions libfut.js
Original file line number Diff line number Diff line change
Expand Up @@ -11591,8 +11591,17 @@ export class GenC extends GenCCpp
{
switch (expr.op) {
case FuToken.PLUS:
if (expr.type.id == FuId.STRING_STORAGE_TYPE)
this.notSupported(expr, "String concatenation");
if (expr.type.id == FuId.STRING_STORAGE_TYPE) {
this.#stringFormat = true;
this.include("stdarg.h");
this.include("stdio.h");
this.write("FuString_Format(\"%s%s\", ");
expr.left.accept(this, FuPriority.ARGUMENT);
this.write(", ");
expr.right.accept(this, FuPriority.ARGUMENT);
this.writeChar(41);
return;
}
break;
case FuToken.EQUAL:
case FuToken.NOT_EQUAL:
Expand Down
2 changes: 1 addition & 1 deletion test/OpAddString.fu
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ public static class Test
{
string() s = "foo"; //FAIL: cl
string p = "bar";
return s + s == "foofoo" //FAIL: c
return s + s == "foofoo"
&& s + p == "foobar"
&& p + s == "barfoo"
&& p + p == "barbar"
Expand Down

0 comments on commit 25a0675

Please sign in to comment.