ForLoopStatement.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using MoonSharp.Interpreter.Execution;
  6. using MoonSharp.Interpreter.Execution.VM;
  7. using MoonSharp.Interpreter.Grammar;
  8. using MoonSharp.Interpreter.Tree.Expressions;
  9. namespace MoonSharp.Interpreter.Tree.Statements
  10. {
  11. class ForLoopStatement : Statement
  12. {
  13. //for' NAME '=' exp ',' exp (',' exp)? 'do' block 'end'
  14. RuntimeScopeFrame m_StackFrame;
  15. Statement m_InnerBlock;
  16. LRef m_VarName;
  17. Expression m_Start, m_End, m_Step;
  18. public ForLoopStatement(LuaParser.Stat_forloopContext context, ScriptLoadingContext lcontext)
  19. : base(context, lcontext)
  20. {
  21. var exps = context.exp();
  22. m_Start = NodeFactory.CreateExpression(exps[0], lcontext);
  23. m_End = NodeFactory.CreateExpression(exps[1], lcontext);
  24. if (exps.Length > 2)
  25. m_Step = NodeFactory.CreateExpression(exps[2], lcontext);
  26. else
  27. m_Step = new LiteralExpression(context, lcontext, new RValue(1));
  28. lcontext.Scope.PushBlock();
  29. m_VarName = lcontext.Scope.DefineLocal(context.NAME().GetText());
  30. m_InnerBlock = NodeFactory.CreateStatement(context.block(), lcontext);
  31. m_StackFrame = lcontext.Scope.Pop();
  32. }
  33. public override void Compile(Chunk bc)
  34. {
  35. Loop L = new Loop()
  36. {
  37. Scope = m_StackFrame
  38. };
  39. bc.LoopTracker.Loops.Push(L);
  40. m_End.Compile(bc);
  41. bc.ToNum();
  42. m_Step.Compile(bc);
  43. bc.ToNum();
  44. m_Start.Compile(bc);
  45. bc.ToNum();
  46. int start = bc.GetJumpPointForNextInstruction();
  47. var jumpend = bc.Jump(OpCode.JFor, -1);
  48. bc.Enter(m_StackFrame);
  49. bc.SymStorN(m_VarName);
  50. m_InnerBlock.Compile(bc);
  51. bc.Debug("..end");
  52. bc.Leave(m_StackFrame);
  53. bc.Incr(1);
  54. bc.Jump(OpCode.Jump, start);
  55. int exitpoint = bc.GetJumpPointForNextInstruction();
  56. foreach (Instruction i in L.BreakJumps)
  57. i.NumVal = exitpoint;
  58. jumpend.NumVal = exitpoint;
  59. bc.Pop(3);
  60. }
  61. }
  62. }