Skip to content

Commit

Permalink
Support different way of adding member functions
Browse files Browse the repository at this point in the history
Different version of CW did the right thing this time where they add
member functions as class/struct members, rather than making them global
and not even adding access level attributes to it.

Though when I say "the right thing" take this with a heavy grain of
salt, as apparently it was at the expense of parameter names. Though
perhaps this game was compiled with different debug parameters?
  • Loading branch information
RA-Kooi committed Dec 21, 2021
1 parent a5b3f76 commit d3fec31
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 2 deletions.
5 changes: 5 additions & 0 deletions DwarfOne2C/CWriter/CWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ private static List<string> TagDispatcher(
case TagType.Enum:
code.AddRange(GenerateEnum(allTags, IDToIndex, current, depth));
break;
case TagType.MemberFunc:
{
code.AddRange(
GenerateMemberFunction(allTags, IDToIndex, current, depth));
} break;
}

return code;
Expand Down
29 changes: 29 additions & 0 deletions DwarfOne2C/CWriter/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,34 @@ private static List<string> GenerateFunction(

return code;
}

private static List<string> GenerateMemberFunction(
List<Tag> allTags,
Dictionary<int, int> IDToIndex,
Tag current,
int depth)
{
List<string> code = new();

string tabs = new('\t', depth);

if(current.comment != null)
code.Add(tabs + "// " + current.comment);

Tag referenced = allTags[IDToIndex[current.typeID]];

// Reforge the tag to generate properly.
referenced.tagType = TagType.GlobalFunc;
referenced.name = current.name;

code.AddRange(
GenerateFunction(
allTags,
IDToIndex,
referenced,
depth));

return code;
}
}
}
1 change: 1 addition & 0 deletions DwarfOne2C/Parsing/BlockParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ private Tag ParseFunction(
&& !ParseName(line, tag)
&& !ParseTypes(line, tag)
&& !ParseLoUser(line, tag)
&& !ParseAccessLevel(line, tag)
&& !ParseAtMember(line, tag))
Console.WriteLine("Unknown attribute: " + line + " @" + current);
}
Expand Down
17 changes: 15 additions & 2 deletions DwarfOne2C/Parsing/CompilationUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,6 @@ public void SecondPass()
}
}

// Fixup TAG_padding being member variables, global variables, and
// global functions at the same time...
void Recurse(Tag parent, int depth)
{
if(parent.firstChild == -1)
Expand All @@ -263,6 +261,8 @@ void Recurse(Tag parent, int depth)
child.sibling != Tag.NoSibling;
child = allTags[IDToIndex[child.sibling]])
{
// Fixup TAG_padding being member variables, global variables,
// and global functions at the same time...
if(child.tagType == TagType.Padding)
{
if(depth == 0)
Expand All @@ -280,6 +280,19 @@ void Recurse(Tag parent, int depth)
break;
}
}
else if(child.tagType == TagType.GlobalFunc)
{
if(depth > 0)
{
// Remove staticness from functions that are
// not top level
child.isStatic = false;

// It is also a class/struct member function
// in this case
child.tagType = TagType.MemberFunc;
}
}

if(child.tagType == TagType.Class
|| child.tagType == TagType.Struct)
Expand Down
1 change: 1 addition & 0 deletions DwarfOne2C/Tags.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public enum TagType
TypeDef, // In reverse order
Union,
Padding, // Local variable OR member...
MemberFunc,
}

public TagType tagType;
Expand Down

0 comments on commit d3fec31

Please sign in to comment.