FlowControl.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  1. //
  2. // FlowControl.cs
  3. //
  4. // Author:
  5. // Chris J Breisch ([email protected])
  6. // Dennis Hayes ([email protected])
  7. //
  8. // (C) 2002 Chris J Breisch
  9. //
  10. /*
  11. * Copyright (c) 2002-2003 Mainsoft Corporation.
  12. * Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a
  15. * copy of this software and associated documentation files (the "Software"),
  16. * to deal in the Software without restriction, including without limitation
  17. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  18. * and/or sell copies of the Software, and to permit persons to whom the
  19. * Software is furnished to do so, subject to the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  30. * DEALINGS IN THE SOFTWARE.
  31. */
  32. /**
  33. * This class allows to execute loop statement of VisualBasic .NET
  34. */
  35. using System;
  36. using System.Collections;
  37. using System.ComponentModel;
  38. namespace Microsoft.VisualBasic.CompilerServices
  39. {
  40. [StandardModule, EditorBrowsable(EditorBrowsableState.Never)]
  41. sealed public class FlowControl {
  42. private FlowControl () {}
  43. private sealed /*static (final)in mainsoft java code*/ class ObjectFor {
  44. public object Counter;
  45. public object Limit;
  46. public object StepValue;
  47. public bool PositiveStep;
  48. public Type EnumType;
  49. }
  50. /**
  51. * This method check if the loop can continued.
  52. * if the StepValue is positive it check that count is smaller than the limit.
  53. * if the StepValue is negative it check that count is bigger than the limit.
  54. * @param count
  55. * @param limit
  56. * @param StepValue
  57. * @return boolean True of the for next loop can continue and false otherwise.
  58. */
  59. public static bool ForNextCheckR4(float count, float limit, float StepValue) {
  60. bool positiveStep = StepValue > 0.0F;
  61. if (positiveStep)
  62. return count <= limit;
  63. else
  64. return count >= limit;
  65. }
  66. /**
  67. * This method check if the loop can continued.
  68. * if the StepValue is positive it check that count is smaller than the limit.
  69. * if the StepValue is negative it check that count is bigger than the limit.
  70. * @param count
  71. * @param limit
  72. * @param StepValue
  73. * @return boolean True of the for next loop can continue and false otherwise.
  74. */
  75. public static bool ForNextCheckR8(double count, double limit, double StepValue) {
  76. bool positiveStep = StepValue > 0.0;
  77. if (positiveStep)
  78. return count <= limit;
  79. else
  80. return count >= limit;
  81. }
  82. /**
  83. * This method check if the loop can continued.
  84. * if the StepValue is positive it check that count is smaller than the limit.
  85. * if the StepValue is negative it check that count is bigger than the limit.
  86. * @param count
  87. * @param limit
  88. * @param StepValue
  89. * @return boolean True of the for next loop can continue and false otherwise.
  90. */
  91. public static bool ForNextCheckDec(Decimal count, Decimal limit, Decimal StepValue) {
  92. bool positiveStep = StepValue.CompareTo(Decimal.Zero) > 0;
  93. if (positiveStep)
  94. return (count.CompareTo(limit) <= 0);
  95. else
  96. return (limit.CompareTo(count) <= 0);
  97. }
  98. /**
  99. * This method method updates the LoopFor reference and the Counter reference
  100. * object according to the given params and returns if this loop can continue.
  101. * @param Counter this loop counter value
  102. * @param Start this loop start value
  103. * @param Limit this loop limitation value
  104. * @param StepValue this loop step value
  105. * @param lfr the LoopFor reference object
  106. * @param cr the Counter object reference
  107. * @return boolean is the returned LoopFor object can continue.
  108. */
  109. public static bool ForLoopInitObj(
  110. object Counter,
  111. object Start,
  112. object Limit,
  113. object StepValue,
  114. ref System.Object lfr,
  115. ref System.Object cr) {
  116. if (Start == null) {
  117. throw new ArgumentException("Argument_InvalidNullValue1 " + " Start");
  118. }
  119. if (Limit == null) {
  120. throw new ArgumentException("Argument_InvalidNullValue1 " + " Limit");
  121. }
  122. if (StepValue == null) {
  123. throw new ArgumentException("Argument_InvalidNullValue1 " + " Step");
  124. }
  125. //gets the type of all the given parameters
  126. Type startType = Start.GetType();
  127. Type limitType = Limit.GetType();
  128. Type stepType = StepValue.GetType();
  129. //gets the widest common type code
  130. TypeCode commonTypeCode = ObjectType.GetWidestType(Start, Limit, false);
  131. commonTypeCode = ObjectType.GetWidestType(StepValue, commonTypeCode);
  132. if (commonTypeCode == TypeCode.String) {
  133. commonTypeCode = TypeCode.Double;
  134. }
  135. if (commonTypeCode == TypeCode.Object) {
  136. //TODO:
  137. //throw new ArgumentException(
  138. // Utils.GetResourceString(
  139. // "ForLoop_CommonType3",
  140. // Utils.VBFriendlyName(startType),
  141. // Utils.VBFriendlyName(limitType),
  142. // Utils.VBFriendlyName(StepValue)));
  143. throw new ArgumentException("ForLoop_CommonType3 startType limitType StepValue");
  144. }
  145. ObjectFor objectFor = new ObjectFor();
  146. TypeCode startTypeCode = Type.GetTypeCode(startType);
  147. TypeCode limitTypeCode = Type.GetTypeCode(limitType);
  148. TypeCode stepTypeCode = Type.GetTypeCode(stepType);
  149. Type enumType = null;
  150. bool isStartTypeValidEnum = (startTypeCode == commonTypeCode) && (startType.IsEnum);
  151. bool isLimitTypeValidEnum = (limitTypeCode == commonTypeCode) && (limitType.IsEnum);
  152. bool isStepTypeValidEnum = (stepTypeCode == commonTypeCode) && (stepType.IsEnum);
  153. bool isStartAndStepTypeEqual = (startType == stepType);
  154. bool isStartAndLimitTypeEqual = (startType == limitType);
  155. // bool isStepAndLimitTypeEqual = (stepType == limitType);
  156. //the For loop has enum type in the following case
  157. //1. step is enum and it's type code equal to commonTypeCode and start and
  158. // limit don't meet this condition.
  159. //2. step and start are enum and their type code equal to commonTypeCode and
  160. // their types are equal. limit doesn't meet this condition about been enum
  161. // or about been equal to commonTypeCode.
  162. //3. step and limit are enum and their type code equal to commonTypeCode and
  163. // their types are equal. start doesn't meet this condition about been enum
  164. // or about been equal to commonTypeCode.
  165. //4. step and limit and start are enum and their type code equal to commonTypeCode and
  166. // their types are equal.
  167. //5. start is enum and it's type code equal to commonTypeCode .step and
  168. // limit don't meet this condition.
  169. //6. limit is enum and it's type code equal to commonTypeCode .step and
  170. // start don't meet this condition.
  171. //7.start and limit are enum and their type code equal to commonTypeCode and
  172. // their types are equal. step doesn't meet this condition about been enum
  173. // or about been equal to commonTypeCode.
  174. //
  175. if (isStartTypeValidEnum && isLimitTypeValidEnum && isStepTypeValidEnum
  176. && isStartAndStepTypeEqual && isStartAndLimitTypeEqual)
  177. enumType = startType;
  178. else if (isStartTypeValidEnum && isStepTypeValidEnum && isStartAndStepTypeEqual)
  179. enumType = startType;
  180. else if (isStartTypeValidEnum && isStepTypeValidEnum && isStartAndStepTypeEqual)
  181. enumType = startType;
  182. else if (isStartTypeValidEnum && isLimitTypeValidEnum && isStartAndLimitTypeEqual)
  183. enumType = startType;
  184. else if (isStartTypeValidEnum && !isLimitTypeValidEnum && !isStepTypeValidEnum)
  185. enumType = startType;
  186. else if (!isStartTypeValidEnum && isLimitTypeValidEnum && !isStepTypeValidEnum)
  187. enumType = limitType;
  188. else if (!isStartTypeValidEnum && !isLimitTypeValidEnum && isStepTypeValidEnum)
  189. enumType = stepType;
  190. objectFor.EnumType = enumType;
  191. //set the counter field of objectFor with Start value transleted to
  192. // the widest common type code
  193. objectFor.Counter = convertType(Start, commonTypeCode,"Start");
  194. //set the Limit field of objectFor with Limit value transleted to
  195. // the widest common type code
  196. objectFor.Limit = convertType(Limit, commonTypeCode,"Limit");
  197. //set the StepValue field of objectFor with StepValue value transleted to
  198. // the widest common type code
  199. objectFor.StepValue = convertType(StepValue, commonTypeCode,"Step");
  200. //local is the value of zero in the widest common type code
  201. object local = ObjectType.CTypeHelper(0, commonTypeCode);
  202. IComparable iComparable = (IComparable)objectFor.StepValue;
  203. objectFor.PositiveStep = iComparable.CompareTo(local) >= 0;
  204. // sets the loop for reference
  205. lfr = objectFor;
  206. //sets the counter reference
  207. if (objectFor.EnumType != null) {
  208. cr = Enum.ToObject(objectFor.EnumType, objectFor.Counter);
  209. }
  210. else {
  211. cr = objectFor.Counter;
  212. }
  213. return CheckContinueLoop(objectFor);
  214. }
  215. private static object convertType(object original, TypeCode typeCode, string fieldName) {
  216. try {
  217. return ObjectType.CTypeHelper(original, typeCode);
  218. }
  219. catch /*(Exception e)*/ {
  220. throw new ArgumentException("ForLoop_ConvertToType3 " + fieldName);
  221. }
  222. }
  223. public static bool ForNextCheckObj(object Counter, object LoopObj,
  224. ref System.Object CounterResult) {// throws java.lang.Exception
  225. TypeCode generalTypeCode = 0;
  226. if (LoopObj == null) {
  227. //TODO: use resource for the correct execption.
  228. throw new Exception("VB error message #92 ForNextCheckObj LoopObj cannot be null");
  229. //throw ExceptionUtils.VbMakeException(92);//correct java version
  230. }
  231. if (Counter == null) {
  232. throw new NullReferenceException("Argument_InvalidNullValue1 " + " Counter");
  233. //TODO:
  234. //throw new NullReferenceException(
  235. // Utils.GetResourceString(
  236. // "Argument_InvalidNullValue1",
  237. // "Counter"));
  238. }
  239. ObjectFor objectFor = (ObjectFor) LoopObj;
  240. IConvertible iConvertible_counter = (IConvertible)Counter;
  241. IConvertible iConvertible_step = (IConvertible) objectFor.StepValue;
  242. TypeCode counterTypeCode = iConvertible_counter.GetTypeCode();
  243. TypeCode stepTypeCode = iConvertible_step.GetTypeCode();
  244. if (counterTypeCode == stepTypeCode && counterTypeCode != TypeCode.String) {
  245. generalTypeCode = counterTypeCode;
  246. }
  247. else {
  248. generalTypeCode = ObjectType.GetWidestType(counterTypeCode, stepTypeCode);
  249. if (generalTypeCode == TypeCode.String) {
  250. generalTypeCode = TypeCode.Double;
  251. }
  252. Counter = convertType(Counter, generalTypeCode,"Start");
  253. objectFor.Limit = convertType(objectFor.Limit, generalTypeCode,"Limit");
  254. objectFor.StepValue = convertType(objectFor.StepValue, generalTypeCode,"Step");
  255. }
  256. //changes the counter field to be the sum of step and counter
  257. objectFor.Counter = ObjectType.AddObj(Counter, objectFor.StepValue);
  258. IConvertible iConvertible_objectCounter = (IConvertible)objectFor.Counter;
  259. TypeCode objectCounterTypeCode = iConvertible_objectCounter.GetTypeCode();
  260. //setting the counter in counter reference.
  261. //if the for is enum type change counter to enum.
  262. if (objectFor.EnumType != null) {
  263. CounterResult = Enum.ToObject(objectFor.EnumType, objectFor.Counter);
  264. }
  265. else {
  266. CounterResult = objectFor.Counter;
  267. }
  268. //if the counter after the change didn't change it's type return true if
  269. // the for object can continue loop and false otherwise.
  270. //if the counter changed it's type change all for object fields to counter
  271. //current type and return false.
  272. if (objectCounterTypeCode == generalTypeCode) {
  273. return CheckContinueLoop(objectFor);
  274. }
  275. else {
  276. objectFor.Limit = ObjectType.CTypeHelper(objectFor.Limit, objectCounterTypeCode);
  277. objectFor.StepValue =
  278. ObjectType.CTypeHelper(objectFor.StepValue, objectCounterTypeCode);
  279. return false;
  280. }
  281. }
  282. /**
  283. * This method returns IEnumertator for a given array
  284. * @param ary the given array
  285. * @return IEnumerator the array's Enumerator
  286. */
  287. public static IEnumerator ForEachInArr(Array ary) {// throws java.lang.Exception
  288. IEnumerator iEnumerator = (IEnumerator)ary;//is ArrayStaticWrapper.GetEnumerator(ary); in java code.
  289. if (iEnumerator != null)
  290. return iEnumerator;
  291. throw ExceptionUtils.VbMakeException(92);
  292. }
  293. /**
  294. * This method gets IEnumerator for a given object that implements IEnumerable
  295. * @param obj the object that implements IEnumerable
  296. * @return IEnumerator the object's IEnumerator.
  297. */
  298. public static IEnumerator ForEachInObj(object obj) {// throws java.lang.Exception
  299. if (obj == null)
  300. throw ExceptionUtils.VbMakeException(91);
  301. IEnumerable iEnumerable = (IEnumerable)obj;
  302. if (iEnumerable != null) {
  303. IEnumerator iEnumerator = iEnumerable.GetEnumerator();
  304. if (iEnumerator != null)
  305. return iEnumerator;
  306. }
  307. string s = obj.GetType().ToString();
  308. ExceptionUtils.ThrowException1(100, s);
  309. return null;
  310. }
  311. /**
  312. * This method set the next value of teh Enumerator in the reference.
  313. * if there isn't next value , null is been set in the referece.
  314. * @param obj
  315. * @param enumerator
  316. * @return boolean returns the value of enumerator.MoveNext().
  317. */
  318. public static bool ForEachNextObj(ref System.Object obj, IEnumerator enumerator) {
  319. if (enumerator.MoveNext()) {
  320. obj = enumerator.Current;
  321. return true;
  322. }
  323. obj = null;
  324. return false;
  325. }
  326. /**
  327. * This method check if the loop can continued.
  328. * if the step is positive it check that the counter is smaller than the limit.
  329. * if the step is negative it check that the counter is bigger than the limit.
  330. * @param LoopFor
  331. * @return boolean
  332. */
  333. private static bool CheckContinueLoop(ObjectFor LoopFor) {
  334. //TODO:
  335. //throw new NotImplementedException("MSVB.Compilerservices.flowcontrol needs help");
  336. IComparable iComparable = (IComparable)LoopFor.Counter;
  337. if (iComparable != null) {
  338. int i = iComparable.CompareTo(LoopFor.Limit);
  339. if (LoopFor.PositiveStep)
  340. return i <= 0;
  341. else
  342. return i >= 0;
  343. }
  344. throw new ArgumentException("Argument_IComparable2 loop control variable"); // + Utils.VBFriendlyName(LoopFor.Counter)));
  345. //TODO: verify this and the above are the same and remove.
  346. //throw new ArgumentException(Utils.GetResourceString(
  347. // "Argument_IComparable2", "loop control variable",
  348. // Utils.VBFriendlyName(LoopFor.Counter)));
  349. }
  350. /**
  351. * This method throws exception if the input is Valuetype
  352. * @param obj the object that need to be checked
  353. */
  354. public static void CheckForSyncLockOnValueType(object obj) {
  355. //TODO:
  356. //throw new NotImplementedException("MSVB.Compilerservices.flowcontrol needs help");
  357. if (obj != null && obj.GetType().IsValueType)
  358. throw new ArgumentException(Utils.GetResourceString("SyncLockRequiresReferenceType1 "));
  359. //TODO: verify this and the above are the same and remove.
  360. //if (obj != null && ObjectStaticWrapper.GetType(obj).get_IsValueType())
  361. // throw new ArgumentException(Utils.GetResourceString(
  362. // "SyncLockRequiresReferenceType1",Utils.VBFriendlyName(obj)));
  363. }
  364. }
  365. }