Code cleanup

This commit is contained in:
Stone_Red
2026-03-05 22:55:11 +01:00
parent 276ae421e8
commit 789f56ba95
35 changed files with 100 additions and 80 deletions
@@ -78,4 +78,3 @@ public class CodeFlowStatementsTests
YesNtAssert.ContainsTerminationMessage(lines, "Function \"nowhere\" not found"); YesNtAssert.ContainsTerminationMessage(lines, "Function \"nowhere\" not found");
} }
} }
-1
View File
@@ -286,4 +286,3 @@ public class CodeFlowTests
YesNtAssert.ContainsTerminationMessage(lines, "No matching while found"); YesNtAssert.ContainsTerminationMessage(lines, "No matching while found");
} }
} }
@@ -107,4 +107,3 @@ public class ConsoleStatementsTests
StringAssert.Contains(output.ToString(), "Terminated by external process"); StringAssert.Contains(output.ToString(), "Terminated by external process");
} }
} }
@@ -224,4 +224,3 @@ public class FunctionStatementsTests
YesNtAssert.IsLastLineEqual(lines, "outer_val"); YesNtAssert.IsLastLineEqual(lines, "outer_val");
} }
} }
@@ -199,4 +199,3 @@ public class ProcessingStatementsTests
YesNtAssert.IsLastLineEqual(lines, "2", timeout: 3000); YesNtAssert.IsLastLineEqual(lines, "2", timeout: 3000);
} }
} }
@@ -46,4 +46,3 @@ public class SystemStatementsTests
YesNtAssert.ContainsTerminationMessage(lines, "Failed to start \"does_not_exist_abc_xyz\""); YesNtAssert.ContainsTerminationMessage(lines, "Failed to start \"does_not_exist_abc_xyz\"");
} }
} }
@@ -129,4 +129,3 @@ public class VariableStatementsTests
YesNtAssert.ContainsTerminationMessage(lines, "Variable \"missing\" not found"); YesNtAssert.ContainsTerminationMessage(lines, "Variable \"missing\" not found");
} }
} }
+1 -3
View File
@@ -1,6 +1,4 @@
using System.Collections.Generic; namespace YesNt.Interpreter.Runtime;
namespace YesNt.Interpreter.Runtime;
/// <summary> /// <summary>
/// Represents a single source line together with its location metadata. /// Represents a single source line together with its location metadata.
@@ -1,4 +1,5 @@
using System; using System;
using YesNt.Interpreter.Attributes; using YesNt.Interpreter.Attributes;
namespace YesNt.Interpreter.Runtime; namespace YesNt.Interpreter.Runtime;
@@ -432,7 +432,7 @@ public class YesNtInterpreter
lineMatchingHandlers = new List<List<StatementHandler>>(runtimeInfo.Lines.Count); lineMatchingHandlers = new List<List<StatementHandler>>(runtimeInfo.Lines.Count);
// Dictionary to track open blocks by their expected end statement name // Dictionary to track open blocks by their expected end statement name
var openBlocks = new Dictionary<string, Stack<int>>(); Dictionary<string, Stack<int>> openBlocks = [];
for (int i = 0; i < runtimeInfo.Lines.Count; i++) for (int i = 0; i < runtimeInfo.Lines.Count; i++)
{ {
@@ -449,7 +449,7 @@ public class YesNtInterpreter
string blockPair = handler.Attribute.BlockPair; string blockPair = handler.Attribute.BlockPair;
if (!string.IsNullOrEmpty(blockPair) && !handler.Attribute.IsBlockIntermediate) if (!string.IsNullOrEmpty(blockPair) && !handler.Attribute.IsBlockIntermediate)
{ {
if (!openBlocks.TryGetValue(blockPair, out var stack)) if (!openBlocks.TryGetValue(blockPair, out Stack<int> stack))
{ {
stack = new Stack<int>(); stack = new Stack<int>();
openBlocks[blockPair] = stack; openBlocks[blockPair] = stack;
@@ -460,7 +460,7 @@ public class YesNtInterpreter
// Track block ends // Track block ends
if (handler.Attribute.IsBlockEnd) if (handler.Attribute.IsBlockEnd)
{ {
if (openBlocks.TryGetValue(handler.Attribute.Name, out var stack) && stack.Count > 0) if (openBlocks.TryGetValue(handler.Attribute.Name, out Stack<int> stack) && stack.Count > 0)
{ {
int startLine = stack.Pop(); int startLine = stack.Pop();
runtimeInfo.BlockBoundaries[startLine] = i; runtimeInfo.BlockBoundaries[startLine] = i;
@@ -474,7 +474,7 @@ public class YesNtInterpreter
string intermediatePair = handler.Attribute.BlockPair; string intermediatePair = handler.Attribute.BlockPair;
if (!string.IsNullOrEmpty(intermediatePair)) if (!string.IsNullOrEmpty(intermediatePair))
{ {
if (!openBlocks.TryGetValue(intermediatePair, out var stack)) if (!openBlocks.TryGetValue(intermediatePair, out Stack<int> stack))
{ {
stack = new Stack<int>(); stack = new Stack<int>();
openBlocks[intermediatePair] = stack; openBlocks[intermediatePair] = stack;
@@ -1,5 +1,4 @@
using System; using System;
using System.Runtime.InteropServices;
using YesNt.Interpreter.Attributes; using YesNt.Interpreter.Attributes;
using YesNt.Interpreter.Enums; using YesNt.Interpreter.Enums;
@@ -1,4 +1,3 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using YesNt.Interpreter.Attributes; using YesNt.Interpreter.Attributes;
@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
using YesNt.Interpreter.Runtime; using YesNt.Interpreter.Runtime;
namespace YesNt.Interpreter.Utilities; namespace YesNt.Interpreter.Utilities;
@@ -16,33 +17,39 @@ internal static class TemplateProcessor
/// </summary> /// </summary>
public static string ProcessVariables(string input, RuntimeInformation runtimeInfo) public static string ProcessVariables(string input, RuntimeInformation runtimeInfo)
{ {
if (string.IsNullOrEmpty(input)) return input; if (string.IsNullOrEmpty(input))
{
return input;
}
int startIdx = input.IndexOf("${", StringComparison.Ordinal); int startIdx = input.IndexOf("${", StringComparison.Ordinal);
if (startIdx == -1) return input; if (startIdx == -1)
{
return input;
}
StringBuilder sb = new StringBuilder(input.Length); StringBuilder sb = new StringBuilder(input.Length);
int lastIdx = 0; int lastIdx = 0;
while (startIdx != -1) while (startIdx != -1)
{ {
sb.Append(input, lastIdx, startIdx - lastIdx); _ = sb.Append(input, lastIdx, startIdx - lastIdx);
int endIdx = input.IndexOf('}', startIdx + 2); int endIdx = input.IndexOf('}', startIdx + 2);
if (endIdx == -1) if (endIdx == -1)
{ {
sb.Append("${"); _ = sb.Append("${");
lastIdx = startIdx + 2; lastIdx = startIdx + 2;
} }
else else
{ {
string varName = input.Substring(startIdx + 2, endIdx - (startIdx + 2)); string varName = input[(startIdx + 2)..endIdx];
if (runtimeInfo.Variables.TryGetValue(varName, out string value)) if (runtimeInfo.Variables.TryGetValue(varName, out string value))
{ {
sb.Append(value); _ = sb.Append(value);
} }
else if (runtimeInfo.GlobalVariables.TryGetValue(varName, out value)) else if (runtimeInfo.GlobalVariables.TryGetValue(varName, out value))
{ {
sb.Append(value); _ = sb.Append(value);
} }
else if (!runtimeInfo.IsSearching) else if (!runtimeInfo.IsSearching)
{ {
@@ -51,9 +58,9 @@ internal static class TemplateProcessor
} }
else else
{ {
sb.Append("${"); _ = sb.Append("${");
sb.Append(varName); _ = sb.Append(varName);
sb.Append('}'); _ = sb.Append('}');
} }
lastIdx = endIdx + 1; lastIdx = endIdx + 1;
@@ -62,7 +69,7 @@ internal static class TemplateProcessor
startIdx = input.IndexOf("${", lastIdx, StringComparison.Ordinal); startIdx = input.IndexOf("${", lastIdx, StringComparison.Ordinal);
} }
sb.Append(input, lastIdx, input.Length - lastIdx); _ = sb.Append(input, lastIdx, input.Length - lastIdx);
return sb.ToString(); return sb.ToString();
} }
@@ -71,10 +78,16 @@ internal static class TemplateProcessor
/// </summary> /// </summary>
public static string ProcessStackParameters(string input, string placeholder, Stack<string> stack, RuntimeInformation runtimeInfo, string emptyStackMessage) public static string ProcessStackParameters(string input, string placeholder, Stack<string> stack, RuntimeInformation runtimeInfo, string emptyStackMessage)
{ {
if (string.IsNullOrEmpty(input)) return input; if (string.IsNullOrEmpty(input))
{
return input;
}
int startIdx = input.IndexOf(placeholder, StringComparison.Ordinal); int startIdx = input.IndexOf(placeholder, StringComparison.Ordinal);
if (startIdx == -1) return input; if (startIdx == -1)
{
return input;
}
StringBuilder sb = new StringBuilder(input.Length); StringBuilder sb = new StringBuilder(input.Length);
int lastIdx = 0; int lastIdx = 0;
@@ -82,18 +95,18 @@ internal static class TemplateProcessor
while (startIdx != -1) while (startIdx != -1)
{ {
sb.Append(input, lastIdx, startIdx - lastIdx); _ = sb.Append(input, lastIdx, startIdx - lastIdx);
if (stack.Count == 0) if (stack.Count == 0)
{ {
runtimeInfo.Exit(emptyStackMessage, true); runtimeInfo.Exit(emptyStackMessage, true);
return input; return input;
} }
sb.Append(stack.Pop()); _ = sb.Append(stack.Pop());
lastIdx = startIdx + placeholderLen; lastIdx = startIdx + placeholderLen;
startIdx = input.IndexOf(placeholder, lastIdx, StringComparison.Ordinal); startIdx = input.IndexOf(placeholder, lastIdx, StringComparison.Ordinal);
} }
sb.Append(input, lastIdx, input.Length - lastIdx); _ = sb.Append(input, lastIdx, input.Length - lastIdx);
return sb.ToString(); return sb.ToString();
} }
@@ -102,9 +115,7 @@ internal static class TemplateProcessor
/// </summary> /// </summary>
public static string ProcessSimplePlaceholders(string input, string placeholder, string value) public static string ProcessSimplePlaceholders(string input, string placeholder, string value)
{ {
if (string.IsNullOrEmpty(input)) return input; return string.IsNullOrEmpty(input) ? input : input.Replace(placeholder, value, StringComparison.Ordinal);
return input.Replace(placeholder, value, StringComparison.Ordinal);
} }
/// <summary> /// <summary>
@@ -112,10 +123,16 @@ internal static class TemplateProcessor
/// </summary> /// </summary>
public static string ProcessDynamicPlaceholders(string input, string placeholder, Func<string> valueProvider) public static string ProcessDynamicPlaceholders(string input, string placeholder, Func<string> valueProvider)
{ {
if (string.IsNullOrEmpty(input)) return input; if (string.IsNullOrEmpty(input))
{
return input;
}
int startIdx = input.IndexOf(placeholder, StringComparison.Ordinal); int startIdx = input.IndexOf(placeholder, StringComparison.Ordinal);
if (startIdx == -1) return input; if (startIdx == -1)
{
return input;
}
StringBuilder sb = new StringBuilder(input.Length); StringBuilder sb = new StringBuilder(input.Length);
int lastIdx = 0; int lastIdx = 0;
@@ -123,13 +140,13 @@ internal static class TemplateProcessor
while (startIdx != -1) while (startIdx != -1)
{ {
sb.Append(input, lastIdx, startIdx - lastIdx); _ = sb.Append(input, lastIdx, startIdx - lastIdx);
sb.Append(valueProvider()); _ = sb.Append(valueProvider());
lastIdx = startIdx + placeholderLen; lastIdx = startIdx + placeholderLen;
startIdx = input.IndexOf(placeholder, lastIdx, StringComparison.Ordinal); startIdx = input.IndexOf(placeholder, lastIdx, StringComparison.Ordinal);
} }
sb.Append(input, lastIdx, input.Length - lastIdx); _ = sb.Append(input, lastIdx, input.Length - lastIdx);
return sb.ToString(); return sb.ToString();
} }
@@ -138,10 +155,16 @@ internal static class TemplateProcessor
/// </summary> /// </summary>
public static string ProcessCalculations(string input, RuntimeInformation runtimeInfo, Regex calculationRegex) public static string ProcessCalculations(string input, RuntimeInformation runtimeInfo, Regex calculationRegex)
{ {
if (string.IsNullOrEmpty(input)) return input; if (string.IsNullOrEmpty(input))
{
return input;
}
MatchCollection matches = calculationRegex.Matches(input); MatchCollection matches = calculationRegex.Matches(input);
if (matches.Count == 0) return input; if (matches.Count == 0)
{
return input;
}
StringBuilder sb = new StringBuilder(input.Length); StringBuilder sb = new StringBuilder(input.Length);
int lastIdx = 0; int lastIdx = 0;
@@ -149,7 +172,7 @@ internal static class TemplateProcessor
for (int i = 0; i < matches.Count; i++) for (int i = 0; i < matches.Count; i++)
{ {
Match match = matches[i]; Match match = matches[i];
sb.Append(input, lastIdx, match.Index - lastIdx); _ = sb.Append(input, lastIdx, match.Index - lastIdx);
string res = Evaluator.Calculate(match.Value); string res = Evaluator.Calculate(match.Value);
if (res is null) if (res is null)
@@ -157,11 +180,11 @@ internal static class TemplateProcessor
runtimeInfo.Exit(ExitMessages.InvalidOperation, true); runtimeInfo.Exit(ExitMessages.InvalidOperation, true);
return input; return input;
} }
sb.Append(res); _ = sb.Append(res);
lastIdx = match.Index + match.Length; lastIdx = match.Index + match.Length;
} }
sb.Append(input, lastIdx, input.Length - lastIdx); _ = sb.Append(input, lastIdx, input.Length - lastIdx);
return sb.ToString(); return sb.ToString();
} }
} }
@@ -47,6 +47,13 @@
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup>
<PackageReference Include="SonarAnalyzer.CSharp" Version="10.20.0.135146">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\YesNt.Interpreter.Generator\YesNt.Interpreter.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" /> <ProjectReference Include="..\YesNt.Interpreter.Generator\YesNt.Interpreter.Generator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
</ItemGroup> </ItemGroup>