Creator.Method.cs 115 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Drawing.Text;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using Microsoft.VisualBasic;
  9. using MinorShift.Emuera.GameData.Expression;
  10. using MinorShift.Emuera.GameData.Variable;
  11. using MinorShift.Emuera.Sub;
  12. using MinorShift._Library;
  13. using NTERA.Game.Display;
  14. using NTERA.Interop;
  15. namespace MinorShift.Emuera.GameData.Function
  16. {
  17. internal static partial class FunctionMethodCreator
  18. {
  19. #region CSVデータ関係
  20. private sealed class GetcharaMethod : FunctionMethod
  21. {
  22. public GetcharaMethod()
  23. {
  24. ReturnType = typeof(Int64);
  25. argumentTypeArray = null;
  26. CanRestructure = false;
  27. }
  28. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  29. {
  30. //通常2つ、1つ省略可能で1~2の引数が必要。
  31. if (arguments.Length < 1)
  32. return name + "関数には少なくとも1つの引数が必要です";
  33. if (arguments.Length > 2)
  34. return name + "関数の引数が多すぎます";
  35. if (arguments[0] == null)
  36. return name + "関数の1番目の引数は省略できません";
  37. if (arguments[0].GetOperandType() != typeof(Int64))
  38. return name + "関数の1番目の引数の型が正しくありません";
  39. //2は省略可能
  40. if ((arguments.Length == 2) && (arguments[1] != null) && (arguments[1].GetOperandType() != typeof(Int64)))
  41. return name + "関数の2番目の引数の型が正しくありません";
  42. return null;
  43. }
  44. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  45. {
  46. Int64 integer = arguments[0].GetIntValue(exm);
  47. Int64 chara = -1L;
  48. if (!Config.CompatiSPChara)
  49. {
  50. //if ((arguments.Length > 1) && (arguments[1] != null) && (arguments[1].GetIntValue(exm) != 0))
  51. return exm.VEvaluator.GetChara(integer);
  52. }
  53. //以下互換性用の旧処理
  54. bool CheckSp = false;
  55. if ((arguments.Length > 1) && (arguments[1] != null) && (arguments[1].GetIntValue(exm) != 0))
  56. CheckSp = true;
  57. if (CheckSp)
  58. {
  59. chara = exm.VEvaluator.GetChara_UseSp(integer, false);
  60. if (chara != -1)
  61. return chara;
  62. return exm.VEvaluator.GetChara_UseSp(integer, true);
  63. }
  64. return exm.VEvaluator.GetChara_UseSp(integer, false);
  65. }
  66. }
  67. private sealed class GetspcharaMethod : FunctionMethod
  68. {
  69. public GetspcharaMethod()
  70. {
  71. ReturnType = typeof(Int64);
  72. argumentTypeArray = new[] { typeof(Int64) };
  73. CanRestructure = false;
  74. }
  75. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  76. {
  77. if(!Config.CompatiSPChara)
  78. throw new CodeEE("SPキャラ関係の機能は標準では使用できません(互換性オプション「SPキャラを使用する」をONにしてください)");
  79. Int64 integer = arguments[0].GetIntValue(exm);
  80. return exm.VEvaluator.GetChara_UseSp(integer, true);
  81. }
  82. }
  83. private sealed class CsvStrDataMethod : FunctionMethod
  84. {
  85. CharacterStrData charaStr;
  86. public CsvStrDataMethod()
  87. {
  88. ReturnType = typeof(string);
  89. argumentTypeArray = null;
  90. charaStr = CharacterStrData.NAME;
  91. CanRestructure = true;
  92. }
  93. public CsvStrDataMethod(CharacterStrData cStr)
  94. {
  95. ReturnType = typeof(string);
  96. argumentTypeArray = null;
  97. charaStr = cStr;
  98. CanRestructure = true;
  99. }
  100. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  101. {
  102. if (arguments.Length < 1)
  103. return name + "関数には少なくとも1つの引数が必要です";
  104. if (arguments.Length > 2)
  105. return name + "関数の引数が多すぎます";
  106. if (arguments[0] == null)
  107. return name + "関数の1番目の引数は省略できません";
  108. if (!arguments[0].IsInteger)
  109. return name + "関数の1番目の引数が数値ではありません";
  110. if (arguments.Length == 1)
  111. return null;
  112. if ((arguments[1] != null) && (arguments[1].GetOperandType() != typeof(Int64)))
  113. return name + "関数の2番目の変数が数値ではありません";
  114. return null;
  115. }
  116. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  117. {
  118. long x = arguments[0].GetIntValue(exm);
  119. long y = (arguments.Length > 1 && arguments[1] != null) ? arguments[1].GetIntValue(exm) : 0;
  120. if (!Config.CompatiSPChara && y != 0)
  121. throw new CodeEE("SPキャラ関係の機能は標準では使用できません(互換性オプション「SPキャラを使用する」をONにしてください)");
  122. return exm.VEvaluator.GetCharacterStrfromCSVData(x, charaStr, (y != 0), 0);
  123. }
  124. }
  125. private sealed class CsvcstrMethod : FunctionMethod
  126. {
  127. public CsvcstrMethod()
  128. {
  129. ReturnType = typeof(string);
  130. argumentTypeArray = null;
  131. CanRestructure = true;
  132. }
  133. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  134. {
  135. if (arguments.Length < 2)
  136. return name + "関数には少なくとも2つの引数が必要です";
  137. if (arguments.Length > 3)
  138. return name + "関数の引数が多すぎます";
  139. if (arguments[0] == null)
  140. return name + "関数の1番目の引数は省略できません";
  141. if (!arguments[0].IsInteger)
  142. return name + "関数の1番目の引数が数値ではありません";
  143. if (arguments[1] == null)
  144. return name + "関数の2番目の引数は省略できません";
  145. if (arguments[1].GetOperandType() != typeof(Int64))
  146. return name + "関数の2番目の変数が数値ではありません";
  147. if (arguments.Length == 2)
  148. return null;
  149. if ((arguments[2] != null) && (arguments[2].GetOperandType() != typeof(Int64)))
  150. return name + "関数の3番目の変数が数値ではありません";
  151. return null;
  152. }
  153. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  154. {
  155. long x = arguments[0].GetIntValue(exm);
  156. long y = arguments[1].GetIntValue(exm);
  157. long z = (arguments.Length == 3 && arguments[2] != null) ? arguments[2].GetIntValue(exm) : 0;
  158. if(!Config.CompatiSPChara && z != 0)
  159. throw new CodeEE("SPキャラ関係の機能は標準では使用できません(互換性オプション「SPキャラを使用する」をONにしてください)");
  160. return exm.VEvaluator.GetCharacterStrfromCSVData(x, CharacterStrData.CSTR, (z != 0), y);
  161. }
  162. }
  163. private sealed class CsvDataMethod : FunctionMethod
  164. {
  165. CharacterIntData charaInt;
  166. public CsvDataMethod()
  167. {
  168. ReturnType = typeof(Int64);
  169. argumentTypeArray = null;
  170. charaInt = CharacterIntData.BASE;
  171. CanRestructure = true;
  172. }
  173. public CsvDataMethod(CharacterIntData cInt)
  174. {
  175. ReturnType = typeof(Int64);
  176. argumentTypeArray = null;
  177. charaInt = cInt;
  178. CanRestructure = true;
  179. }
  180. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  181. {
  182. if (arguments.Length < 2)
  183. return name + "関数には少なくとも2つの引数が必要です";
  184. if (arguments.Length > 3)
  185. return name + "関数の引数が多すぎます";
  186. if (arguments[0] == null)
  187. return name + "関数の1番目の引数は省略できません";
  188. if (!arguments[0].IsInteger)
  189. return name + "関数の1番目の引数が数値ではありません";
  190. if (arguments[1] == null)
  191. return name + "関数の2番目の引数は省略できません";
  192. if (arguments[1].GetOperandType() != typeof(Int64))
  193. return name + "関数の2番目の変数が数値ではありません";
  194. if (arguments.Length == 2)
  195. return null;
  196. if ((arguments[2] != null) && (arguments[2].GetOperandType() != typeof(Int64)))
  197. return name + "関数の3番目の変数が数値ではありません";
  198. return null;
  199. }
  200. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  201. {
  202. long x = arguments[0].GetIntValue(exm);
  203. long y = arguments[1].GetIntValue(exm);
  204. long z = (arguments.Length == 3 && arguments[2] != null) ? arguments[2].GetIntValue(exm) : 0;
  205. if(!Config.CompatiSPChara && z != 0)
  206. throw new CodeEE("SPキャラ関係の機能は標準では使用できません(互換性オプション「SPキャラを使用する」をONにしてください)");
  207. return exm.VEvaluator.GetCharacterIntfromCSVData(x, charaInt, (z != 0), y);
  208. }
  209. }
  210. private sealed class FindcharaMethod : FunctionMethod
  211. {
  212. public FindcharaMethod(bool last)
  213. {
  214. ReturnType = typeof(Int64);
  215. argumentTypeArray = null;
  216. CanRestructure = false;
  217. isLast = last;
  218. }
  219. bool isLast;
  220. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  221. {
  222. //通常3つ、1つ省略可能で2~3の引数が必要。
  223. if (arguments.Length < 2)
  224. return name + "関数には少なくとも2つの引数が必要です";
  225. if (arguments.Length > 4)
  226. return name + "関数の引数が多すぎます";
  227. if (arguments[0] == null)
  228. return name + "関数の1番目の引数は省略できません";
  229. if (!(arguments[0] is VariableTerm))
  230. return name + "関数の1番目の引数の型が正しくありません";
  231. if (!(((VariableTerm)arguments[0]).Identifier.IsCharacterData))
  232. return name + "関数の1番目の引数の変数がキャラクタ変数ではありません";
  233. if (arguments[1] == null)
  234. return name + "関数の2番目の引数は省略できません";
  235. if (arguments[1].GetOperandType() != arguments[0].GetOperandType())
  236. return name + "関数の2番目の引数の型が正しくありません";
  237. //3番目は省略可能
  238. if ((arguments.Length >= 3) && (arguments[2] != null) && (arguments[2].GetOperandType() != typeof(Int64)))
  239. return name + "関数の3番目の引数の型が正しくありません";
  240. //4番目は省略可能
  241. if ((arguments.Length >= 4) && (arguments[3] != null) && (arguments[3].GetOperandType() != typeof(Int64)))
  242. return name + "関数の4番目の引数の型が正しくありません";
  243. return null;
  244. }
  245. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  246. {
  247. VariableTerm vTerm = (VariableTerm)arguments[0];
  248. VariableToken varID = vTerm.Identifier;
  249. Int64 elem = 0;
  250. if (vTerm.Identifier.IsArray1D)
  251. elem = vTerm.GetElementInt(1, exm);
  252. else if (vTerm.Identifier.IsArray2D)
  253. {
  254. elem = vTerm.GetElementInt(1, exm) << 32;
  255. elem += vTerm.GetElementInt(2, exm);
  256. }
  257. Int64 startindex = 0;
  258. Int64 lastindex = exm.VEvaluator.CHARANUM;
  259. if (arguments.Length >= 3 && arguments[2] != null)
  260. startindex = arguments[2].GetIntValue(exm);
  261. if (arguments.Length >= 4 && arguments[3] != null)
  262. lastindex = arguments[3].GetIntValue(exm);
  263. Int64 ret = -1;
  264. if (startindex < 0 || startindex >= exm.VEvaluator.CHARANUM)
  265. throw new CodeEE((isLast ? "" : "") + "関数の第3引数(" + startindex + ")はキャラクタ位置の範囲外です");
  266. if (lastindex < 0 || lastindex > exm.VEvaluator.CHARANUM)
  267. throw new CodeEE((isLast ? "" : "") + "関数の第4引数(" + lastindex + ")はキャラクタ位置の範囲外です");
  268. if (varID.IsString)
  269. {
  270. string word = arguments[1].GetStrValue(exm);
  271. ret = exm.VEvaluator.FindChara(varID, elem, word, startindex, lastindex, isLast);
  272. }
  273. else
  274. {
  275. Int64 word = arguments[1].GetIntValue(exm);
  276. ret = exm.VEvaluator.FindChara(varID, elem, word, startindex, lastindex, isLast);
  277. }
  278. return (ret);
  279. }
  280. }
  281. private sealed class ExistCsvMethod : FunctionMethod
  282. {
  283. public ExistCsvMethod()
  284. {
  285. ReturnType = typeof(Int64);
  286. argumentTypeArray = null;
  287. CanRestructure = true;
  288. }
  289. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  290. {
  291. if (arguments.Length < 1)
  292. return name + "関数には少なくとも1つの引数が必要です";
  293. if (arguments.Length > 2)
  294. return name + "関数の引数が多すぎます";
  295. if (arguments[0] == null)
  296. return name + "関数の1番目の引数は省略できません";
  297. if (!arguments[0].IsInteger)
  298. return name + "関数の1番目の引数が数値ではありません";
  299. if (arguments.Length == 1)
  300. return null;
  301. if ((arguments[1] != null) && (arguments[1].GetOperandType() != typeof(Int64)))
  302. return name + "関数の2番目の変数が数値ではありません";
  303. return null;
  304. }
  305. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  306. {
  307. Int64 no = arguments[0].GetIntValue(exm);
  308. bool isSp =(arguments.Length == 2 && arguments[1] != null) ? (arguments[1].GetIntValue(exm) != 0) : false;
  309. if(!Config.CompatiSPChara && isSp)
  310. throw new CodeEE("SPキャラ関係の機能は標準では使用できません(互換性オプション「SPキャラを使用する」をONにしてください)");
  311. return (exm.VEvaluator.ExistCsv(no, isSp));
  312. }
  313. }
  314. #endregion
  315. #region 汎用処理系
  316. private sealed class VarsizeMethod : FunctionMethod
  317. {
  318. public VarsizeMethod()
  319. {
  320. ReturnType = typeof(Int64);
  321. argumentTypeArray = null;
  322. CanRestructure = true;
  323. //1808beta009 参照型変数の追加によりちょっと面倒になった
  324. HasUniqueRestructure = true;
  325. }
  326. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  327. {
  328. if (arguments.Length < 1)
  329. return name + "関数には少なくとも1つの引数が必要です";
  330. if (arguments.Length > 2)
  331. return name + "関数の引数が多すぎます";
  332. if (arguments[0] == null)
  333. return name + "関数の1番目の引数は省略できません";
  334. if (!arguments[0].IsString)
  335. return name + "関数の1番目の引数が文字列ではありません";
  336. if (arguments[0] is SingleTerm)
  337. {
  338. string varName = ((SingleTerm)arguments[0]).Str;
  339. if (varName == "PARAM") ((SingleTerm)arguments[0]).Str = varName = "PALAM"; // JVN: Uses added setter to change PARAM to PALAM internally
  340. if (GlobalStatic.IdentifierDictionary.GetVariableToken(varName, null, true) == null)
  341. return name + "関数の1番目の引数が変数名ではありません";
  342. }
  343. if (arguments.Length == 1)
  344. return null;
  345. if ((arguments[1] != null) && (arguments[1].GetOperandType() != typeof(Int64)))
  346. return name + "関数の2番目の変数が数値ではありません";
  347. if (arguments.Length == 2)
  348. return null;
  349. return null;
  350. }
  351. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  352. {
  353. VariableToken var = GlobalStatic.IdentifierDictionary.GetVariableToken(arguments[0].GetStrValue(exm), null, true);
  354. if (var == null)
  355. throw new CodeEE("VARSIZEの1番目の引数(\"" + arguments[0].GetStrValue(exm) + "\")が変数名ではありません");
  356. int dim = 0;
  357. if (arguments.Length == 2 && arguments[1] != null)
  358. dim = (int)arguments[1].GetIntValue(exm);
  359. return (var.GetLength(dim));
  360. }
  361. public override bool UniqueRestructure(ExpressionMediator exm, IOperandTerm[] arguments)
  362. {
  363. arguments[0].Restructure(exm);
  364. if (arguments.Length > 1)
  365. arguments[1].Restructure(exm);
  366. if (arguments[0] is SingleTerm && (arguments.Length == 1 || arguments[1] is SingleTerm))
  367. {
  368. VariableToken var = GlobalStatic.IdentifierDictionary.GetVariableToken(arguments[0].GetStrValue(exm), null, true);
  369. if (var == null || var.IsReference)//可変長の場合は定数化できない
  370. return false;
  371. return true;
  372. }
  373. return false;
  374. }
  375. }
  376. private sealed class CheckfontMethod : FunctionMethod
  377. {
  378. public CheckfontMethod()
  379. {
  380. ReturnType = typeof(Int64);
  381. argumentTypeArray = new[] { typeof(string) };
  382. CanRestructure = true;//起動中に変わることもそうそうないはず……
  383. }
  384. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  385. {
  386. string str = arguments[0].GetStrValue(exm);
  387. InstalledFontCollection ifc = new InstalledFontCollection();
  388. Int64 isInstalled = 0;
  389. foreach (FontFamily ff in ifc.Families)
  390. {
  391. if (ff.Name == str)
  392. {
  393. isInstalled = 1;
  394. break;
  395. }
  396. }
  397. return (isInstalled);
  398. }
  399. }
  400. private sealed class CheckdataMethod : FunctionMethod
  401. {
  402. public CheckdataMethod(string name, EraSaveFileType type)
  403. {
  404. ReturnType = typeof(Int64);
  405. argumentTypeArray = new[] { typeof(Int64) };
  406. CanRestructure = false;
  407. this.name = name;
  408. this.type = type;
  409. }
  410. string name;
  411. EraSaveFileType type;
  412. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  413. {
  414. Int64 target = arguments[0].GetIntValue(exm);
  415. if (target < 0)
  416. throw new CodeEE(name + "の引数に負の値(" + target + ")が指定されました");
  417. if (target > int.MaxValue)
  418. throw new CodeEE(name + "の引数(" + target + ")が大きすぎます");
  419. EraDataResult result = null;
  420. result = exm.VEvaluator.CheckData((int)target, type);
  421. exm.VEvaluator.RESULTS = result.DataMes;
  422. return ((long)result.State);
  423. }
  424. }
  425. /// <summary>
  426. /// ファイル名をstringで指定する版・CHKVARDATAとCHKCHARADATAはこっちに分類
  427. /// </summary>
  428. private sealed class CheckdataStrMethod : FunctionMethod
  429. {
  430. public CheckdataStrMethod(string name, EraSaveFileType type)
  431. {
  432. ReturnType = typeof(Int64);
  433. argumentTypeArray = new[] { typeof(string) };
  434. CanRestructure = false;
  435. this.name = name;
  436. this.type = type;
  437. }
  438. string name;
  439. EraSaveFileType type;
  440. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  441. {
  442. string datFilename = arguments[0].GetStrValue(exm);
  443. EraDataResult result = null;
  444. result = exm.VEvaluator.CheckData(datFilename, type);
  445. exm.VEvaluator.RESULTS = result.DataMes;
  446. return ((long)result.State);
  447. }
  448. }
  449. /// <summary>
  450. /// ファイル探索関数
  451. /// </summary>
  452. private sealed class FindFilesMethod : FunctionMethod
  453. {
  454. public FindFilesMethod(string name, EraSaveFileType type)
  455. {
  456. ReturnType = typeof(Int64);
  457. argumentTypeArray = null;
  458. CanRestructure = false;
  459. this.name = name;
  460. this.type = type;
  461. }
  462. string name;
  463. EraSaveFileType type;
  464. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  465. {
  466. if (arguments.Length > 1)
  467. return name + "関数の引数が多すぎます";
  468. if (arguments.Length == 0 || arguments[0] == null)
  469. return null;
  470. if (!arguments[0].IsString)
  471. return name + "関数の1番目の引数が文字列ではありません";
  472. return null;
  473. }
  474. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  475. {
  476. string pattern = "*";
  477. if (arguments.Length > 0 && arguments[0] != null)
  478. pattern = arguments[0].GetStrValue(exm);
  479. List<string> filepathes = null;
  480. filepathes = exm.VEvaluator.GetDatFiles(type == EraSaveFileType.CharVar, pattern);
  481. string[] results = exm.VEvaluator.VariableData.DataStringArray[(int)(VariableCode.RESULTS & VariableCode.__LOWERCASE__)];
  482. if (filepathes.Count <= results.Length)
  483. filepathes.CopyTo(results);
  484. else
  485. filepathes.CopyTo(0, results, 0, results.Length);
  486. return filepathes.Count;
  487. }
  488. }
  489. private sealed class IsSkipMethod : FunctionMethod
  490. {
  491. public IsSkipMethod()
  492. {
  493. ReturnType = typeof(Int64);
  494. argumentTypeArray = new Type[] { };
  495. CanRestructure = false;
  496. }
  497. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  498. {
  499. return exm.Process.SkipPrint ? 1L : 0L;
  500. }
  501. }
  502. private sealed class MesSkipMethod : FunctionMethod
  503. {
  504. public MesSkipMethod(bool warn)
  505. {
  506. ReturnType = typeof(Int64);
  507. argumentTypeArray = null;
  508. CanRestructure = false;
  509. this.warn = warn;
  510. }
  511. bool warn;
  512. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  513. {
  514. if (arguments.Length > 0)
  515. return name + "関数の引数が多すぎます";
  516. if (warn)
  517. ParserMediator.Warn("関数MOUSESKIP()は推奨されません。代わりに関数MESSKIP()を使用してください", GlobalStatic.Process.GetScaningLine(), 1, false, false, null);
  518. return null;
  519. }
  520. public override long GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  521. {
  522. return GlobalStatic.Console.MesSkip ? 1L : 0L;
  523. }
  524. }
  525. private sealed class GetColorMethod : FunctionMethod
  526. {
  527. public GetColorMethod(bool isDef)
  528. {
  529. ReturnType = typeof(Int64);
  530. argumentTypeArray = new Type[] { };
  531. CanRestructure = isDef;
  532. defaultColor = isDef;
  533. }
  534. bool defaultColor;
  535. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  536. {
  537. Color color = (defaultColor) ? Config.ForeColor : GlobalStatic.Console.StringStyle.Color;
  538. return (color.ToArgb() & 0xFFFFFF);
  539. }
  540. }
  541. private sealed class GetFocusColorMethod : FunctionMethod
  542. {
  543. public GetFocusColorMethod()
  544. {
  545. ReturnType = typeof(Int64);
  546. argumentTypeArray = new Type[] { };
  547. CanRestructure = true;
  548. }
  549. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  550. {
  551. return (Config.FocusColor.ToArgb() & 0xFFFFFF);
  552. }
  553. }
  554. private sealed class GetBGColorMethod : FunctionMethod
  555. {
  556. public GetBGColorMethod(bool isDef)
  557. {
  558. ReturnType = typeof(Int64);
  559. argumentTypeArray = new Type[] { };
  560. CanRestructure = isDef;
  561. defaultColor = isDef;
  562. }
  563. bool defaultColor;
  564. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  565. {
  566. Color color = (defaultColor) ? Config.BackColor : GlobalStatic.Console.bgColor;
  567. return (color.ToArgb() & 0xFFFFFF);
  568. }
  569. }
  570. private sealed class GetStyleMethod : FunctionMethod
  571. {
  572. public GetStyleMethod()
  573. {
  574. ReturnType = typeof(Int64);
  575. argumentTypeArray = new Type[] { };
  576. CanRestructure = false;
  577. }
  578. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  579. {
  580. FontStyle fontstyle = GlobalStatic.Console.StringStyle.FontStyle;
  581. long ret = 0;
  582. if ((fontstyle & FontStyle.Bold) == FontStyle.Bold)
  583. ret |= 1;
  584. if ((fontstyle & FontStyle.Italic) == FontStyle.Italic)
  585. ret |= 2;
  586. if ((fontstyle & FontStyle.Strikeout) == FontStyle.Strikeout)
  587. ret |= 4;
  588. if ((fontstyle & FontStyle.Underline) == FontStyle.Underline)
  589. ret |= 8;
  590. return (ret);
  591. }
  592. }
  593. private sealed class GetFontMethod : FunctionMethod
  594. {
  595. public GetFontMethod()
  596. {
  597. ReturnType = typeof(string);
  598. argumentTypeArray = new Type[] { };
  599. CanRestructure = false;
  600. }
  601. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  602. {
  603. return (GlobalStatic.Console.StringStyle.Fontname);
  604. }
  605. }
  606. private sealed class BarStringMethod : FunctionMethod
  607. {
  608. public BarStringMethod()
  609. {
  610. ReturnType = typeof(string);
  611. argumentTypeArray = new[] { typeof(long), typeof(long), typeof(long) };
  612. CanRestructure = true;
  613. }
  614. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  615. {
  616. long var = arguments[0].GetIntValue(exm);
  617. long max = arguments[1].GetIntValue(exm);
  618. long length = arguments[2].GetIntValue(exm);
  619. return exm.CreateBar(var, max, length);
  620. }
  621. }
  622. private sealed class CurrentAlignMethod : FunctionMethod
  623. {
  624. public CurrentAlignMethod()
  625. {
  626. ReturnType = typeof(string);
  627. argumentTypeArray = new Type[] { };
  628. CanRestructure = false;
  629. }
  630. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  631. {
  632. if (exm.Console.Alignment == DisplayLineAlignment.LEFT)
  633. return "LEFT";
  634. if (exm.Console.Alignment == DisplayLineAlignment.CENTER)
  635. return "CENTER";
  636. return "RIGHT";
  637. }
  638. }
  639. private sealed class CurrentRedrawMethod : FunctionMethod
  640. {
  641. public CurrentRedrawMethod()
  642. {
  643. ReturnType = typeof(Int64);
  644. argumentTypeArray = new Type[] { };
  645. CanRestructure = false;
  646. }
  647. public override long GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  648. {
  649. return (exm.Console.Redraw == ConsoleRedraw.None) ? 0L : 1L;
  650. }
  651. }
  652. private sealed class ColorFromNameMethod : FunctionMethod
  653. {
  654. public ColorFromNameMethod()
  655. {
  656. ReturnType = typeof(Int64);
  657. argumentTypeArray = new[] { typeof(string) };
  658. CanRestructure = true;
  659. }
  660. public override long GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  661. {
  662. string colorName = arguments[0].GetStrValue(exm);
  663. Color color = Color.FromName(colorName);
  664. int i = 0;
  665. if (color.A > 0)
  666. i = (color.R << 16) + (color.G << 8) + color.B;
  667. else
  668. {
  669. if (colorName.Equals("transparent", StringComparison.OrdinalIgnoreCase))
  670. throw new CodeEE("無色透明(Transparent)は色として指定できません");
  671. //throw new CodeEE("指定された色名\"" + colorName + "\"は無効な色名です");
  672. i = -1;
  673. }
  674. return i;
  675. }
  676. }
  677. private sealed class ColorFromRGBMethod : FunctionMethod
  678. {
  679. public ColorFromRGBMethod()
  680. {
  681. ReturnType = typeof(Int64);
  682. argumentTypeArray = new[] { typeof(long), typeof(long), typeof(long) };
  683. CanRestructure = true;
  684. }
  685. public override long GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  686. {
  687. long r = arguments[0].GetIntValue(exm);
  688. if(r < 0 || r > 255)
  689. throw new CodeEE("第1引数が0から255の範囲外です");
  690. long g = arguments[1].GetIntValue(exm);
  691. if(g< 0 || g > 255)
  692. throw new CodeEE("第2引数が0から255の範囲外です");
  693. long b = arguments[2].GetIntValue(exm);
  694. if(b < 0 || b > 255)
  695. throw new CodeEE("第3引数が0から255の範囲外です");
  696. return (r << 16) + (g << 8) + b;
  697. }
  698. }
  699. /// <summary>
  700. /// 1810 作ったけど保留
  701. /// </summary>
  702. private sealed class GetRefMethod : FunctionMethod
  703. {
  704. public GetRefMethod()
  705. {
  706. ReturnType = typeof(string);
  707. argumentTypeArray = null;
  708. CanRestructure = false;
  709. }
  710. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  711. {
  712. if (arguments.Length < 1)
  713. return name + "関数には少なくとも1つの引数が必要です";
  714. if (arguments.Length > 1)
  715. return name + "関数の引数が多すぎます";
  716. if (arguments[0] == null)
  717. return name + "関数の1番目の引数は省略できません";
  718. if (!(arguments[0] is UserDefinedRefMethodNoArgTerm))
  719. return name + "関数の1番目の引数が関数参照ではありません";
  720. return null;
  721. }
  722. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  723. {
  724. return ((UserDefinedRefMethodNoArgTerm)arguments[0]).GetRefName();
  725. }
  726. }
  727. #endregion
  728. #region 定数取得
  729. private sealed class MoneyStrMethod : FunctionMethod
  730. {
  731. public MoneyStrMethod()
  732. {
  733. ReturnType = typeof(string);
  734. argumentTypeArray = null;
  735. CanRestructure = true;
  736. }
  737. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  738. {
  739. //通常2つ、1つ省略可能で1~2の引数が必要。
  740. if (arguments.Length < 1)
  741. return name + "関数には少なくとも1つの引数が必要です";
  742. if (arguments.Length > 2)
  743. return name + "関数の引数が多すぎます";
  744. if (arguments[0] == null)
  745. return name + "関数の1番目の引数は省略できません";
  746. if (arguments[0].GetOperandType() != typeof(Int64))
  747. return name + "関数の1番目の引数の型が正しくありません";
  748. if ((arguments.Length >= 2) && (arguments[1] != null) && (arguments[1].GetOperandType() != typeof(string)))
  749. return name + "関数の2番目の引数の型が正しくありません";
  750. return null;
  751. }
  752. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  753. {
  754. long money = arguments[0].GetIntValue(exm);
  755. if ((arguments.Length < 2) || (arguments[1] == null))
  756. return (Config.MoneyFirst) ? Config.MoneyLabel + money : money + Config.MoneyLabel;
  757. string format = arguments[1].GetStrValue(exm);
  758. string ret;
  759. try
  760. {
  761. ret = money.ToString(format);
  762. }
  763. catch (FormatException)
  764. {
  765. throw new CodeEE("MONEYSTR関数の第2引数の書式指定が間違っています");
  766. }
  767. return (Config.MoneyFirst) ? Config.MoneyLabel + ret : ret + Config.MoneyLabel;
  768. }
  769. }
  770. private sealed class GetPrintCPerLineMethod : FunctionMethod
  771. {
  772. public GetPrintCPerLineMethod()
  773. {
  774. ReturnType = typeof(Int64);
  775. argumentTypeArray = new Type[] { };
  776. CanRestructure = true;
  777. }
  778. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  779. {
  780. return (Config.PrintCPerLine);
  781. }
  782. }
  783. private sealed class PrintCLengthMethod : FunctionMethod
  784. {
  785. public PrintCLengthMethod()
  786. {
  787. ReturnType = typeof(Int64);
  788. argumentTypeArray = new Type[] { };
  789. CanRestructure = true;
  790. }
  791. public override long GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  792. {
  793. return (Config.PrintCLength);
  794. }
  795. }
  796. private sealed class GetSaveNosMethod : FunctionMethod
  797. {
  798. public GetSaveNosMethod()
  799. {
  800. ReturnType = typeof(Int64);
  801. argumentTypeArray = new Type[] { };
  802. CanRestructure = true;
  803. }
  804. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  805. {
  806. return (Config.SaveDataNos);
  807. }
  808. }
  809. private sealed class GettimeMethod : FunctionMethod
  810. {
  811. public GettimeMethod()
  812. {
  813. ReturnType = typeof(Int64);
  814. argumentTypeArray = new Type[] { };
  815. CanRestructure = false;
  816. }
  817. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  818. {
  819. long date = DateTime.Now.Year;
  820. date = date * 100 + DateTime.Now.Month;
  821. date = date * 100 + DateTime.Now.Day;
  822. date = date * 100 + DateTime.Now.Hour;
  823. date = date * 100 + DateTime.Now.Minute;
  824. date = date * 100 + DateTime.Now.Second;
  825. date = date * 1000 + DateTime.Now.Millisecond;
  826. return (date);//17桁。2京くらい。
  827. }
  828. }
  829. private sealed class GettimesMethod : FunctionMethod
  830. {
  831. public GettimesMethod()
  832. {
  833. ReturnType = typeof(string);
  834. argumentTypeArray = new Type[] { };
  835. CanRestructure = false;
  836. }
  837. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  838. {
  839. return (DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
  840. }
  841. }
  842. private sealed class GetmsMethod : FunctionMethod
  843. {
  844. public GetmsMethod()
  845. {
  846. ReturnType = typeof(Int64);
  847. argumentTypeArray = new Type[] { };
  848. CanRestructure = false;
  849. }
  850. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  851. {
  852. //西暦0001年1月1日からの経過時間をミリ秒で。
  853. //Ticksは100ナノ秒単位であるが実際にはそんな精度はないので無駄。
  854. return (DateTime.Now.Ticks / 10000);
  855. }
  856. }
  857. private sealed class GetSecondMethod : FunctionMethod
  858. {
  859. public GetSecondMethod()
  860. {
  861. ReturnType = typeof(Int64);
  862. argumentTypeArray = new Type[] { };
  863. CanRestructure = false;
  864. }
  865. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  866. {
  867. //西暦0001年1月1日からの経過時間を秒で。
  868. //Ticksは100ナノ秒単位であるが実際にはそんな精度はないので無駄。
  869. return (DateTime.Now.Ticks / 10000000);
  870. }
  871. }
  872. #endregion
  873. #region 数学関数
  874. private sealed class RandMethod : FunctionMethod
  875. {
  876. public RandMethod()
  877. {
  878. ReturnType = typeof(Int64);
  879. argumentTypeArray = null;
  880. CanRestructure = false;
  881. }
  882. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  883. {
  884. //通常2つ、1つ省略可能で1~2の引数が必要。
  885. if (arguments.Length < 1)
  886. return name + "関数には少なくとも1つの引数が必要です";
  887. if (arguments.Length > 2)
  888. return name + "関数の引数が多すぎます";
  889. if (arguments.Length == 1)
  890. {
  891. if (arguments[0] == null)
  892. return name + "関数には少なくとも1つの引数が必要です";
  893. if ((arguments[0].GetOperandType() != typeof(Int64)))
  894. return name + "関数の1番目の引数の型が正しくありません";
  895. return null;
  896. }
  897. //1番目は省略可能
  898. if ((arguments[0] != null) && (arguments[0].GetOperandType() != typeof(Int64)))
  899. return name + "関数の1番目の引数の型が正しくありません";
  900. if ((arguments[1] != null) && (arguments[1].GetOperandType() != typeof(Int64)))
  901. return name + "関数の2番目の引数の型が正しくありません";
  902. return null;
  903. }
  904. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  905. {
  906. Int64 max = 0;
  907. Int64 min = 0;
  908. if (arguments.Length == 1)
  909. max = arguments[0].GetIntValue(exm);
  910. else
  911. {
  912. if (arguments[0] != null)
  913. min = arguments[0].GetIntValue(exm);
  914. max = arguments[1].GetIntValue(exm);
  915. }
  916. if (max <= min)
  917. {
  918. if (min == 0)
  919. throw new CodeEE("RANDの最大値に0以下の値(" + max + ")が指定されました");
  920. throw new CodeEE("RANDの最大値に最小値以下の値(" + max + ")が指定されました");
  921. }
  922. return (exm.VEvaluator.GetNextRand(max - min) + min);
  923. }
  924. }
  925. private sealed class MaxMethod : FunctionMethod
  926. {
  927. bool isMax;
  928. public MaxMethod()
  929. {
  930. ReturnType = typeof(Int64);
  931. argumentTypeArray = null;
  932. isMax = true;
  933. CanRestructure = true;
  934. }
  935. public MaxMethod(bool max)
  936. {
  937. ReturnType = typeof(Int64);
  938. argumentTypeArray = null;
  939. isMax = max;
  940. CanRestructure = true;
  941. }
  942. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  943. {
  944. if (arguments.Length < 1)
  945. return name + "関数には少なくとも1つの引数が必要です";
  946. for (int i = 0; i < arguments.Length; i++)
  947. {
  948. if (arguments[i] == null)
  949. return name + "関数の" + (i + 1) + "番目の引数は省略できません";
  950. if (arguments[i].GetOperandType() != typeof(Int64))
  951. return name + "関数の" + (i + 1) + "番目の引数の型が正しくありません";
  952. }
  953. return null;
  954. }
  955. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  956. {
  957. Int64 ret = arguments[0].GetIntValue(exm);
  958. for (int i = 1; i < arguments.Length; i++)
  959. {
  960. Int64 newRet = arguments[i].GetIntValue(exm);
  961. if (isMax)
  962. {
  963. if (ret < newRet)
  964. ret = newRet;
  965. }
  966. else
  967. {
  968. if (ret > newRet)
  969. ret = newRet;
  970. }
  971. }
  972. return (ret);
  973. }
  974. }
  975. private sealed class AbsMethod : FunctionMethod
  976. {
  977. public AbsMethod()
  978. {
  979. ReturnType = typeof(Int64);
  980. argumentTypeArray = new[] { typeof(Int64) };
  981. CanRestructure = true;
  982. }
  983. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  984. {
  985. Int64 ret = arguments[0].GetIntValue(exm);
  986. return (Math.Abs(ret));
  987. }
  988. }
  989. private sealed class PowerMethod : FunctionMethod
  990. {
  991. public PowerMethod()
  992. {
  993. ReturnType = typeof(Int64);
  994. argumentTypeArray = new[] { typeof(Int64), typeof(Int64) };
  995. CanRestructure = true;
  996. }
  997. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  998. {
  999. Int64 x = arguments[0].GetIntValue(exm);
  1000. Int64 y = arguments[1].GetIntValue(exm);
  1001. double pow = Math.Pow(x, y);
  1002. if (double.IsNaN(pow))
  1003. throw new CodeEE("累乗結果が非数値です");
  1004. if (double.IsInfinity(pow))
  1005. throw new CodeEE("累乗結果が無限大です");
  1006. if ((pow >= Int64.MaxValue) || (pow <= Int64.MinValue))
  1007. throw new CodeEE("累乗結果(" + pow + ")が64ビット符号付き整数の範囲外です");
  1008. return ((long)pow);
  1009. }
  1010. }
  1011. private sealed class SqrtMethod : FunctionMethod
  1012. {
  1013. public SqrtMethod()
  1014. {
  1015. ReturnType = typeof(Int64);
  1016. argumentTypeArray = new[] { typeof(Int64) };
  1017. CanRestructure = true;
  1018. }
  1019. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1020. {
  1021. Int64 ret = arguments[0].GetIntValue(exm);
  1022. if (ret < 0)
  1023. throw new CodeEE("SQRT関数の引数に負の値が指定されました");
  1024. return ((Int64)Math.Sqrt(ret));
  1025. }
  1026. }
  1027. private sealed class CbrtMethod : FunctionMethod
  1028. {
  1029. public CbrtMethod()
  1030. {
  1031. ReturnType = typeof(Int64);
  1032. argumentTypeArray = new[] { typeof(Int64) };
  1033. CanRestructure = true;
  1034. }
  1035. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1036. {
  1037. Int64 ret = arguments[0].GetIntValue(exm);
  1038. if (ret < 0)
  1039. throw new CodeEE("CBRT関数の引数に負の値が指定されました");
  1040. return ((Int64)Math.Pow(ret, 1.0 / 3.0));
  1041. }
  1042. }
  1043. private sealed class LogMethod : FunctionMethod
  1044. {
  1045. double Base;
  1046. public LogMethod()
  1047. {
  1048. ReturnType = typeof(Int64);
  1049. argumentTypeArray = new[] { typeof(Int64) };
  1050. Base = Math.E;
  1051. CanRestructure = true;
  1052. }
  1053. public LogMethod(double b)
  1054. {
  1055. ReturnType = typeof(Int64);
  1056. argumentTypeArray = new[] { typeof(Int64) };
  1057. Base = b;
  1058. CanRestructure = true;
  1059. }
  1060. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1061. {
  1062. Int64 ret = arguments[0].GetIntValue(exm);
  1063. if (ret <= 0)
  1064. throw new CodeEE("対数関数の引数に0以下の値が指定されました");
  1065. if (Base <= 0.0d)
  1066. throw new CodeEE("対数関数の底に0以下の値が指定されました");
  1067. double dret = ret;
  1068. if (Base == Math.E)
  1069. dret = Math.Log(dret);
  1070. else
  1071. dret = Math.Log10(dret);
  1072. if (double.IsNaN(dret))
  1073. throw new CodeEE("計算値が非数値です");
  1074. if (double.IsInfinity(dret))
  1075. throw new CodeEE("計算値が無限大です");
  1076. if ((dret >= Int64.MaxValue) || (dret <= Int64.MinValue))
  1077. throw new CodeEE("計算結果(" + dret + ")が64ビット符号付き整数の範囲外です");
  1078. return ((Int64)dret);
  1079. }
  1080. }
  1081. private sealed class ExpMethod : FunctionMethod
  1082. {
  1083. public ExpMethod()
  1084. {
  1085. ReturnType = typeof(Int64);
  1086. argumentTypeArray = new[] { typeof(Int64) };
  1087. CanRestructure = true;
  1088. }
  1089. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1090. {
  1091. Int64 ret = arguments[0].GetIntValue(exm);
  1092. double dret = Math.Exp(ret);
  1093. if (double.IsNaN(dret))
  1094. throw new CodeEE("計算値が非数値です");
  1095. if (double.IsInfinity(dret))
  1096. throw new CodeEE("計算値が無限大です");
  1097. if ((dret >= Int64.MaxValue) || (dret <= Int64.MinValue))
  1098. throw new CodeEE("計算結果(" + dret + ")が64ビット符号付き整数の範囲外です");
  1099. return ((Int64)dret);
  1100. }
  1101. }
  1102. private sealed class SignMethod : FunctionMethod
  1103. {
  1104. public SignMethod()
  1105. {
  1106. ReturnType = typeof(Int64);
  1107. argumentTypeArray = new[] { typeof(Int64) };
  1108. CanRestructure = true;
  1109. }
  1110. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1111. {
  1112. Int64 ret = arguments[0].GetIntValue(exm);
  1113. return (Math.Sign(ret));
  1114. }
  1115. }
  1116. private sealed class GetLimitMethod : FunctionMethod
  1117. {
  1118. public GetLimitMethod()
  1119. {
  1120. ReturnType = typeof(Int64);
  1121. argumentTypeArray = new[] { typeof(Int64), typeof(Int64), typeof(Int64) };
  1122. CanRestructure = true;
  1123. }
  1124. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1125. {
  1126. Int64 value = arguments[0].GetIntValue(exm);
  1127. Int64 min = arguments[1].GetIntValue(exm);
  1128. Int64 max = arguments[2].GetIntValue(exm);
  1129. Int64 ret = 0;
  1130. if (value < min)
  1131. ret = min;
  1132. else if (value > max)
  1133. ret = max;
  1134. else
  1135. ret = value;
  1136. return (ret);
  1137. }
  1138. }
  1139. #endregion
  1140. #region 変数操作系
  1141. private sealed class SumArrayMethod : FunctionMethod
  1142. {
  1143. bool isCharaRange;
  1144. public SumArrayMethod()
  1145. {
  1146. ReturnType = typeof(Int64);
  1147. argumentTypeArray = null;
  1148. isCharaRange = false;
  1149. CanRestructure = false;
  1150. }
  1151. public SumArrayMethod(bool isChara)
  1152. {
  1153. ReturnType = typeof(Int64);
  1154. argumentTypeArray = null;
  1155. isCharaRange = isChara;
  1156. CanRestructure = false;
  1157. }
  1158. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  1159. {
  1160. if (arguments.Length < 1)
  1161. return name + "関数には少なくとも1つの引数が必要です";
  1162. if (arguments.Length > 3)
  1163. return name + "関数の引数が多すぎます";
  1164. if (arguments[0] == null)
  1165. return name + "関数の1番目の引数は省略できません";
  1166. if (!(arguments[0] is VariableTerm))
  1167. return name + "関数の1番目の引数が変数ではありません";
  1168. VariableTerm varToken = (VariableTerm)arguments[0];
  1169. if (varToken.IsString)
  1170. return name + "関数の1番目の引数が数値変数ではありません";
  1171. if (isCharaRange && !varToken.Identifier.IsCharacterData)
  1172. return name + "関数の1番目の引数がキャラクタ変数ではありません";
  1173. if (!isCharaRange && !varToken.Identifier.IsArray1D && !varToken.Identifier.IsArray2D && !varToken.Identifier.IsArray3D)
  1174. return name + "関数の1番目の引数が配列変数ではありません";
  1175. if (arguments.Length == 1)
  1176. return null;
  1177. if ((arguments[1] != null) && (arguments[1].GetOperandType() != typeof(Int64)))
  1178. return name + "関数の2番目の変数が数値ではありません";
  1179. if (arguments.Length == 2)
  1180. return null;
  1181. if ((arguments[2] != null) && (arguments[2].GetOperandType() != typeof(Int64)))
  1182. return name + "関数の3番目の変数が数値ではありません";
  1183. return null;
  1184. }
  1185. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1186. {
  1187. VariableTerm varTerm = (VariableTerm)arguments[0];
  1188. Int64 index1 = (arguments.Length >= 2 && arguments[1] != null) ? arguments[1].GetIntValue(exm) : 0;
  1189. Int64 index2 = (arguments.Length == 3 && arguments[2] != null) ? arguments[2].GetIntValue(exm) : (isCharaRange ? exm.VEvaluator.CHARANUM : varTerm.GetLastLength());
  1190. FixedVariableTerm p = varTerm.GetFixedVariableTerm(exm);
  1191. if (!isCharaRange)
  1192. {
  1193. p.IsArrayRangeValid(index1, index2, "SUMARRAY", 2L, 3L);
  1194. return (exm.VEvaluator.GetArraySum(p, index1, index2));
  1195. }
  1196. Int64 charaNum = exm.VEvaluator.CHARANUM;
  1197. if (index1 >= charaNum || index1 < 0 || index2 > charaNum || index2 < 0)
  1198. throw new CodeEE("SUMCARRAY関数の範囲指定がキャラクタ配列の範囲を超えています(" + index1 + "~" + index2 + ")");
  1199. return (exm.VEvaluator.GetArraySumChara(p, index1, index2));
  1200. }
  1201. }
  1202. private sealed class MatchMethod : FunctionMethod
  1203. {
  1204. bool isCharaRange;
  1205. public MatchMethod()
  1206. {
  1207. ReturnType = typeof(Int64);
  1208. argumentTypeArray = null;
  1209. isCharaRange = false;
  1210. CanRestructure = false;
  1211. HasUniqueRestructure = true;
  1212. }
  1213. public MatchMethod(bool isChara)
  1214. {
  1215. ReturnType = typeof(Int64);
  1216. argumentTypeArray = null;
  1217. isCharaRange = isChara;
  1218. CanRestructure = false;
  1219. HasUniqueRestructure = true;
  1220. }
  1221. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  1222. {
  1223. if (arguments.Length < 2)
  1224. return name + "関数には少なくとも2つの引数が必要です";
  1225. if (arguments.Length > 4)
  1226. return name + "関数の引数が多すぎます";
  1227. if (arguments[0] == null)
  1228. return name + "関数の1番目の引数は省略できません";
  1229. if (!(arguments[0] is VariableTerm))
  1230. return name + "関数の1番目の引数が変数ではありません";
  1231. VariableTerm varToken = (VariableTerm)arguments[0];
  1232. if (isCharaRange && !varToken.Identifier.IsCharacterData)
  1233. return name + "関数の1番目の引数がキャラクタ変数ではありません";
  1234. if (!isCharaRange && (varToken.Identifier.IsArray2D || varToken.Identifier.IsArray3D))
  1235. return name + "関数は二重配列・三重配列には対応していません";
  1236. if (!isCharaRange && !varToken.Identifier.IsArray1D)
  1237. return name + "関数の1番目の引数が配列変数ではありません";
  1238. if (arguments[1] == null)
  1239. return name + "関数の2番目の引数は省略できません";
  1240. if (arguments[1].GetOperandType() != arguments[0].GetOperandType())
  1241. return name + "関数の1番目の引数と2番目の引数の型が異なります";
  1242. if ((arguments.Length >= 3) && (arguments[2] != null) && (arguments[2].GetOperandType() != typeof(Int64)))
  1243. return name + "関数の3番目の引数の型が正しくありません";
  1244. if ((arguments.Length >= 4) && (arguments[3] != null) && (arguments[3].GetOperandType() != typeof(Int64)))
  1245. return name + "関数の4番目の引数の型が正しくありません";
  1246. return null;
  1247. }
  1248. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1249. {
  1250. VariableTerm varTerm = arguments[0] as VariableTerm;
  1251. Int64 start = (arguments.Length > 2 && arguments[2] != null) ? arguments[2].GetIntValue(exm) : 0;
  1252. Int64 end = (arguments.Length > 3 && arguments[3] != null) ? arguments[3].GetIntValue(exm) : (isCharaRange ? exm.VEvaluator.CHARANUM : varTerm.GetLength());
  1253. FixedVariableTerm p = varTerm.GetFixedVariableTerm(exm);
  1254. if (!isCharaRange)
  1255. {
  1256. p.IsArrayRangeValid(start, end, "MATCH", 3L, 4L);
  1257. if (arguments[0].GetOperandType() == typeof(Int64))
  1258. {
  1259. Int64 targetValue = arguments[1].GetIntValue(exm);
  1260. return (exm.VEvaluator.GetMatch(p, targetValue, start, end));
  1261. }
  1262. string targetStr = arguments[1].GetStrValue(exm);
  1263. return (exm.VEvaluator.GetMatch(p, targetStr, start, end));
  1264. }
  1265. Int64 charaNum = exm.VEvaluator.CHARANUM;
  1266. if (start >= charaNum || start < 0 || end > charaNum || end < 0)
  1267. throw new CodeEE("CMATCH関数の範囲指定がキャラクタ配列の範囲を超えています(" + start + "~" + end + ")");
  1268. if (arguments[0].GetOperandType() == typeof(Int64))
  1269. {
  1270. Int64 targetValue = arguments[1].GetIntValue(exm);
  1271. return (exm.VEvaluator.GetMatchChara(p, targetValue, start, end));
  1272. }
  1273. {
  1274. string targetStr = arguments[1].GetStrValue(exm);
  1275. return (exm.VEvaluator.GetMatchChara(p, targetStr, start, end));
  1276. }
  1277. }
  1278. public override bool UniqueRestructure(ExpressionMediator exm, IOperandTerm[] arguments)
  1279. {
  1280. arguments[0].Restructure(exm);
  1281. for (int i = 1; i < arguments.Length; i++)
  1282. {
  1283. if (arguments[i] == null)
  1284. continue;
  1285. arguments[i] = arguments[i].Restructure(exm);
  1286. }
  1287. return false;
  1288. }
  1289. }
  1290. private sealed class GroupMatchMethod : FunctionMethod
  1291. {
  1292. public GroupMatchMethod()
  1293. {
  1294. ReturnType = typeof(Int64);
  1295. argumentTypeArray = null;
  1296. CanRestructure = false;
  1297. }
  1298. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  1299. {
  1300. if (arguments.Length < 2)
  1301. return name + "関数には少なくとも2つの引数が必要です";
  1302. if (arguments[0] == null)
  1303. return name + "関数の1番目の引数は省略できません";
  1304. Type baseType = arguments[0].GetOperandType();
  1305. for (int i = 1; i < arguments.Length; i++)
  1306. {
  1307. if (arguments[i] == null)
  1308. return name + "関数の" + (i + 1) + "番目の引数は省略できません";
  1309. if (arguments[i].GetOperandType() != baseType)
  1310. return name + "関数の" + (i + 1) + "番目の引数の型が正しくありません";
  1311. }
  1312. return null;
  1313. }
  1314. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1315. {
  1316. Int64 ret = 0;
  1317. if (arguments[0].GetOperandType() == typeof(Int64))
  1318. {
  1319. Int64 baseValue = arguments[0].GetIntValue(exm);
  1320. for (int i = 1; i < arguments.Length; i++)
  1321. {
  1322. if (baseValue == arguments[i].GetIntValue(exm))
  1323. ret += 1;
  1324. }
  1325. }
  1326. else
  1327. {
  1328. string baseString = arguments[0].GetStrValue(exm);
  1329. for (int i = 1; i < arguments.Length; i++)
  1330. {
  1331. if (baseString == arguments[i].GetStrValue(exm))
  1332. ret += 1;
  1333. }
  1334. }
  1335. return (ret);
  1336. }
  1337. }
  1338. private sealed class NosamesMethod : FunctionMethod
  1339. {
  1340. public NosamesMethod()
  1341. {
  1342. ReturnType = typeof(Int64);
  1343. argumentTypeArray = null;
  1344. CanRestructure = false;
  1345. }
  1346. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  1347. {
  1348. if (arguments.Length < 2)
  1349. return name + "関数には少なくとも2つの引数が必要です";
  1350. if (arguments[0] == null)
  1351. return name + "関数の1番目の引数は省略できません";
  1352. Type baseType = arguments[0].GetOperandType();
  1353. for (int i = 1; i < arguments.Length; i++)
  1354. {
  1355. if (arguments[i] == null)
  1356. return name + "関数の" + (i + 1) + "番目の引数は省略できません";
  1357. if (arguments[i].GetOperandType() != baseType)
  1358. return name + "関数の" + (i + 1) + "番目の引数の型が正しくありません";
  1359. }
  1360. return null;
  1361. }
  1362. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1363. {
  1364. if (arguments[0].GetOperandType() == typeof(Int64))
  1365. {
  1366. Int64 baseValue = arguments[0].GetIntValue(exm);
  1367. for (int i = 1; i < arguments.Length; i++)
  1368. {
  1369. if (baseValue == arguments[i].GetIntValue(exm))
  1370. return 0L;
  1371. }
  1372. }
  1373. else
  1374. {
  1375. string baseValue = arguments[0].GetStrValue(exm);
  1376. for (int i = 1; i < arguments.Length; i++)
  1377. {
  1378. if (baseValue == arguments[i].GetStrValue(exm))
  1379. return 0L;
  1380. }
  1381. }
  1382. return 1L;
  1383. }
  1384. }
  1385. private sealed class AllsamesMethod : FunctionMethod
  1386. {
  1387. public AllsamesMethod()
  1388. {
  1389. ReturnType = typeof(Int64);
  1390. argumentTypeArray = null;
  1391. CanRestructure = false;
  1392. }
  1393. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  1394. {
  1395. if (arguments.Length < 2)
  1396. return name + "関数には少なくとも2つの引数が必要です";
  1397. if (arguments[0] == null)
  1398. return name + "関数の1番目の引数は省略できません";
  1399. Type baseType = arguments[0].GetOperandType();
  1400. for (int i = 1; i < arguments.Length; i++)
  1401. {
  1402. if (arguments[i] == null)
  1403. return name + "関数の" + (i + 1) + "番目の引数は省略できません";
  1404. if (arguments[i].GetOperandType() != baseType)
  1405. return name + "関数の" + (i + 1) + "番目の引数の型が正しくありません";
  1406. }
  1407. return null;
  1408. }
  1409. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1410. {
  1411. if (arguments[0].GetOperandType() == typeof(Int64))
  1412. {
  1413. Int64 baseValue = arguments[0].GetIntValue(exm);
  1414. for (int i = 1; i < arguments.Length; i++)
  1415. {
  1416. if (baseValue != arguments[i].GetIntValue(exm))
  1417. return 0L;
  1418. }
  1419. }
  1420. else
  1421. {
  1422. string baseValue = arguments[0].GetStrValue(exm);
  1423. for (int i = 1; i < arguments.Length; i++)
  1424. {
  1425. if (baseValue != arguments[i].GetStrValue(exm))
  1426. return 0L;
  1427. }
  1428. }
  1429. return 1L;
  1430. }
  1431. }
  1432. private sealed class MaxArrayMethod : FunctionMethod
  1433. {
  1434. bool isCharaRange;
  1435. bool isMax;
  1436. string funcName;
  1437. public MaxArrayMethod()
  1438. {
  1439. ReturnType = typeof(Int64);
  1440. argumentTypeArray = null;
  1441. isCharaRange = false;
  1442. isMax = true;
  1443. funcName = "MAXARRAY";
  1444. CanRestructure = false;
  1445. }
  1446. public MaxArrayMethod(bool isChara)
  1447. {
  1448. ReturnType = typeof(Int64);
  1449. argumentTypeArray = null;
  1450. isCharaRange = isChara;
  1451. isMax = true;
  1452. if (isCharaRange)
  1453. funcName = "MAXCARRAY";
  1454. else
  1455. funcName = "MAXARRAY";
  1456. CanRestructure = false;
  1457. }
  1458. public MaxArrayMethod(bool isChara, bool isMaxFunc)
  1459. {
  1460. ReturnType = typeof(Int64);
  1461. argumentTypeArray = null;
  1462. isCharaRange = isChara;
  1463. isMax = isMaxFunc;
  1464. funcName = (isMax ? "MAX" : "MIN") + (isCharaRange ? "C" : "") + "ARRAY";
  1465. CanRestructure = false;
  1466. }
  1467. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  1468. {
  1469. if (arguments.Length < 1)
  1470. return name + "関数には少なくとも1つの引数が必要です";
  1471. if (arguments.Length > 3)
  1472. return name + "関数の引数が多すぎます";
  1473. if (arguments[0] == null)
  1474. return name + "関数の1番目の引数は省略できません";
  1475. if (!(arguments[0] is VariableTerm))
  1476. return name + "関数の1番目の引数が変数ではありません";
  1477. VariableTerm varToken = (VariableTerm)arguments[0];
  1478. if (isCharaRange && !varToken.Identifier.IsCharacterData)
  1479. return name + "関数の1番目の引数がキャラクタ変数ではありません";
  1480. if (!varToken.IsInteger)
  1481. return name + "関数の1番目の引数が数値変数ではありません";
  1482. if (!isCharaRange && (varToken.Identifier.IsArray2D || varToken.Identifier.IsArray3D))
  1483. return name + "関数は二重配列・三重配列には対応していません";
  1484. if (!varToken.Identifier.IsArray1D)
  1485. return name + "関数の1番目の引数が配列変数ではありません";
  1486. if ((arguments.Length >= 2) && (arguments[1] != null) && (arguments[1].GetOperandType() != typeof(Int64)))
  1487. return name + "関数の2番目の引数の型が正しくありません";
  1488. if ((arguments.Length >= 3) && (arguments[2] != null) && (arguments[2].GetOperandType() != typeof(Int64)))
  1489. return name + "関数の3番目の引数の型が正しくありません";
  1490. return null;
  1491. }
  1492. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1493. {
  1494. VariableTerm vTerm = (VariableTerm)arguments[0];
  1495. Int64 start = (arguments.Length > 1 && arguments[1] != null) ? arguments[1].GetIntValue(exm) : 0;
  1496. Int64 end = (arguments.Length > 2 && arguments[2] != null) ? end = arguments[2].GetIntValue(exm) : (isCharaRange ? exm.VEvaluator.CHARANUM : vTerm.GetLength());
  1497. FixedVariableTerm p = vTerm.GetFixedVariableTerm(exm);
  1498. if (!isCharaRange)
  1499. {
  1500. p.IsArrayRangeValid(start, end, funcName, 2L, 3L);
  1501. return (exm.VEvaluator.GetMaxArray(p, start, end, isMax));
  1502. }
  1503. Int64 charaNum = exm.VEvaluator.CHARANUM;
  1504. if (start >= charaNum || start < 0 || end > charaNum || end < 0)
  1505. throw new CodeEE(funcName + "関数の範囲指定がキャラクタ配列の範囲を超えています(" + start + "~" + end + ")");
  1506. return (exm.VEvaluator.GetMaxArrayChara(p, start, end, isMax));
  1507. }
  1508. }
  1509. private sealed class GetbitMethod : FunctionMethod
  1510. {
  1511. public GetbitMethod()
  1512. {
  1513. ReturnType = typeof(Int64);
  1514. argumentTypeArray = new[] { typeof(Int64), typeof(Int64) };
  1515. CanRestructure = true;
  1516. }
  1517. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  1518. {
  1519. string ret = base.CheckArgumentType(name, arguments);
  1520. if (ret != null)
  1521. return ret;
  1522. if (arguments[1] is SingleTerm)
  1523. {
  1524. Int64 m = ((SingleTerm)arguments[1]).Int;
  1525. if (m < 0 || m > 63)
  1526. return "GETBIT関数の第2引数(" + m + ")が範囲(0~63)を超えています";
  1527. }
  1528. return null;
  1529. }
  1530. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1531. {
  1532. Int64 n = arguments[0].GetIntValue(exm);
  1533. Int64 m = arguments[1].GetIntValue(exm);
  1534. if ((m < 0) || (m > 63))
  1535. throw new CodeEE("GETBIT関数の第2引数(" + m + ")が範囲(0~63)を超えています");
  1536. int mi = (int)m;
  1537. return ((n >> mi) & 1);
  1538. }
  1539. }
  1540. private sealed class GetnumMethod : FunctionMethod
  1541. {
  1542. public GetnumMethod()
  1543. {
  1544. ReturnType = typeof(Int64);
  1545. argumentTypeArray = null;
  1546. CanRestructure = true;
  1547. HasUniqueRestructure = true;
  1548. }
  1549. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  1550. {
  1551. if (arguments.Length != 2)
  1552. return name + "関数には2つの引数が必要です";
  1553. if (arguments[0] == null)
  1554. return name + "関数の1番目の引数は省略できません";
  1555. if (!(arguments[0] is VariableTerm))
  1556. return name + "関数の1番目の引数の型が正しくありません";
  1557. if (arguments[1] == null)
  1558. return name + "関数の2番目の引数は省略できません";
  1559. if (arguments[1].GetOperandType() != typeof(string))
  1560. return name + "関数の2番目の引数の型が正しくありません";
  1561. return null;
  1562. }
  1563. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1564. {
  1565. VariableTerm vToken = (VariableTerm)arguments[0];
  1566. VariableCode varCode = vToken.Identifier.Code;
  1567. string key = arguments[1].GetStrValue(exm);
  1568. int ret = 0;
  1569. if (exm.VEvaluator.Constant.TryKeywordToInteger(out ret, varCode, key, -1))
  1570. return ret;
  1571. return -1;
  1572. }
  1573. public override bool UniqueRestructure(ExpressionMediator exm, IOperandTerm[] arguments)
  1574. {
  1575. arguments[1] = arguments[1].Restructure(exm);
  1576. return arguments[1] is SingleTerm;
  1577. }
  1578. }
  1579. private sealed class GetPalamLVMethod : FunctionMethod
  1580. {
  1581. public GetPalamLVMethod()
  1582. {
  1583. ReturnType = typeof(Int64);
  1584. argumentTypeArray = new[] { typeof(Int64), typeof(Int64) };
  1585. CanRestructure = false;
  1586. }
  1587. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  1588. {
  1589. string errStr = base.CheckArgumentType(name, arguments);
  1590. if (errStr != null)
  1591. return errStr;
  1592. if (arguments[0] == null)
  1593. return name + "関数の1番目の引数は省略できません";
  1594. return null;
  1595. }
  1596. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1597. {
  1598. Int64 value = arguments[0].GetIntValue(exm);
  1599. Int64 maxLv = arguments[1].GetIntValue(exm);
  1600. return (exm.VEvaluator.getPalamLv(value, maxLv));
  1601. }
  1602. }
  1603. private sealed class GetExpLVMethod : FunctionMethod
  1604. {
  1605. public GetExpLVMethod()
  1606. {
  1607. ReturnType = typeof(Int64);
  1608. argumentTypeArray = new[] { typeof(Int64), typeof(Int64) };
  1609. CanRestructure = false;
  1610. }
  1611. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  1612. {
  1613. string errStr = base.CheckArgumentType(name, arguments);
  1614. if (errStr != null)
  1615. return errStr;
  1616. if (arguments[0] == null)
  1617. return name + "関数の1番目の引数は省略できません";
  1618. return null;
  1619. }
  1620. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1621. {
  1622. Int64 value = arguments[0].GetIntValue(exm);
  1623. Int64 maxLv = arguments[1].GetIntValue(exm);
  1624. return (exm.VEvaluator.getExpLv(value, maxLv));
  1625. }
  1626. }
  1627. private sealed class FindElementMethod : FunctionMethod
  1628. {
  1629. public FindElementMethod(bool last)
  1630. {
  1631. ReturnType = typeof(Int64);
  1632. argumentTypeArray = null;
  1633. CanRestructure = true; //すべて定数項ならできるはず
  1634. HasUniqueRestructure = true;
  1635. isLast = last;
  1636. funcName = isLast ? "FINDLASTELEMENT" : "FINDELEMENT";
  1637. }
  1638. bool isLast;
  1639. string funcName;
  1640. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  1641. {
  1642. if (arguments.Length < 2)
  1643. return name + "関数には少なくとも2つの引数が必要です";
  1644. if (arguments.Length > 5)
  1645. return name + "関数の引数が多すぎます";
  1646. if (arguments[0] == null)
  1647. return name + "関数の1番目の引数は省略できません";
  1648. VariableTerm varToken = arguments[0] as VariableTerm;
  1649. if (varToken == null)
  1650. return name + "関数の1番目の引数が変数ではありません";
  1651. if (varToken.Identifier.IsArray2D || varToken.Identifier.IsArray3D)
  1652. return name + "関数は二重配列・三重配列には対応していません";
  1653. if (!varToken.Identifier.IsArray1D)
  1654. return name + "関数の1番目の引数が配列変数ではありません";
  1655. Type baseType = arguments[0].GetOperandType();
  1656. if (arguments[1] == null)
  1657. return name + "関数の2番目の引数は省略できません";
  1658. if (arguments[1].GetOperandType() != baseType)
  1659. return name + "関数の2番目の引数の型が正しくありません";
  1660. if ((arguments.Length >= 3) && (arguments[2] != null) && (arguments[2].GetOperandType() != typeof(Int64)))
  1661. return name + "関数の3番目の引数の型が正しくありません";
  1662. if ((arguments.Length >= 4) && (arguments[3] != null) && (arguments[3].GetOperandType() != typeof(Int64)))
  1663. return name + "関数の4番目の引数の型が正しくありません";
  1664. if ((arguments.Length >= 5) && (arguments[4] != null) && (arguments[4].GetOperandType() != typeof(Int64)))
  1665. return name + "関数の5番目の引数の型が正しくありません";
  1666. return null;
  1667. }
  1668. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1669. {
  1670. bool isExact = false;
  1671. VariableTerm varTerm = (VariableTerm)arguments[0];
  1672. Int64 start = (arguments.Length > 2 && arguments[2] != null) ? arguments[2].GetIntValue(exm) : 0;
  1673. Int64 end = (arguments.Length > 3 && arguments[3] != null) ? arguments[3].GetIntValue(exm) : varTerm.GetLength();
  1674. if (arguments.Length > 4 && arguments[4] != null)
  1675. isExact = (arguments[4].GetIntValue(exm) != 0);
  1676. FixedVariableTerm p = varTerm.GetFixedVariableTerm(exm);
  1677. p.IsArrayRangeValid(start, end, funcName, 3L, 4L);
  1678. if (arguments[0].GetOperandType() == typeof(Int64))
  1679. {
  1680. Int64 targetValue = arguments[1].GetIntValue(exm);
  1681. return exm.VEvaluator.FindElement(p, targetValue, start, end, isExact, isLast);
  1682. }
  1683. Regex targetString = null;
  1684. try
  1685. {
  1686. targetString = new Regex(arguments[1].GetStrValue(exm));
  1687. }
  1688. catch (ArgumentException)
  1689. {
  1690. throw new CodeEE("第2引数が正規表現として不正です");
  1691. }
  1692. return exm.VEvaluator.FindElement(p, targetString, start, end, isExact, isLast);
  1693. }
  1694. public override bool UniqueRestructure(ExpressionMediator exm, IOperandTerm[] arguments)
  1695. {
  1696. bool isConst = true;
  1697. arguments[0].Restructure(exm);
  1698. VariableTerm varToken = arguments[0] as VariableTerm;
  1699. isConst = varToken.Identifier.IsConst;
  1700. for (int i = 1; i < arguments.Length; i++)
  1701. {
  1702. if (arguments[i] == null)
  1703. continue;
  1704. arguments[i] = arguments[i].Restructure(exm);
  1705. if (isConst && !(arguments[i] is SingleTerm))
  1706. isConst = false;
  1707. }
  1708. return isConst;
  1709. }
  1710. }
  1711. private sealed class InRangeMethod : FunctionMethod
  1712. {
  1713. public InRangeMethod()
  1714. {
  1715. ReturnType = typeof(Int64);
  1716. argumentTypeArray = new[] { typeof(Int64), typeof(Int64), typeof(Int64) };
  1717. CanRestructure = true;
  1718. }
  1719. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1720. {
  1721. Int64 value = arguments[0].GetIntValue(exm);
  1722. Int64 min = arguments[1].GetIntValue(exm);
  1723. Int64 max = arguments[2].GetIntValue(exm);
  1724. return ((value >= min) && (value <= max)) ? 1L : 0L;
  1725. }
  1726. }
  1727. private sealed class InRangeArrayMethod : FunctionMethod
  1728. {
  1729. public InRangeArrayMethod()
  1730. {
  1731. ReturnType = typeof(Int64);
  1732. argumentTypeArray = null;
  1733. CanRestructure = false;
  1734. }
  1735. public InRangeArrayMethod(bool isChara)
  1736. {
  1737. ReturnType = typeof(Int64);
  1738. argumentTypeArray = null;
  1739. isCharaRange = isChara;
  1740. CanRestructure = false;
  1741. }
  1742. private bool isCharaRange;
  1743. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  1744. {
  1745. if (arguments.Length < 2)
  1746. return name + "関数には少なくとも2つの引数が必要です";
  1747. if (arguments.Length > 6)
  1748. return name + "関数の引数が多すぎます";
  1749. if (arguments[0] == null)
  1750. return name + "関数の1番目の引数は省略できません";
  1751. if (!(arguments[0] is VariableTerm))
  1752. return name + "関数の1番目の引数が変数ではありません";
  1753. VariableTerm varToken = (VariableTerm)arguments[0];
  1754. if (isCharaRange && !varToken.Identifier.IsCharacterData)
  1755. return name + "関数の1番目の引数がキャラクタ変数ではありません";
  1756. if (!isCharaRange && (varToken.Identifier.IsArray2D || varToken.Identifier.IsArray3D))
  1757. return name + "関数は二重配列・三重配列には対応していません";
  1758. if (!isCharaRange && !varToken.Identifier.IsArray1D)
  1759. return name + "関数の1番目の引数が配列変数ではありません";
  1760. if (!varToken.IsInteger)
  1761. return name + "関数の1番目の引数が数値型変数ではありません";
  1762. if (arguments[1] == null)
  1763. return name + "関数の2番目の引数は省略できません";
  1764. if (arguments[1].GetOperandType() != typeof(Int64))
  1765. return name + "関数の2番目の引数が数値型ではありません";
  1766. if (arguments[2] == null)
  1767. return name + "関数の3番目の引数は省略できません";
  1768. if (arguments[2].GetOperandType() != typeof(Int64))
  1769. return name + "関数の3番目の引数が数値型ではありません";
  1770. if ((arguments.Length >= 4) && (arguments[3] != null) && (arguments[3].GetOperandType() != typeof(Int64)))
  1771. return name + "関数の4番目の引数の型が正しくありません";
  1772. if ((arguments.Length >= 5) && (arguments[4] != null) && (arguments[4].GetOperandType() != typeof(Int64)))
  1773. return name + "関数の5番目の引数の型が正しくありません";
  1774. return null;
  1775. }
  1776. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1777. {
  1778. Int64 min = arguments[1].GetIntValue(exm);
  1779. Int64 max = arguments[2].GetIntValue(exm);
  1780. VariableTerm varTerm = arguments[0] as VariableTerm;
  1781. Int64 start = (arguments.Length > 3 && arguments[3] != null) ? arguments[3].GetIntValue(exm) : 0;
  1782. Int64 end = (arguments.Length > 4 && arguments[4] != null) ? arguments[4].GetIntValue(exm) : (isCharaRange ? exm.VEvaluator.CHARANUM : varTerm.GetLength());
  1783. FixedVariableTerm p = varTerm.GetFixedVariableTerm(exm);
  1784. if (!isCharaRange)
  1785. {
  1786. p.IsArrayRangeValid(start, end, "INRANGEARRAY", 4L, 5L);
  1787. return (exm.VEvaluator.GetInRangeArray(p, min, max, start, end));
  1788. }
  1789. Int64 charaNum = exm.VEvaluator.CHARANUM;
  1790. if (start >= charaNum || start < 0 || end > charaNum || end < 0)
  1791. throw new CodeEE("INRANGECARRAY関数の範囲指定がキャラクタ配列の範囲を超えています(" + start + "~" + end + ")");
  1792. return (exm.VEvaluator.GetInRangeArrayChara(p, min, max, start, end));
  1793. }
  1794. }
  1795. #endregion
  1796. #region 文字列操作系
  1797. private sealed class StrlenMethod : FunctionMethod
  1798. {
  1799. public StrlenMethod()
  1800. {
  1801. ReturnType = typeof(Int64);
  1802. argumentTypeArray = new[] { typeof(string) };
  1803. CanRestructure = true;
  1804. }
  1805. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1806. {
  1807. string str = arguments[0].GetStrValue(exm, true); // JVN: Return TR string's length when necessary
  1808. str = Translation.translate(str, "ALL", true); //Bartoum : Check if it's not present in all (Ex: Names in gvt for Pedo)
  1809. //Translation.isStrlens = false;
  1810. return (LangManager.GetStrlenLang(str));
  1811. }
  1812. }
  1813. private sealed class StrlenuMethod : FunctionMethod
  1814. {
  1815. public StrlenuMethod()
  1816. {
  1817. ReturnType = typeof(Int64);
  1818. argumentTypeArray = new[] { typeof(string) };
  1819. CanRestructure = true;
  1820. }
  1821. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1822. {
  1823. string str = arguments[0].GetStrValue(exm, true); // JVN: Return TR string's length when necessary
  1824. str = Translation.translate(str, "ALL", true); //Bartoum : Check if it's not present in all (Ex: Names in gvt for Pedo)
  1825. return (str.Length);
  1826. }
  1827. }
  1828. private sealed class SubstringMethod : FunctionMethod
  1829. {
  1830. public SubstringMethod()
  1831. {
  1832. ReturnType = typeof(string);
  1833. argumentTypeArray = null;
  1834. CanRestructure = true;
  1835. }
  1836. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  1837. {
  1838. //通常3つ、2つ省略可能で1~3の引数が必要。
  1839. if (arguments.Length < 1)
  1840. return name + "関数には少なくとも1つの引数が必要です";
  1841. if (arguments.Length > 3)
  1842. return name + "関数の引数が多すぎます";
  1843. if (arguments[0] == null)
  1844. return name + "関数の1番目の引数は省略できません";
  1845. if (arguments[0].GetOperandType() != typeof(string))
  1846. return name + "関数の1番目の引数の型が正しくありません";
  1847. //2、3は省略可能
  1848. if ((arguments.Length >= 2) && (arguments[1] != null) && (arguments[1].GetOperandType() != typeof(Int64)))
  1849. return name + "関数の2番目の引数の型が正しくありません";
  1850. if ((arguments.Length >= 3) && (arguments[2] != null) && (arguments[2].GetOperandType() != typeof(Int64)))
  1851. return name + "関数の3番目の引数の型が正しくありません";
  1852. return null;
  1853. }
  1854. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  1855. {
  1856. string str = arguments[0].GetStrValue(exm);
  1857. int start = 0;
  1858. int length = -1;
  1859. if ((arguments.Length >= 2) && (arguments[1] != null))
  1860. start = (int)arguments[1].GetIntValue(exm);
  1861. if ((arguments.Length >= 3) && (arguments[2] != null))
  1862. length = (int)arguments[2].GetIntValue(exm);
  1863. return (LangManager.GetSubStringLang(str, start, length));
  1864. }
  1865. }
  1866. private sealed class SubstringuMethod : FunctionMethod
  1867. {
  1868. public SubstringuMethod()
  1869. {
  1870. ReturnType = typeof(string);
  1871. argumentTypeArray = null;
  1872. CanRestructure = true;
  1873. }
  1874. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  1875. {
  1876. //通常3つ、2つ省略可能で1~3の引数が必要。
  1877. if (arguments.Length < 1)
  1878. return name + "関数には少なくとも1つの引数が必要です";
  1879. if (arguments.Length > 3)
  1880. return name + "関数の引数が多すぎます";
  1881. if (arguments[0] == null)
  1882. return name + "関数の1番目の引数は省略できません";
  1883. if (arguments[0].GetOperandType() != typeof(string))
  1884. return name + "関数の1番目の引数の型が正しくありません";
  1885. //2、3は省略可能
  1886. if ((arguments.Length >= 2) && (arguments[1] != null) && (arguments[1].GetOperandType() != typeof(Int64)))
  1887. return name + "関数の2番目の引数の型が正しくありません";
  1888. if ((arguments.Length >= 3) && (arguments[2] != null) && (arguments[2].GetOperandType() != typeof(Int64)))
  1889. return name + "関数の3番目の引数の型が正しくありません";
  1890. return null;
  1891. }
  1892. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  1893. {
  1894. string str = arguments[0].GetStrValue(exm);
  1895. int start = 0;
  1896. int length = -1;
  1897. if ((arguments.Length >= 2) && (arguments[1] != null))
  1898. start = (int)arguments[1].GetIntValue(exm);
  1899. if ((arguments.Length >= 3) && (arguments[2] != null))
  1900. length = (int)arguments[2].GetIntValue(exm);
  1901. if ((start >= str.Length) || (length == 0))
  1902. return ("");
  1903. if ((length < 0) || (length > str.Length))
  1904. length = str.Length;
  1905. if (start <= 0)
  1906. {
  1907. if (length == str.Length)
  1908. return (str);
  1909. start = 0;
  1910. }
  1911. if ((start + length) > str.Length)
  1912. length = str.Length - start;
  1913. return (str.Substring(start, length));
  1914. }
  1915. }
  1916. private sealed class StrfindMethod : FunctionMethod
  1917. {
  1918. public StrfindMethod(bool unicode)
  1919. {
  1920. ReturnType = typeof(Int64);
  1921. argumentTypeArray = null;
  1922. CanRestructure = true;
  1923. this.unicode = unicode;
  1924. }
  1925. bool unicode;
  1926. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  1927. {
  1928. //通常3つ、1つ省略可能で2~3の引数が必要。
  1929. if (arguments.Length < 2)
  1930. return name + "関数には少なくとも2つの引数が必要です";
  1931. if (arguments.Length > 3)
  1932. return name + "関数の引数が多すぎます";
  1933. if (arguments[0] == null)
  1934. return name + "関数の1番目の引数は省略できません";
  1935. if (arguments[0].GetOperandType() != typeof(string))
  1936. return name + "関数の1番目の引数の型が正しくありません";
  1937. if (arguments[1] == null)
  1938. return name + "関数の2番目の引数は省略できません";
  1939. if (arguments[1].GetOperandType() != typeof(string))
  1940. return name + "関数の2番目の引数の型が正しくありません";
  1941. //3つ目は省略可能
  1942. if ((arguments.Length >= 3) && (arguments[2] != null) && (arguments[2].GetOperandType() != typeof(Int64)))
  1943. return name + "関数の3番目の引数の型が正しくありません";
  1944. return null;
  1945. }
  1946. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1947. {
  1948. string target = arguments[0].GetStrValue(exm);
  1949. string word = arguments[1].GetStrValue(exm);
  1950. int JISstart = 0, UFTstart = 0;
  1951. if ((arguments.Length >= 3) && (arguments[2] != null))
  1952. {
  1953. if (unicode)
  1954. {
  1955. UFTstart = (int)arguments[2].GetIntValue(exm);
  1956. }
  1957. else
  1958. {
  1959. JISstart = (int)arguments[2].GetIntValue(exm);
  1960. UFTstart = LangManager.GetUFTIndex(target, JISstart);
  1961. }
  1962. }
  1963. if (UFTstart < 0 || UFTstart >= target.Length)
  1964. return (-1);
  1965. int index = target.IndexOf(word, UFTstart);
  1966. if (index > 0 && !unicode)
  1967. {
  1968. string subStr = target.Substring(0, index);
  1969. index = LangManager.GetStrlenLang(subStr);
  1970. }
  1971. return (index);
  1972. }
  1973. }
  1974. private sealed class StrCountMethod : FunctionMethod
  1975. {
  1976. public StrCountMethod()
  1977. {
  1978. ReturnType = typeof(Int64);
  1979. argumentTypeArray = new[] { typeof(string), typeof(string) };
  1980. CanRestructure = true;
  1981. }
  1982. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  1983. {
  1984. Regex reg = null;
  1985. try
  1986. {
  1987. reg = new Regex(arguments[1].GetStrValue(exm));
  1988. }
  1989. catch (ArgumentException e)
  1990. {
  1991. throw new CodeEE("第2引数が正規表現として不正です:" + e.Message);
  1992. }
  1993. return (reg.Matches(arguments[0].GetStrValue(exm)).Count);
  1994. }
  1995. }
  1996. private sealed class ToStrMethod : FunctionMethod
  1997. {
  1998. public ToStrMethod()
  1999. {
  2000. ReturnType = typeof(string);
  2001. argumentTypeArray = null;
  2002. CanRestructure = true;
  2003. }
  2004. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  2005. {
  2006. //通常2つ、1つ省略可能で1~2の引数が必要。
  2007. if (arguments.Length < 1)
  2008. return name + "関数には少なくとも1つの引数が必要です";
  2009. if (arguments.Length > 2)
  2010. return name + "関数の引数が多すぎます";
  2011. if (arguments[0] == null)
  2012. return name + "関数の1番目の引数は省略できません";
  2013. if (arguments[0].GetOperandType() != typeof(Int64))
  2014. return name + "関数の1番目の引数の型が正しくありません";
  2015. if ((arguments.Length >= 2) && (arguments[1] != null) && (arguments[1].GetOperandType() != typeof(string)))
  2016. return name + "関数の2番目の引数の型が正しくありません";
  2017. return null;
  2018. }
  2019. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  2020. {
  2021. Int64 i = arguments[0].GetIntValue(exm);
  2022. if ((arguments.Length < 2) || (arguments[1] == null))
  2023. return (i.ToString());
  2024. string format = arguments[1].GetStrValue(exm);
  2025. string ret;
  2026. try
  2027. {
  2028. ret = i.ToString(format);
  2029. }
  2030. catch (FormatException)
  2031. {
  2032. throw new CodeEE("TOSTR関数の書式指定が間違っています");
  2033. }
  2034. return (ret);
  2035. }
  2036. }
  2037. private sealed class ToIntMethod : FunctionMethod
  2038. {
  2039. public ToIntMethod()
  2040. {
  2041. ReturnType = typeof(Int64);
  2042. argumentTypeArray = new[] { typeof(string) };
  2043. CanRestructure = true;
  2044. }
  2045. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  2046. {
  2047. string str = arguments[0].GetStrValue(exm);
  2048. if (str == null || str == "")
  2049. return (0);
  2050. //全角文字が入ってるなら無条件で0を返す
  2051. if (str.Length < LangManager.GetStrlenLang(str))
  2052. return (0);
  2053. StringStream st = new StringStream(str);
  2054. if (!char.IsDigit(st.Current) && st.Current != '+' && st.Current != '-')
  2055. return (0);
  2056. if ((st.Current == '+' || st.Current == '-') && !char.IsDigit(st.Next))
  2057. return (0);
  2058. Int64 ret = LexicalAnalyzer.ReadInt64(st, true);
  2059. if (!st.EOS)
  2060. {
  2061. if (st.Current == '.')
  2062. {
  2063. st.ShiftNext();
  2064. while (!st.EOS)
  2065. {
  2066. if (!char.IsDigit(st.Current))
  2067. return (0);
  2068. st.ShiftNext();
  2069. }
  2070. }
  2071. else
  2072. return (0);
  2073. }
  2074. return ret;
  2075. }
  2076. }
  2077. //難読化用属性。enum.ToString()やenum.Parse()を行うなら(Exclude=true)にすること。
  2078. [Obfuscation(Exclude = false)]
  2079. //TOUPPER等の処理を汎用化するためのenum
  2080. enum StrFormType
  2081. {
  2082. Upper = 0,
  2083. Lower = 1,
  2084. Half = 2,
  2085. Full = 3
  2086. }
  2087. private sealed class StrChangeStyleMethod : FunctionMethod
  2088. {
  2089. StrFormType strType;
  2090. public StrChangeStyleMethod(StrFormType type)
  2091. {
  2092. ReturnType = typeof(string);
  2093. argumentTypeArray = new[] { typeof(string) };
  2094. strType = type;
  2095. CanRestructure = true;
  2096. }
  2097. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  2098. {
  2099. string str = arguments[0].GetStrValue(exm, tryTranslate);
  2100. if (str == null || str == "")
  2101. return ("");
  2102. switch (strType)
  2103. {
  2104. case StrFormType.Upper:
  2105. return (str.ToUpper());
  2106. case StrFormType.Lower:
  2107. return (str.ToLower());
  2108. case StrFormType.Half:
  2109. return (Strings.StrConv(str, VbStrConv.Narrow, Config.Language));
  2110. case StrFormType.Full:
  2111. return (Strings.StrConv(str, VbStrConv.Wide, Config.Language));
  2112. }
  2113. return ("");
  2114. }
  2115. }
  2116. private sealed class LineIsEmptyMethod : FunctionMethod
  2117. {
  2118. public LineIsEmptyMethod()
  2119. {
  2120. ReturnType = typeof(Int64);
  2121. argumentTypeArray = new Type[] { };
  2122. CanRestructure = false;
  2123. }
  2124. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  2125. {
  2126. return GlobalStatic.Console.EmptyLine ? 1L : 0L;
  2127. }
  2128. }
  2129. private sealed class ReplaceMethod : FunctionMethod
  2130. {
  2131. //Bartoum : Replace method
  2132. public ReplaceMethod()
  2133. {
  2134. ReturnType = typeof(string);
  2135. argumentTypeArray = new[] { typeof(string), typeof(string), typeof(string) };
  2136. CanRestructure = true;
  2137. }
  2138. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  2139. {
  2140. string baseString = arguments[0].GetStrValue(exm, tryTranslate);
  2141. Regex reg;
  2142. try
  2143. {
  2144. reg = new Regex(arguments[1].GetStrValue(exm, tryTranslate));
  2145. }
  2146. catch (ArgumentException e)
  2147. {
  2148. throw new CodeEE("第2引数が正規表現として不正です:" + e.Message);
  2149. }
  2150. return (reg.Replace(baseString, arguments[2].GetStrValue(exm, tryTranslate)));
  2151. }
  2152. }
  2153. private sealed class UnicodeMethod : FunctionMethod
  2154. {
  2155. public UnicodeMethod()
  2156. {
  2157. ReturnType = typeof(string);
  2158. argumentTypeArray = new[] { typeof(Int64) };
  2159. CanRestructure = true;
  2160. }
  2161. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  2162. {
  2163. Int64 i = arguments[0].GetIntValue(exm);
  2164. if ((i < 0) || (i > 0xFFFF))
  2165. throw new CodeEE("UNICODE関数に範囲外の値(" + i + ")が渡されました");
  2166. string s = new string(new[] { (char)i });// char.ConvertFromUtf32(i);
  2167. return (s);
  2168. }
  2169. }
  2170. private sealed class UnicodeByteMethod : FunctionMethod
  2171. {
  2172. public UnicodeByteMethod()
  2173. {
  2174. ReturnType = typeof(Int64);
  2175. argumentTypeArray = new[] { typeof(string) };
  2176. CanRestructure = true;
  2177. }
  2178. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  2179. {
  2180. string target = arguments[0].GetStrValue(exm);
  2181. int length = Encoding.UTF32.GetEncoder().GetByteCount(target.ToCharArray(), 0, target.Length, false);
  2182. byte[] bytes = new byte[length];
  2183. Encoding.UTF32.GetEncoder().GetBytes(target.ToCharArray(), 0, target.Length, bytes, 0, false);
  2184. Int64 i = BitConverter.ToInt32(bytes, 0);
  2185. return (i);
  2186. }
  2187. }
  2188. private sealed class ConvertIntMethod : FunctionMethod
  2189. {
  2190. public ConvertIntMethod()
  2191. {
  2192. ReturnType = typeof(string);
  2193. argumentTypeArray = new[] { typeof(Int64), typeof(Int64) };
  2194. CanRestructure = true;
  2195. }
  2196. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  2197. {
  2198. Int64 toBase = arguments[1].GetIntValue(exm);
  2199. if ((toBase != 2) && (toBase != 8) && (toBase != 10) && (toBase != 16))
  2200. throw new CodeEE("CONVERT関数の第2引数は2, 8, 10, 16のいずれかでなければなりません");
  2201. return Convert.ToString(arguments[0].GetIntValue(exm), (int)toBase);
  2202. }
  2203. }
  2204. private sealed class IsNumericMethod : FunctionMethod
  2205. {
  2206. public IsNumericMethod()
  2207. {
  2208. ReturnType = typeof(Int64);
  2209. argumentTypeArray = new[] { typeof(string) };
  2210. CanRestructure = true;
  2211. }
  2212. public override long GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  2213. {
  2214. string baseStr = arguments[0].GetStrValue(exm);
  2215. //全角文字があるなら数値ではない
  2216. if (baseStr.Length < LangManager.GetStrlenLang(baseStr))
  2217. return (0);
  2218. StringStream st = new StringStream(baseStr);
  2219. if (!char.IsDigit(st.Current) && st.Current != '+' && st.Current != '-')
  2220. return (0);
  2221. if ((st.Current == '+' || st.Current == '-') && !char.IsDigit(st.Next))
  2222. return (0);
  2223. Int64 ret = LexicalAnalyzer.ReadInt64(st, true);
  2224. if (!st.EOS)
  2225. {
  2226. if (st.Current == '.')
  2227. {
  2228. st.ShiftNext();
  2229. while (!st.EOS)
  2230. {
  2231. if (!char.IsDigit(st.Current))
  2232. return (0);
  2233. st.ShiftNext();
  2234. }
  2235. }
  2236. else
  2237. return (0);
  2238. }
  2239. return 1;
  2240. }
  2241. }
  2242. private sealed class EscapeMethod : FunctionMethod
  2243. {
  2244. public EscapeMethod()
  2245. {
  2246. ReturnType = typeof(string);
  2247. argumentTypeArray = new[] { typeof(string) };
  2248. CanRestructure = true;
  2249. }
  2250. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  2251. {
  2252. return Regex.Escape(arguments[0].GetStrValue(exm));
  2253. }
  2254. }
  2255. private sealed class EncodeToUniMethod : FunctionMethod
  2256. {
  2257. public EncodeToUniMethod()
  2258. {
  2259. ReturnType = typeof(Int64);
  2260. argumentTypeArray = new Type[] { null };
  2261. CanRestructure = true;
  2262. }
  2263. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  2264. {
  2265. //通常2つ、1つ省略可能で1~2の引数が必要。
  2266. if (arguments.Length < 1)
  2267. return name + "関数には少なくとも1つの引数が必要です";
  2268. if (arguments.Length > 2)
  2269. return name + "関数の引数が多すぎます";
  2270. if (arguments[0] == null)
  2271. return name + "関数の1番目の引数は省略できません";
  2272. if (arguments[0].GetOperandType() != typeof(string))
  2273. return name + "関数の1番目の引数の型が正しくありません";
  2274. if ((arguments.Length >= 2) && (arguments[1] != null) && (arguments[1].GetOperandType() != typeof(Int64)))
  2275. return name + "関数の2番目の引数の型が正しくありません";
  2276. return null;
  2277. }
  2278. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  2279. {
  2280. string baseStr = arguments[0].GetStrValue(exm);
  2281. if (baseStr.Length == 0)
  2282. return -1;
  2283. Int64 position = (arguments.Length > 1 && arguments[1] != null) ? arguments[1].GetIntValue(exm) : 0;
  2284. if (position < 0)
  2285. throw new CodeEE("ENCOIDETOUNI関数の第2引数(" + position + ")が負の値です");
  2286. if (position >= baseStr.Length)
  2287. throw new CodeEE("ENCOIDETOUNI関数の第2引数(" + position + ")が第1引数の文字列(" + baseStr + ")の文字数を超えています");
  2288. return char.ConvertToUtf32(baseStr, (int)position);
  2289. }
  2290. }
  2291. public sealed class CharAtMethod : FunctionMethod
  2292. {
  2293. public CharAtMethod()
  2294. {
  2295. ReturnType = typeof(string);
  2296. argumentTypeArray = new[] { typeof(string), typeof(Int64) };
  2297. CanRestructure = true;
  2298. }
  2299. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  2300. {
  2301. string str = arguments[0].GetStrValue(exm);
  2302. Int64 pos = arguments[1].GetIntValue(exm);
  2303. if (pos < 0 || pos >= str.Length)
  2304. return "";
  2305. return str[(int)pos].ToString();
  2306. }
  2307. }
  2308. public sealed class GetLineStrMethod : FunctionMethod
  2309. {
  2310. public GetLineStrMethod()
  2311. {
  2312. ReturnType = typeof(string);
  2313. argumentTypeArray = new[] { typeof(string) };
  2314. CanRestructure = true;
  2315. }
  2316. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  2317. {
  2318. string str = arguments[0].GetStrValue(exm);
  2319. if (string.IsNullOrEmpty(str))
  2320. throw new CodeEE("GETLINESTR関数の引数が空文字列です");
  2321. return exm.Console.getStBar(str);
  2322. }
  2323. }
  2324. public sealed class StrFormMethod : FunctionMethod
  2325. {
  2326. public StrFormMethod()
  2327. {
  2328. ReturnType = typeof(string);
  2329. argumentTypeArray = new[] { typeof(string) };
  2330. HasUniqueRestructure = true;
  2331. CanRestructure = true;
  2332. }
  2333. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  2334. {
  2335. string str = arguments[0].GetStrValue(exm, tryTranslate);
  2336. string destStr = str;
  2337. try
  2338. {
  2339. StrFormWord wt = LexicalAnalyzer.AnalyseFormattedString(new StringStream(str), FormStrEndWith.EoL, false);
  2340. StrForm strForm = StrForm.FromWordToken(wt);
  2341. destStr = strForm.GetString(exm, tryTranslate);
  2342. }
  2343. catch(CodeEE e)
  2344. {
  2345. throw new CodeEE("STRFORM関数:文字列\"" + str + "\"の展開エラー:" + e.Message);
  2346. }
  2347. catch
  2348. {
  2349. throw new CodeEE("STRFORM関数:文字列\"" + str+ "\"の展開処理中にエラーが発生しました");
  2350. }
  2351. return destStr;
  2352. }
  2353. public override bool UniqueRestructure(ExpressionMediator exm, IOperandTerm[] arguments)
  2354. {
  2355. arguments[0].Restructure(exm);
  2356. //引数が文字列式等ならお手上げなので諦める
  2357. if (!(arguments[0] is SingleTerm) && !(arguments[0] is VariableTerm))
  2358. return false;
  2359. //引数が確定値でない文字列変数なら無条件で不可(結果が可変なため)
  2360. if ((arguments[0] is VariableTerm) && !(((VariableTerm)arguments[0]).Identifier.IsConst))
  2361. return false;
  2362. string str = arguments[0].GetStrValue(exm);
  2363. try
  2364. {
  2365. StrFormWord wt = LexicalAnalyzer.AnalyseFormattedString(new StringStream(str), FormStrEndWith.EoL, false);
  2366. StrForm strForm = StrForm.FromWordToken(wt);
  2367. if (!strForm.IsConst)
  2368. return false;
  2369. }
  2370. catch
  2371. {
  2372. //パースできないのはエラーがあるかここではわからないからとりあえず考えない
  2373. return false;
  2374. }
  2375. return true;
  2376. }
  2377. }
  2378. public sealed class JoinMethod : FunctionMethod
  2379. {
  2380. public JoinMethod()
  2381. {
  2382. ReturnType = typeof(string);
  2383. argumentTypeArray = null;
  2384. HasUniqueRestructure = false;
  2385. CanRestructure = false;
  2386. }
  2387. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  2388. {
  2389. if (arguments.Length < 1)
  2390. return name + "関数には少なくとも1つの引数が必要です";
  2391. if (arguments.Length > 4)
  2392. return name + "関数の引数が多すぎます";
  2393. if (arguments[0] == null)
  2394. return name + "関数の1番目の引数は省略できません";
  2395. if (!(arguments[0] is VariableTerm))
  2396. return name + "関数の1番目の引数が変数ではありません";
  2397. VariableTerm varToken = (VariableTerm)arguments[0];
  2398. if (!varToken.Identifier.IsArray1D && !varToken.Identifier.IsArray2D && !varToken.Identifier.IsArray3D)
  2399. return name + "関数の1番目の引数が配列変数ではありません";
  2400. if (arguments.Length == 1)
  2401. return null;
  2402. if ((arguments[1] != null) && (arguments[1].GetOperandType() != typeof(string)))
  2403. return name + "関数の2番目の変数が文字列ではありません";
  2404. if (arguments.Length == 2)
  2405. return null;
  2406. if ((arguments[2] != null) && (arguments[2].GetOperandType() != typeof(Int64)))
  2407. return name + "関数の3番目の変数が数値ではありません";
  2408. if (arguments.Length == 3)
  2409. return null;
  2410. if ((arguments[3] != null) && (arguments[3].GetOperandType() != typeof(Int64)))
  2411. return name + "関数の4番目の変数が数値ではありません";
  2412. return null;
  2413. }
  2414. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = true)
  2415. {
  2416. VariableTerm varTerm = (VariableTerm)arguments[0];
  2417. string delimiter = (arguments.Length >= 2 && arguments[1] != null) ? arguments[1].GetStrValue(exm) : ",";
  2418. Int64 index1 = (arguments.Length >= 3 && arguments[2] != null) ? arguments[2].GetIntValue(exm) : 0;
  2419. Int64 index2 = (arguments.Length == 4 && arguments[3] != null) ? arguments[3].GetIntValue(exm) : varTerm.GetLastLength();
  2420. FixedVariableTerm p = varTerm.GetFixedVariableTerm(exm);
  2421. p.IsArrayRangeValid(index1, index2, "STRJOIN", 2L, 3L);
  2422. return (exm.VEvaluator.GetJoinedStr(p, delimiter, index1, index2));
  2423. }
  2424. }
  2425. public sealed class GetConfigMethod : FunctionMethod
  2426. {
  2427. public GetConfigMethod(bool typeisInt)
  2428. {
  2429. if(typeisInt)
  2430. {
  2431. funcname = "GETCONFIG";
  2432. ReturnType = typeof(Int64);
  2433. }
  2434. else
  2435. {
  2436. funcname = "GETCONFIGS";
  2437. ReturnType = typeof(string);
  2438. }
  2439. argumentTypeArray = new[] { typeof(string) };
  2440. CanRestructure = true;
  2441. }
  2442. private readonly string funcname;
  2443. private SingleTerm getSingleTerm(ExpressionMediator exm, IOperandTerm[] arguments)
  2444. {
  2445. string str = arguments[0].GetStrValue(exm);
  2446. if(str == null || str.Length == 0)
  2447. throw new CodeEE(funcname + "関数に空文字列が渡されました");
  2448. string errMes = null;
  2449. SingleTerm term = ConfigData.Instance.GetConfigValueInERB(str, ref errMes);
  2450. if(errMes != null)
  2451. throw new CodeEE(funcname + "関数:" + errMes);
  2452. return term;
  2453. }
  2454. public override Int64 GetIntValue(ExpressionMediator exm, IOperandTerm[] arguments)
  2455. {
  2456. if(ReturnType != typeof(Int64))
  2457. throw new ExeEE(funcname + "関数:不正な呼び出し");
  2458. SingleTerm term = getSingleTerm(exm, arguments);
  2459. if(term.GetOperandType() != typeof(Int64))
  2460. throw new CodeEE(funcname + "関数:型が違います(GETCONFIGS関数を使用してください)");
  2461. return term.Int;
  2462. }
  2463. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  2464. {
  2465. if(ReturnType != typeof(string))
  2466. throw new ExeEE(funcname + "関数:不正な呼び出し");
  2467. SingleTerm term = getSingleTerm(exm, arguments);
  2468. if (term.GetOperandType() != typeof(string))
  2469. throw new CodeEE(funcname + "関数:型が違います(GETCONFIG関数を使用してください)");
  2470. return term.Str;
  2471. }
  2472. }
  2473. #endregion
  2474. #region html系
  2475. private sealed class HtmlGetPrintedStrMethod : FunctionMethod
  2476. {
  2477. public HtmlGetPrintedStrMethod()
  2478. {
  2479. ReturnType = typeof(string);
  2480. argumentTypeArray = null;
  2481. CanRestructure = false;
  2482. }
  2483. public override string CheckArgumentType(string name, IOperandTerm[] arguments)
  2484. {
  2485. //通常1つ。省略可能。
  2486. if (arguments.Length > 1)
  2487. return name + "関数の引数が多すぎます";
  2488. if (arguments.Length == 0|| arguments[0] == null)
  2489. return null;
  2490. if (arguments[0].GetOperandType() != typeof(Int64))
  2491. return name + "関数の1番目の引数の型が正しくありません";
  2492. return null;
  2493. }
  2494. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  2495. {
  2496. //Int64 lineNo = 0;
  2497. //if (arguments.Length > 0)
  2498. // lineNo = arguments[0].GetIntValue(exm);
  2499. //if (lineNo < 0)
  2500. // throw new CodeEE("引数を0未満にできません");
  2501. //ConsoleDisplayLine[] dispLines = exm.Console.GetDisplayLines(lineNo);
  2502. //if (dispLines == null)
  2503. // return "";
  2504. // return HtmlManager.DisplayLine2Html(dispLines, true);
  2505. return "";
  2506. }
  2507. }
  2508. private sealed class HtmlPopPrintingStrMethod : FunctionMethod
  2509. {
  2510. public HtmlPopPrintingStrMethod()
  2511. {
  2512. ReturnType = typeof(string);
  2513. argumentTypeArray = new Type[] { };
  2514. CanRestructure = false;
  2515. }
  2516. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  2517. {
  2518. //ConsoleDisplayLine[] dispLines = exm.Console.PopDisplayingLines();
  2519. //if (dispLines == null)
  2520. // return "";
  2521. //return HtmlManager.DisplayLine2Html(dispLines, false);
  2522. return "";
  2523. }
  2524. }
  2525. private sealed class HtmlToPlainTextMethod : FunctionMethod
  2526. {
  2527. public HtmlToPlainTextMethod()
  2528. {
  2529. ReturnType = typeof(string);
  2530. argumentTypeArray = new[] { typeof(string) };
  2531. CanRestructure = false;
  2532. }
  2533. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  2534. {
  2535. //return HtmlManager.Html2PlainText(arguments[0].GetStrValue(exm));
  2536. return "";
  2537. }
  2538. }
  2539. private sealed class HtmlEscapeMethod : FunctionMethod
  2540. {
  2541. public HtmlEscapeMethod()
  2542. {
  2543. ReturnType = typeof(string);
  2544. argumentTypeArray = new[] { typeof(string) };
  2545. CanRestructure = false;
  2546. }
  2547. public override string GetStrValue(ExpressionMediator exm, IOperandTerm[] arguments, bool tryTranslate = false)
  2548. {
  2549. //return HtmlManager.Escape(arguments[0].GetStrValue(exm));
  2550. return System.Security.SecurityElement.Escape(arguments[0].GetStrValue(exm));
  2551. }
  2552. }
  2553. #endregion
  2554. }
  2555. }