PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 1ms

/Dependencies/boo/src/Boo.Lang/Builtins.cs

https://github.com/w4x/boolangstudio
C# | 444 lines | 362 code | 43 blank | 39 comment | 40 complexity | e47f24301ea569f30b13b1672100af10 MD5 | raw file
Possible License(s): GPL-2.0
  1. #region license
  2. // Copyright (c) 2004, Rodrigo B. de Oliveira (rbo@acm.org)
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without modification,
  6. // are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Rodrigo B. de Oliveira nor the names of its
  14. // contributors may be used to endorse or promote products derived from this
  15. // software without specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
  21. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  23. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  24. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  25. // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  26. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. #endregion
  28. using System;
  29. using System.Collections;
  30. using System.Collections.Generic;
  31. using System.IO;
  32. using System.Text;
  33. using System.Reflection;
  34. using Boo.Lang.Runtime;
  35. namespace Boo.Lang
  36. {
  37. /// <summary>
  38. /// boo language builtin functions.
  39. /// </summary>
  40. public class Builtins
  41. {
  42. public class duck
  43. {
  44. }
  45. public static System.Version BooVersion
  46. {
  47. get
  48. {
  49. return new System.Version("0.8.2.2963");
  50. }
  51. }
  52. public static void print(object o)
  53. {
  54. Console.WriteLine(o);
  55. }
  56. public static string gets()
  57. {
  58. return Console.ReadLine();
  59. }
  60. public static string prompt(string message)
  61. {
  62. Console.Write(message);
  63. return Console.ReadLine();
  64. }
  65. public static string join(IEnumerable enumerable, string separator)
  66. {
  67. StringBuilder sb = new StringBuilder();
  68. IEnumerator enumerator = enumerable.GetEnumerator();
  69. using (enumerator as IDisposable)
  70. {
  71. if (enumerator.MoveNext())
  72. {
  73. sb.Append(enumerator.Current);
  74. while (enumerator.MoveNext())
  75. {
  76. sb.Append(separator);
  77. sb.Append(enumerator.Current);
  78. }
  79. }
  80. }
  81. return sb.ToString();
  82. }
  83. public static string join(IEnumerable enumerable, char separator)
  84. {
  85. StringBuilder sb = new StringBuilder();
  86. IEnumerator enumerator = enumerable.GetEnumerator();
  87. using (enumerator as IDisposable)
  88. {
  89. if (enumerator.MoveNext())
  90. {
  91. sb.Append(enumerator.Current);
  92. while (enumerator.MoveNext())
  93. {
  94. sb.Append(separator);
  95. sb.Append(enumerator.Current);
  96. }
  97. }
  98. }
  99. return sb.ToString();
  100. }
  101. public static string join(IEnumerable enumerable)
  102. {
  103. return join(enumerable, ' ');
  104. }
  105. public static IEnumerable map(object enumerable, ICallable function)
  106. {
  107. if (null == enumerable) throw new ArgumentNullException("enumerable");
  108. if (null == function) throw new ArgumentNullException("function");
  109. object[] args = new object[1];
  110. foreach (object item in iterator(enumerable))
  111. {
  112. args[0] = item;
  113. yield return function.Call(args);
  114. }
  115. }
  116. public static object[] array(IEnumerable enumerable)
  117. {
  118. return new List(enumerable).ToArray();
  119. }
  120. public static Array array(Type elementType, ICollection collection)
  121. {
  122. if (null == collection)
  123. {
  124. throw new ArgumentNullException("collection");
  125. }
  126. if (null == elementType)
  127. {
  128. throw new ArgumentNullException("elementType");
  129. }
  130. Array array = Array.CreateInstance(elementType, collection.Count);
  131. if (RuntimeServices.IsPromotableNumeric(Type.GetTypeCode(elementType)))
  132. {
  133. int i=0;
  134. foreach (object item in collection)
  135. {
  136. object value = RuntimeServices.CheckNumericPromotion(item).ToType(elementType, null);
  137. array.SetValue(value, i);
  138. ++i;
  139. }
  140. }
  141. else
  142. {
  143. collection.CopyTo(array, 0);
  144. }
  145. return array;
  146. }
  147. public static Array array(Type elementType, IEnumerable enumerable)
  148. {
  149. if (null == enumerable)
  150. {
  151. throw new ArgumentNullException("enumerable");
  152. }
  153. if (null == elementType)
  154. {
  155. throw new ArgumentNullException("elementType");
  156. }
  157. // future optimization, check EnumeratorItemType of enumerable
  158. // and get the fast path whenever possible
  159. List l = null;
  160. if (RuntimeServices.IsPromotableNumeric(Type.GetTypeCode(elementType)))
  161. {
  162. l = new List();
  163. foreach (object item in enumerable)
  164. {
  165. object value = RuntimeServices.CheckNumericPromotion(item).ToType(elementType, null);
  166. l.Add(value);
  167. }
  168. }
  169. else
  170. {
  171. l = new List(enumerable);
  172. }
  173. return l.ToArray(elementType);
  174. }
  175. public static Array array(Type elementType, int length)
  176. {
  177. return matrix(elementType, length);
  178. }
  179. public static Array matrix(Type elementType, params int[] lengths)
  180. {
  181. if (null == elementType)
  182. {
  183. throw new ArgumentNullException("elementType");
  184. }
  185. return Array.CreateInstance(elementType, lengths);
  186. }
  187. public static IEnumerable iterator(object enumerable)
  188. {
  189. return RuntimeServices.GetEnumerable(enumerable);
  190. }
  191. #if !NO_SYSTEM_DLL
  192. public static System.Diagnostics.Process shellp(string filename, string arguments)
  193. {
  194. System.Diagnostics.Process p = new System.Diagnostics.Process();
  195. p.StartInfo.Arguments = arguments;
  196. p.StartInfo.CreateNoWindow = true;
  197. p.StartInfo.UseShellExecute = false;
  198. p.StartInfo.RedirectStandardOutput = true;
  199. p.StartInfo.RedirectStandardInput = true;
  200. p.StartInfo.RedirectStandardError = true;
  201. p.StartInfo.FileName = filename;
  202. p.Start();
  203. return p;
  204. }
  205. public static string shell(string filename, string arguments)
  206. {
  207. System.Diagnostics.Process p = shellp(filename, arguments);
  208. string output = p.StandardOutput.ReadToEnd();
  209. p.WaitForExit();
  210. return output;
  211. }
  212. #endif
  213. internal class AssemblyExecutor : MarshalByRefObject
  214. {
  215. string _filename;
  216. string[] _arguments;
  217. string _capturedOutput = "";
  218. public AssemblyExecutor(string filename, string[] arguments)
  219. {
  220. _filename = filename;
  221. _arguments = arguments;
  222. }
  223. public string CapturedOutput
  224. {
  225. get
  226. {
  227. return _capturedOutput;
  228. }
  229. }
  230. public void Execute()
  231. {
  232. StringWriter output = new System.IO.StringWriter();
  233. TextWriter saved = Console.Out;
  234. try
  235. {
  236. Console.SetOut(output);
  237. //AppDomain.CurrentDomain.ExecuteAssembly(_filename, null, _arguments);
  238. Assembly.LoadFrom(_filename).EntryPoint.Invoke(null, new object[1] { _arguments });
  239. }
  240. finally
  241. {
  242. Console.SetOut(saved);
  243. _capturedOutput = output.ToString();
  244. }
  245. }
  246. }
  247. /// <summary>
  248. /// Execute the specified MANAGED application in a new AppDomain.
  249. ///
  250. /// The base directory for the new application domain will be set to
  251. /// directory containing filename (Path.GetDirectoryName(Path.GetFullPath(filename))).
  252. /// </summary>
  253. public static string shellm(string filename, params string[] arguments)
  254. {
  255. AppDomainSetup setup = new AppDomainSetup();
  256. setup.ApplicationBase = Path.GetDirectoryName(Path.GetFullPath(filename));
  257. AppDomain domain = AppDomain.CreateDomain("shellm", null, setup);
  258. try
  259. {
  260. AssemblyExecutor executor = new AssemblyExecutor(filename, arguments);
  261. domain.DoCallBack(new CrossAppDomainDelegate(executor.Execute));
  262. return executor.CapturedOutput;
  263. }
  264. finally
  265. {
  266. AppDomain.Unload(domain);
  267. }
  268. }
  269. public static IEnumerable<object[]> enumerate(object enumerable)
  270. {
  271. int i = 0;
  272. foreach (object item in iterator(enumerable))
  273. {
  274. yield return new object[] { i++, item };
  275. }
  276. }
  277. public static IEnumerable<int> range(int max)
  278. {
  279. if (max < 0) /* added for coherence with behavior of compiler-optimized
  280. * for-in-range() loops, should compiler loops automatically
  281. * inverse iteration in this case? */
  282. {
  283. throw new ArgumentOutOfRangeException("max < 0");
  284. }
  285. return range(0, max);
  286. }
  287. public static IEnumerable<int> range(int begin, int end)
  288. {
  289. if (begin < end)
  290. {
  291. for (int i = begin; i < end; ++i) yield return i;
  292. }
  293. else if (begin > end)
  294. {
  295. for (int i = begin; i > end; --i) yield return i;
  296. }
  297. }
  298. public static IEnumerable<int> range(int begin, int end, int step)
  299. {
  300. if (0 ==step)
  301. {
  302. throw new ArgumentOutOfRangeException("step == 0");
  303. }
  304. if (step < 0)
  305. {
  306. if (begin < end)
  307. {
  308. throw new ArgumentOutOfRangeException("begin < end && step < 0");
  309. }
  310. for (int i = begin; i > end; i += step) yield return i;
  311. }
  312. else
  313. {
  314. if (begin > end)
  315. {
  316. throw new ArgumentOutOfRangeException("begin > end && step > 0");
  317. }
  318. for (int i = begin; i < end; i += step) yield return i;
  319. }
  320. }
  321. public static IEnumerable reversed(object enumerable)
  322. {
  323. return new List(iterator(enumerable)).Reversed;
  324. }
  325. public static ZipEnumerator zip(params object[] enumerables)
  326. {
  327. IEnumerator[] enumerators = new IEnumerator[enumerables.Length];
  328. for (int i=0; i<enumerables.Length; ++i)
  329. {
  330. enumerators[i] = GetEnumerator(enumerables[i]);
  331. }
  332. return new ZipEnumerator(enumerators);
  333. }
  334. public static IEnumerable<object> cat(params object[] args)
  335. {
  336. foreach (object e in args)
  337. {
  338. foreach (object item in iterator(e))
  339. {
  340. yield return item;
  341. }
  342. }
  343. }
  344. [EnumeratorItemType(typeof(object[]))]
  345. public class ZipEnumerator : IEnumerator, IEnumerable, IDisposable
  346. {
  347. IEnumerator[] _enumerators;
  348. internal ZipEnumerator(params IEnumerator[] enumerators)
  349. {
  350. _enumerators = enumerators;
  351. }
  352. public void Dispose()
  353. {
  354. for (int i=0; i<_enumerators.Length; ++i)
  355. {
  356. IDisposable d = _enumerators[i] as IDisposable;
  357. if (d != null)
  358. d.Dispose();
  359. }
  360. }
  361. public void Reset()
  362. {
  363. for (int i=0; i<_enumerators.Length; ++i)
  364. {
  365. _enumerators[i].Reset();
  366. }
  367. }
  368. public bool MoveNext()
  369. {
  370. for (int i=0; i<_enumerators.Length; ++i)
  371. {
  372. if (!_enumerators[i].MoveNext())
  373. {
  374. return false;
  375. }
  376. }
  377. return true;
  378. }
  379. public object Current
  380. {
  381. get
  382. {
  383. object[] current = new object[_enumerators.Length];
  384. for (int i=0; i<current.Length; ++i)
  385. {
  386. current[i] = _enumerators[i].Current;
  387. }
  388. return current;
  389. }
  390. }
  391. public IEnumerator GetEnumerator()
  392. {
  393. return this;
  394. }
  395. }
  396. private static IEnumerator GetEnumerator(object enumerable)
  397. {
  398. return RuntimeServices.GetEnumerable(enumerable).GetEnumerator();
  399. }
  400. }
  401. }