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