Jsm.LabeledStack.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. using System;
  2. using System.Collections.Generic;
  3. namespace FF8
  4. {
  5. public static partial class Jsm
  6. {
  7. public sealed class LabeledStack : IStack<IJsmExpression>
  8. {
  9. private readonly Stack<IJsmExpression> _stack = new Stack<IJsmExpression>();
  10. private readonly Dictionary<IJsmExpression, Int32> _positions = new Dictionary<IJsmExpression, Int32>();
  11. public Int32 Count => _stack.Count;
  12. public Int32 CurrentLabel { get; set; }
  13. public void Push(IJsmExpression item)
  14. {
  15. _positions.Add(item, CurrentLabel);
  16. _stack.Push(item);
  17. }
  18. public IJsmExpression Peek()
  19. {
  20. return _stack.Peek();
  21. }
  22. public IJsmExpression Pop()
  23. {
  24. IJsmExpression result = _stack.Pop();
  25. CurrentLabel = _positions[result];
  26. _positions.Remove(result);
  27. return result;
  28. }
  29. }
  30. }
  31. }