Creator.Method.cs 115 KB

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