OpenSim
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros
LSL2CSCodeTransformer.cs
Go to the documentation of this file.
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 
28 using System;
29 using System.Collections.Generic;
30 using System.Reflection;
31 using log4net;
32 using Tools;
33 
34 namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
35 {
36  public class LSL2CSCodeTransformer
37  {
38 // private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
39 
40  private SYMBOL m_astRoot = null;
41  private static Dictionary<string, string> m_datatypeLSL2OpenSim = null;
42 
47  public LSL2CSCodeTransformer(SYMBOL astRoot)
48  {
49  m_astRoot = astRoot;
50 
51  // let's populate the dictionary
52  if (null == m_datatypeLSL2OpenSim)
53  {
54  m_datatypeLSL2OpenSim = new Dictionary<string, string>();
55  m_datatypeLSL2OpenSim.Add("integer", "LSL_Types.LSLInteger");
56  m_datatypeLSL2OpenSim.Add("float", "LSL_Types.LSLFloat");
57  //m_datatypeLSL2OpenSim.Add("key", "LSL_Types.key"); // key doesn't seem to be used
58  m_datatypeLSL2OpenSim.Add("key", "LSL_Types.LSLString");
59  m_datatypeLSL2OpenSim.Add("string", "LSL_Types.LSLString");
60  m_datatypeLSL2OpenSim.Add("vector", "LSL_Types.Vector3");
61  m_datatypeLSL2OpenSim.Add("rotation", "LSL_Types.Quaternion");
62  m_datatypeLSL2OpenSim.Add("list", "LSL_Types.list");
63  }
64  }
65 
70  public SYMBOL Transform()
71  {
72  foreach (SYMBOL s in m_astRoot.kids)
73  TransformNode(s);
74 
75  return m_astRoot;
76  }
77 
83  private void TransformNode(SYMBOL s)
84  {
85 // m_log.DebugFormat("[LSL2CSCODETRANSFORMER]: Tranforming node {0}", s);
86 
87  // make sure to put type lower in the inheritance hierarchy first
88  // ie: since IdentConstant and StringConstant inherit from Constant,
89  // put IdentConstant and StringConstant before Constant
90  if (s is Declaration)
91  ((Declaration) s).Datatype = m_datatypeLSL2OpenSim[((Declaration) s).Datatype];
92  else if (s is Constant)
93  ((Constant) s).Type = m_datatypeLSL2OpenSim[((Constant) s).Type];
94  else if (s is TypecastExpression)
95  ((TypecastExpression) s).TypecastType = m_datatypeLSL2OpenSim[((TypecastExpression) s).TypecastType];
96  else if (s is GlobalFunctionDefinition && "void" != ((GlobalFunctionDefinition) s).ReturnType) // we don't need to translate "void"
97  ((GlobalFunctionDefinition) s).ReturnType = m_datatypeLSL2OpenSim[((GlobalFunctionDefinition) s).ReturnType];
98 
99  for (int i = 0; i < s.kids.Count; i++)
100  {
101  // It's possible that a child is null, for instance when the
102  // assignment part in a for-loop is left out, ie:
103  //
104  // for (; i < 10; i++)
105  // {
106  // ...
107  // }
108  //
109  // We need to check for that here.
110  if (null != s.kids[i])
111  {
112 // m_log.Debug("[LSL2CSCODETRANSFORMER]: Moving down level");
113 
114  if (!(s is Assignment || s is ArgumentDeclarationList) && s.kids[i] is Declaration)
115  AddImplicitInitialization(s, i);
116 
117  TransformNode((SYMBOL) s.kids[i]);
118 
119 // m_log.Debug("[LSL2CSCODETRANSFORMER]: Moving up level");
120  }
121  }
122  }
123 
133  private void AddImplicitInitialization(SYMBOL s, int didx)
134  {
135  // We take the kids for a while to play with them.
136  int sKidSize = s.kids.Count;
137  object [] sKids = new object[sKidSize];
138  for (int i = 0; i < sKidSize; i++)
139  sKids[i] = s.kids.Pop();
140 
141  // The child to be changed.
142  Declaration currentDeclaration = (Declaration) sKids[didx];
143 
144  // We need an assignment node.
145  Assignment newAssignment = new Assignment(currentDeclaration.yyps,
146  currentDeclaration,
147  GetZeroConstant(currentDeclaration.yyps, currentDeclaration.Datatype),
148  "=");
149  sKids[didx] = newAssignment;
150 
151  // Put the kids back where they belong.
152  for (int i = 0; i < sKidSize; i++)
153  s.kids.Add(sKids[i]);
154  }
155 
168  private SYMBOL GetZeroConstant(Parser p, string constantType)
169  {
170  switch (constantType)
171  {
172  case "integer":
173  return new Constant(p, constantType, "0");
174  case "float":
175  return new Constant(p, constantType, "0.0");
176  case "string":
177  case "key":
178  return new Constant(p, constantType, "");
179  case "list":
180  ArgumentList al = new ArgumentList(p);
181  return new ListConstant(p, al);
182  case "vector":
183  Constant vca = new Constant(p, "float", "0.0");
184  Constant vcb = new Constant(p, "float", "0.0");
185  Constant vcc = new Constant(p, "float", "0.0");
186  ConstantExpression vcea = new ConstantExpression(p, vca);
187  ConstantExpression vceb = new ConstantExpression(p, vcb);
188  ConstantExpression vcec = new ConstantExpression(p, vcc);
189  return new VectorConstant(p, vcea, vceb, vcec);
190  case "rotation":
191  Constant rca = new Constant(p, "float", "0.0");
192  Constant rcb = new Constant(p, "float", "0.0");
193  Constant rcc = new Constant(p, "float", "0.0");
194  Constant rcd = new Constant(p, "float", "0.0");
195  ConstantExpression rcea = new ConstantExpression(p, rca);
196  ConstantExpression rceb = new ConstantExpression(p, rcb);
197  ConstantExpression rcec = new ConstantExpression(p, rcc);
198  ConstantExpression rced = new ConstantExpression(p, rcd);
199  return new RotationConstant(p, rcea, rceb, rcec, rced);
200  default:
201  return null; // this will probably break stuff
202  }
203  }
204  }
205 }
LSL2CSCodeTransformer(SYMBOL astRoot)
Pass the new CodeTranformer an abstract syntax tree.
SYMBOL Transform()
Transform the code in the AST we have.