ValueOnStack.cs 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace CLRSharp
  5. {
  6. //栈上值类型,拆箱,装箱转换非常频繁,需要处理一下。
  7. //
  8. public class ValueOnStack
  9. {
  10. static Dictionary<Type, NumberType> typecode = null;
  11. public static NumberType GetTypeCode(Type type)
  12. {
  13. if (typecode == null)
  14. {
  15. typecode = new Dictionary<Type, NumberType>();
  16. //typecode[null] = 0;
  17. typecode[typeof(bool)] = NumberType.BOOL;
  18. typecode[typeof(sbyte)] = NumberType.SBYTE;
  19. typecode[typeof(byte)] = NumberType.BYTE;
  20. typecode[typeof(Int16)] = NumberType.INT16;
  21. typecode[typeof(UInt16)] = NumberType.UINT16;
  22. typecode[typeof(Int32)] = NumberType.INT32;
  23. typecode[typeof(UInt32)] = NumberType.UINT32;
  24. typecode[typeof(Int64)] = NumberType.INT64;
  25. typecode[typeof(UInt64)] = NumberType.UINT64;
  26. typecode[typeof(float)] = NumberType.FLOAT;
  27. typecode[typeof(double)] = NumberType.DOUBLE;
  28. typecode[typeof(IntPtr)] = NumberType.INTPTR;
  29. typecode[typeof(UIntPtr)] = NumberType.UINTPTR;
  30. typecode[typeof(decimal)] = NumberType.DECIMAL;
  31. typecode[typeof(char)] = NumberType.CHAR;
  32. }
  33. if (type.IsEnum) return NumberType.ENUM;
  34. NumberType t = NumberType.IsNotNumber;
  35. typecode.TryGetValue(type, out t);
  36. return t;
  37. }
  38. ////valuetype
  39. // public NumberType TypeOnDef;
  40. // public NumberOnStack TypeOnStack;
  41. // public IBox box;
  42. //public static IBox Make(ICLRType type)
  43. //{
  44. // return Make(type.TypeForSystem);
  45. //}
  46. public static VBox MakeVBox(ICLRType type)
  47. {
  48. return MakeVBox(type.TypeForSystem);
  49. }
  50. //public static IBox MakeBool(bool b)
  51. //{
  52. // BoxInt32 box = Make(NumberType.BOOL) as BoxInt32;
  53. // box.value = b ? 1 : 0;
  54. // return box;
  55. //}
  56. public static VBox MakeVBoxBool(bool b)
  57. {
  58. VBox box = MakeVBox(NumberType.BOOL);
  59. box.v32 = b ? 1 : 0;
  60. return box;
  61. }
  62. //public static IBox Make(System.Type type)
  63. //{
  64. // NumberType code = GetTypeCode(type);
  65. // return Make(code);
  66. //}
  67. public static VBox MakeVBox(System.Type type)
  68. {
  69. NumberType code = GetTypeCode(type);
  70. return MakeVBox(code);
  71. }
  72. //public static IBox Make(NumberType code)
  73. //{
  74. // switch (code)
  75. // {
  76. // case NumberType.BOOL:
  77. // case NumberType.SBYTE:
  78. // case NumberType.BYTE:
  79. // case NumberType.CHAR:
  80. // case NumberType.INT16:
  81. // case NumberType.UINT16:
  82. // case NumberType.INT32:
  83. // case NumberType.UINT32:
  84. // if (unusedInt32.Count > 0)
  85. // {
  86. // var b = unusedInt32.Dequeue();
  87. // b.type = code;
  88. // return b;
  89. // }
  90. // else
  91. // return new BoxInt32(code);
  92. // case NumberType.INT64:
  93. // case NumberType.UINT64:
  94. // if (unusedInt64.Count > 0)
  95. // {
  96. // var b = unusedInt64.Dequeue();
  97. // b.type = code;
  98. // return b;
  99. // }
  100. // else
  101. // return new BoxInt64(code);
  102. // case NumberType.FLOAT:
  103. // case NumberType.DOUBLE:
  104. // if (unusedIntFL.Count > 0)
  105. // {
  106. // var b = unusedIntFL.Dequeue();
  107. // b.type = code;
  108. // return b;
  109. // }
  110. // else
  111. // return new BoxDouble(code);
  112. // default:
  113. // return null;
  114. // }
  115. //}
  116. public static VBox MakeVBox(NumberType code)
  117. {
  118. switch (code)
  119. {
  120. case NumberType.BOOL:
  121. case NumberType.SBYTE:
  122. case NumberType.BYTE:
  123. case NumberType.CHAR:
  124. case NumberType.INT16:
  125. case NumberType.UINT16:
  126. case NumberType.INT32:
  127. case NumberType.UINT32:
  128. case NumberType.ENUM:
  129. if (unusedVBox.Count > 0)
  130. {
  131. var b = unusedVBox.Dequeue();
  132. b.typeStack = NumberOnStack.Int32;
  133. b.type = code;
  134. return b;
  135. }
  136. return new VBox(NumberOnStack.Int32, code);
  137. case NumberType.INT64:
  138. case NumberType.UINT64:
  139. if (unusedVBox.Count > 0)
  140. {
  141. var b = unusedVBox.Dequeue();
  142. b.typeStack = NumberOnStack.Int64;
  143. b.type = code;
  144. return b;
  145. }
  146. return new VBox(NumberOnStack.Int64, code);
  147. case NumberType.FLOAT:
  148. case NumberType.DOUBLE:
  149. if (unusedVBox.Count > 0)
  150. {
  151. var b = unusedVBox.Dequeue();
  152. b.typeStack = NumberOnStack.Double;
  153. b.type = code;
  154. return b;
  155. }
  156. return new VBox(NumberOnStack.Double, code);
  157. default:
  158. return null;
  159. }
  160. }
  161. //public static IBox Convert(IBox box, NumberType type)
  162. //{
  163. // switch (type)
  164. // {
  165. // case NumberType.BOOL:
  166. // case NumberType.SBYTE:
  167. // case NumberType.BYTE:
  168. // case NumberType.CHAR:
  169. // case NumberType.INT16:
  170. // case NumberType.UINT16:
  171. // case NumberType.INT32:
  172. // case NumberType.UINT32:
  173. // {
  174. // if (box is BoxInt32) return box;
  175. // BoxInt32 v32 = ValueOnStack.Make(type) as BoxInt32;
  176. // BoxInt64 b64 = box as BoxInt64;
  177. // BoxDouble bdb = box as BoxDouble;
  178. // if (b64 != null)
  179. // v32.value = (int)b64.value;
  180. // else
  181. // v32.value = (int)bdb.value;
  182. // return v32;
  183. // }
  184. // case NumberType.INT64:
  185. // case NumberType.UINT64:
  186. // {
  187. // if (box is BoxInt64) return box;
  188. // BoxInt64 v64 = ValueOnStack.Make(type) as BoxInt64;
  189. // BoxInt32 b32 = box as BoxInt32;
  190. // BoxDouble bdb = box as BoxDouble;
  191. // if (b32 != null)
  192. // v64.value = b32.value;
  193. // else
  194. // v64.value = (Int64)bdb.value;
  195. // return v64;
  196. // }
  197. // case NumberType.FLOAT:
  198. // case NumberType.DOUBLE:
  199. // {
  200. // if (box is BoxDouble) return box;
  201. // BoxDouble vdb = new BoxDouble(type);
  202. // BoxInt32 b32 = box as BoxInt32;
  203. // BoxInt64 b64 = box as BoxInt64;
  204. // if (b32 != null)
  205. // vdb.value = b32.value;
  206. // else
  207. // vdb.value = b64.value;
  208. // return vdb;
  209. // }
  210. // default:
  211. // return null;
  212. // }
  213. //}
  214. public static VBox Convert(VBox box, NumberType type)
  215. {
  216. VBox b = MakeVBox(type);
  217. b.Set(box);
  218. return b;
  219. }
  220. //public static Queue<IBox> unusedInt32 = new Queue<IBox>();
  221. //public static Queue<IBox> unusedInt64 = new Queue<IBox>();
  222. //public static Queue<IBox> unusedIntFL = new Queue<IBox>();
  223. public static Queue<VBox> unusedVBox = new Queue<VBox>();
  224. public static void UnUse(VBox box)
  225. {
  226. box.refcount = 0;
  227. unusedVBox.Enqueue(box);
  228. }
  229. //public static void UnUse(IBox box)
  230. //{
  231. // switch (box.typeStack)
  232. // {
  233. // case NumberOnStack.Int32:
  234. // unusedInt32.Enqueue(box);
  235. // break;
  236. // case NumberOnStack.Int64:
  237. // unusedInt64.Enqueue(box);
  238. // break;
  239. // case NumberOnStack.Double:
  240. // unusedIntFL.Enqueue(box);
  241. // break;
  242. // }
  243. //}
  244. }
  245. public enum NumberType
  246. {
  247. IsNotNumber = 0,
  248. SBYTE = 1,
  249. BYTE = 2,
  250. INT16 = 3,
  251. UINT16 = 4,
  252. INT32 = 5,
  253. UINT32 = 6,
  254. INT64 = 7,
  255. UINT64 = 8,
  256. FLOAT = 9,
  257. DOUBLE = 10,
  258. INTPTR = 11,
  259. UINTPTR = 12,
  260. DECIMAL = 13,
  261. CHAR = 14,
  262. BOOL = 15,
  263. ENUM = 16,
  264. };
  265. public enum NumberOnStack
  266. {
  267. Int32,
  268. Int64,
  269. Double,
  270. }
  271. public class VBox
  272. {
  273. public VBox(NumberOnStack typeStack, NumberType thistype)
  274. {
  275. this.typeStack = typeStack;
  276. this.type = thistype;
  277. }
  278. public VBox Clone()
  279. {
  280. VBox b = ValueOnStack.MakeVBox(this.type);
  281. switch (typeStack)
  282. {
  283. case NumberOnStack.Int32:
  284. b.v32 = this.v32;
  285. break;
  286. case NumberOnStack.Int64:
  287. b.v64 = this.v64;
  288. break;
  289. case NumberOnStack.Double:
  290. b.vDF = this.v64;
  291. break;
  292. }
  293. return b;
  294. }
  295. public int refcount = 0;
  296. public NumberOnStack typeStack;
  297. public NumberType type;
  298. public Int32 v32;
  299. public Int64 v64;
  300. public Double vDF;
  301. public object BoxStack()
  302. {
  303. switch (typeStack)
  304. {
  305. case NumberOnStack.Int32:
  306. return v32;
  307. case NumberOnStack.Int64:
  308. return v64;
  309. case NumberOnStack.Double:
  310. return vDF;
  311. default:
  312. return null;
  313. }
  314. }
  315. public object BoxDefine()
  316. {
  317. switch (typeStack)
  318. {
  319. case NumberOnStack.Int32:
  320. switch (type)
  321. {
  322. case NumberType.ENUM:
  323. return v32;
  324. case NumberType.BOOL:
  325. return (v32 > 0);
  326. case NumberType.SBYTE:
  327. return (sbyte)v32;
  328. case NumberType.BYTE:
  329. return (byte)v32;
  330. case NumberType.CHAR:
  331. return (char)v32;
  332. case NumberType.INT16:
  333. return (Int16)v32;
  334. case NumberType.UINT16:
  335. return (UInt16)v32;
  336. case NumberType.INT32:
  337. return (Int32)v32;
  338. case NumberType.UINT32:
  339. return (UInt32)v32;
  340. case NumberType.INT64:
  341. return (Int64)v32;
  342. case NumberType.UINT64:
  343. return (UInt64)v32;
  344. case NumberType.FLOAT:
  345. return (float)v32;
  346. case NumberType.DOUBLE:
  347. return (double)v32;
  348. default:
  349. return null;
  350. }
  351. case NumberOnStack.Int64:
  352. switch (type)
  353. {
  354. case NumberType.BOOL:
  355. return (v64 > 0);
  356. case NumberType.SBYTE:
  357. return (sbyte)v64;
  358. case NumberType.BYTE:
  359. return (byte)v64;
  360. case NumberType.CHAR:
  361. return (char)v64;
  362. case NumberType.INT16:
  363. return (Int16)v64;
  364. case NumberType.UINT16:
  365. return (UInt16)v64;
  366. case NumberType.INT32:
  367. return (Int32)v64;
  368. case NumberType.UINT32:
  369. return (UInt32)v64;
  370. case NumberType.INT64:
  371. return (Int64)v64;
  372. case NumberType.UINT64:
  373. return (UInt64)v64;
  374. case NumberType.FLOAT:
  375. return (float)v64;
  376. case NumberType.DOUBLE:
  377. return (double)v64;
  378. default:
  379. return null;
  380. }
  381. case NumberOnStack.Double:
  382. switch (type)
  383. {
  384. case NumberType.BOOL:
  385. return (vDF > 0);
  386. case NumberType.SBYTE:
  387. return (sbyte)vDF;
  388. case NumberType.BYTE:
  389. return (byte)vDF;
  390. case NumberType.CHAR:
  391. return (char)vDF;
  392. case NumberType.INT16:
  393. return (Int16)vDF;
  394. case NumberType.UINT16:
  395. return (UInt16)vDF;
  396. case NumberType.INT32:
  397. return (Int32)vDF;
  398. case NumberType.UINT32:
  399. return (UInt32)vDF;
  400. case NumberType.INT64:
  401. return (Int64)vDF;
  402. case NumberType.UINT64:
  403. return (UInt64)vDF;
  404. case NumberType.FLOAT:
  405. return (float)vDF;
  406. case NumberType.DOUBLE:
  407. return (double)vDF;
  408. default:
  409. return null;
  410. }
  411. default:
  412. return null;
  413. }
  414. }
  415. public void Add(VBox right)
  416. {
  417. switch (typeStack)
  418. {
  419. case NumberOnStack.Int32:
  420. v32 += right.v32;
  421. break;
  422. case NumberOnStack.Int64:
  423. v64 += right.v64;
  424. break;
  425. case NumberOnStack.Double:
  426. vDF += right.vDF;
  427. break;
  428. }
  429. }
  430. public void Sub(VBox right)
  431. {
  432. switch (typeStack)
  433. {
  434. case NumberOnStack.Int32:
  435. v32 -= right.v32;
  436. break;
  437. case NumberOnStack.Int64:
  438. v64 -= right.v64;
  439. break;
  440. case NumberOnStack.Double:
  441. vDF -= right.vDF;
  442. break;
  443. }
  444. }
  445. public void Mul(VBox right)
  446. {
  447. switch (typeStack)
  448. {
  449. case NumberOnStack.Int32:
  450. v32 *= right.v32;
  451. break;
  452. case NumberOnStack.Int64:
  453. v64 *= right.v64;
  454. break;
  455. case NumberOnStack.Double:
  456. vDF *= right.vDF;
  457. break;
  458. }
  459. }
  460. public void Div(VBox right)
  461. {
  462. switch (typeStack)
  463. {
  464. case NumberOnStack.Int32:
  465. v32 /= right.v32;
  466. break;
  467. case NumberOnStack.Int64:
  468. v64 /= right.v64;
  469. break;
  470. case NumberOnStack.Double:
  471. vDF /= right.vDF;
  472. break;
  473. }
  474. }
  475. public void Mod(VBox right)
  476. {
  477. switch (typeStack)
  478. {
  479. case NumberOnStack.Int32:
  480. v32 %= right.v32;
  481. break;
  482. case NumberOnStack.Int64:
  483. v64 %= right.v64;
  484. break;
  485. case NumberOnStack.Double:
  486. vDF %= right.vDF;
  487. break;
  488. }
  489. }
  490. //似乎Clone可以替代New系列
  491. public VBox Mod_New(VBox right)
  492. {
  493. VBox newbox = ValueOnStack.MakeVBox(type);
  494. switch (typeStack)
  495. {
  496. case NumberOnStack.Int32:
  497. newbox.v32 = v32 % right.v32;
  498. break;
  499. case NumberOnStack.Int64:
  500. newbox.v64 = v64 % right.v64;
  501. break;
  502. case NumberOnStack.Double:
  503. newbox.vDF = vDF % right.vDF;
  504. break;
  505. }
  506. return newbox;
  507. }
  508. //SetValue
  509. public void SetDirect(object value)
  510. {
  511. switch (typeStack)
  512. {
  513. case NumberOnStack.Int32:
  514. if (value is bool)
  515. {
  516. v32 = ((bool)value) ? 1 : 0;
  517. }
  518. else
  519. {
  520. v32 = (int)value;
  521. }
  522. break;
  523. case NumberOnStack.Int64:
  524. v64 = (Int64)value;
  525. break;
  526. case NumberOnStack.Double:
  527. vDF = (double)Convert.ToDecimal(value);
  528. break;
  529. }
  530. }
  531. public void Set(VBox value)
  532. {
  533. switch (typeStack)
  534. {
  535. case NumberOnStack.Int32:
  536. if (value.typeStack == typeStack)
  537. v32 = value.v32;
  538. else
  539. v32 = value.ToInt();
  540. break;
  541. case NumberOnStack.Int64:
  542. if (value.typeStack == typeStack)
  543. v64 = value.v64;
  544. else
  545. v64 = value.ToInt64();
  546. break;
  547. case NumberOnStack.Double:
  548. if (value.typeStack == typeStack)
  549. vDF = value.vDF;
  550. else
  551. vDF = value.ToDouble();
  552. break;
  553. }
  554. }
  555. public bool logic_eq(VBox right)//=
  556. {
  557. switch (typeStack)
  558. {
  559. case NumberOnStack.Int32:
  560. return v32 == right.v32;
  561. case NumberOnStack.Int64:
  562. return v64 == right.v64;
  563. case NumberOnStack.Double:
  564. return vDF == right.vDF;
  565. default:
  566. return false;
  567. }
  568. }
  569. public bool logic_ne(VBox right)//!=
  570. {
  571. switch (typeStack)
  572. {
  573. case NumberOnStack.Int32:
  574. return v32 != right.v32;
  575. case NumberOnStack.Int64:
  576. return v64 != right.v64;
  577. case NumberOnStack.Double:
  578. return vDF != right.vDF;
  579. default:
  580. return false;
  581. }
  582. }
  583. public bool logic_ne_Un(VBox right)//!=
  584. {
  585. switch (typeStack)
  586. {
  587. case NumberOnStack.Int32:
  588. return (uint)v32 != (uint)right.v32;
  589. case NumberOnStack.Int64:
  590. return (UInt64)v64 != (UInt64)right.v64;
  591. case NumberOnStack.Double:
  592. return vDF != right.vDF;
  593. default:
  594. return false;
  595. }
  596. }
  597. public bool logic_ge(VBox right)//>=
  598. {
  599. switch (typeStack)
  600. {
  601. case NumberOnStack.Int32:
  602. return v32 >= right.v32;
  603. case NumberOnStack.Int64:
  604. return v64 >= right.v64;
  605. case NumberOnStack.Double:
  606. return vDF >= right.vDF;
  607. default:
  608. return false;
  609. }
  610. }
  611. public bool logic_ge_Un(VBox right)//>=
  612. {
  613. switch (typeStack)
  614. {
  615. case NumberOnStack.Int32:
  616. return (uint)v32 >= (uint)right.v32;
  617. case NumberOnStack.Int64:
  618. return (UInt64)v64 >= (UInt64)right.v64;
  619. case NumberOnStack.Double:
  620. return vDF >= right.vDF;
  621. default:
  622. return false;
  623. }
  624. }
  625. public bool logic_le(VBox right)//<=
  626. {
  627. switch (typeStack)
  628. {
  629. case NumberOnStack.Int32:
  630. return v32 <= right.v32;
  631. case NumberOnStack.Int64:
  632. return v64 <= right.v64;
  633. case NumberOnStack.Double:
  634. return vDF <= right.vDF;
  635. default:
  636. return false;
  637. }
  638. }
  639. public bool logic_le_Un(VBox right)
  640. {
  641. switch (typeStack)
  642. {
  643. case NumberOnStack.Int32:
  644. return (uint)v32 <= (uint)right.v32;
  645. case NumberOnStack.Int64:
  646. return (UInt64)v64 <= (UInt64)right.v64;
  647. case NumberOnStack.Double:
  648. return vDF <= right.vDF;
  649. default:
  650. return false;
  651. }
  652. }
  653. public bool logic_gt(VBox right)//>
  654. {
  655. switch (typeStack)
  656. {
  657. case NumberOnStack.Int32:
  658. return v32 > right.v32;
  659. case NumberOnStack.Int64:
  660. return v64 > right.v64;
  661. case NumberOnStack.Double:
  662. return vDF > right.vDF;
  663. default:
  664. return false;
  665. }
  666. }
  667. public bool logic_gt_Un(VBox right)//>
  668. {
  669. switch (typeStack)
  670. {
  671. case NumberOnStack.Int32:
  672. return (uint)v32 > (uint)right.v32;
  673. case NumberOnStack.Int64:
  674. return (UInt64)v64 > (UInt64)right.v64;
  675. case NumberOnStack.Double:
  676. return vDF > right.vDF;
  677. default:
  678. return false;
  679. }
  680. }
  681. public bool logic_lt(VBox right)//<
  682. {
  683. switch (typeStack)
  684. {
  685. case NumberOnStack.Int32:
  686. return v32 < right.v32;
  687. case NumberOnStack.Int64:
  688. return v64 < right.v64;
  689. case NumberOnStack.Double:
  690. return vDF < right.vDF;
  691. default:
  692. return false;
  693. }
  694. }
  695. public bool logic_lt_Un(VBox right)//<
  696. {
  697. switch (typeStack)
  698. {
  699. case NumberOnStack.Int32:
  700. return (uint)v32 < (uint)right.v32;
  701. case NumberOnStack.Int64:
  702. return (UInt64)v64 < (UInt64)right.v64;
  703. case NumberOnStack.Double:
  704. return vDF < right.vDF;
  705. default:
  706. return false;
  707. }
  708. }
  709. //////////////////////
  710. //To
  711. public bool ToBool()
  712. {
  713. switch (typeStack)
  714. {
  715. case NumberOnStack.Int32:
  716. return v32 > 0;
  717. case NumberOnStack.Int64:
  718. return v64 > 0;
  719. default:
  720. return false;
  721. }
  722. }
  723. public char ToChar()
  724. {
  725. switch (typeStack)
  726. {
  727. case NumberOnStack.Int32:
  728. return (char)v32;
  729. case NumberOnStack.Int64:
  730. return (char)v64;
  731. case NumberOnStack.Double:
  732. return (char)vDF;
  733. default:
  734. return (char)0;
  735. }
  736. }
  737. public byte ToByte()
  738. {
  739. switch (typeStack)
  740. {
  741. case NumberOnStack.Int32:
  742. return (byte)v32;
  743. case NumberOnStack.Int64:
  744. return (byte)v64;
  745. case NumberOnStack.Double:
  746. return (byte)vDF;
  747. default:
  748. return 0;
  749. }
  750. }
  751. public sbyte ToSByte()
  752. {
  753. switch (typeStack)
  754. {
  755. case NumberOnStack.Int32:
  756. return (sbyte)v32;
  757. case NumberOnStack.Int64:
  758. return (sbyte)v64;
  759. case NumberOnStack.Double:
  760. return (sbyte)vDF;
  761. default:
  762. return 0;
  763. }
  764. }
  765. public Int16 ToInt16()
  766. {
  767. switch (typeStack)
  768. {
  769. case NumberOnStack.Int32:
  770. return (Int16)v32;
  771. case NumberOnStack.Int64:
  772. return (Int16)v64;
  773. case NumberOnStack.Double:
  774. return (Int16)vDF;
  775. default:
  776. return 0;
  777. }
  778. }
  779. public UInt16 ToUInt16()
  780. {
  781. switch (typeStack)
  782. {
  783. case NumberOnStack.Int32:
  784. return (UInt16)v32;
  785. case NumberOnStack.Int64:
  786. return (UInt16)v64;
  787. case NumberOnStack.Double:
  788. return (UInt16)vDF;
  789. default:
  790. return 0;
  791. }
  792. }
  793. public int ToInt()
  794. {
  795. switch (typeStack)
  796. {
  797. case NumberOnStack.Int32:
  798. return (int)v32;
  799. case NumberOnStack.Int64:
  800. return (int)v64;
  801. case NumberOnStack.Double:
  802. return (int)vDF;
  803. default:
  804. return 0;
  805. }
  806. }
  807. public uint ToUInt()
  808. {
  809. switch (typeStack)
  810. {
  811. case NumberOnStack.Int32:
  812. return (uint)v32;
  813. case NumberOnStack.Int64:
  814. return (uint)v64;
  815. case NumberOnStack.Double:
  816. return (uint)vDF;
  817. default:
  818. return 0;
  819. }
  820. }
  821. public Int64 ToInt64()
  822. {
  823. switch (typeStack)
  824. {
  825. case NumberOnStack.Int32:
  826. return (Int64)v32;
  827. case NumberOnStack.Int64:
  828. return (Int64)v64;
  829. case NumberOnStack.Double:
  830. return (Int64)vDF;
  831. default:
  832. return 0;
  833. }
  834. }
  835. public UInt64 ToUInt64()
  836. {
  837. switch (typeStack)
  838. {
  839. case NumberOnStack.Int32:
  840. return (UInt64)v32;
  841. case NumberOnStack.Int64:
  842. return (UInt64)v64;
  843. case NumberOnStack.Double:
  844. return (UInt64)vDF;
  845. default:
  846. return 0;
  847. }
  848. }
  849. public float ToFloat()
  850. {
  851. switch (typeStack)
  852. {
  853. case NumberOnStack.Int32:
  854. return (float)v32;
  855. case NumberOnStack.Int64:
  856. return (float)v64;
  857. case NumberOnStack.Double:
  858. return (float)vDF;
  859. default:
  860. return 0;
  861. }
  862. }
  863. public double ToDouble()
  864. {
  865. switch (typeStack)
  866. {
  867. case NumberOnStack.Int32:
  868. return (double)v32;
  869. case NumberOnStack.Int64:
  870. return (double)v64;
  871. case NumberOnStack.Double:
  872. return (double)vDF;
  873. default:
  874. return 0;
  875. }
  876. }
  877. }
  878. //public interface IBox
  879. //{
  880. // object BoxStack();
  881. // object BoxDefine();
  882. // void Add(IBox right);
  883. // void Sub(IBox right);
  884. // void Mul(IBox right);
  885. // void Div(IBox right);
  886. // void Mod(IBox right);
  887. // IBox Mod_New(IBox right);
  888. // void SetDirect(object value);
  889. // void Set(IBox value);
  890. // NumberType type
  891. // {
  892. // get;
  893. // set;
  894. // }
  895. // NumberOnStack typeStack
  896. // {
  897. // get;
  898. // }
  899. // bool logic_eq(IBox right);//=
  900. // bool logic_ne(IBox right);//!=
  901. // bool logic_ne_Un(IBox right);//!=
  902. // bool logic_ge(IBox right);//>=
  903. // bool logic_ge_Un(IBox right);//>=
  904. // bool logic_le(IBox right);//<=
  905. // bool logic_le_Un(IBox right);
  906. // bool logic_gt(IBox right);//>
  907. // bool logic_gt_Un(IBox right);//>
  908. // bool logic_lt(IBox right);//<
  909. // bool logic_lt_Un(IBox right);//<
  910. // bool ToBool();
  911. // int ToInt();
  912. // uint ToUint();
  913. // Int64 ToInt64();
  914. // float ToFloat();
  915. // double ToDouble();
  916. // int refcount
  917. // {
  918. // get;
  919. // set;
  920. // }
  921. //}
  922. //public class BoxInt32 : IBox
  923. //{
  924. // public int refcount
  925. // {
  926. // get;
  927. // set;
  928. // }
  929. // public BoxInt32(NumberType type)
  930. // {
  931. // this.type = type;
  932. // }
  933. // public NumberType type
  934. // {
  935. // get;
  936. // set;
  937. // }
  938. // public NumberOnStack typeStack
  939. // {
  940. // get
  941. // {
  942. // return NumberOnStack.Int32;
  943. // }
  944. // }
  945. // public Int32 value;
  946. // public object BoxStack()
  947. // {
  948. // return value;
  949. // }
  950. // public object BoxDefine()
  951. // {
  952. // switch (type)
  953. // {
  954. // case NumberType.BOOL:
  955. // return (value > 0);
  956. // case NumberType.SBYTE:
  957. // return (sbyte)value;
  958. // case NumberType.BYTE:
  959. // return (byte)value;
  960. // case NumberType.CHAR:
  961. // return (char)value;
  962. // case NumberType.INT16:
  963. // return (Int16)value;
  964. // case NumberType.UINT16:
  965. // return (UInt16)value;
  966. // case NumberType.INT32:
  967. // return (Int32)value;
  968. // case NumberType.UINT32:
  969. // return (UInt32)value;
  970. // case NumberType.INT64:
  971. // return (Int64)value;
  972. // case NumberType.UINT64:
  973. // return (UInt64)value;
  974. // case NumberType.FLOAT:
  975. // return (float)value;
  976. // case NumberType.DOUBLE:
  977. // return (double)value;
  978. // default:
  979. // return null;
  980. // }
  981. // }
  982. // public void Set(IBox value)
  983. // {
  984. // this.value = (value as BoxInt32).value;
  985. // }
  986. // public void SetDirect(object value)
  987. // {
  988. // if (value is bool)
  989. // {
  990. // this.value = (bool)value ? 1 : 0;
  991. // }
  992. // else
  993. // {
  994. // this.value = (int)value;
  995. // }
  996. // }
  997. // public void Add(IBox right)
  998. // {
  999. // this.value += (right as BoxInt32).value;
  1000. // }
  1001. // public void Sub(IBox right)
  1002. // {
  1003. // this.value -= (right as BoxInt32).value;
  1004. // }
  1005. // public void Mul(IBox right)
  1006. // {
  1007. // this.value *= (right as BoxInt32).value;
  1008. // }
  1009. // public void Div(IBox right)
  1010. // {
  1011. // this.value /= (right as BoxInt32).value;
  1012. // }
  1013. // public void Mod(IBox right)
  1014. // {
  1015. // this.value %= (right as BoxInt32).value;
  1016. // }
  1017. // public IBox Mod_New(IBox right)
  1018. // {
  1019. // BoxInt32 b = ValueOnStack.Make(this.type) as BoxInt32;
  1020. // b.value = this.value % (right as BoxInt32).value;
  1021. // return b;
  1022. // }
  1023. // public bool logic_eq(IBox right)
  1024. // {
  1025. // return value == (right as BoxInt32).value;
  1026. // }
  1027. // public bool logic_ne(IBox right)
  1028. // {
  1029. // return value != (right as BoxInt32).value;
  1030. // }
  1031. // public bool logic_ne_Un(IBox right)
  1032. // {
  1033. // return (UInt32)value != (UInt32)(right as BoxInt32).value;
  1034. // }
  1035. // public bool logic_ge(IBox right)
  1036. // {
  1037. // return value >= (right as BoxInt32).value;
  1038. // }
  1039. // public bool logic_ge_Un(IBox right)
  1040. // {
  1041. // return (UInt32)value >= (UInt32)(right as BoxInt32).value;
  1042. // }
  1043. // public bool logic_le(IBox right)
  1044. // {
  1045. // return value <= (right as BoxInt32).value;
  1046. // }
  1047. // public bool logic_le_Un(IBox right)
  1048. // {
  1049. // return (UInt32)value <= (UInt32)(right as BoxInt32).value;
  1050. // }
  1051. // public bool logic_gt(IBox right)
  1052. // {
  1053. // return value > (right as BoxInt32).value;
  1054. // }
  1055. // public bool logic_gt_Un(IBox right)
  1056. // {
  1057. // return (UInt32)value > (UInt32)(right as BoxInt32).value;
  1058. // }
  1059. // public bool logic_lt(IBox right)
  1060. // {
  1061. // return value < (right as BoxInt32).value;
  1062. // }
  1063. // public bool logic_lt_Un(IBox right)
  1064. // {
  1065. // return (UInt32)value < (UInt32)(right as BoxInt32).value;
  1066. // }
  1067. // public bool ToBool()
  1068. // {
  1069. // return value > 0;
  1070. // }
  1071. // public int ToInt()
  1072. // {
  1073. // return (int)value;
  1074. // }
  1075. // public uint ToUint()
  1076. // {
  1077. // return (uint)value;
  1078. // }
  1079. // public Int64 ToInt64()
  1080. // {
  1081. // return (Int64)value;
  1082. // }
  1083. // public float ToFloat()
  1084. // {
  1085. // return (float)value;
  1086. // }
  1087. // public double ToDouble()
  1088. // {
  1089. // return (double)value;
  1090. // }
  1091. //}
  1092. //public class BoxInt64 : IBox
  1093. //{
  1094. // public int refcount
  1095. // {
  1096. // get;
  1097. // set;
  1098. // }
  1099. // public BoxInt64(NumberType type)
  1100. // {
  1101. // this.type = type;
  1102. // }
  1103. // public NumberType type
  1104. // {
  1105. // get;
  1106. // set;
  1107. // }
  1108. // public NumberOnStack typeStack
  1109. // {
  1110. // get
  1111. // {
  1112. // return NumberOnStack.Int64;
  1113. // }
  1114. // }
  1115. // public Int64 value;
  1116. // public object BoxStack()
  1117. // {
  1118. // return value;
  1119. // }
  1120. // public object BoxDefine()
  1121. // {
  1122. // switch (type)
  1123. // {
  1124. // case NumberType.BOOL:
  1125. // return (value > 0);
  1126. // case NumberType.SBYTE:
  1127. // return (sbyte)value;
  1128. // case NumberType.BYTE:
  1129. // return (byte)value;
  1130. // case NumberType.CHAR:
  1131. // return (char)value;
  1132. // case NumberType.INT16:
  1133. // return (Int16)value;
  1134. // case NumberType.UINT16:
  1135. // return (UInt16)value;
  1136. // case NumberType.INT32:
  1137. // return (Int32)value;
  1138. // case NumberType.UINT32:
  1139. // return (UInt32)value;
  1140. // case NumberType.INT64:
  1141. // return (Int64)value;
  1142. // case NumberType.UINT64:
  1143. // return (UInt64)value;
  1144. // case NumberType.FLOAT:
  1145. // return (float)value;
  1146. // case NumberType.DOUBLE:
  1147. // return (double)value;
  1148. // default:
  1149. // return null;
  1150. // }
  1151. // }
  1152. // public void Set(IBox value)
  1153. // {
  1154. // this.value = (value as BoxInt64).value;
  1155. // }
  1156. // public void SetDirect(object value)
  1157. // {
  1158. // this.value = (Int64)value;
  1159. // }
  1160. // public void Add(IBox right)
  1161. // {
  1162. // this.value += (right as BoxInt64).value;
  1163. // }
  1164. // public void Sub(IBox right)
  1165. // {
  1166. // this.value -= (right as BoxInt64).value;
  1167. // }
  1168. // public void Mul(IBox right)
  1169. // {
  1170. // this.value *= (right as BoxInt64).value;
  1171. // }
  1172. // public void Div(IBox right)
  1173. // {
  1174. // this.value /= (right as BoxInt64).value;
  1175. // }
  1176. // public void Mod(IBox right)
  1177. // {
  1178. // this.value %= (right as BoxInt64).value;
  1179. // }
  1180. // public IBox Mod_New(IBox right)
  1181. // {
  1182. // BoxInt64 b = ValueOnStack.Make(this.type) as BoxInt64;
  1183. // b.value = this.value % (right as BoxInt64).value;
  1184. // return b;
  1185. // }
  1186. // public bool logic_eq(IBox right)
  1187. // {
  1188. // return value == (right as BoxInt64).value;
  1189. // }
  1190. // public bool logic_ne(IBox right)
  1191. // {
  1192. // return value != (right as BoxInt64).value;
  1193. // }
  1194. // public bool logic_ne_Un(IBox right)
  1195. // {
  1196. // return (UInt64)value != (UInt64)(right as BoxInt64).value;
  1197. // }
  1198. // public bool logic_ge(IBox right)
  1199. // {
  1200. // return value >= (right as BoxInt64).value;
  1201. // }
  1202. // public bool logic_ge_Un(IBox right)
  1203. // {
  1204. // return (UInt64)value >= (UInt64)(right as BoxInt64).value;
  1205. // }
  1206. // public bool logic_le(IBox right)
  1207. // {
  1208. // return value <= (right as BoxInt64).value;
  1209. // }
  1210. // public bool logic_le_Un(IBox right)
  1211. // {
  1212. // return (UInt64)value <= (UInt64)(right as BoxInt64).value;
  1213. // }
  1214. // public bool logic_gt(IBox right)
  1215. // {
  1216. // return value > (right as BoxInt64).value;
  1217. // }
  1218. // public bool logic_gt_Un(IBox right)
  1219. // {
  1220. // return (UInt64)value > (UInt64)(right as BoxInt64).value;
  1221. // }
  1222. // public bool logic_lt(IBox right)
  1223. // {
  1224. // return value < (right as BoxInt64).value;
  1225. // }
  1226. // public bool logic_lt_Un(IBox right)
  1227. // {
  1228. // return (UInt64)value < (UInt64)(right as BoxInt64).value;
  1229. // }
  1230. // public bool ToBool()
  1231. // {
  1232. // return value > 0;
  1233. // }
  1234. // public int ToInt()
  1235. // {
  1236. // return (int)value;
  1237. // }
  1238. // public uint ToUint()
  1239. // {
  1240. // return (uint)value;
  1241. // }
  1242. // public Int64 ToInt64()
  1243. // {
  1244. // return (Int64)value;
  1245. // }
  1246. // public float ToFloat()
  1247. // {
  1248. // return (float)value;
  1249. // }
  1250. // public double ToDouble()
  1251. // {
  1252. // return (double)value;
  1253. // }
  1254. //}
  1255. //public class BoxDouble : IBox
  1256. //{
  1257. // public int refcount
  1258. // {
  1259. // get;
  1260. // set;
  1261. // }
  1262. // public BoxDouble(NumberType type)
  1263. // {
  1264. // this.type = type;
  1265. // }
  1266. // public NumberType type
  1267. // {
  1268. // get;
  1269. // set;
  1270. // }
  1271. // public NumberOnStack typeStack
  1272. // {
  1273. // get
  1274. // {
  1275. // return NumberOnStack.Double;
  1276. // }
  1277. // }
  1278. // public double value;
  1279. // public object BoxStack()
  1280. // {
  1281. // return value;
  1282. // }
  1283. // public object BoxDefine()
  1284. // {
  1285. // switch (type)
  1286. // {
  1287. // case NumberType.BOOL:
  1288. // return (value > 0);
  1289. // case NumberType.SBYTE:
  1290. // return (sbyte)value;
  1291. // case NumberType.BYTE:
  1292. // return (byte)value;
  1293. // case NumberType.CHAR:
  1294. // return (char)value;
  1295. // case NumberType.INT16:
  1296. // return (Int16)value;
  1297. // case NumberType.UINT16:
  1298. // return (UInt16)value;
  1299. // case NumberType.INT32:
  1300. // return (Int32)value;
  1301. // case NumberType.UINT32:
  1302. // return (UInt32)value;
  1303. // case NumberType.INT64:
  1304. // return (Int64)value;
  1305. // case NumberType.UINT64:
  1306. // return (UInt64)value;
  1307. // case NumberType.FLOAT:
  1308. // return (float)value;
  1309. // case NumberType.DOUBLE:
  1310. // return (double)value;
  1311. // default:
  1312. // return null;
  1313. // }
  1314. // }
  1315. // public void Set(IBox value)
  1316. // {
  1317. // this.value = (value as BoxDouble).value;
  1318. // }
  1319. // public void SetDirect(object value)
  1320. // {
  1321. // this.value = (double)Convert.ToDecimal(value);
  1322. // }
  1323. // public void Add(IBox right)
  1324. // {
  1325. // this.value += (right as BoxDouble).value;
  1326. // }
  1327. // public void Sub(IBox right)
  1328. // {
  1329. // this.value -= (right as BoxDouble).value;
  1330. // }
  1331. // public void Mul(IBox right)
  1332. // {
  1333. // this.value *= (right as BoxDouble).value;
  1334. // }
  1335. // public void Div(IBox right)
  1336. // {
  1337. // this.value /= (right as BoxDouble).value;
  1338. // }
  1339. // public void Mod(IBox right)
  1340. // {
  1341. // this.value %= (right as BoxDouble).value;
  1342. // }
  1343. // public IBox Mod_New(IBox right)
  1344. // {
  1345. // BoxDouble b = new BoxDouble(this.type);
  1346. // b.value = this.value % (right as BoxDouble).value;
  1347. // return b;
  1348. // }
  1349. // public bool logic_eq(IBox right)
  1350. // {
  1351. // return value == (right as BoxDouble).value;
  1352. // }
  1353. // public bool logic_ne(IBox right)
  1354. // {
  1355. // return value != (right as BoxDouble).value;
  1356. // }
  1357. // public bool logic_ne_Un(IBox right)
  1358. // {
  1359. // return value != (right as BoxDouble).value;
  1360. // }
  1361. // public bool logic_ge(IBox right)
  1362. // {
  1363. // return value >= (right as BoxDouble).value;
  1364. // }
  1365. // public bool logic_ge_Un(IBox right)
  1366. // {
  1367. // return value >= (right as BoxDouble).value;
  1368. // }
  1369. // public bool logic_le(IBox right)
  1370. // {
  1371. // return value <= (right as BoxDouble).value;
  1372. // }
  1373. // public bool logic_le_Un(IBox right)
  1374. // {
  1375. // return value <= (right as BoxDouble).value;
  1376. // }
  1377. // public bool logic_gt(IBox right)
  1378. // {
  1379. // return value > (right as BoxDouble).value;
  1380. // }
  1381. // public bool logic_gt_Un(IBox right)
  1382. // {
  1383. // return value > (right as BoxDouble).value;
  1384. // }
  1385. // public bool logic_lt(IBox right)
  1386. // {
  1387. // return value < (right as BoxDouble).value;
  1388. // }
  1389. // public bool logic_lt_Un(IBox right)
  1390. // {
  1391. // return value < (right as BoxDouble).value;
  1392. // }
  1393. // public bool ToBool()
  1394. // {
  1395. // throw new NotImplementedException();
  1396. // }
  1397. // public int ToInt()
  1398. // {
  1399. // return (int)value;
  1400. // }
  1401. // public uint ToUint()
  1402. // {
  1403. // return (uint)value;
  1404. // }
  1405. // public Int64 ToInt64()
  1406. // {
  1407. // return (Int64)value;
  1408. // }
  1409. // public float ToFloat()
  1410. // {
  1411. // return (float)value;
  1412. // }
  1413. // public double ToDouble()
  1414. // {
  1415. // return (double)value;
  1416. // }
  1417. //}
  1418. }