SymbolRefExpression.cs 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Antlr4.Runtime.Tree;
  6. using MoonSharp.Interpreter.Execution;
  7. using MoonSharp.Interpreter.Grammar;
  8. namespace MoonSharp.Interpreter.Tree.Expressions
  9. {
  10. class SymbolRefExpression : Expression, IVariable
  11. {
  12. SymbolRef m_Ref;
  13. string m_VarName;
  14. public SymbolRefExpression(Token T, ScriptLoadingContext lcontext)
  15. : base(lcontext)
  16. {
  17. m_VarName = T.Text;
  18. if (T.Type == TokenType.VarArgs)
  19. {
  20. m_Ref = lcontext.Scope.TryDefineLocal(WellKnownSymbols.VARARGS);
  21. if (!lcontext.Scope.CurrentFunctionHasVarArgs())
  22. throw new SyntaxErrorException("error:0: cannot use '...' outside a vararg function");
  23. if (lcontext.IsDynamicExpression)
  24. throw new DynamicExpressionException("Cannot use '...' in a dynamic expression.");
  25. }
  26. else
  27. {
  28. if (!lcontext.IsDynamicExpression)
  29. m_Ref = lcontext.Scope.Find(m_VarName);
  30. }
  31. }
  32. public SymbolRefExpression(ScriptLoadingContext lcontext, SymbolRef refr)
  33. : base(lcontext)
  34. {
  35. m_Ref = refr;
  36. if (lcontext.IsDynamicExpression)
  37. {
  38. throw new DynamicExpressionException("Unsupported symbol reference expression detected.");
  39. }
  40. }
  41. public SymbolRefExpression(IParseTree context, ScriptLoadingContext lcontext, SymbolRef refr)
  42. : base(context, lcontext)
  43. {
  44. m_Ref = refr;
  45. if (lcontext.IsDynamicExpression)
  46. {
  47. throw new DynamicExpressionException("Unsupported symbol reference expression detected.");
  48. }
  49. }
  50. public SymbolRefExpression(LuaParser.VarargContext context, ScriptLoadingContext lcontext)
  51. : base(context, lcontext)
  52. {
  53. m_Ref = lcontext.Scope.TryDefineLocal(WellKnownSymbols.VARARGS);
  54. if (!lcontext.Scope.CurrentFunctionHasVarArgs())
  55. {
  56. throw new SyntaxErrorException("error:0: cannot use '...' outside a vararg function");
  57. }
  58. if (lcontext.IsDynamicExpression)
  59. {
  60. throw new DynamicExpressionException("Cannot use '...' in a dynamic expression.");
  61. }
  62. }
  63. public SymbolRefExpression(ITerminalNode terminalNode, ScriptLoadingContext lcontext)
  64. : base(terminalNode, lcontext)
  65. {
  66. m_VarName = terminalNode.GetText();
  67. if (!lcontext.IsDynamicExpression)
  68. {
  69. m_Ref = lcontext.Scope.Find(m_VarName);
  70. }
  71. }
  72. public override void Compile(Execution.VM.ByteCode bc)
  73. {
  74. bc.Emit_Load(m_Ref);
  75. }
  76. public void CompileAssignment(Execution.VM.ByteCode bc, int stackofs, int tupleidx)
  77. {
  78. bc.Emit_Store(m_Ref, stackofs, tupleidx);
  79. }
  80. public override DynValue Eval(ScriptExecutionContext context)
  81. {
  82. return context.EvaluateSymbolByName(m_VarName);
  83. }
  84. public override SymbolRef FindDynamic(ScriptExecutionContext context)
  85. {
  86. return context.FindSymbolByName(m_VarName);
  87. }
  88. }
  89. }