VariableParser.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using NTERA.EmuEra.Game.EraEmu.GameData.Expression;
  2. using NTERA.EmuEra.Game.EraEmu.Sub;
  3. namespace NTERA.EmuEra.Game.EraEmu.GameData.Variable
  4. {
  5. internal static class VariableParser
  6. {
  7. public static void Initialize()
  8. {
  9. ZeroTerm = new SingleTerm(0);
  10. IOperandTerm[] zeroArgs = { ZeroTerm };
  11. TARGET = new VariableTerm(GlobalStatic.VariableData.GetSystemVariableToken("TARGET"), zeroArgs);
  12. }
  13. public static SingleTerm ZeroTerm { get; private set; }
  14. public static VariableTerm TARGET { get; private set; }
  15. public static bool IsVariable(string ids)
  16. {
  17. if (string.IsNullOrEmpty(ids))
  18. return false;
  19. string[] idlist = ids.Split(':');
  20. //idlist = synonym.ApplySynonym(idlist);
  21. VariableToken id = GlobalStatic.IdentifierDictionary.GetVariableToken(idlist[0], null, false);
  22. return id != null;
  23. }
  24. ///// <summary>
  25. ///// まだ最初の識別子を読んでいない状態から決め打ちで変数を解読する
  26. ///// </summary>
  27. ///// <param name="st"></param>
  28. ///// <returns></returns>
  29. //public static VariableTerm ReduceVariable(WordCollection wc)
  30. //{
  31. // IdentifierWord id = wc.Current as IdentifierWord;
  32. // if (id == null)
  33. // return null;
  34. // wc.ShiftNext();
  35. // VariableToken vid = ExpressionParser.ReduceVariableIdentifier(wc, id.Code);
  36. // if (vid == null)
  37. // throw new CodeEE("\"" + id.Code + "\" cannot be interpreted");
  38. // return ReduceVariable(vid, wc);
  39. //}
  40. /// <summary>
  41. /// 識別子を読み終えた状態からの解析
  42. /// </summary>
  43. /// <param name="st"></param>
  44. /// <returns></returns>
  45. public static VariableTerm ReduceVariable(VariableToken id, WordCollection wc)
  46. {
  47. IOperandTerm operand = null;
  48. IOperandTerm op1 = null;
  49. IOperandTerm op2 = null;
  50. IOperandTerm op3 = null;
  51. int i = 0;
  52. while (true)
  53. {
  54. if (wc.Current.Type != ':')
  55. break;
  56. if (i >= 3)
  57. throw new CodeEE(id.Code + " Too many arguments");
  58. wc.ShiftNext();
  59. operand = ExpressionParser.ReduceVariableArgument(wc, id.Code);
  60. if (i == 0)
  61. op1 = operand;
  62. else if (i == 1)
  63. op2 = operand;
  64. else if (i == 2)
  65. op3 = operand;
  66. i++;
  67. }
  68. return ReduceVariable(id, op1, op2, op3);
  69. }
  70. public static VariableTerm ReduceVariable(VariableToken id, IOperandTerm p1, IOperandTerm p2, IOperandTerm p3)
  71. {
  72. IOperandTerm[] terms = null;
  73. IOperandTerm op1 = p1;
  74. IOperandTerm op2 = p2;
  75. IOperandTerm op3 = p3;
  76. //引数の推測
  77. if (id.IsCharacterData)
  78. {
  79. if (id.IsArray2D)
  80. {
  81. if ((op1 == null) && (op2 == null) && (op3 == null))
  82. return new VariableNoArgTerm(id);
  83. if ((op1 == null) || (op2 == null) || (op3 == null))
  84. throw new CodeEE("You cannot omit arguments for character two-dimensional array variable " + id.Name + "");
  85. terms = new IOperandTerm[3];
  86. terms[0] = op1;
  87. terms[1] = op2;
  88. terms[2] = op3;
  89. }
  90. else if (id.IsArray1D)
  91. {
  92. if (op3 != null)
  93. throw new CodeEE("Character variable " + id.Name + " has too many arguments");
  94. if ((op1 == null) && (op2 == null) && (op3 == null) && Config.Config.SystemNoTarget)
  95. return new VariableNoArgTerm(id);
  96. if (op2 == null)
  97. {
  98. if (Config.Config.SystemNoTarget)
  99. throw new CodeEE("You cannot omit arguments for character two-dimensional array variable " + id.Name + " (Prohibited by current config)");
  100. if (op1 == null)
  101. op2 = ZeroTerm;
  102. else
  103. op2 = op1;
  104. op1 = TARGET;
  105. }
  106. terms = new IOperandTerm[2];
  107. terms[0] = op1;
  108. terms[1] = op2;
  109. }
  110. else
  111. {
  112. if (op2 != null)
  113. throw new CodeEE("Character variable " + id.Name + " has too many arguments");
  114. if ((op1 == null) && (op2 == null) && (op3 == null) && Config.Config.SystemNoTarget)
  115. return new VariableNoArgTerm(id);
  116. if (op1 == null)
  117. {
  118. if (Config.Config.SystemNoTarget)
  119. throw new CodeEE("Character variable " + id.Name + " cannot have omitted arguments (Prohibited by current config)");
  120. op1 = TARGET;
  121. }
  122. terms = new IOperandTerm[1];
  123. terms[0] = op1;
  124. }
  125. }
  126. else if (id.IsArray3D)
  127. {
  128. if ((op1 == null) && (op2 == null) && (op3 == null))
  129. return new VariableNoArgTerm(id);
  130. if ((op1 == null) || (op2 == null) || (op3 == null))
  131. throw new CodeEE("Three-dimensional array variable " + id.Name + " cannot have omitted arguments");
  132. terms = new IOperandTerm[3];
  133. terms[0] = op1;
  134. terms[1] = op2;
  135. terms[2] = op3;
  136. }
  137. else if (id.IsArray2D)
  138. {
  139. if ((op1 == null) && (op2 == null) && (op3 == null))
  140. return new VariableNoArgTerm(id);
  141. if ((op1 == null) || (op2 == null))
  142. throw new CodeEE("Two-dimensional array variable " + id.Name + " cannot have omitted arguments");
  143. if (op3 != null)
  144. throw new CodeEE("Two-dimensional array " + id.Name + " has too many arguments");
  145. terms = new IOperandTerm[2];
  146. terms[0] = op1;
  147. terms[1] = op2;
  148. }
  149. else if (id.IsArray1D)
  150. {
  151. if (op2 != null)
  152. throw new CodeEE("One-dimensional array variable " + id.Name + " has too many arguments");
  153. if (op1 == null)
  154. {
  155. op1 = ZeroTerm;
  156. if (!Config.Config.CompatiRAND && id.Code == VariableCode.RAND)
  157. {
  158. throw new CodeEE("RAND has an omitted argument");
  159. }
  160. }
  161. if (!Config.Config.CompatiRAND && op1 is SingleTerm && id.Code == VariableCode.RAND)
  162. {
  163. if (((SingleTerm)op1).Int == 0)
  164. throw new CodeEE("RAND has 0 as an argument");
  165. }
  166. terms = new IOperandTerm[1];
  167. terms[0] = op1;
  168. }
  169. else if (op1 != null)
  170. {
  171. throw new CodeEE("Variable that is not an array " + id.Name + " is being called with an argument");
  172. }
  173. else
  174. terms = new IOperandTerm[0];
  175. for (int i = 0; i < terms.Length; i++)
  176. if (terms[i].IsString)
  177. terms[i] = new VariableStrArgTerm(id.Code, terms[i], i);
  178. return new VariableTerm(id, terms);
  179. }
  180. //public static string ErrorMes = null;
  181. //public static void ResetError()
  182. //{
  183. // ErrorMes = null;
  184. //}
  185. }
  186. }