Skip to content

Commit

Permalink
Fix arrays generating with the wrong type/modifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
RA-Kooi committed Dec 31, 2021
1 parent 0ada041 commit b319c2c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
3 changes: 1 addition & 2 deletions DwarfOne2C/CWriter/Types.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private static (string part1, string part2) GetArray(
if(current.typeID > 0)
{
Tag referenced = allTags[IDToIndex[current.typeID]];
(type, part2) = GetType(allTags, IDToIndex, current);
(type, part2) = GetType(allTags, IDToIndex, referenced);
type += GetModifiers(allTags, IDToIndex, current);
}
else
Expand Down Expand Up @@ -108,7 +108,6 @@ private static (string part1, string part2) GetArray(
? ""
: "" + (current.length + 1));
}


return (type, part2);
}
Expand Down
34 changes: 21 additions & 13 deletions DwarfOne2C/Parsing/BlockParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,28 @@ private Tag ParseArray(
if(line.StartsWith("AT_subscr_data"))
{
Regex regex;
int count = 0;
string searchFull = @"(?>AT_subscr_data\(<\d+>";

int dimensionCount = 0;

if(line.Contains("FT_long["))
{
string searchStr = @"FT_long\[0:(\d+)\], ";
count = Regex.Matches(line, searchStr).Count;
for (int i = count; i > 0; --i)
{
dimensionCount = Regex.Matches(line, searchStr).Count;

for (int i = dimensionCount; i > 0; --i)
searchFull += searchStr;
}

regex = new(searchFull + @"FMT_ET: (.*)\))");
}
else if(line.Contains("FT_integer["))
{
string searchStr = @"FT_integer\[0:(\d+)\], ";
count = Regex.Matches(line, searchStr).Count;
for (int i = count; i > 0; --i)
{
dimensionCount = Regex.Matches(line, searchStr).Count;

for (int i = dimensionCount; i > 0; --i)
searchFull += searchStr;
}

regex = new(searchFull + @"FMT_ET: (.*)\))");
}
else
Expand All @@ -124,26 +126,32 @@ private Tag ParseArray(
}

Match match = regex.Match(line);

if (match.Success)
{
GroupCollection groups = match.Groups;
tag.isMultidimArray = count > 1;
string typeRef;

GroupCollection groups = match.Groups;

tag.isMultidimArray = dimensionCount > 1;

if (tag.isMultidimArray)
{
for (int i = 0; i < count; ++i)
for (int i = 0; i < dimensionCount; ++i)
{
uint length = Convert.ToUInt32(groups[1 + i].Value);
tag.arrayDimLengths.Add(unchecked((int)length));
}
typeRef = groups[1 + count].Value;

typeRef = groups[1 + dimensionCount].Value;
}
else
{
uint length = Convert.ToUInt32(groups[1].Value);
tag.length = unchecked((int)length);
typeRef = groups[2].Value;
}

ParseTypes(typeRef, tag);
}
else
Expand Down
12 changes: 8 additions & 4 deletions DwarfOne2C/Parsing/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,26 @@ private static void ParseModifiers(string line, int index, Tag tag)
if(line[index] != 'M')
break;

if(index + pointerLen <= line.Length && line.Substring(index, pointerLen) == "MOD_pointer_to")
if(index + pointerLen <= line.Length
&& line.Substring(index, pointerLen) == "MOD_pointer_to")
{
tag.modifiers.Add(Type.Modifier.Pointer);
index += pointerLen + 1;
}
else if(index + refLen <= line.Length && line.Substring(index, refLen) == "MOD_reference_to")
else if(index + refLen <= line.Length
&& line.Substring(index, refLen) == "MOD_reference_to")
{
tag.modifiers.Add(Type.Modifier.Reference);
index += refLen + 1;
}
else if(index + constLen <= line.Length && line.Substring(index, constLen) == "MOD_const")
else if(index + constLen <= line.Length
&& line.Substring(index, constLen) == "MOD_const")
{
tag.modifiers.Add(Type.Modifier.Const);
index += constLen + 1;
}
else if(index + volatileLen <= line.Length && line.Substring(index, volatileLen) == "MOD_volatile")
else if(index + volatileLen <= line.Length
&& line.Substring(index, volatileLen) == "MOD_volatile")
{
tag.modifiers.Add(Type.Modifier.Volatile);
index += volatileLen + 1;
Expand Down

0 comments on commit b319c2c

Please sign in to comment.