PageRenderTime 24ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/Dependencies/boo/src/Boo.Lang.Compiler/Ast/Visitors/PseudoCSharpPrinterVisitor.cs

https://github.com/w4x/boolangstudio
C# | 285 lines | 16 code | 4 blank | 265 comment | 0 complexity | bd4c2d9b54505a75b698b9ae34c26539 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. namespace Boo.Lang.Compiler.Ast.Visitors
  30. {
  31. /// <summary>
  32. /// Prints a boo ast in pseudo C#.
  33. /// </summary>
  34. public class PseudoCSharpPrinterVisitor : TextEmitter
  35. {
  36. public PseudoCSharpPrinterVisitor(System.IO.TextWriter writer) : base(writer)
  37. {
  38. }
  39. public void Print(Node node)
  40. {
  41. node.Accept(this);
  42. }
  43. /*
  44. #region IVisitor Members
  45. override public bool EnterModule(Module g)
  46. {
  47. WriteLine("public static final class Module");
  48. WriteLine("{");
  49. Indent();
  50. g.Members.Accept(this);
  51. if (g.Globals.Statements.Count > 0)
  52. {
  53. WriteLine("public static void Main(string[] args)");
  54. WriteLine("{");
  55. Indent();
  56. g.Globals.Statements.Accept(this);
  57. Dedent();
  58. WriteLine("}");
  59. }
  60. Dedent();
  61. WriteLine("}");
  62. return false;
  63. }
  64. override public bool EnterImport(Import p)
  65. {
  66. WriteLine("using {0};", p.Namespace);
  67. WriteLine();
  68. return true;
  69. }
  70. override public bool EnterClassDefinition(ClassDefinition c)
  71. {
  72. WriteLine("[Serializable]");
  73. WriteLine("public class {0}", c.Name);
  74. WriteLine("{");
  75. Indent();
  76. return true;
  77. }
  78. override public bool LeaveClassDefinition(ClassDefinition c)
  79. {
  80. Dedent();
  81. WriteLine("}");
  82. WriteLine();
  83. return true;
  84. }
  85. override public bool EnterField(Field f)
  86. {
  87. WriteLine("protected {0} {1};", ResolveType(f.Type), f.Name);
  88. return true;
  89. }
  90. override public bool EnterMethod(Method m)
  91. {
  92. WriteIndented("public {0} {1}(", ResolveType(m.ReturnType), m.Name);
  93. for (int i=0; i<m.Parameters.Count; ++i)
  94. {
  95. if (i > 0)
  96. {
  97. Write(", ");
  98. }
  99. ParameterDeclaration pd = m.Parameters[i];
  100. Write("{0} {1}", ResolveType(pd.Type), pd.Name);
  101. }
  102. WriteLine(")");
  103. return true;
  104. }
  105. override public bool EnterBlock(Block b)
  106. {
  107. WriteLine("{");
  108. Indent();
  109. return true;
  110. }
  111. override public bool LeaveBlock(Block b)
  112. {
  113. Dedent();
  114. WriteLine("}");
  115. return true;
  116. }
  117. override public bool EnterReturnStatement(ReturnStatement r)
  118. {
  119. WriteIndented("return ");
  120. return true;
  121. }
  122. override public bool LeaveReturnStatement(ReturnStatement r)
  123. {
  124. WriteLine(";");
  125. return true;
  126. }
  127. override public bool EnterExpressionStatement(ExpressionStatement es)
  128. {
  129. WriteIndented("");
  130. return true;
  131. }
  132. override public bool LeaveExpressionStatement(ExpressionStatement es)
  133. {
  134. WriteLine(";");
  135. return true;
  136. }
  137. override public bool EnterBinaryExpression(BinaryExpression e)
  138. {
  139. e.Left.Accept(this);
  140. Write(ResolveOperator(e.Operator));
  141. e.Right.Accept(this);
  142. return false;
  143. }
  144. override public bool EnterReferenceExpression(ReferenceExpression e)
  145. {
  146. Write(e.Name);
  147. return true;
  148. }
  149. override public bool EnterMethodInvocationExpression(MethodInvocationExpression e)
  150. {
  151. e.Target.Accept(this);
  152. Write("(");
  153. for (int i=0; i<e.Parameters.Count; ++i)
  154. {
  155. if (i>0)
  156. {
  157. Write(", ");
  158. }
  159. e.Parameters[i].Accept(this);
  160. }
  161. Write(")");
  162. return false;
  163. }
  164. override public bool EnterIntegerLiteralExpression(IntegerLiteralExpression e)
  165. {
  166. Write(e.Value.ToString());
  167. return true;
  168. }
  169. override public bool EnterStringLiteralExpression(StringLiteralExpression e)
  170. {
  171. Write("\"");
  172. Write(e.Value);
  173. Write("\"");
  174. return true;
  175. }
  176. override public bool EnterListLiteralExpression(ListLiteralExpression lle)
  177. {
  178. Write("new ArrayList(new object[] { ");
  179. for (int i=0; i<lle.Items.Count; ++i)
  180. {
  181. if (i>0)
  182. {
  183. Write(", ");
  184. }
  185. lle.Items[i].Accept(this);
  186. }
  187. Write(" })");
  188. return false;
  189. }
  190. override public bool EnterForStatement(ForStatement fs)
  191. {
  192. WriteIndented("foreach (");
  193. for (int i=0; i<fs.Declarations.Count; ++i)
  194. {
  195. if (i>0)
  196. {
  197. Write(", ");
  198. }
  199. Write(ResolveType(fs.Declarations[i].Type));
  200. Write(" {0}", fs.Declarations[i].Name);
  201. }
  202. Write(" in ");
  203. fs.Iterator.Accept(this);
  204. WriteLine(")");
  205. WriteLine("{");
  206. Indent();
  207. fs.Statements.Accept(this);
  208. Dedent();
  209. WriteLine("}");
  210. return false;
  211. }
  212. #endregion
  213. string ResolveType(TypeReference t)
  214. {
  215. if (null == t)
  216. {
  217. return "object";
  218. }
  219. return t.Name;
  220. }
  221. string ResolveOperator(BinaryOperatorType o)
  222. {
  223. switch (o)
  224. {
  225. case BinaryOperatorType.Addition:
  226. {
  227. return "+";
  228. }
  229. case BinaryOperatorType.Subtraction:
  230. {
  231. return "-";
  232. }
  233. case BinaryOperatorType.Multiply:
  234. {
  235. return "*";
  236. }
  237. case BinaryOperatorType.Division:
  238. {
  239. return "/";
  240. }
  241. case BinaryOperatorType.LessThan:
  242. {
  243. return "<";
  244. }
  245. }
  246. return "?";
  247. }
  248. */
  249. }
  250. }