LSL2CSCodeTransformer.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. 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. if (!(s is Assignment || s is ArgumentDeclarationList) && s.kids[i] is Declaration)
  88. AddImplicitInitialization(s, i);
  89. TransformNode((SYMBOL) s.kids[i]);
  90. }
  91. }
  92. /// <summary>
  93. /// Replaces an instance of the node at s.kids[didx] with an assignment
  94. /// node. The assignment node has the Declaration node on the left hand
  95. /// side and a default initializer on the right hand side.
  96. /// </summary>
  97. /// <param name="s">
  98. /// The node containing the Declaration node that needs replacing.
  99. /// </param>
  100. /// <param name="didx">Index of the Declaration node to replace.</param>
  101. private void AddImplicitInitialization(SYMBOL s, int didx)
  102. {
  103. // We take the kids for a while to play with them.
  104. int sKidSize = s.kids.Count;
  105. object [] sKids = new object[sKidSize];
  106. for (int i = 0; i < sKidSize; i++)
  107. sKids[i] = s.kids.Pop();
  108. // The child to be changed.
  109. Declaration currentDeclaration = (Declaration) sKids[didx];
  110. // We need an assignment node.
  111. Assignment newAssignment = new Assignment(currentDeclaration.yyps,
  112. currentDeclaration,
  113. GetZeroConstant(currentDeclaration.yyps, currentDeclaration.Datatype),
  114. "=");
  115. sKids[didx] = newAssignment;
  116. // Put the kids back where they belong.
  117. for (int i = 0; i < sKidSize; i++)
  118. s.kids.Add(sKids[i]);
  119. }
  120. /// <summary>
  121. /// Generates the node structure required to generate a default
  122. /// initialization.
  123. /// </summary>
  124. /// <param name="p">
  125. /// Tools.Parser instance to use when instantiating nodes.
  126. /// </param>
  127. /// <param name="constantType">String describing the datatype.</param>
  128. /// <returns>
  129. /// A SYMBOL node conaining the appropriate structure for intializing a
  130. /// constantType.
  131. /// </returns>
  132. private SYMBOL GetZeroConstant(Parser p, string constantType)
  133. {
  134. switch (constantType)
  135. {
  136. case "integer":
  137. return new Constant(p, constantType, "0");
  138. case "float":
  139. return new Constant(p, constantType, "0.0");
  140. case "string":
  141. case "key":
  142. return new Constant(p, constantType, "");
  143. case "list":
  144. ArgumentList al = new ArgumentList(p);
  145. return new ListConstant(p, al);
  146. case "vector":
  147. Constant vca = new Constant(p, "float", "0.0");
  148. Constant vcb = new Constant(p, "float", "0.0");
  149. Constant vcc = new Constant(p, "float", "0.0");
  150. ConstantExpression vcea = new ConstantExpression(p, vca);
  151. ConstantExpression vceb = new ConstantExpression(p, vcb);
  152. ConstantExpression vcec = new ConstantExpression(p, vcc);
  153. return new VectorConstant(p, vcea, vceb, vcec);
  154. case "rotation":
  155. Constant rca = new Constant(p, "float", "0.0");
  156. Constant rcb = new Constant(p, "float", "0.0");
  157. Constant rcc = new Constant(p, "float", "0.0");
  158. Constant rcd = new Constant(p, "float", "0.0");
  159. ConstantExpression rcea = new ConstantExpression(p, rca);
  160. ConstantExpression rceb = new ConstantExpression(p, rcb);
  161. ConstantExpression rcec = new ConstantExpression(p, rcc);
  162. ConstantExpression rced = new ConstantExpression(p, rcd);
  163. return new RotationConstant(p, rcea, rceb, rcec, rced);
  164. default:
  165. return null; // this will probably break stuff
  166. }
  167. }
  168. }
  169. }