FlowControl.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. bool isCountSmallThenLimit = count <= limit;
  62. return positiveStep? isCountSmallThenLimit : !isCountSmallThenLimit;
  63. }
  64. /**
  65. * This method check if the loop can continued.
  66. * if the StepValue is positive it check that count is smaller than the limit.
  67. * if the StepValue is negative it check that count is bigger than the limit.
  68. * @param count
  69. * @param limit
  70. * @param StepValue
  71. * @return boolean True of the for next loop can continue and false otherwise.
  72. */
  73. public static bool ForNextCheckR8(double count, double limit, double StepValue) {
  74. bool positiveStep = StepValue > 0.0;
  75. bool isCountSmallThenLimit = count <= limit;
  76. return positiveStep? isCountSmallThenLimit : !isCountSmallThenLimit;
  77. }
  78. /**
  79. * This method check if the loop can continued.
  80. * if the StepValue is positive it check that count is smaller than the limit.
  81. * if the StepValue is negative it check that count is bigger than the limit.
  82. * @param count
  83. * @param limit
  84. * @param StepValue
  85. * @return boolean True of the for next loop can continue and false otherwise.
  86. */
  87. public static bool ForNextCheckDec(Decimal count, Decimal limit, Decimal StepValue) {
  88. bool positiveStep = StepValue.CompareTo(Decimal.Zero) < 0;
  89. bool isCountSmallThenLimit = count.CompareTo(limit) >= 0;
  90. return positiveStep? isCountSmallThenLimit : !isCountSmallThenLimit;
  91. }
  92. /**
  93. * This method method updates the LoopFor reference and the Counter reference
  94. * object according to the given params and returns if this loop can continue.
  95. * @param Counter this loop counter value
  96. * @param Start this loop start value
  97. * @param Limit this loop limitation value
  98. * @param StepValue this loop step value
  99. * @param lfr the LoopFor reference object
  100. * @param cr the Counter object reference
  101. * @return boolean is the returned LoopFor object can continue.
  102. */
  103. public static bool ForLoopInitObj(
  104. object Counter,
  105. object Start,
  106. object Limit,
  107. object StepValue,
  108. ref System.Object lfr,
  109. ref System.Object cr) {
  110. object CounterResult = cr;
  111. if (Start == null) {
  112. throw new ArgumentException("Argument_InvalidNullValue1 " + " Start");
  113. }
  114. if (Limit == null) {
  115. throw new ArgumentException("Argument_InvalidNullValue1 " + " Limit");
  116. }
  117. if (StepValue == null) {
  118. throw new ArgumentException("Argument_InvalidNullValue1 " + " Step");
  119. }
  120. //gets the type of all the given parameters
  121. Type startType = Start.GetType();
  122. Type limitType = Limit.GetType();
  123. Type stepType = StepValue.GetType();
  124. //gets the widest common type code
  125. TypeCode commonTypeCode = ObjectType.GetWidestType(Start, Limit, false);
  126. commonTypeCode = ObjectType.GetWidestType(StepValue, commonTypeCode);
  127. if (commonTypeCode == TypeCode.String) {
  128. commonTypeCode = TypeCode.Double;
  129. }
  130. if (commonTypeCode == TypeCode.Object) {
  131. //TODO:
  132. //throw new ArgumentException(
  133. // Utils.GetResourceString(
  134. // "ForLoop_CommonType3",
  135. // Utils.VBFriendlyName(startType),
  136. // Utils.VBFriendlyName(limitType),
  137. // Utils.VBFriendlyName(StepValue)));
  138. throw new ArgumentException("ForLoop_CommonType3 startType limitType StepValue");
  139. }
  140. ObjectFor objectFor = new ObjectFor();
  141. TypeCode startTypeCode = Type.GetTypeCode(startType);
  142. TypeCode limitTypeCode = Type.GetTypeCode(limitType);
  143. TypeCode stepTypeCode = Type.GetTypeCode(stepType);
  144. Type enumType = null;
  145. bool isStartTypeValidEnum = (startTypeCode == commonTypeCode) && (startType.IsEnum);
  146. bool isLimitTypeValidEnum = (limitTypeCode == commonTypeCode) && (limitType.IsEnum);
  147. bool isStepTypeValidEnum = (stepTypeCode == commonTypeCode) && (stepType.IsEnum);
  148. bool isStartAndStepTypeEqual = (startType == stepType);
  149. bool isStartAndLimitTypeEqual = (startType == limitType);
  150. bool isStepAndLimitTypeEqual = (stepType == limitType);
  151. //the For loop has enum type in the following case
  152. //1. step is enum and it's type code equal to commonTypeCode and start and
  153. // limit don't meet this condition.
  154. //2. step and start are enum and their type code equal to commonTypeCode and
  155. // their types are equal. limit doesn't meet this condition about been enum
  156. // or about been equal to commonTypeCode.
  157. //3. step and limit are enum and their type code equal to commonTypeCode and
  158. // their types are equal. start doesn't meet this condition about been enum
  159. // or about been equal to commonTypeCode.
  160. //4. step and limit and start are enum and their type code equal to commonTypeCode and
  161. // their types are equal.
  162. //5. start is enum and it's type code equal to commonTypeCode .step and
  163. // limit don't meet this condition.
  164. //6. limit is enum and it's type code equal to commonTypeCode .step and
  165. // start don't meet this condition.
  166. //7.start and limit are enum and their type code equal to commonTypeCode and
  167. // their types are equal. step doesn't meet this condition about been enum
  168. // or about been equal to commonTypeCode.
  169. //
  170. if (isStartTypeValidEnum && isLimitTypeValidEnum && isStepTypeValidEnum
  171. && isStartAndStepTypeEqual && isStartAndLimitTypeEqual)
  172. enumType = startType;
  173. else if (isStartTypeValidEnum && isStepTypeValidEnum && isStartAndStepTypeEqual)
  174. enumType = startType;
  175. else if (isStartTypeValidEnum && isStepTypeValidEnum && isStartAndStepTypeEqual)
  176. enumType = startType;
  177. else if (isStartTypeValidEnum && isLimitTypeValidEnum && isStartAndLimitTypeEqual)
  178. enumType = startType;
  179. else if (isStartTypeValidEnum && !isLimitTypeValidEnum && !isStepTypeValidEnum)
  180. enumType = startType;
  181. else if (!isStartTypeValidEnum && isLimitTypeValidEnum && !isStepTypeValidEnum)
  182. enumType = limitType;
  183. else if (!isStartTypeValidEnum && !isLimitTypeValidEnum && isStepTypeValidEnum)
  184. enumType = stepType;
  185. objectFor.EnumType = enumType;
  186. //set the counter field of objectFor with Start value transleted to
  187. // the widest common type code
  188. objectFor.Counter = convertType(Start, commonTypeCode,"Start");
  189. //set the Limit field of objectFor with Limit value transleted to
  190. // the widest common type code
  191. objectFor.Limit = convertType(Limit, commonTypeCode,"Limit");
  192. //set the StepValue field of objectFor with StepValue value transleted to
  193. // the widest common type code
  194. objectFor.StepValue = convertType(StepValue, commonTypeCode,"Step");
  195. //local is the value of zero in the widest common type code
  196. object local = ObjectType.CTypeHelper(0, commonTypeCode);
  197. IComparable iComparable = (IComparable)objectFor.StepValue;
  198. objectFor.PositiveStep = iComparable.CompareTo(local) >= 0;
  199. // sets the loop for reference
  200. lfr = objectFor;
  201. //sets the counter reference
  202. if (objectFor.EnumType != null) {
  203. cr = Enum.ToObject(objectFor.EnumType, objectFor.Counter);
  204. }
  205. else {
  206. cr = objectFor.Counter;
  207. }
  208. return CheckContinueLoop(objectFor);
  209. }
  210. private static object convertType(object original, TypeCode typeCode, string fieldName) {
  211. try {
  212. return ObjectType.CTypeHelper(original, typeCode);
  213. }
  214. catch /*(Exception e)*/ {
  215. throw new ArgumentException("ForLoop_ConvertToType3 " + fieldName);
  216. }
  217. }
  218. public static bool ForNextCheckObj(object Counter, object LoopObj,
  219. ref System.Object CounterResult) {// throws java.lang.Exception
  220. TypeCode generalTypeCode = 0;
  221. if (LoopObj == null) {
  222. //TODO: use resource for the correct execption.
  223. throw new Exception("VB error message #92 ForNextCheckObj LoopObj cannot be null");
  224. //throw ExceptionUtils.VbMakeException(92);//correct java version
  225. }
  226. if (Counter == null) {
  227. throw new NullReferenceException("Argument_InvalidNullValue1 " + " Counter");
  228. //TODO:
  229. //throw new NullReferenceException(
  230. // Utils.GetResourceString(
  231. // "Argument_InvalidNullValue1",
  232. // "Counter"));
  233. }
  234. ObjectFor objectFor = (ObjectFor) LoopObj;
  235. IConvertible iConvertible_counter = (IConvertible)Counter;
  236. IConvertible iConvertible_step = (IConvertible) objectFor.StepValue;
  237. TypeCode counterTypeCode = iConvertible_counter.GetTypeCode();
  238. TypeCode stepTypeCode = iConvertible_step.GetTypeCode();
  239. if (counterTypeCode == stepTypeCode && counterTypeCode != TypeCode.String) {
  240. generalTypeCode = counterTypeCode;
  241. }
  242. else {
  243. generalTypeCode = ObjectType.GetWidestType(counterTypeCode, stepTypeCode);
  244. if (generalTypeCode == TypeCode.String) {
  245. generalTypeCode = TypeCode.Double;
  246. }
  247. Counter = convertType(Counter, generalTypeCode,"Start");
  248. objectFor.Limit = convertType(objectFor.Limit, generalTypeCode,"Limit");
  249. objectFor.StepValue = convertType(objectFor.StepValue, generalTypeCode,"Step");
  250. }
  251. //changes the counter field to be the sum of step and counter
  252. objectFor.Counter = ObjectType.AddObj(Counter, objectFor.StepValue);
  253. IConvertible iConvertible_objectCounter = (IConvertible)objectFor.Counter;
  254. TypeCode objectCounterTypeCode = iConvertible_objectCounter.GetTypeCode();
  255. //setting the counter in counter reference.
  256. //if the for is enum type change counter to enum.
  257. if (objectFor.EnumType != null) {
  258. CounterResult = Enum.ToObject(objectFor.EnumType, objectFor.Counter);
  259. }
  260. else {
  261. CounterResult = objectFor.Counter;
  262. }
  263. //if the counter after the change didn't change it's type return true if
  264. // the for object can continue loop and false otherwise.
  265. //if the counter changed it's type change all for object fields to counter
  266. //current type and return false.
  267. if (objectCounterTypeCode == generalTypeCode) {
  268. return CheckContinueLoop(objectFor);
  269. }
  270. else {
  271. objectFor.Limit = ObjectType.CTypeHelper(objectFor.Limit, objectCounterTypeCode);
  272. objectFor.StepValue =
  273. ObjectType.CTypeHelper(objectFor.StepValue, objectCounterTypeCode);
  274. return false;
  275. }
  276. }
  277. /**
  278. * This method returns IEnumertator for a given array
  279. * @param ary the given array
  280. * @return IEnumerator the array's Enumerator
  281. */
  282. public static IEnumerator ForEachInArr(Array ary) {// throws java.lang.Exception
  283. IEnumerator iEnumerator = (IEnumerator)ary;//is ArrayStaticWrapper.GetEnumerator(ary); in java code.
  284. if (iEnumerator != null)
  285. return iEnumerator;
  286. throw ExceptionUtils.VbMakeException(92);
  287. }
  288. /**
  289. * This method gets IEnumerator for a given object that implements IEnumerable
  290. * @param obj the object that implements IEnumerable
  291. * @return IEnumerator the object's IEnumerator.
  292. */
  293. public static IEnumerator ForEachInObj(object obj) {// throws java.lang.Exception
  294. if (obj == null)
  295. throw ExceptionUtils.VbMakeException(91);
  296. IEnumerable iEnumerable = (IEnumerable)obj;
  297. if (iEnumerable != null) {
  298. IEnumerator iEnumerator = iEnumerable.GetEnumerator();
  299. if (iEnumerator != null)
  300. return iEnumerator;
  301. }
  302. string s = obj.GetType().ToString();
  303. ExceptionUtils.ThrowException1(100, s);
  304. return null;
  305. }
  306. /**
  307. * This method set the next value of teh Enumerator in the reference.
  308. * if there isn't next value , null is been set in the referece.
  309. * @param obj
  310. * @param enumerator
  311. * @return boolean returns the value of enumerator.MoveNext().
  312. */
  313. public static bool ForEachNextObj(ref System.Object obj, IEnumerator enumerator) {
  314. if (enumerator.MoveNext()) {
  315. obj = enumerator.Current;
  316. return true;
  317. }
  318. obj = null;
  319. return false;
  320. }
  321. /**
  322. * This method check if the loop can continued.
  323. * if the step is positive it check that the counter is smaller than the limit.
  324. * if the step is negative it check that the counter is bigger than the limit.
  325. * @param LoopFor
  326. * @return boolean
  327. */
  328. private static bool CheckContinueLoop(ObjectFor LoopFor) {
  329. //TODO:
  330. //throw new NotImplementedException("MSVB.Compilerservices.flowcontrol needs help");
  331. IComparable iComparable = (IComparable)LoopFor.Counter;
  332. if (iComparable != null) {
  333. int i = iComparable.CompareTo(LoopFor.Limit);
  334. bool isCountSmallThenLimit = i<=0;
  335. return LoopFor.PositiveStep ? isCountSmallThenLimit : !isCountSmallThenLimit;
  336. }
  337. throw new ArgumentException("Argument_IComparable2 loop control variable"); // + Utils.VBFriendlyName(LoopFor.Counter)));
  338. //TODO: verify this and the above are the same and remove.
  339. //throw new ArgumentException(Utils.GetResourceString(
  340. // "Argument_IComparable2", "loop control variable",
  341. // Utils.VBFriendlyName(LoopFor.Counter)));
  342. }
  343. /**
  344. * This method throws exception if the input is Valuetype
  345. * @param obj the object that need to be checked
  346. */
  347. public static void CheckForSyncLockOnValueType(object obj) {
  348. //TODO:
  349. //throw new NotImplementedException("MSVB.Compilerservices.flowcontrol needs help");
  350. if (obj != null && obj.GetType().IsValueType)
  351. throw new ArgumentException(Utils.GetResourceString("SyncLockRequiresReferenceType1 "));
  352. //TODO: verify this and the above are the same and remove.
  353. //if (obj != null && ObjectStaticWrapper.GetType(obj).get_IsValueType())
  354. // throw new ArgumentException(Utils.GetResourceString(
  355. // "SyncLockRequiresReferenceType1",Utils.VBFriendlyName(obj)));
  356. }
  357. }
  358. }