1
0

LSL2CSCodeTransformer.cs 8.9 KB

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