Skip to content

Commit c893fe9

Browse files
committed
fix: Allow tracing string variables with "$" in the name
1 parent a5e708c commit c893fe9

2 files changed

Lines changed: 22 additions & 31 deletions

File tree

ZXBStudio/BuildSystem/ZXBasicMap.cs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public ZXBasicMap(ZXCodeFile MainFile, IEnumerable<ZXCodeFile> AllFiles, string
179179
ParseInputParameters(funcMatch.Groups[5].Value, currentFunction.InputParameters);
180180

181181
if (funcMatch.Groups[7].Success)
182-
currentFunction.ReturnType = StorageFromString(funcMatch.Groups[5].Value, currentFunction.Name);
182+
currentFunction.ReturnType = StorageFromString(funcMatch.Groups[7].Value, currentFunction.Name);
183183
else
184184
currentFunction.ReturnType = ZXVariableStorage.F;
185185

@@ -202,8 +202,7 @@ public ZXBasicMap(ZXCodeFile MainFile, IEnumerable<ZXCodeFile> AllFiles, string
202202
if (varNameDef.Contains("(")) //array
203203
{
204204
string varName = varNameDef.Substring(0, varNameDef.IndexOf("(")).Trim();
205-
206-
if (!jointLines.Skip(buc + 1).Any(l => Regex.IsMatch(l, $"(^|[^a-zA-Z0-9_]){varName}($|[^a-zA-Z0-9_])", RegexOptions.Multiline)))
205+
if (!jointLines.Skip(buc + 1).Any(l => Regex.IsMatch(l, $"(^|[^a-zA-Z0-9_$]){Regex.Escape(varName)}($|[^a-zA-Z0-9_$])", RegexOptions.Multiline)))
207206
continue;
208207

209208
string[] dims = varNameDef.Substring(varNameDef.IndexOf("(") + 1).Replace(")", "").Split(",", StringSplitOptions.RemoveEmptyEntries);
@@ -219,8 +218,7 @@ public ZXBasicMap(ZXCodeFile MainFile, IEnumerable<ZXCodeFile> AllFiles, string
219218
foreach (var vName in varNames)
220219
{
221220
string varName = vName.Trim();
222-
223-
if (!jointLines.Skip(buc + 1).Any(l => Regex.IsMatch(l, $"(^|[^a-zA-Z0-9_]){varName}($|[^a-zA-Z0-9_])", RegexOptions.Multiline)))
221+
if (!jointLines.Skip(buc + 1).Any(l => Regex.IsMatch(l, $"(^|[^a-zA-Z0-9_$]){Regex.Escape(varName)}($|[^a-zA-Z0-9_$])", RegexOptions.Multiline)))
224222
continue;
225223

226224
var storage = StorageFromString(dimMatch.Groups[5].Value, varName);
@@ -291,15 +289,15 @@ public ZXBasicMap(ZXCodeFile MainFile, IEnumerable<ZXCodeFile> AllFiles, string
291289
//Search for the var in the sub/function that the location points to
292290
if (location.LocationType == ZXBasicLocationType.Sub)
293291
{
294-
var sub = subs.Where(s => s.Name == location.Name).FirstOrDefault();
292+
var sub = subs.FirstOrDefault(s => string.Equals(s.Name, location.Name, StringComparison.OrdinalIgnoreCase));
295293
if(sub != null)
296-
foundVar = sub.LocalVariables.Where(v => v.Name == varName).FirstOrDefault();
294+
foundVar = sub.LocalVariables.FirstOrDefault(v => string.Equals(v.Name, varName, StringComparison.OrdinalIgnoreCase));
297295
}
298296
else
299297
{
300-
var func = functions.Where(f => f.Name == location.Name).FirstOrDefault();
298+
var func = functions.FirstOrDefault(f => string.Equals(f.Name, location.Name, StringComparison.OrdinalIgnoreCase));
301299
if (func != null)
302-
foundVar = func.LocalVariables.Where(v => v.Name == varName).FirstOrDefault();
300+
foundVar = func.LocalVariables.FirstOrDefault(v => string.Equals(v.Name, varName, StringComparison.OrdinalIgnoreCase));
303301
}
304302
}
305303

@@ -322,15 +320,15 @@ public ZXBasicMap(ZXCodeFile MainFile, IEnumerable<ZXCodeFile> AllFiles, string
322320
//(to avoid the very unprobable case where the same var is defined in different files in locations that match the same range)
323321
if (possibleLocation.LocationType == ZXBasicLocationType.Sub)
324322
{
325-
var sub = subs.Where(s => s.Name == possibleLocation.Name).FirstOrDefault();
323+
var sub = subs.FirstOrDefault(s => string.Equals(s.Name, possibleLocation.Name, StringComparison.OrdinalIgnoreCase));
326324
if (sub != null)
327-
foundVar = sub.LocalVariables.Where(v => v.Name == varName && !v.Unused).FirstOrDefault();
325+
foundVar = sub.LocalVariables.FirstOrDefault(v => string.Equals(v.Name, varName, StringComparison.OrdinalIgnoreCase) && !v.Unused);
328326
}
329327
else
330328
{
331-
var func = functions.Where(f => f.Name == possibleLocation.Name).FirstOrDefault();
329+
var func = functions.FirstOrDefault(f => string.Equals(f.Name, possibleLocation.Name, StringComparison.OrdinalIgnoreCase));
332330
if (func != null)
333-
foundVar = func.LocalVariables.Where(v => v.Name == varName && !v.Unused).FirstOrDefault();
331+
foundVar = func.LocalVariables.FirstOrDefault(v => string.Equals(v.Name, varName, StringComparison.OrdinalIgnoreCase) && !v.Unused);
334332
}
335333

336334
//If the criteria finds a var, return it
@@ -359,11 +357,7 @@ void GetSubVars(ZXBasicSub Sub, string[] Lines)
359357
if (varNameDef.Contains("(")) //array
360358
{
361359
string varName = varNameDef.Substring(0, varNameDef.IndexOf("(")).Trim();
362-
363-
//Ignore unused vars (vars that are found only on its dim line, there may be the improbable
364-
//case where a var is defined and used in the same line using a colon and not used
365-
//anywhere else, but that would be an awful code :) )
366-
if (!Lines.Skip(buc+1).Any(l => Regex.IsMatch(l, $"(^|[^a-zA-Z0-9_]){varName}($|[^a-zA-Z0-9_])", RegexOptions.Multiline)))
360+
if (!Lines.Skip(buc+1).Any(l => Regex.IsMatch(l, $"(^|[^a-zA-Z0-9_$]){Regex.Escape(varName)}($|[^a-zA-Z0-9_$])", RegexOptions.Multiline)))
367361
continue;
368362

369363
string[] dims = varNameDef.Substring(varNameDef.IndexOf("(") + 1).Replace(")", "").Split(",", StringSplitOptions.RemoveEmptyEntries);
@@ -379,9 +373,7 @@ void GetSubVars(ZXBasicSub Sub, string[] Lines)
379373
foreach (var vName in varNames)
380374
{
381375
string varName = vName.Trim();
382-
383-
//Ignore unused vars
384-
if (!Lines.Skip(buc+1).Any(l => Regex.IsMatch(l, $"(^|[^a-zA-Z0-9_]){varName}($|[^a-zA-Z0-9_])", RegexOptions.Multiline)))
376+
if (!Lines.Skip(buc+1).Any(l => Regex.IsMatch(l, $"(^|[^a-zA-Z0-9_$]){Regex.Escape(varName)}($|[^a-zA-Z0-9_$])", RegexOptions.Multiline)))
385377
continue;
386378

387379
var storage = StorageFromString(dimMatch.Groups[5].Value, varName);
@@ -415,15 +407,15 @@ public List<ZXBasicLocation> GetBuildLocations(ZXCodeFile CodeFile)
415407

416408
if (subMatch != null && subMatch.Success)
417409
{
418-
loc = new ZXBasicLocation { Name = subMatch.Groups[2].Value.Trim(), LocationType = ZXBasicLocationType.Sub, FirstLine = buc, File = Path.Combine(CodeFile.Directory, CodeFile.TempFileName) };
410+
loc = new ZXBasicLocation { Name = subMatch.Groups[4].Value.Trim(), LocationType = ZXBasicLocationType.Sub, FirstLine = buc, File = Path.Combine(CodeFile.Directory, CodeFile.TempFileName) };
419411
continue;
420412
}
421413

422414
var funcMatch = regFunc.Match(line);
423415

424416
if (funcMatch != null && funcMatch.Success)
425417
{
426-
loc = new ZXBasicLocation { Name = funcMatch.Groups[2].Value.Trim(), LocationType = ZXBasicLocationType.Function, FirstLine = buc, File = Path.Combine(CodeFile.Directory, CodeFile.TempFileName) };
418+
loc = new ZXBasicLocation { Name = funcMatch.Groups[4].Value.Trim(), LocationType = ZXBasicLocationType.Function, FirstLine = buc, File = Path.Combine(CodeFile.Directory, CodeFile.TempFileName) };
427419
continue;
428420
}
429421
}
@@ -465,7 +457,7 @@ public bool ContainsBuildDim(ZXCodeFile CodeFile, string VarName, int LineNumber
465457
if (LineNumber >= lines.Length)
466458
return false;
467459

468-
return Regex.IsMatch(lines[LineNumber], $"(\\s|,){VarName}(\\s|,|\\(|$)", RegexOptions.Multiline);
460+
return Regex.IsMatch(lines[LineNumber], $"(\\s|,){Regex.Escape(VarName)}(\\s|,|\\(|$)", RegexOptions.Multiline);
469461
}
470462

471463
private static void ParseInputParameters(string ParameterString, List<ZXBasicParameter> Storage)

ZXBStudio/BuildSystem/ZXVariableMap.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ private void ProcessGlobalVariables(string icContent, string mapContent, ZXBasic
6060
string varName = m.Groups[1].Value;
6161
string bVarName = varName.Substring(1);
6262

63-
var basicVar = BasicMap.GlobalVariables.FirstOrDefault(v => v.Name == bVarName);
63+
var basicVar = BasicMap.GlobalVariables.FirstOrDefault(v => string.Equals(v.Name.TrimEnd('$'), bVarName.TrimEnd('$'), StringComparison.OrdinalIgnoreCase));
6464

6565
if (basicVar == null)
6666
continue;
@@ -78,7 +78,7 @@ private void ProcessGlobalVariables(string icContent, string mapContent, ZXBasic
7878

7979
ZXVariable newVar = new ZXVariable
8080
{
81-
Name = bVarName,
81+
Name = basicVar.Name,
8282
Address = new ZXVariableAddress { AddressType = ZXVariableAddressType.Absolute, AddressValue = addr },
8383
Scope = ZXVariableScope.GlobalScope,
8484
VariableType = ZXVariableType.Flat,
@@ -98,7 +98,7 @@ private void ProcessGlobalVariables(string icContent, string mapContent, ZXBasic
9898

9999
string bVarName = varName.Substring(1);
100100

101-
var basicVar = BasicMap.GlobalVariables.FirstOrDefault(v => v.Name == bVarName);
101+
var basicVar = BasicMap.GlobalVariables.FirstOrDefault(v => string.Equals(v.Name.TrimEnd('$'), bVarName.TrimEnd('$'), StringComparison.OrdinalIgnoreCase));
102102

103103
if (basicVar == null)
104104
continue;
@@ -133,7 +133,7 @@ private void ProcessGlobalVariables(string icContent, string mapContent, ZXBasic
133133

134134
ZXVariable newVar = new ZXVariable
135135
{
136-
Name = bVarName,
136+
Name = basicVar.Name,
137137
Address = new ZXVariableAddress { AddressType = ZXVariableAddressType.Absolute, AddressValue = addr },
138138
Scope = ZXVariableScope.GlobalScope,
139139
VariableType = ZXVariableType.Array,
@@ -179,10 +179,9 @@ private void ProcessLocalVariables(string icContent, string mapContent, ZXBasicM
179179

180180
ZXVariableScope currentScope = new ZXVariableScope { ScopeName = locName, StartAddress = startAddr, EndAddress = endAddr };
181181

182-
ZXBasicSub? sub = BasicMap.Subs.Where(m => m.Name == locName).FirstOrDefault();
183-
182+
ZXBasicSub? sub = BasicMap.Subs.FirstOrDefault(m => string.Equals(m.Name.TrimEnd('$'), locName.TrimEnd('$'), StringComparison.OrdinalIgnoreCase));
184183
if (sub == null)
185-
sub = BasicMap.Functions.Where(m => m.Name == locName).FirstOrDefault();
184+
sub = BasicMap.Functions.FirstOrDefault(m => string.Equals(m.Name.TrimEnd('$'), locName.TrimEnd('$'), StringComparison.OrdinalIgnoreCase));
186185

187186
//Function params
188187
if (sub != null)

0 commit comments

Comments
 (0)