LSL2CSCodeTransformer.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 = new Dictionary<string, string>();
  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. try
  45. {
  46. m_datatypeLSL2OpenSim.Add("integer", "LSL_Types.LSLInteger");
  47. m_datatypeLSL2OpenSim.Add("float", "LSL_Types.LSLFloat");
  48. //m_datatypeLSL2OpenSim.Add("key", "LSL_Types.key"); // key doesn't seem to be used
  49. m_datatypeLSL2OpenSim.Add("key", "LSL_Types.LSLString");
  50. m_datatypeLSL2OpenSim.Add("string", "LSL_Types.LSLString");
  51. m_datatypeLSL2OpenSim.Add("vector", "LSL_Types.Vector3");
  52. m_datatypeLSL2OpenSim.Add("rotation", "LSL_Types.Quaternion");
  53. m_datatypeLSL2OpenSim.Add("list", "LSL_Types.list");
  54. }
  55. catch
  56. {
  57. // temporary workaround since we are adding to a static datatype
  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. // make sure to put type lower in the inheritance hierarchy first
  78. // ie: since IdentConstant and StringConstant inherit from Constant,
  79. // put IdentConstant and StringConstant before Constant
  80. if (s is Declaration)
  81. ((Declaration) s).Datatype = m_datatypeLSL2OpenSim[((Declaration) s).Datatype];
  82. else if (s is Constant)
  83. ((Constant) s).Type = m_datatypeLSL2OpenSim[((Constant) s).Type];
  84. else if (s is TypecastExpression)
  85. ((TypecastExpression) s).TypecastType = m_datatypeLSL2OpenSim[((TypecastExpression) s).TypecastType];
  86. else if (s is GlobalFunctionDefinition && "void" != ((GlobalFunctionDefinition) s).ReturnType) // we don't need to translate "void"
  87. ((GlobalFunctionDefinition) s).ReturnType = m_datatypeLSL2OpenSim[((GlobalFunctionDefinition) s).ReturnType];
  88. for (int i = 0; i < s.kids.Count; i++)
  89. {
  90. if (!(s is Assignment || s is ArgumentDeclarationList) && s.kids[i] is Declaration)
  91. AddImplicitInitialization(s, i);
  92. TransformNode((SYMBOL) s.kids[i]);
  93. }
  94. }
  95. /// <summary>
  96. /// Replaces an instance of the node at s.kids[didx] with an assignment
  97. /// node. The assignment node has the Declaration node on the left hand
  98. /// side and a default initializer on the right hand side.
  99. /// </summary>
  100. /// <param name="s">
  101. /// The node containing the Declaration node that needs replacing.
  102. /// </param>
  103. /// <param name="didx">Index of the Declaration node to replace.</param>
  104. private void AddImplicitInitialization(SYMBOL s, int didx)
  105. {
  106. // We take the kids for a while to play with them.
  107. int sKidSize = s.kids.Count;
  108. object [] sKids = new object[sKidSize];
  109. for (int i = 0; i < sKidSize; i++)
  110. sKids[i] = s.kids.Pop();
  111. // The child to be changed.
  112. Declaration currentDeclaration = (Declaration) sKids[didx];
  113. // We need an assignment node.
  114. Assignment newAssignment = new Assignment(currentDeclaration.yyps,
  115. currentDeclaration,
  116. GetZeroConstant(currentDeclaration.yyps, currentDeclaration.Datatype),
  117. "=");
  118. sKids[didx] = newAssignment;
  119. // Put the kids back where they belong.
  120. for (int i = 0; i < sKidSize; i++)
  121. s.kids.Add(sKids[i]);
  122. }
  123. /// <summary>
  124. /// Generates the node structure required to generate a default
  125. /// initialization.
  126. /// </summary>
  127. /// <param name="p">
  128. /// Tools.Parser instance to use when instantiating nodes.
  129. /// </param>
  130. /// <param name="constantType">String describing the datatype.</param>
  131. /// <returns>
  132. /// A SYMBOL node conaining the appropriate structure for intializing a
  133. /// constantType.
  134. /// </returns>
  135. private SYMBOL GetZeroConstant(Parser p, string constantType)
  136. {
  137. switch (constantType)
  138. {
  139. case "integer":
  140. return new Constant(p, constantType, "0");
  141. case "float":
  142. return new Constant(p, constantType, "0.0");
  143. case "string":
  144. case "key":
  145. return new Constant(p, constantType, "");
  146. case "list":
  147. ArgumentList al = new ArgumentList(p);
  148. return new ListConstant(p, al);
  149. case "vector":
  150. Constant vca = new Constant(p, "float", "0.0");
  151. Constant vcb = new Constant(p, "float", "0.0");
  152. Constant vcc = new Constant(p, "float", "0.0");
  153. ConstantExpression vcea = new ConstantExpression(p, vca);
  154. ConstantExpression vceb = new ConstantExpression(p, vcb);
  155. ConstantExpression vcec = new ConstantExpression(p, vcc);
  156. return new VectorConstant(p, vcea, vceb, vcec);
  157. case "rotation":
  158. Constant rca = new Constant(p, "float", "0.0");
  159. Constant rcb = new Constant(p, "float", "0.0");
  160. Constant rcc = new Constant(p, "float", "0.0");
  161. Constant rcd = new Constant(p, "float", "0.0");
  162. ConstantExpression rcea = new ConstantExpression(p, rca);
  163. ConstantExpression rceb = new ConstantExpression(p, rcb);
  164. ConstantExpression rcec = new ConstantExpression(p, rcc);
  165. ConstantExpression rced = new ConstantExpression(p, rcd);
  166. return new RotationConstant(p, rcea, rceb, rcec, rced);
  167. default:
  168. return null; // this will probably break stuff
  169. }
  170. }
  171. }
  172. }