Skip to content

Commit

Permalink
[cleanup] Refactor with WriteArrayRef.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfusik committed Nov 2, 2024
1 parent ebffcf7 commit 318ff24
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 56 deletions.
27 changes: 13 additions & 14 deletions GenSwift.fu
Original file line number Diff line number Diff line change
Expand Up @@ -197,17 +197,22 @@ public class GenSwift : GenPySwift

static bool IsArrayRef(FuArrayStorageType array) => array.PtrTaken || array.GetElementType() is FuStorageType;

void WriteArrayRef!(FuType elementType)
{
this.ArrayRef = true;
Write("ArrayRef<");
WriteType(elementType);
WriteChar('>');
}

void WriteClassName!(FuClassType klass)
{
switch (klass.Class.Id) {
case FuId.StringClass:
Write("String");
break;
case FuId.ArrayPtrClass:
this.ArrayRef = true;
Write("ArrayRef<");
WriteType(klass.GetElementType());
WriteChar('>');
WriteArrayRef(klass.GetElementType());
break;
case FuId.ArrayStorageClass: // Main(string[] args)
case FuId.ListClass:
Expand Down Expand Up @@ -285,12 +290,8 @@ public class GenSwift : GenPySwift
Write(type.Id == FuId.BoolType ? "Bool" : type.Name);
break;
case FuArrayStorageType arrayStg:
if (IsArrayRef(arrayStg)) {
this.ArrayRef = true;
Write("ArrayRef<");
WriteType(arrayStg.GetElementType());
WriteChar('>');
}
if (IsArrayRef(arrayStg))
WriteArrayRef(arrayStg.GetElementType());
else {
WriteChar('[');
WriteType(arrayStg.GetElementType());
Expand Down Expand Up @@ -879,10 +880,8 @@ public class GenSwift : GenPySwift

protected override void WriteNewArray!(FuType elementType, FuExpr lengthExpr, FuPriority parent)
{
this.ArrayRef = true;
Write("ArrayRef<");
WriteType(elementType);
Write(">(");
WriteArrayRef(elementType);
WriteChar('(');
switch (elementType) {
case FuArrayStorageType:
Write("factory: { ");
Expand Down
27 changes: 13 additions & 14 deletions libfut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21652,17 +21652,22 @@ bool GenSwift::isArrayRef(const FuArrayStorageType * array)
return array->ptrTaken || dynamic_cast<const FuStorageType *>(array->getElementType().get());
}

void GenSwift::writeArrayRef(const FuType * elementType)
{
this->arrayRef = true;
write("ArrayRef<");
writeType(elementType);
writeChar('>');
}

void GenSwift::writeClassName(const FuClassType * klass)
{
switch (klass->class_->id) {
case FuId::stringClass:
write("String");
break;
case FuId::arrayPtrClass:
this->arrayRef = true;
write("ArrayRef<");
writeType(klass->getElementType().get());
writeChar('>');
writeArrayRef(klass->getElementType().get());
break;
case FuId::arrayStorageClass:
case FuId::listClass:
Expand Down Expand Up @@ -21738,12 +21743,8 @@ void GenSwift::writeType(const FuType * type)
else if (dynamic_cast<const FuEnum *>(type))
write(type->id == FuId::boolType ? "Bool" : type->name);
else if (const FuArrayStorageType *arrayStg = dynamic_cast<const FuArrayStorageType *>(type)) {
if (isArrayRef(arrayStg)) {
this->arrayRef = true;
write("ArrayRef<");
writeType(arrayStg->getElementType().get());
writeChar('>');
}
if (isArrayRef(arrayStg))
writeArrayRef(arrayStg->getElementType().get());
else {
writeChar('[');
writeType(arrayStg->getElementType().get());
Expand Down Expand Up @@ -22340,10 +22341,8 @@ void GenSwift::writeDefaultValue(const FuType * type)

void GenSwift::writeNewArray(const FuType * elementType, const FuExpr * lengthExpr, FuPriority parent)
{
this->arrayRef = true;
write("ArrayRef<");
writeType(elementType);
write(">(");
writeArrayRef(elementType);
writeChar('(');
if (dynamic_cast<const FuArrayStorageType *>(elementType)) {
write("factory: { ");
writeNewStorage(elementType);
Expand Down
27 changes: 13 additions & 14 deletions libfut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22412,17 +22412,22 @@ void OpenIndexing(FuExpr collection)

static bool IsArrayRef(FuArrayStorageType array) => array.PtrTaken || array.GetElementType() is FuStorageType;

void WriteArrayRef(FuType elementType)
{
this.ArrayRef = true;
Write("ArrayRef<");
WriteType(elementType);
WriteChar('>');
}

void WriteClassName(FuClassType klass)
{
switch (klass.Class.Id) {
case FuId.StringClass:
Write("String");
break;
case FuId.ArrayPtrClass:
this.ArrayRef = true;
Write("ArrayRef<");
WriteType(klass.GetElementType());
WriteChar('>');
WriteArrayRef(klass.GetElementType());
break;
case FuId.ArrayStorageClass:
case FuId.ListClass:
Expand Down Expand Up @@ -22500,12 +22505,8 @@ void WriteType(FuType type)
Write(type.Id == FuId.BoolType ? "Bool" : type.Name);
break;
case FuArrayStorageType arrayStg:
if (IsArrayRef(arrayStg)) {
this.ArrayRef = true;
Write("ArrayRef<");
WriteType(arrayStg.GetElementType());
WriteChar('>');
}
if (IsArrayRef(arrayStg))
WriteArrayRef(arrayStg.GetElementType());
else {
WriteChar('[');
WriteType(arrayStg.GetElementType());
Expand Down Expand Up @@ -23091,10 +23092,8 @@ void WriteDefaultValue(FuType type)

protected override void WriteNewArray(FuType elementType, FuExpr lengthExpr, FuPriority parent)
{
this.ArrayRef = true;
Write("ArrayRef<");
WriteType(elementType);
Write(">(");
WriteArrayRef(elementType);
WriteChar('(');
switch (elementType) {
case FuArrayStorageType:
Write("factory: { ");
Expand Down
1 change: 1 addition & 0 deletions libfut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2911,6 +2911,7 @@ class GenSwift : public GenPySwift
void writeCamelCaseNotKeyword(std::string_view name);
void openIndexing(const FuExpr * collection);
static bool isArrayRef(const FuArrayStorageType * array);
void writeArrayRef(const FuType * elementType);
void writeClassName(const FuClassType * klass);
void writeType(const FuType * type);
void writeUnwrapped(const FuExpr * expr, FuPriority parent, bool substringOk);
Expand Down
27 changes: 13 additions & 14 deletions libfut.js
Original file line number Diff line number Diff line change
Expand Up @@ -23042,17 +23042,22 @@ export class GenSwift extends GenPySwift
return array.ptrTaken || array.getElementType() instanceof FuStorageType;
}

#writeArrayRef(elementType)
{
this.#arrayRef = true;
this.write("ArrayRef<");
this.#writeType(elementType);
this.writeChar(62);
}

#writeClassName(klass)
{
switch (klass.class.id) {
case FuId.STRING_CLASS:
this.write("String");
break;
case FuId.ARRAY_PTR_CLASS:
this.#arrayRef = true;
this.write("ArrayRef<");
this.#writeType(klass.getElementType());
this.writeChar(62);
this.#writeArrayRef(klass.getElementType());
break;
case FuId.ARRAY_STORAGE_CLASS:
case FuId.LIST_CLASS:
Expand Down Expand Up @@ -23129,12 +23134,8 @@ export class GenSwift extends GenPySwift
this.write(type.id == FuId.BOOL_TYPE ? "Bool" : type.name);
else if (type instanceof FuArrayStorageType) {
const arrayStg = type;
if (GenSwift.#isArrayRef(arrayStg)) {
this.#arrayRef = true;
this.write("ArrayRef<");
this.#writeType(arrayStg.getElementType());
this.writeChar(62);
}
if (GenSwift.#isArrayRef(arrayStg))
this.#writeArrayRef(arrayStg.getElementType());
else {
this.writeChar(91);
this.#writeType(arrayStg.getElementType());
Expand Down Expand Up @@ -23727,10 +23728,8 @@ export class GenSwift extends GenPySwift

writeNewArray(elementType, lengthExpr, parent)
{
this.#arrayRef = true;
this.write("ArrayRef<");
this.#writeType(elementType);
this.write(">(");
this.#writeArrayRef(elementType);
this.writeChar(40);
if (elementType instanceof FuArrayStorageType) {
this.write("factory: { ");
this.writeNewStorage(elementType);
Expand Down

0 comments on commit 318ff24

Please sign in to comment.