Skip to content

Commit 3f3a73b

Browse files
committed
[c] if (conditionWithTemporaries).
fusionlanguage#26, fusionlanguage#140
1 parent bdd2fba commit 3f3a73b

File tree

7 files changed

+87
-10
lines changed

7 files changed

+87
-10
lines changed

GenBase.fu

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1650,10 +1650,15 @@ public abstract class GenBase : FuVisitor
16501650
WriteChar(')');
16511651
}
16521652

1653-
void WriteIf!(FuIf statement)
1653+
protected virtual void StartIf!(FuExpr expr)
16541654
{
16551655
Write("if (");
1656-
StartIfWhile(statement.Cond);
1656+
StartIfWhile(expr);
1657+
}
1658+
1659+
void WriteIf!(FuIf statement)
1660+
{
1661+
StartIf(statement.Cond);
16571662
WriteChild(statement.OnTrue);
16581663
if (statement.OnFalse != null) {
16591664
Write("else");

GenC.fu

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2881,6 +2881,20 @@ public class GenC : GenCCpp
28812881
NotSupported(statement.Collection, statement.Collection.Type.ToString());
28822882
}
28832883

2884+
protected override void StartIf!(FuExpr expr)
2885+
{
2886+
WriteCTemporaries(expr);
2887+
if (this.CurrentTemporaries.Any(temp => !(temp is FuType))) {
2888+
Write("bool fucondition = "); // FIXME: already in scope
2889+
expr.Accept(this, FuPriority.Argument);
2890+
WriteCharLine(';');
2891+
CleanupTemporaries();
2892+
Write("if (fucondition)");
2893+
}
2894+
else
2895+
base.StartIf(expr);
2896+
}
2897+
28842898
internal override void VisitLock!(FuLock statement)
28852899
{
28862900
Write("mtx_lock(&");

libfut.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8026,10 +8026,15 @@ void GenBase::startIfWhile(const FuExpr * expr)
80268026
writeChar(')');
80278027
}
80288028

8029-
void GenBase::writeIf(const FuIf * statement)
8029+
void GenBase::startIf(const FuExpr * expr)
80308030
{
80318031
write("if (");
8032-
startIfWhile(statement->cond.get());
8032+
startIfWhile(expr);
8033+
}
8034+
8035+
void GenBase::writeIf(const FuIf * statement)
8036+
{
8037+
startIf(statement->cond.get());
80338038
writeChild(statement->onTrue.get());
80348039
if (statement->onFalse != nullptr) {
80358040
write("else");
@@ -11797,6 +11802,20 @@ void GenC::visitForeach(const FuForeach * statement)
1179711802
notSupported(statement->collection.get(), statement->collection->type->toString());
1179811803
}
1179911804

11805+
void GenC::startIf(const FuExpr * expr)
11806+
{
11807+
writeCTemporaries(expr);
11808+
if (std::any_of(this->currentTemporaries.begin(), this->currentTemporaries.end(), [](const FuExpr * temp) { return !dynamic_cast<const FuType *>(temp); })) {
11809+
write("bool fucondition = ");
11810+
expr->accept(this, FuPriority::argument);
11811+
writeCharLine(';');
11812+
cleanupTemporaries();
11813+
write("if (fucondition)");
11814+
}
11815+
else
11816+
GenBase::startIf(expr);
11817+
}
11818+
1180011819
void GenC::visitLock(const FuLock * statement)
1180111820
{
1180211821
write("mtx_lock(&");

libfut.cs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8215,10 +8215,15 @@ void StartIfWhile(FuExpr expr)
82158215
WriteChar(')');
82168216
}
82178217

8218-
void WriteIf(FuIf statement)
8218+
protected virtual void StartIf(FuExpr expr)
82198219
{
82208220
Write("if (");
8221-
StartIfWhile(statement.Cond);
8221+
StartIfWhile(expr);
8222+
}
8223+
8224+
void WriteIf(FuIf statement)
8225+
{
8226+
StartIf(statement.Cond);
82228227
WriteChild(statement.OnTrue);
82238228
if (statement.OnFalse != null) {
82248229
Write("else");
@@ -12069,6 +12074,20 @@ internal override void VisitForeach(FuForeach statement)
1206912074
NotSupported(statement.Collection, statement.Collection.Type.ToString());
1207012075
}
1207112076

12077+
protected override void StartIf(FuExpr expr)
12078+
{
12079+
WriteCTemporaries(expr);
12080+
if (this.CurrentTemporaries.Exists(temp => !(temp is FuType))) {
12081+
Write("bool fucondition = ");
12082+
expr.Accept(this, FuPriority.Argument);
12083+
WriteCharLine(';');
12084+
CleanupTemporaries();
12085+
Write("if (fucondition)");
12086+
}
12087+
else
12088+
base.StartIf(expr);
12089+
}
12090+
1207212091
internal override void VisitLock(FuLock statement)
1207312092
{
1207412093
Write("mtx_lock(&");

libfut.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,6 +1857,7 @@ class GenBase : public FuVisitor
18571857
virtual void writeChild(FuStatement * statement);
18581858
virtual void startBreakGoto();
18591859
virtual bool embedIfWhileIsVar(const FuExpr * expr, bool write);
1860+
virtual void startIf(const FuExpr * expr);
18601861
void defineVar(const FuExpr * value);
18611862
virtual void writeSwitchCaseTypeVar(const FuExpr * value);
18621863
virtual void writeSwitchValue(const FuExpr * expr);
@@ -2080,6 +2081,7 @@ class GenC : public GenCCpp
20802081
void writeIndexingExpr(const FuBinaryExpr * expr, FuPriority parent) override;
20812082
void writeResource(std::string_view name, int length) override;
20822083
void cleanupBlock(const FuBlock * statement) override;
2084+
void startIf(const FuExpr * expr) override;
20832085
void writeSwitchCaseBody(const std::vector<std::shared_ptr<FuStatement>> * statements) override;
20842086
void writeStatements(const std::vector<std::shared_ptr<FuStatement>> * statements) override;
20852087
void writeEnum(const FuEnum * enu) override;

libfut.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8537,10 +8537,15 @@ export class GenBase extends FuVisitor
85378537
this.writeChar(41);
85388538
}
85398539

8540-
#writeIf(statement)
8540+
startIf(expr)
85418541
{
85428542
this.write("if (");
8543-
this.#startIfWhile(statement.cond);
8543+
this.#startIfWhile(expr);
8544+
}
8545+
8546+
#writeIf(statement)
8547+
{
8548+
this.startIf(statement.cond);
85448549
this.writeChild(statement.onTrue);
85458550
if (statement.onFalse != null) {
85468551
this.write("else");
@@ -12465,6 +12470,20 @@ export class GenC extends GenCCpp
1246512470
this.notSupported(statement.collection, statement.collection.type.toString());
1246612471
}
1246712472

12473+
startIf(expr)
12474+
{
12475+
this.#writeCTemporaries(expr);
12476+
if (this.currentTemporaries.some(temp => !(temp instanceof FuType))) {
12477+
this.write("bool fucondition = ");
12478+
expr.accept(this, FuPriority.ARGUMENT);
12479+
this.writeCharLine(59);
12480+
this.cleanupTemporaries();
12481+
this.write("if (fucondition)");
12482+
}
12483+
else
12484+
super.startIf(expr);
12485+
}
12486+
1246812487
visitLock(statement)
1246912488
{
1247012489
this.write("mtx_lock(&");

test/JsonElement.fu

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,7 @@ public static class Test
352352
{
353353
public static bool Run()
354354
{
355-
JsonElement# json; //FAIL: c leak TODO; cl
356-
json = JsonElement.Parse("\"foo\"");
355+
JsonElement# json = JsonElement.Parse("\"foo\""); //FAIL: cl
357356
if (!json.IsString() || json.GetString() != "foo")
358357
return false;
359358
json = JsonElement.Parse("10.5");

0 commit comments

Comments
 (0)