public static void DelLoopCounter(EngineState s) { const string rawCode = "Set,#c,NIL"; s.CompatOverridableLoopCounter = true; s.LoopStateStack.Push(new EngineLoopState(100)); EngineTests.Eval(s, rawCode, CodeType.Set, ErrorCheck.Warning); EngineLoopState loop = s.LoopStateStack.Pop(); Assert.AreEqual(100, loop.CounterIndex); Assert.AreEqual('\0', loop.CounterLetter); s.LoopStateStack.Push(new EngineLoopState('C')); EngineTests.Eval(s, rawCode, CodeType.Set, ErrorCheck.Warning); loop = s.LoopStateStack.Pop(); Assert.AreEqual(0, loop.CounterIndex); Assert.AreEqual('C', loop.CounterLetter); s.CompatOverridableLoopCounter = false; s.LoopStateStack.Push(new EngineLoopState(100)); EngineTests.Eval(s, rawCode, CodeType.Set, ErrorCheck.Warning); loop = s.LoopStateStack.Pop(); Assert.AreEqual(100, loop.CounterIndex); Assert.AreEqual('\0', loop.CounterLetter); s.LoopStateStack.Push(new EngineLoopState('C')); EngineTests.Eval(s, rawCode, CodeType.Set, ErrorCheck.Warning); loop = s.LoopStateStack.Pop(); Assert.AreEqual(0, loop.CounterIndex); Assert.AreEqual('C', loop.CounterLetter); }
public static void SetLoopCounter(EngineState s) { s.LoopStateStack.Clear(); // Simulate Loop command const string rawLoopCode = "Set,#c,110"; s.CompatOverridableLoopCounter = true; s.LoopStateStack.Push(new EngineLoopState(100)); EngineTests.Eval(s, rawLoopCode, CodeType.Set, ErrorCheck.Success); EngineLoopState loop = s.LoopStateStack.Pop(); Assert.AreEqual(110, loop.CounterIndex); s.CompatOverridableLoopCounter = false; s.LoopStateStack.Push(new EngineLoopState(100)); EngineTests.Eval(s, rawLoopCode, CodeType.Set, ErrorCheck.Warning); loop = s.LoopStateStack.Pop(); Assert.AreEqual(100, loop.CounterIndex); // Simulate LoopLetter command const string rawLoopLetterCode = "Set,#c,Z"; s.CompatOverridableLoopCounter = true; s.LoopStateStack.Push(new EngineLoopState('C')); EngineTests.Eval(s, rawLoopLetterCode, CodeType.Set, ErrorCheck.Success); loop = s.LoopStateStack.Pop(); Assert.AreEqual('Z', loop.CounterLetter); s.CompatOverridableLoopCounter = false; s.LoopStateStack.Push(new EngineLoopState('C')); EngineTests.Eval(s, rawLoopLetterCode, CodeType.Set, ErrorCheck.Warning); loop = s.LoopStateStack.Pop(); Assert.AreEqual('C', loop.CounterLetter); // Error s.LoopStateStack.Clear(); EngineTests.Eval(s, rawLoopCode, CodeType.Set, ErrorCheck.Warning); Assert.AreEqual(0, s.LoopStateStack.Count); s.LoopStateStack.Clear(); EngineTests.Eval(s, rawLoopCode, CodeType.Set, ErrorCheck.Warning); Assert.AreEqual(0, s.LoopStateStack.Count); s.LoopStateStack.Clear(); EngineTests.Eval(s, rawLoopLetterCode, CodeType.Set, ErrorCheck.Warning); Assert.AreEqual(0, s.LoopStateStack.Count); s.LoopStateStack.Clear(); EngineTests.Eval(s, rawLoopLetterCode, CodeType.Set, ErrorCheck.Warning); Assert.AreEqual(0, s.LoopStateStack.Count); }
/// <summary> /// Expand #1, #2, #3, etc... /// </summary> public static string ExpandSectionParams(EngineState s, string str) { // Expand #1 into its value Regex inRegex = new Regex(@"(?<!#)(#[1-9])", RegexOptions.Compiled | RegexOptions.CultureInvariant); MatchCollection matches = inRegex.Matches(str); while (0 < matches.Count) { StringBuilder b = new StringBuilder(); for (int x = 0; x < matches.Count; x++) { string pIdxStr = matches[x].Groups[1].ToString().Substring(1); if (!int.TryParse(pIdxStr, NumberStyles.Integer, CultureInfo.InvariantCulture, out int pIdx)) throw new InternalException("ExpandSectionParams failure"); if (x == 0) { b.Append(str.Substring(0, matches[0].Index)); } else { int startOffset = matches[x - 1].Index + matches[x - 1].Value.Length; int endOffset = matches[x].Index - startOffset; b.Append(str.Substring(startOffset, endOffset)); } string param; if (s.CurSectionInParams.ContainsKey(pIdx)) { param = s.CurSectionInParams[pIdx]; } else { if (s.PeekDepth == 1) // Dirty Hack for WB082 compatibility param = $"##{pIdx}"; // [Process] -> Should return #{pIdx} even it was not found else param = string.Empty; // Not in entry section -> return string.Empty; } b.Append(param); if (x + 1 == matches.Count) // Last iteration { b.Append(str.Substring(matches[x].Index + matches[x].Value.Length)); } } str = b.ToString(); matches = inRegex.Matches(str); } if (!s.CompatDisableExtendedSectionParams) { // Escape #o1, #o2, ... (Section Out Parameter) // string outParamRegex = s.CompatDisableExtendedSectionParams ? @"(?<!#)(#[oO][1-9])" : @"(?<!#)(#[oO][1-9][0-9]*)"; // Regex outRegex = new Regex(outParamRegex, RegexOptions.Compiled | RegexOptions.CultureInvariant); Regex outRegex = new Regex(@"(?<!#)(#[oO][1-9])", RegexOptions.Compiled | RegexOptions.CultureInvariant); matches = outRegex.Matches(str); while (0 < matches.Count) { StringBuilder b = new StringBuilder(); for (int x = 0; x < matches.Count; x++) { string pIdxStr = matches[x].Groups[1].ToString().Substring(2); if (!int.TryParse(pIdxStr, NumberStyles.Integer, CultureInfo.InvariantCulture, out int pIdx)) throw new InternalException("ExpandSectionParams failure"); if (x == 0) { b.Append(str.Substring(0, matches[0].Index)); } else { int startOffset = matches[x - 1].Index + matches[x - 1].Value.Length; int endOffset = matches[x].Index - startOffset; b.Append(str.Substring(startOffset, endOffset)); } string param; if (1 <= pIdx && pIdx <= s.CurSectionOutParams.Count) { string varKey = s.CurSectionOutParams[pIdx - 1]; param = s.Variables.Expand(varKey); } else { param = string.Empty; } b.Append(param); if (x + 1 == matches.Count) // Last iteration { b.Append(str.Substring(matches[x].Index + matches[x].Value.Length)); } } str = b.ToString(); matches = inRegex.Matches(str); } // Escape #a (Section In Params Count) if (str.IndexOf("#a", StringComparison.OrdinalIgnoreCase) != -1) str = StringHelper.ReplaceRegex(str, @"(?<!#)(#[aA])", s.CurSectionInParamsCount.ToString()); // Escape #oa (Section Out Params Count) if (str.IndexOf("#oa", StringComparison.OrdinalIgnoreCase) != -1) str = StringHelper.ReplaceRegex(str, @"(?<!#)(#[oO][aA])", s.CurSectionInParamsCount.ToString()); // Escape #r (Return Value) if (str.IndexOf("#r", StringComparison.OrdinalIgnoreCase) != -1) str = StringHelper.ReplaceRegex(str, @"(?<!#)(#[rR])", s.ReturnValue); } // Escape #c (Loop Counter) if (0 < s.LoopStateStack.Count) { EngineLoopState loop = s.LoopStateStack.Peek(); switch (loop.State) { case EngineLoopState.LoopState.OnIndex: str = StringHelper.ReplaceRegex(str, @"(?<!#)(#[cC])", loop.CounterIndex.ToString()); break; case EngineLoopState.LoopState.OnDriveLetter: str = StringHelper.ReplaceRegex(str, @"(?<!#)(#[cC])", loop.CounterLetter.ToString()); break; } } return str; }