Skip to content

Commit

Permalink
[const] Allow const arrays of same name within a method.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfusik committed May 23, 2024
1 parent feefdcf commit 694b5fe
Show file tree
Hide file tree
Showing 13 changed files with 146 additions and 69 deletions.
3 changes: 2 additions & 1 deletion AST.fu
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,7 @@ enum FuVisitStatus
public class FuConst : FuMember
{
internal FuMethodBase? InMethod;
internal int InMethodIndex = 0;
internal FuVisitStatus VisitStatus;
public override void AcceptStatement(FuVisitor! visitor) { visitor.VisitConst(this); }
public override bool IsStatic() => true;
Expand Down Expand Up @@ -1326,7 +1327,7 @@ public class FuClass : FuContainerType
internal bool HasSubclasses = false;
internal FuSymbolReference() BaseClass;
internal FuMethodBase#? Constructor;
internal List<FuConst>() ConstArrays;
internal List<FuConst!>() ConstArrays;
public bool HasBaseClass() => this.BaseClass.Name.Length > 0;
public bool AddsVirtualMethods()
{
Expand Down
13 changes: 13 additions & 0 deletions GenBase.fu
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,19 @@ public abstract class GenBase : FuVisitor
WriteNewLine();
}

protected void WriteUppercaseConstName!(FuConst konst)
{
if (konst.InMethod != null) {
WriteUppercaseWithUnderscores(konst.InMethod.Name);
WriteChar('_');
}
WriteUppercaseWithUnderscores(konst.Name);
if (konst.InMethodIndex > 0) {
WriteChar('_');
VisitLiteralLong(konst.InMethodIndex);
}
}

protected abstract void WriteName!(FuSymbol symbol);

protected virtual void WriteBanner!()
Expand Down
7 changes: 6 additions & 1 deletion GenCs.fu
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,13 @@ public class GenCs : GenTyped

protected override void WriteName!(FuSymbol symbol)
{
if (symbol is FuConst konst && konst.InMethod != null)
if (symbol is FuConst konst && konst.InMethod != null) {
Write(konst.InMethod.Name);
Write(symbol.Name);
if (konst.InMethodIndex > 0)
VisitLiteralLong(konst.InMethodIndex);
return;
}
Write(symbol.Name);
switch (symbol.Name) {
case "as":
Expand Down
6 changes: 1 addition & 5 deletions GenJava.fu
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,7 @@ public class GenJava : GenTyped
Write(symbol.Name);
break;
case FuConst konst:
if (konst.InMethod != null) {
WriteUppercaseWithUnderscores(konst.InMethod.Name);
WriteChar('_');
}
WriteUppercaseWithUnderscores(symbol.Name);
WriteUppercaseConstName(konst);
break;
case FuVar:
if (symbol.Parent is FuForeach forEach && forEach.Count() == 2) {
Expand Down
6 changes: 1 addition & 5 deletions GenJs.fu
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,7 @@ public class GenJsNoModule : GenBase
case FuConst konst:
if (konst.Visibility == FuVisibility.Private)
WriteChar('#');
if (konst.InMethod != null) {
WriteUppercaseWithUnderscores(konst.InMethod.Name);
WriteChar('_');
}
WriteUppercaseWithUnderscores(symbol.Name);
WriteUppercaseConstName(konst);
break;
case FuVar:
WriteCamelCaseNotKeyword(symbol.Name);
Expand Down
6 changes: 1 addition & 5 deletions GenPy.fu
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,7 @@ public class GenPy : GenPySwift
case FuConst konst:
if (konst.Visibility != FuVisibility.Public)
WriteChar('_');
if (konst.InMethod != null) {
WriteUppercaseWithUnderscores(konst.InMethod.Name);
WriteChar('_');
}
WriteUppercaseWithUnderscores(symbol.Name);
WriteUppercaseConstName(konst);
break;
case FuVar:
WriteNameNotKeyword(symbol.Name);
Expand Down
2 changes: 2 additions & 0 deletions GenSwift.fu
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,8 @@ public class GenSwift : GenPySwift
case FuConst konst when konst.InMethod != null:
WriteCamelCase(konst.InMethod.Name);
WritePascalCase(symbol.Name);
if (konst.InMethodIndex > 0)
VisitLiteralLong(konst.InMethodIndex);
break;
case FuVar:
case FuMember:
Expand Down
10 changes: 10 additions & 0 deletions Sema.fu
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,16 @@ public class FuSema
this.CurrentScope.Add(konst);
if (konst.Type is FuArrayStorageType) {
assert GetCurrentContainer() is FuClass! klass;
FuConst!? last = null;
foreach (FuConst! previous in klass.ConstArrays) {
if (previous.Name == konst.Name && previous.InMethod == konst.InMethod)
last = previous;
}
if (last != null) {
if (last.InMethodIndex == 0)
last.InMethodIndex = 1;
konst.InMethodIndex = last.InMethodIndex + 1;
}
klass.ConstArrays.Add(konst);
}
}
Expand Down
53 changes: 35 additions & 18 deletions libfut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6160,6 +6160,16 @@ bool FuSema::resolveStatements(const std::vector<std::shared_ptr<FuStatement>> *
this->currentScope->add(konst);
if (dynamic_cast<const FuArrayStorageType *>(konst->type.get())) {
FuClass * klass = static_cast<FuClass *>(getCurrentContainer());
FuConst * last = nullptr;
for (FuConst * previous : klass->constArrays) {
if (previous->name == konst->name && previous->inMethod == konst->inMethod)
last = previous;
}
if (last != nullptr) {
if (last->inMethodIndex == 0)
last->inMethodIndex = 1;
konst->inMethodIndex = last->inMethodIndex + 1;
}
klass->constArrays.push_back(konst.get());
}
}
Expand Down Expand Up @@ -6976,6 +6986,19 @@ void GenBase::writeLine(std::string_view s)
writeNewLine();
}

void GenBase::writeUppercaseConstName(const FuConst * konst)
{
if (konst->inMethod != nullptr) {
writeUppercaseWithUnderscores(konst->inMethod->name);
writeChar('_');
}
writeUppercaseWithUnderscores(konst->name);
if (konst->inMethodIndex > 0) {
writeChar('_');
visitLiteralLong(konst->inMethodIndex);
}
}

void GenBase::writeBanner()
{
writeLine("// Generated automatically with \"fut\". Do not edit.");
Expand Down Expand Up @@ -15419,8 +15442,13 @@ void GenCs::writeDoc(const FuCodeDoc * doc)
void GenCs::writeName(const FuSymbol * symbol)
{
const FuConst * konst;
if ((konst = dynamic_cast<const FuConst *>(symbol)) && konst->inMethod != nullptr)
if ((konst = dynamic_cast<const FuConst *>(symbol)) && konst->inMethod != nullptr) {
write(konst->inMethod->name);
write(symbol->name);
if (konst->inMethodIndex > 0)
visitLiteralLong(konst->inMethodIndex);
return;
}
write(symbol->name);
if (symbol->name == "as" || symbol->name == "await" || symbol->name == "catch" || symbol->name == "char" || symbol->name == "checked" || symbol->name == "decimal" || symbol->name == "delegate" || symbol->name == "event" || symbol->name == "explicit" || symbol->name == "extern" || symbol->name == "finally" || symbol->name == "fixed" || symbol->name == "goto" || symbol->name == "implicit" || symbol->name == "interface" || symbol->name == "is" || symbol->name == "lock" || symbol->name == "namespace" || symbol->name == "object" || symbol->name == "operator" || symbol->name == "out" || symbol->name == "params" || symbol->name == "private" || symbol->name == "readonly" || symbol->name == "ref" || symbol->name == "sbyte" || symbol->name == "sizeof" || symbol->name == "stackalloc" || symbol->name == "struct" || symbol->name == "try" || symbol->name == "typeof" || symbol->name == "ulong" || symbol->name == "unchecked" || symbol->name == "unsafe" || symbol->name == "using" || symbol->name == "volatile")
writeChar('_');
Expand Down Expand Up @@ -17851,13 +17879,8 @@ void GenJava::writeName(const FuSymbol * symbol)
{
if (dynamic_cast<const FuContainerType *>(symbol))
write(symbol->name);
else if (const FuConst *konst = dynamic_cast<const FuConst *>(symbol)) {
if (konst->inMethod != nullptr) {
writeUppercaseWithUnderscores(konst->inMethod->name);
writeChar('_');
}
writeUppercaseWithUnderscores(symbol->name);
}
else if (const FuConst *konst = dynamic_cast<const FuConst *>(symbol))
writeUppercaseConstName(konst);
else if (dynamic_cast<const FuVar *>(symbol)) {
const FuForeach * forEach;
if ((forEach = dynamic_cast<const FuForeach *>(symbol->parent)) && forEach->count() == 2) {
Expand Down Expand Up @@ -19162,11 +19185,7 @@ void GenJsNoModule::writeName(const FuSymbol * symbol)
else if (const FuConst *konst = dynamic_cast<const FuConst *>(symbol)) {
if (konst->visibility == FuVisibility::private_)
writeChar('#');
if (konst->inMethod != nullptr) {
writeUppercaseWithUnderscores(konst->inMethod->name);
writeChar('_');
}
writeUppercaseWithUnderscores(symbol->name);
writeUppercaseConstName(konst);
}
else if (dynamic_cast<const FuVar *>(symbol))
writeCamelCaseNotKeyword(symbol->name);
Expand Down Expand Up @@ -21230,6 +21249,8 @@ void GenSwift::writeName(const FuSymbol * symbol)
else if ((konst = dynamic_cast<const FuConst *>(symbol)) && konst->inMethod != nullptr) {
writeCamelCase(konst->inMethod->name);
writePascalCase(symbol->name);
if (konst->inMethodIndex > 0)
visitLiteralLong(konst->inMethodIndex);
}
else if (dynamic_cast<const FuVar *>(symbol) || dynamic_cast<const FuMember *>(symbol))
writeCamelCaseNotKeyword(symbol->name);
Expand Down Expand Up @@ -23078,11 +23099,7 @@ void GenPy::writeName(const FuSymbol * symbol)
else if (const FuConst *konst = dynamic_cast<const FuConst *>(symbol)) {
if (konst->visibility != FuVisibility::public_)
writeChar('_');
if (konst->inMethod != nullptr) {
writeUppercaseWithUnderscores(konst->inMethod->name);
writeChar('_');
}
writeUppercaseWithUnderscores(symbol->name);
writeUppercaseConstName(konst);
}
else if (dynamic_cast<const FuVar *>(symbol))
writeNameNotKeyword(symbol->name);
Expand Down
52 changes: 36 additions & 16 deletions libfut.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2735,6 +2735,8 @@ public class FuConst : FuMember

internal FuMethodBase InMethod;

internal int InMethodIndex = 0;

internal FuVisitStatus VisitStatus;

public override void AcceptStatement(FuVisitor visitor)
Expand Down Expand Up @@ -6259,6 +6261,16 @@ bool ResolveStatements(List<FuStatement> statements)
this.CurrentScope.Add(konst);
if (konst.Type is FuArrayStorageType) {
FuClass klass = (FuClass) GetCurrentContainer();
FuConst last = null;
foreach (FuConst previous in klass.ConstArrays) {
if (previous.Name == konst.Name && previous.InMethod == konst.InMethod)
last = previous;
}
if (last != null) {
if (last.InMethodIndex == 0)
last.InMethodIndex = 1;
konst.InMethodIndex = last.InMethodIndex + 1;
}
klass.ConstArrays.Add(konst);
}
}
Expand Down Expand Up @@ -7117,6 +7129,19 @@ protected void WriteLine(string s)
WriteNewLine();
}

protected void WriteUppercaseConstName(FuConst konst)
{
if (konst.InMethod != null) {
WriteUppercaseWithUnderscores(konst.InMethod.Name);
WriteChar('_');
}
WriteUppercaseWithUnderscores(konst.Name);
if (konst.InMethodIndex > 0) {
WriteChar('_');
VisitLiteralLong(konst.InMethodIndex);
}
}

protected abstract void WriteName(FuSymbol symbol);

protected virtual void WriteBanner()
Expand Down Expand Up @@ -15778,8 +15803,13 @@ protected override void WriteDoc(FuCodeDoc doc)

protected override void WriteName(FuSymbol symbol)
{
if (symbol is FuConst konst && konst.InMethod != null)
if (symbol is FuConst konst && konst.InMethod != null) {
Write(konst.InMethod.Name);
Write(symbol.Name);
if (konst.InMethodIndex > 0)
VisitLiteralLong(konst.InMethodIndex);
return;
}
Write(symbol.Name);
switch (symbol.Name) {
case "as":
Expand Down Expand Up @@ -18481,11 +18511,7 @@ protected override void WriteName(FuSymbol symbol)
Write(symbol.Name);
break;
case FuConst konst:
if (konst.InMethod != null) {
WriteUppercaseWithUnderscores(konst.InMethod.Name);
WriteChar('_');
}
WriteUppercaseWithUnderscores(symbol.Name);
WriteUppercaseConstName(konst);
break;
case FuVar:
if (symbol.Parent is FuForeach forEach && forEach.Count() == 2) {
Expand Down Expand Up @@ -19797,11 +19823,7 @@ protected override void WriteName(FuSymbol symbol)
case FuConst konst:
if (konst.Visibility == FuVisibility.Private)
WriteChar('#');
if (konst.InMethod != null) {
WriteUppercaseWithUnderscores(konst.InMethod.Name);
WriteChar('_');
}
WriteUppercaseWithUnderscores(symbol.Name);
WriteUppercaseConstName(konst);
break;
case FuVar:
WriteCamelCaseNotKeyword(symbol.Name);
Expand Down Expand Up @@ -21999,6 +22021,8 @@ protected override void WriteName(FuSymbol symbol)
case FuConst konst when konst.InMethod != null:
WriteCamelCase(konst.InMethod.Name);
WritePascalCase(symbol.Name);
if (konst.InMethodIndex > 0)
VisitLiteralLong(konst.InMethodIndex);
break;
case FuVar:
case FuMember:
Expand Down Expand Up @@ -23932,11 +23956,7 @@ protected override void WriteName(FuSymbol symbol)
case FuConst konst:
if (konst.Visibility != FuVisibility.Public)
WriteChar('_');
if (konst.InMethod != null) {
WriteUppercaseWithUnderscores(konst.InMethod.Name);
WriteChar('_');
}
WriteUppercaseWithUnderscores(symbol.Name);
WriteUppercaseConstName(konst);
break;
case FuVar:
WriteNameNotKeyword(symbol.Name);
Expand Down
4 changes: 3 additions & 1 deletion libfut.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,7 @@ class FuConst : public FuMember
bool isStatic() const override;
public:
const FuMethodBase * inMethod;
int inMethodIndex = 0;
FuVisitStatus visitStatus;
};

Expand Down Expand Up @@ -1399,7 +1400,7 @@ class FuClass : public FuContainerType
bool hasSubclasses = false;
FuSymbolReference baseClass;
std::shared_ptr<FuMethodBase> constructor;
std::vector<const FuConst *> constArrays;
std::vector<FuConst *> constArrays;
};

class FuClassType : public FuType
Expand Down Expand Up @@ -1813,6 +1814,7 @@ class GenBase : public FuVisitor
void writeNewLine();
void writeCharLine(int c);
void writeLine(std::string_view s);
void writeUppercaseConstName(const FuConst * konst);
virtual void writeName(const FuSymbol * symbol) = 0;
virtual void writeBanner();
void createFile(std::string_view directory, std::string_view filename);
Expand Down
Loading

0 comments on commit 694b5fe

Please sign in to comment.