PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/Ruby/Libraries/Builtins/PrintOps.cs

http://github.com/IronLanguages/main
C# | 143 lines | 103 code | 21 blank | 19 comment | 13 complexity | c6d51f3b1e918a5cdce08309700eb307 MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. /* ****************************************************************************
  2. *
  3. * Copyright (c) Microsoft Corporation.
  4. *
  5. * This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. * copy of the license can be found in the License.html file at the root of this distribution. If
  7. * you cannot locate the Apache License, Version 2.0, please send an email to
  8. * ironruby@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. * by the terms of the Apache License, Version 2.0.
  10. *
  11. * You must not remove this notice, or any other, from this software.
  12. *
  13. *
  14. * ***************************************************************************/
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.Globalization;
  18. using IronRuby.Runtime;
  19. using Microsoft.Scripting.Runtime;
  20. using IronRuby.Runtime.Conversions;
  21. namespace IronRuby.Builtins {
  22. /// <summary>
  23. /// Mixin. Implements print, puts, putc methods.
  24. /// </summary>
  25. [RubyModule("Print", DefineIn = typeof(IronRubyOps))]
  26. public static class PrintOps {
  27. [RubyMethod("<<")]
  28. public static object/*!*/ Output(BinaryOpStorage/*!*/ writeStorage, object self, object value) {
  29. Protocols.Write(writeStorage, self, value);
  30. return self;
  31. }
  32. [RubyMethod("print")]
  33. public static void Print(BinaryOpStorage/*!*/ writeStorage, RubyScope/*!*/ scope, object self) {
  34. Print(writeStorage, self, scope.GetInnerMostClosureScope().LastInputLine);
  35. }
  36. [RubyMethod("print")]
  37. public static void Print(BinaryOpStorage/*!*/ writeStorage, object self, params object[]/*!*/ args) {
  38. foreach (object arg in args) {
  39. Print(writeStorage, self, arg);
  40. }
  41. }
  42. [RubyMethod("print")]
  43. public static void Print(BinaryOpStorage/*!*/ writeStorage, object self, object value) {
  44. Protocols.Write(writeStorage, self, value ?? MutableString.CreateAscii("nil"));
  45. MutableString delimiter = writeStorage.Context.OutputSeparator;
  46. if (delimiter != null) {
  47. Protocols.Write(writeStorage, self, delimiter);
  48. }
  49. }
  50. [RubyMethod("putc")]
  51. public static MutableString/*!*/ Putc(BinaryOpStorage/*!*/ writeStorage, object self, [NotNull]MutableString/*!*/ val) {
  52. if (val.IsEmpty) {
  53. throw RubyExceptions.CreateTypeError("can't convert String into Integer");
  54. }
  55. // writes a single byte into the output stream:
  56. var c = MutableString.CreateBinary(val.GetBinarySlice(0, 1));
  57. Protocols.Write(writeStorage, self, c);
  58. return val;
  59. }
  60. [RubyMethod("putc")]
  61. public static int Putc(BinaryOpStorage/*!*/ writeStorage, object self, [DefaultProtocol]int c) {
  62. MutableString str = MutableString.CreateBinary(1).Append(unchecked((byte)c));
  63. Protocols.Write(writeStorage, self, str);
  64. return c;
  65. }
  66. public static MutableString/*!*/ ToPrintedString(ConversionStorage<MutableString>/*!*/ tosConversion, object obj) {
  67. if (obj == null) {
  68. return MutableString.CreateAscii("nil");
  69. } else {
  70. return Protocols.ConvertToString(tosConversion, obj);
  71. }
  72. }
  73. [RubyMethod("puts")]
  74. public static void PutsEmptyLine(BinaryOpStorage/*!*/ writeStorage, object self) {
  75. Protocols.Write(writeStorage, self, MutableString.CreateAscii("\n"));
  76. }
  77. [RubyMethod("puts")]
  78. public static void Puts(BinaryOpStorage/*!*/ writeStorage, object self, [NotNull]MutableString/*!*/ str) {
  79. Protocols.Write(writeStorage, self, str);
  80. if (!str.EndsWith('\n')) {
  81. PutsEmptyLine(writeStorage, self);
  82. }
  83. }
  84. [RubyMethod("puts")]
  85. public static void Puts(BinaryOpStorage/*!*/ writeStorage, ConversionStorage<MutableString>/*!*/ tosConversion,
  86. ConversionStorage<IList>/*!*/ tryToAry, object self, [NotNull]object/*!*/ val) {
  87. IList list = Protocols.TryCastToArray(tryToAry, val);
  88. if (list != null) {
  89. IEnumerable recEnum = IListOps.EnumerateRecursively(tryToAry, list, -1, (_) => MutableString.CreateAscii("[...]"));
  90. foreach (object item in recEnum ?? list) {
  91. Puts(writeStorage, self, ToPrintedString(tosConversion, item));
  92. }
  93. } else {
  94. Puts(writeStorage, self, ToPrintedString(tosConversion, val));
  95. }
  96. }
  97. [RubyMethod("puts")]
  98. public static void Puts(BinaryOpStorage/*!*/ writeStorage, ConversionStorage<MutableString>/*!*/ tosConversion,
  99. ConversionStorage<IList>/*!*/ tryToAry, object self, params object[]/*!*/ vals) {
  100. for (int i = 0; i < vals.Length; i++) {
  101. Puts(writeStorage, tosConversion, tryToAry, self, vals[i]);
  102. }
  103. }
  104. [RubyMethod("printf")]
  105. public static void PrintFormatted(
  106. StringFormatterSiteStorage/*!*/ storage,
  107. ConversionStorage<MutableString>/*!*/ stringCast,
  108. BinaryOpStorage/*!*/ writeStorage,
  109. object/*!*/ self, [DefaultProtocol, NotNull]MutableString/*!*/ format, params object[]/*!*/ args) {
  110. KernelOps.PrintFormatted(storage, stringCast, writeStorage, null, self, format, args);
  111. }
  112. internal static void ReportWarning(BinaryOpStorage/*!*/ writeStorage, ConversionStorage<MutableString>/*!*/ tosConversion, object message) {
  113. if (writeStorage.Context.Verbose != null) {
  114. var output = writeStorage.Context.StandardErrorOutput;
  115. // MRI: unlike Kernel#puts this outputs \n even if the message ends with \n:
  116. var site = writeStorage.GetCallSite("write", 1);
  117. site.Target(site, output, PrintOps.ToPrintedString(tosConversion, message));
  118. PrintOps.PutsEmptyLine(writeStorage, output);
  119. }
  120. }
  121. }
  122. }