Skip to content

Commit

Permalink
[json] Refactor Swift.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfusik committed Feb 28, 2024
1 parent a7cb0c7 commit eddb25e
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 81 deletions.
27 changes: 7 additions & 20 deletions GenSwift.fu
Original file line number Diff line number Diff line change
Expand Up @@ -446,17 +446,6 @@ public class GenSwift : GenPySwift
WriteChar(')');
}

void WriteJsonElementGet!(FuExpr obj, string name, FuPriority parent)
{
if (parent > FuPriority.Equality)
WriteChar('(');
obj.Accept(this, FuPriority.Equality); // FIXME?
Write(" as! ");
Write(name);
if (parent > FuPriority.Equality)
WriteChar(')');
}

protected override void WriteCallExpr!(FuExpr? obj, FuMethod method, List<FuExpr#> args, FuPriority parent)
{
switch (method.Id) {
Expand Down Expand Up @@ -779,19 +768,17 @@ public class GenSwift : GenPySwift
WriteJsonElementIs(obj, "NSNull", parent);
break;
case FuId.JsonElementGetObject:
WriteJsonElementGet(obj, "[String: Any]", parent);
break;
case FuId.JsonElementGetArray:
WriteJsonElementGet(obj, "[Any]", parent);
break;
case FuId.JsonElementGetString:
WriteJsonElementGet(obj, "String", parent);
break;
case FuId.JsonElementGetDouble:
WriteJsonElementGet(obj, "Double", parent);
break;
case FuId.JsonElementGetBoolean:
WriteJsonElementGet(obj, "Bool", parent);
if (parent > FuPriority.Equality)
WriteChar('(');
obj.Accept(this, FuPriority.Equality); // FIXME?
Write(" as! ");
WriteType(method.Type);
if (parent > FuPriority.Equality)
WriteChar(')');
break;
case FuId.MathMethod:
case FuId.MathLog2:
Expand Down
27 changes: 7 additions & 20 deletions libfut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21083,17 +21083,6 @@ void GenSwift::writeJsonElementIs(const FuExpr * obj, std::string_view name, FuP
writeChar(')');
}

void GenSwift::writeJsonElementGet(const FuExpr * obj, std::string_view name, FuPriority parent)
{
if (parent > FuPriority::equality)
writeChar('(');
obj->accept(this, FuPriority::equality);
write(" as! ");
write(name);
if (parent > FuPriority::equality)
writeChar(')');
}

void GenSwift::writeCallExpr(const FuExpr * obj, const FuMethod * method, const std::vector<std::shared_ptr<FuExpr>> * args, FuPriority parent)
{
switch (method->id) {
Expand Down Expand Up @@ -21424,19 +21413,17 @@ void GenSwift::writeCallExpr(const FuExpr * obj, const FuMethod * method, const
writeJsonElementIs(obj, "NSNull", parent);
break;
case FuId::jsonElementGetObject:
writeJsonElementGet(obj, "[String: Any]", parent);
break;
case FuId::jsonElementGetArray:
writeJsonElementGet(obj, "[Any]", parent);
break;
case FuId::jsonElementGetString:
writeJsonElementGet(obj, "String", parent);
break;
case FuId::jsonElementGetDouble:
writeJsonElementGet(obj, "Double", parent);
break;
case FuId::jsonElementGetBoolean:
writeJsonElementGet(obj, "Bool", parent);
if (parent > FuPriority::equality)
writeChar('(');
obj->accept(this, FuPriority::equality);
write(" as! ");
writeType(method->type.get());
if (parent > FuPriority::equality)
writeChar(')');
break;
case FuId::mathMethod:
case FuId::mathLog2:
Expand Down
27 changes: 7 additions & 20 deletions libfut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21881,17 +21881,6 @@ void WriteJsonElementIs(FuExpr obj, string name, FuPriority parent)
WriteChar(')');
}

void WriteJsonElementGet(FuExpr obj, string name, FuPriority parent)
{
if (parent > FuPriority.Equality)
WriteChar('(');
obj.Accept(this, FuPriority.Equality);
Write(" as! ");
Write(name);
if (parent > FuPriority.Equality)
WriteChar(')');
}

protected override void WriteCallExpr(FuExpr obj, FuMethod method, List<FuExpr> args, FuPriority parent)
{
switch (method.Id) {
Expand Down Expand Up @@ -22212,19 +22201,17 @@ protected override void WriteCallExpr(FuExpr obj, FuMethod method, List<FuExpr>
WriteJsonElementIs(obj, "NSNull", parent);
break;
case FuId.JsonElementGetObject:
WriteJsonElementGet(obj, "[String: Any]", parent);
break;
case FuId.JsonElementGetArray:
WriteJsonElementGet(obj, "[Any]", parent);
break;
case FuId.JsonElementGetString:
WriteJsonElementGet(obj, "String", parent);
break;
case FuId.JsonElementGetDouble:
WriteJsonElementGet(obj, "Double", parent);
break;
case FuId.JsonElementGetBoolean:
WriteJsonElementGet(obj, "Bool", parent);
if (parent > FuPriority.Equality)
WriteChar('(');
obj.Accept(this, FuPriority.Equality);
Write(" as! ");
WriteType(method.Type);
if (parent > FuPriority.Equality)
WriteChar(')');
break;
case FuId.MathMethod:
case FuId.MathLog2:
Expand Down
1 change: 0 additions & 1 deletion libfut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2789,7 +2789,6 @@ class GenSwift : public GenPySwift
void writeRange(const FuExpr * startIndex, const FuExpr * length);
bool addVar(std::string_view name);
void writeJsonElementIs(const FuExpr * obj, std::string_view name, FuPriority parent);
void writeJsonElementGet(const FuExpr * obj, std::string_view name, FuPriority parent);
void writeDefaultValue(const FuType * type);
void writeEnumFlagsAnd(const FuExpr * left, std::string_view method, std::string_view notMethod, const FuExpr * right);
const FuExpr * writeAssignNested(const FuBinaryExpr * expr);
Expand Down
27 changes: 7 additions & 20 deletions libfut.js
Original file line number Diff line number Diff line change
Expand Up @@ -22441,17 +22441,6 @@ export class GenSwift extends GenPySwift
this.writeChar(41);
}

#writeJsonElementGet(obj, name, parent)
{
if (parent > FuPriority.EQUALITY)
this.writeChar(40);
obj.accept(this, FuPriority.EQUALITY);
this.write(" as! ");
this.write(name);
if (parent > FuPriority.EQUALITY)
this.writeChar(41);
}

writeCallExpr(obj, method, args, parent)
{
switch (method.id) {
Expand Down Expand Up @@ -22774,19 +22763,17 @@ export class GenSwift extends GenPySwift
this.#writeJsonElementIs(obj, "NSNull", parent);
break;
case FuId.JSON_ELEMENT_GET_OBJECT:
this.#writeJsonElementGet(obj, "[String: Any]", parent);
break;
case FuId.JSON_ELEMENT_GET_ARRAY:
this.#writeJsonElementGet(obj, "[Any]", parent);
break;
case FuId.JSON_ELEMENT_GET_STRING:
this.#writeJsonElementGet(obj, "String", parent);
break;
case FuId.JSON_ELEMENT_GET_DOUBLE:
this.#writeJsonElementGet(obj, "Double", parent);
break;
case FuId.JSON_ELEMENT_GET_BOOLEAN:
this.#writeJsonElementGet(obj, "Bool", parent);
if (parent > FuPriority.EQUALITY)
this.writeChar(40);
obj.accept(this, FuPriority.EQUALITY);
this.write(" as! ");
this.#writeType(method.type);
if (parent > FuPriority.EQUALITY)
this.writeChar(41);
break;
case FuId.MATH_METHOD:
case FuId.MATH_LOG2:
Expand Down

0 comments on commit eddb25e

Please sign in to comment.