LSL2CSCodeTransformer.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 OpenSimulator 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. using System;
  28. using System.Collections.Generic;
  29. using System.Reflection;
  30. using log4net;
  31. using Tools;
  32. namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
  33. {
  34. public class LSL2CSCodeTransformer
  35. {
  36. // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
  37. private SYMBOL m_astRoot = null;
  38. private static Dictionary<string, string> m_datatypeLSL2OpenSim = null;
  39. /// <summary>
  40. /// Pass the new CodeTranformer an abstract syntax tree.
  41. /// </summary>
  42. /// <param name="astRoot">The root node of the AST.</param>
  43. public LSL2CSCodeTransformer(SYMBOL astRoot)
  44. {
  45. m_astRoot = astRoot;
  46. // let's populate the dictionary
  47. if (null == m_datatypeLSL2OpenSim)
  48. {
  49. m_datatypeLSL2OpenSim = new Dictionary<string, string>();
  50. m_datatypeLSL2OpenSim.Add("integer", "LSL_Types.LSLInteger");
  51. m_datatypeLSL2OpenSim.Add("float", "LSL_Types.LSLFloat");
  52. //m_datatypeLSL2OpenSim.Add("key", "LSL_Types.key"); // key doesn't seem to be used
  53. m_datatypeLSL2OpenSim.Add("key", "LSL_Types.LSLString");
  54. m_datatypeLSL2OpenSim.Add("string", "LSL_Types.LSLString");
  55. m_datatypeLSL2OpenSim.Add("vector", "LSL_Types.Vector3");
  56. m_datatypeLSL2OpenSim.Add("rotation", "LSL_Types.Quaternion");
  57. m_datatypeLSL2OpenSim.Add("list", "LSL_Types.list");
  58. }
  59. }
  60. /// <summary>
  61. /// Transform the code in the AST we have.
  62. /// </summary>
  63. /// <returns>The root node of the transformed AST</returns>
  64. public SYMBOL Transform()
  65. {
  66. foreach (SYMBOL s in m_astRoot.kids)
  67. TransformNode(s);
  68. return m_astRoot;
  69. }
  70. /// <summary>
  71. /// Recursively called to transform each type of node. Will transform this
  72. /// node, then all it's children.
  73. /// </summary>
  74. /// <param name="s">The current node to transform.</param>
  75. private void TransformNode(SYMBOL s)
  76. {
  77. // m_log.DebugFormat("[LSL2CSCODETRANSFORMER]: Tranforming node {0}", s);
  78. // make sure to put type lower in the inheritance hierarchy first
  79. // ie: since IdentConstant and StringConstant inherit from Constant,
  80. // put IdentConstant and StringConstant before Constant
  81. if (s is Declaration)
  82. ((Declaration) s).Datatype = m_datatypeLSL2OpenSim[((Declaration) s).Datatype];
  83. else if (s is Constant)
  84. ((Constant) s).Type = m_datatypeLSL2OpenSim[((Constant) s).Type];
  85. else if (s is TypecastExpression)
  86. ((TypecastExpression) s).TypecastType = m_datatypeLSL2OpenSim[((TypecastExpression) s).TypecastType];
  87. else if (s is GlobalFunctionDefinition && "void" != ((GlobalFunctionDefinition) s).ReturnType) // we don't need to translate "void"
  88. ((GlobalFunctionDefinition) s).ReturnType = m_datatypeLSL2OpenSim[((GlobalFunctionDefinition) s).ReturnType];
  89. for (int i = 0; i < s.kids.Count; i++)
  90. {
  91. // It's possible that a child is null, for instance when the
  92. // assignment part in a for-loop is left out, ie:
  93. //
  94. // for (; i < 10; i++)
  95. // {
  96. // ...
  97. // }
  98. //
  99. // We need to check for that here.
  100. if (null != s.kids[i])
  101. {
  102. // m_log.Debug("[LSL2CSCODETRANSFORMER]: Moving down level");
  103. if (!(s is Assignment || s is ArgumentDeclarationList) && s.kids[i] is Declaration)
  104. AddImplicitInitialization(s, i);
  105. TransformNode((SYMBOL) s.kids[i]);
  106. // m_log.Debug("[LSL2CSCODETRANSFORMER]: Moving up level");
  107. }
  108. }
  109. }
  110. /// <summary>
  111. /// Replaces an instance of the node at s.kids[didx] with an assignment
  112. /// node. The assignment node has the Declaration node on the left hand
  113. /// side and a default initializer on the right hand side.
  114. /// </summary>
  115. /// <param name="s">
  116. /// The node containing the Declaration node that needs replacing.
  117. /// </param>
  118. /// <param name="didx">Index of the Declaration node to replace.</param>
  119. private void AddImplicitInitialization(SYMBOL s, int didx)
  120. {
  121. // We take the kids for a while to play with them.
  122. int sKidSize = s.kids.Count;
  123. object [] sKids = new object[sKidSize];
  124. for (int i = 0; i < sKidSize; i++)
  125. sKids[i] = s.kids.Pop();
  126. // The child to be changed.
  127. Declaration currentDeclaration = (Declaration) sKids[didx];
  128. // We need an assignment node.
  129. Assignment newAssignment = new Assignment(currentDeclaration.yyps,
  130. currentDeclaration,
  131. GetZeroConstant(currentDeclaration.yyps, currentDeclaration.Datatype),
  132. "=");
  133. sKids[didx] = newAssignment;
  134. // Put the kids back where they belong.
  135. for (int i = 0; i < sKidSize; i++)
  136. s.kids.Add(sKids[i]);
  137. }
  138. /// <summary>
  139. /// Generates the node structure required to generate a default
  140. /// initialization.
  141. /// </summary>
  142. /// <param name="p">
  143. /// Tools.Parser instance to use when instantiating nodes.
  144. /// </param>
  145. /// <param name="constantType">String describing the datatype.</param>
  146. /// <returns>
  147. /// A SYMBOL node conaining the appropriate structure for intializing a
  148. /// constantType.
  149. /// </returns>
  150. private SYMBOL GetZeroConstant(Parser p, string constantType)
  151. {
  152. switch (constantType)
  153. {
  154. case "integer":
  155. return new Constant(p, constantType, "0");
  156. case "float":
  157. return new Constant(p, constantType, "0.0");
  158. case "string":
  159. case "key":
  160. return new Constant(p, constantType, "");
  161. case "list":
  162. ArgumentList al = new ArgumentList(p);
  163. return new ListConstant(p, al);
  164. case "vector":
  165. Constant vca = new Constant(p, "float", "0.0");
  166. Constant vcb = new Constant(p, "float", "0.0");
  167. Constant vcc = new Constant(p, "float", "0.0");
  168. ConstantExpression vcea = new ConstantExpression(p, vca);
  169. ConstantExpression vceb = new ConstantExpression(p, vcb);
  170. ConstantExpression vcec = new ConstantExpression(p, vcc);
  171. return new VectorConstant(p, vcea, vceb, vcec);
  172. case "rotation":
  173. Constant rca = new Constant(p, "float", "0.0");
  174. Constant rcb = new Constant(p, "float", "0.0");
  175. Constant rcc = new Constant(p, "float", "0.0");
  176. Constant rcd = new Constant(p, "float", "1.0");
  177. ConstantExpression rcea = new ConstantExpression(p, rca);
  178. ConstantExpression rceb = new ConstantExpression(p, rcb);
  179. ConstantExpression rcec = new ConstantExpression(p, rcc);
  180. ConstantExpression rced = new ConstantExpression(p, rcd);
  181. return new RotationConstant(p, rcea, rceb, rcec, rced);
  182. default:
  183. return null; // this will probably break stuff
  184. }
  185. }
  186. }
  187. }