Skip to content

Commit

Permalink
[cleanup] Refactor moving WriteTemporaryName to GenBase.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfusik committed Jan 24, 2024
1 parent eb47ff1 commit 76d2cdb
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 61 deletions.
20 changes: 11 additions & 9 deletions GenBase.fu
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
Expand Down
6 changes: 0 additions & 6 deletions GenC.fu
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
24 changes: 10 additions & 14 deletions libfut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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());
}
}
Expand Down Expand Up @@ -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);
Expand Down
24 changes: 10 additions & 14 deletions libfut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7407,6 +7407,12 @@ protected void OpenLoop(string intString, int nesting, int count)
OpenBlock();
}

protected void WriteTemporaryName(int id)
{
Write("futemp");
VisitLiteralLong(id);
}

protected void WriteResourceName(string name)
{
foreach (int c in name)
Expand Down Expand Up @@ -7441,10 +7447,8 @@ internal override void VisitPrefixExpr(FuPrefixExpr expr, FuPriority parent)
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);
}
Expand Down Expand Up @@ -7956,15 +7960,13 @@ protected virtual void DefineObjectLiteralTemporary(FuUnaryExpr expr)
}
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);
}
}
Expand Down Expand Up @@ -9321,12 +9323,6 @@ protected override void StartTemporaryVar(FuType type)
StartDefinition(type, true, true);
}

void WriteTemporaryName(int id)
{
Write("futemp");
VisitLiteralLong(id);
}

void WriteTemporaryOrExpr(FuExpr expr, FuPriority parent)
{
int id = this.CurrentTemporaries.IndexOf(expr);
Expand Down
2 changes: 1 addition & 1 deletion libfut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1789,6 +1789,7 @@ class GenBase : public FuVisitor
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;
Expand Down Expand Up @@ -2122,7 +2123,6 @@ class GenC : public GenCCpp
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);
Expand Down
30 changes: 13 additions & 17 deletions libfut.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
}
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -10557,15 +10553,15 @@ 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);
this.writeCharLine(59);
this.currentTemporaries.push(expr);
}
else if (assign) {
this.#writeTemporaryName(id);
this.writeTemporaryName(id);
this.#writeAssignTemporary(type, expr);
this.writeCharLine(59);
this.currentTemporaries[id] = expr;
Expand Down

0 comments on commit 76d2cdb

Please sign in to comment.