diff options
Diffstat (limited to 'OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/LSL/LSL2CSCodeTransformer.cs')
-rw-r--r-- | OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/LSL/LSL2CSCodeTransformer.cs | 183 |
1 files changed, 0 insertions, 183 deletions
diff --git a/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/LSL/LSL2CSCodeTransformer.cs b/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/LSL/LSL2CSCodeTransformer.cs deleted file mode 100644 index 8abf3b9..0000000 --- a/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/LSL/LSL2CSCodeTransformer.cs +++ /dev/null | |||
@@ -1,183 +0,0 @@ | |||
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 | |||
28 | using System; | ||
29 | using System.Collections.Generic; | ||
30 | using Tools; | ||
31 | |||
32 | namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL | ||
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 | if (!(s is Assignment || s is ArgumentDeclarationList) && s.kids[i] is Declaration) | ||
96 | AddImplicitInitialization(s, i); | ||
97 | |||
98 | TransformNode((SYMBOL) s.kids[i]); | ||
99 | } | ||
100 | } | ||
101 | |||
102 | /// <summary> | ||
103 | /// Replaces an instance of the node at s.kids[didx] with an assignment | ||
104 | /// node. The assignment node has the Declaration node on the left hand | ||
105 | /// side and a default initializer on the right hand side. | ||
106 | /// </summary> | ||
107 | /// <param name="s"> | ||
108 | /// The node containing the Declaration node that needs replacing. | ||
109 | /// </param> | ||
110 | /// <param name="didx">Index of the Declaration node to replace.</param> | ||
111 | private void AddImplicitInitialization(SYMBOL s, int didx) | ||
112 | { | ||
113 | // We take the kids for a while to play with them. | ||
114 | int sKidSize = s.kids.Count; | ||
115 | object [] sKids = new object[sKidSize]; | ||
116 | for (int i = 0; i < sKidSize; i++) | ||
117 | sKids[i] = s.kids.Pop(); | ||
118 | |||
119 | // The child to be changed. | ||
120 | Declaration currentDeclaration = (Declaration) sKids[didx]; | ||
121 | |||
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 | |||
129 | // Put the kids back where they belong. | ||
130 | for (int i = 0; i < sKidSize; i++) | ||
131 | s.kids.Add(sKids[i]); | ||
132 | } | ||
133 | |||
134 | /// <summary> | ||
135 | /// Generates the node structure required to generate a default | ||
136 | /// initialization. | ||
137 | /// </summary> | ||
138 | /// <param name="p"> | ||
139 | /// Tools.Parser instance to use when instantiating nodes. | ||
140 | /// </param> | ||
141 | /// <param name="constantType">String describing the datatype.</param> | ||
142 | /// <returns> | ||
143 | /// A SYMBOL node conaining the appropriate structure for intializing a | ||
144 | /// constantType. | ||
145 | /// </returns> | ||
146 | private SYMBOL GetZeroConstant(Parser p, string constantType) | ||
147 | { | ||
148 | switch (constantType) | ||
149 | { | ||
150 | case "integer": | ||
151 | return new Constant(p, constantType, "0"); | ||
152 | case "float": | ||
153 | return new Constant(p, constantType, "0.0"); | ||
154 | case "string": | ||
155 | case "key": | ||
156 | return new Constant(p, constantType, ""); | ||
157 | case "list": | ||
158 | ArgumentList al = new ArgumentList(p); | ||
159 | return new ListConstant(p, al); | ||
160 | case "vector": | ||
161 | Constant vca = new Constant(p, "float", "0.0"); | ||
162 | Constant vcb = new Constant(p, "float", "0.0"); | ||
163 | Constant vcc = new Constant(p, "float", "0.0"); | ||
164 | ConstantExpression vcea = new ConstantExpression(p, vca); | ||
165 | ConstantExpression vceb = new ConstantExpression(p, vcb); | ||
166 | ConstantExpression vcec = new ConstantExpression(p, vcc); | ||
167 | return new VectorConstant(p, vcea, vceb, vcec); | ||
168 | case "rotation": | ||
169 | Constant rca = new Constant(p, "float", "0.0"); | ||
170 | Constant rcb = new Constant(p, "float", "0.0"); | ||
171 | Constant rcc = new Constant(p, "float", "0.0"); | ||
172 | Constant rcd = new Constant(p, "float", "0.0"); | ||
173 | ConstantExpression rcea = new ConstantExpression(p, rca); | ||
174 | ConstantExpression rceb = new ConstantExpression(p, rcb); | ||
175 | ConstantExpression rcec = new ConstantExpression(p, rcc); | ||
176 | ConstantExpression rced = new ConstantExpression(p, rcd); | ||
177 | return new RotationConstant(p, rcea, rceb, rcec, rced); | ||
178 | default: | ||
179 | return null; // this will probably break stuff | ||
180 | } | ||
181 | } | ||
182 | } | ||
183 | } | ||