From eddb25e7de0704eb6110d7244f00b071d3dc85f5 Mon Sep 17 00:00:00 2001 From: Piotr Fusik Date: Wed, 28 Feb 2024 09:37:41 +0100 Subject: [PATCH] [json] Refactor Swift. #140 --- GenSwift.fu | 27 +++++++-------------------- libfut.cpp | 27 +++++++-------------------- libfut.cs | 27 +++++++-------------------- libfut.hpp | 1 - libfut.js | 27 +++++++-------------------- 5 files changed, 28 insertions(+), 81 deletions(-) diff --git a/GenSwift.fu b/GenSwift.fu index b3092661..96294f28 100644 --- a/GenSwift.fu +++ b/GenSwift.fu @@ -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 args, FuPriority parent) { switch (method.Id) { @@ -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: diff --git a/libfut.cpp b/libfut.cpp index f413ff0c..099f9626 100644 --- a/libfut.cpp +++ b/libfut.cpp @@ -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> * args, FuPriority parent) { switch (method->id) { @@ -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: diff --git a/libfut.cs b/libfut.cs index d1da3910..858c6d23 100644 --- a/libfut.cs +++ b/libfut.cs @@ -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 args, FuPriority parent) { switch (method.Id) { @@ -22212,19 +22201,17 @@ protected override void WriteCallExpr(FuExpr obj, FuMethod method, List 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: diff --git a/libfut.hpp b/libfut.hpp index 58050b0e..8c54e160 100644 --- a/libfut.hpp +++ b/libfut.hpp @@ -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); diff --git a/libfut.js b/libfut.js index 86122ffb..d7e2ab9d 100644 --- a/libfut.js +++ b/libfut.js @@ -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) { @@ -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: