diff options
author | Piotr Fusik <piotr@fusion-lang.org> | 2024-01-24 14:59:41 +0100 |
---|---|---|
committer | Piotr Fusik <piotr@fusion-lang.org> | 2024-01-24 14:59:41 +0100 |
commit | 76d2cdbc6b69c7ae0d06617a8f97c3e06afcf201 (patch) | |
tree | 38db1f466abfed607409732307d8792b99dfb870 | |
parent | eb47ff1fe40032f54da5b4d86da000fc384a35af (diff) |
[cleanup] Refactor moving WriteTemporaryName to GenBase.
-rw-r--r-- | GenBase.fu | 20 | ||||
-rw-r--r-- | GenC.fu | 6 | ||||
-rw-r--r-- | libfut.cpp | 24 | ||||
-rw-r--r-- | libfut.cs | 24 | ||||
-rw-r--r-- | libfut.hpp | 2 | ||||
-rw-r--r-- | libfut.js | 30 |
6 files changed, 45 insertions, 61 deletions
@@ -1,6 +1,6 @@ // GenBase.fu - base class for code generators // -// Copyright (C) 2011-2023 Piotr Fusik +// Copyright (C) 2011-2024 Piotr Fusik // // This file is part of Fusion Transpiler, // see https://github.com/fusionlanguage/fut @@ -870,6 +870,12 @@ public abstract class GenBase : FuVisitor OpenBlock(); } + protected void WriteTemporaryName!(int id) + { + Write("futemp"); + VisitLiteralLong(id); + } + protected void WriteResourceName!(string name) { foreach (int c in name) @@ -905,10 +911,8 @@ public abstract class GenBase : FuVisitor WriteNewArray(dynamic.GetElementType(), expr.Inner, parent); else if (expr.Inner is FuAggregateInitializer init) { int tempId = this.CurrentTemporaries.IndexOf(expr); - if (tempId >= 0) { - Write("futemp"); - VisitLiteralLong(tempId); - } + if (tempId >= 0) + WriteTemporaryName(tempId); else WriteNewWithFields(dynamic, init); } @@ -1424,15 +1428,13 @@ public abstract class GenBase : FuVisitor } else this.CurrentTemporaries[id] = expr; - Write("futemp"); - VisitLiteralLong(id); + WriteTemporaryName(id); Write(" = "); assert expr.Type is FuDynamicPtrType dynamic; WriteNew(dynamic, FuPriority.Argument); EndStatement(); foreach (FuExpr item in init.Items) { - Write("futemp"); - VisitLiteralLong(id); + WriteTemporaryName(id); WriteAggregateInitField(expr, item); } } @@ -140,12 +140,6 @@ public class GenC : GenCCpp StartDefinition(type, true, true); } - void WriteTemporaryName!(int id) - { - Write("futemp"); - VisitLiteralLong(id); - } - void WriteTemporaryOrExpr!(FuExpr expr, FuPriority parent) { int id = this.CurrentTemporaries.IndexOf(expr); @@ -7215,6 +7215,12 @@ void GenBase::openLoop(std::string_view intString, int nesting, int count) openBlock(); } +void GenBase::writeTemporaryName(int id) +{ + write("futemp"); + visitLiteralLong(id); +} + void GenBase::writeResourceName(std::string_view name) { for (int c : name) @@ -7251,10 +7257,8 @@ void GenBase::visitPrefixExpr(const FuPrefixExpr * expr, FuPriority parent) writeNewArray(dynamic->getElementType().get(), expr->inner.get(), parent); else if (const FuAggregateInitializer *init = dynamic_cast<const FuAggregateInitializer *>(expr->inner.get())) { int tempId = [](const std::vector<const FuExpr *> &v, const FuExpr * value) { auto i = std::find(v.begin(), v.end(), value); return i == v.end() ? -1 : i - v.begin(); }(this->currentTemporaries, expr); - if (tempId >= 0) { - write("futemp"); - visitLiteralLong(tempId); - } + if (tempId >= 0) + writeTemporaryName(tempId); else writeNewWithFields(dynamic, init); } @@ -7769,15 +7773,13 @@ void GenBase::defineObjectLiteralTemporary(const FuUnaryExpr * expr) } else this->currentTemporaries[id] = expr; - write("futemp"); - visitLiteralLong(id); + writeTemporaryName(id); write(" = "); const FuDynamicPtrType * dynamic = static_cast<const FuDynamicPtrType *>(expr->type.get()); writeNew(dynamic, FuPriority::argument); endStatement(); for (const std::shared_ptr<FuExpr> &item : init->items) { - write("futemp"); - visitLiteralLong(id); + writeTemporaryName(id); writeAggregateInitField(expr, item.get()); } } @@ -9084,12 +9086,6 @@ void GenC::startTemporaryVar(const FuType * type) startDefinition(type, true, true); } -void GenC::writeTemporaryName(int id) -{ - write("futemp"); - visitLiteralLong(id); -} - void GenC::writeTemporaryOrExpr(const FuExpr * expr, FuPriority parent) { int id = [](const std::vector<const FuExpr *> &v, const FuExpr * value) { auto i = std::find(v.begin(), v.end(), value); return i == v.end() ? -1 : i - v.begin(); }(this->currentTemporaries, expr); @@ -7407,6 +7407,12 @@ namespace Fusion OpenBlock(); } + protected void WriteTemporaryName(int id) + { + Write("futemp"); + VisitLiteralLong(id); + } + protected void WriteResourceName(string name) { foreach (int c in name) @@ -7441,10 +7447,8 @@ namespace Fusion WriteNewArray(dynamic.GetElementType(), expr.Inner, parent); else if (expr.Inner is FuAggregateInitializer init) { int tempId = this.CurrentTemporaries.IndexOf(expr); - if (tempId >= 0) { - Write("futemp"); - VisitLiteralLong(tempId); - } + if (tempId >= 0) + WriteTemporaryName(tempId); else WriteNewWithFields(dynamic, init); } @@ -7956,15 +7960,13 @@ namespace Fusion } else this.CurrentTemporaries[id] = expr; - Write("futemp"); - VisitLiteralLong(id); + WriteTemporaryName(id); Write(" = "); FuDynamicPtrType dynamic = (FuDynamicPtrType) expr.Type; WriteNew(dynamic, FuPriority.Argument); EndStatement(); foreach (FuExpr item in init.Items) { - Write("futemp"); - VisitLiteralLong(id); + WriteTemporaryName(id); WriteAggregateInitField(expr, item); } } @@ -9321,12 +9323,6 @@ namespace Fusion StartDefinition(type, true, true); } - void WriteTemporaryName(int id) - { - Write("futemp"); - VisitLiteralLong(id); - } - void WriteTemporaryOrExpr(FuExpr expr, FuPriority parent) { int id = this.CurrentTemporaries.IndexOf(expr); @@ -1789,6 +1789,7 @@ protected: virtual void defineIsVar(const FuBinaryExpr * binary); void writeArrayElement(const FuNamedValue * def, int nesting); void openLoop(std::string_view intString, int nesting, int count); + void writeTemporaryName(int id); void writeResourceName(std::string_view name); virtual void writeResource(std::string_view name, int length) = 0; bool isWholeArray(const FuExpr * array, const FuExpr * offset, const FuExpr * length) const; @@ -2122,7 +2123,6 @@ private: std::vector<const FuVar *> varsToDestruct; void writeStringPtrAddCast(const FuCallExpr * call); static bool isDictionaryClassStgIndexing(const FuExpr * expr); - void writeTemporaryName(int id); void writeTemporaryOrExpr(const FuExpr * expr, FuPriority parent); void writeUpcast(const FuClass * resultClass, const FuSymbol * klass); void writeClassPtr(const FuClass * resultClass, const FuExpr * expr, FuPriority parent); @@ -7698,6 +7698,12 @@ export class GenBase extends FuVisitor this.openBlock(); } + writeTemporaryName(id) + { + this.write("futemp"); + this.visitLiteralLong(BigInt(id)); + } + writeResourceName(name) { for (const c of name) @@ -7733,10 +7739,8 @@ export class GenBase extends FuVisitor let init; if ((init = expr.inner) instanceof FuAggregateInitializer) { let tempId = this.currentTemporaries.indexOf(expr); - if (tempId >= 0) { - this.write("futemp"); - this.visitLiteralLong(BigInt(tempId)); - } + if (tempId >= 0) + this.writeTemporaryName(tempId); else this.writeNewWithFields(dynamic, init); } @@ -8270,15 +8274,13 @@ export class GenBase extends FuVisitor } else this.currentTemporaries[id] = expr; - this.write("futemp"); - this.visitLiteralLong(BigInt(id)); + this.writeTemporaryName(id); this.write(" = "); let dynamic = expr.type; this.writeNew(dynamic, FuPriority.ARGUMENT); this.endStatement(); for (const item of init.items) { - this.write("futemp"); - this.visitLiteralLong(BigInt(id)); + this.writeTemporaryName(id); this.#writeAggregateInitField(expr, item); } } @@ -9658,17 +9660,11 @@ export class GenC extends GenCCpp this.#startDefinition(type, true, true); } - #writeTemporaryName(id) - { - this.write("futemp"); - this.visitLiteralLong(BigInt(id)); - } - #writeTemporaryOrExpr(expr, parent) { let id = this.currentTemporaries.indexOf(expr); if (id >= 0) - this.#writeTemporaryName(id); + this.writeTemporaryName(id); else expr.accept(this, parent); } @@ -10557,7 +10553,7 @@ export class GenC extends GenCCpp if (id < 0) { id = this.currentTemporaries.length; this.#startDefinition(type, false, true); - this.#writeTemporaryName(id); + this.writeTemporaryName(id); this.#endDefinition(type); if (assign) this.#writeAssignTemporary(type, expr); @@ -10565,7 +10561,7 @@ export class GenC extends GenCCpp this.currentTemporaries.push(expr); } else if (assign) { - this.#writeTemporaryName(id); + this.writeTemporaryName(id); this.#writeAssignTemporary(type, expr); this.writeCharLine(59); this.currentTemporaries[id] = expr; |