YP2CSConverter.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS AS IS AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. *
  27. */
  28. using System;
  29. using System.IO;
  30. using System.Collections.Generic;
  31. using System.Text;
  32. using System.Text.RegularExpressions;
  33. using OpenSim.Region.ScriptEngine.Shared.YieldProlog;
  34. namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
  35. {
  36. public class YP2CSConverter
  37. {
  38. public YP2CSConverter()
  39. {
  40. }
  41. public string Convert(string Script)
  42. {
  43. string CS_code = GenCode(Script);
  44. return CS_code;
  45. }
  46. static string GenCode(string myCode)
  47. {
  48. Variable TermList = new Variable();
  49. Variable FunctionCode = new Variable();
  50. string CS_code = "";
  51. int cs_pointer = myCode.IndexOf("\n//cs");
  52. if (cs_pointer > 0)
  53. {
  54. CS_code = myCode.Substring(cs_pointer); // CS code comes after
  55. myCode = myCode.Substring(0, cs_pointer);
  56. }
  57. myCode.Replace("//yp", "%YPCode");
  58. StringWriter myCS_SW = new StringWriter();
  59. StringReader myCode_SR = new StringReader(" yp_nop_header_nop. \n "+myCode + "\n");
  60. YP.see(myCode_SR);
  61. YP.tell(myCS_SW);
  62. //Console.WriteLine("Mycode\n ===================================\n" + myCode+"\n");
  63. // disable warning: don't see how we can code this differently short
  64. // of rewriting the whole thing
  65. #pragma warning disable 0168, 0219
  66. foreach (bool l1 in Parser.parseInput(TermList))
  67. {
  68. foreach (bool l2 in YPCompiler.makeFunctionPseudoCode(TermList, FunctionCode))
  69. {
  70. // ListPair VFC = new ListPair(FunctionCode, new Variable());
  71. //Console.WriteLine("-------------------------")
  72. //Console.WriteLine(FunctionCode.ToString())
  73. //Console.WriteLine("-------------------------")
  74. YPCompiler.convertFunctionCSharp(FunctionCode);
  75. //YPCompiler.convertStringCodesCSharp(VFC);
  76. }
  77. }
  78. #pragma warning restore 0168, 0219
  79. YP.seen();
  80. myCS_SW.Close();
  81. YP.told();
  82. StringBuilder bu = myCS_SW.GetStringBuilder();
  83. string finalcode = "//YPEncoded\n" + bu.ToString();
  84. // FIX script events (we're in the same script)
  85. // 'YP.script_event(Atom.a(@"sayit"),' ==> 'sayit('
  86. finalcode = Regex.Replace(finalcode,
  87. @"YP.script_event\(Atom.a\(\@\""(.*?)""\)\,",
  88. @"this.$1(",
  89. RegexOptions.Compiled | RegexOptions.Singleline);
  90. finalcode = Regex.Replace(finalcode,
  91. @"YP.script_event\(Atom.a\(\""(.*?)""\)\,",
  92. @"this.$1(",
  93. RegexOptions.Compiled | RegexOptions.Singleline);
  94. finalcode = Regex.Replace(finalcode,
  95. @" static ",
  96. @" ",
  97. RegexOptions.Compiled | RegexOptions.Singleline);
  98. finalcode = CS_code+"\n\r"+ finalcode;
  99. finalcode = Regex.Replace(finalcode,
  100. @"PrologCallback",
  101. @"public IEnumerable<bool> ",
  102. RegexOptions.Compiled | RegexOptions.Singleline);
  103. return finalcode;
  104. }
  105. }
  106. }