aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/Shared/CodeTools/LSL2CSCodeTransformer.cs
diff options
context:
space:
mode:
authorJohan Berntsson2008-07-08 03:02:11 +0000
committerJohan Berntsson2008-07-08 03:02:11 +0000
commita73e3b4e3fdb6b563dddaa7f67d5fe69e65ca752 (patch)
tree920460cb6108ee9ecb2504f39a0cbf2e86ea2477 /OpenSim/Region/ScriptEngine/Shared/CodeTools/LSL2CSCodeTransformer.cs
parentllscript compiler patch from Mike: adds LSL jumps and implicit variable initi... (diff)
downloadopensim-SC_OLD-a73e3b4e3fdb6b563dddaa7f67d5fe69e65ca752.zip
opensim-SC_OLD-a73e3b4e3fdb6b563dddaa7f67d5fe69e65ca752.tar.gz
opensim-SC_OLD-a73e3b4e3fdb6b563dddaa7f67d5fe69e65ca752.tar.bz2
opensim-SC_OLD-a73e3b4e3fdb6b563dddaa7f67d5fe69e65ca752.tar.xz
another patch from Mike: the llscript compiler is now available in XEngine as well. Thanks Mike
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Shared/CodeTools/LSL2CSCodeTransformer.cs')
-rw-r--r--OpenSim/Region/ScriptEngine/Shared/CodeTools/LSL2CSCodeTransformer.cs186
1 files changed, 186 insertions, 0 deletions
diff --git a/OpenSim/Region/ScriptEngine/Shared/CodeTools/LSL2CSCodeTransformer.cs b/OpenSim/Region/ScriptEngine/Shared/CodeTools/LSL2CSCodeTransformer.cs
new file mode 100644
index 0000000..a7ca18c
--- /dev/null
+++ b/OpenSim/Region/ScriptEngine/Shared/CodeTools/LSL2CSCodeTransformer.cs
@@ -0,0 +1,186 @@
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
28using System;
29using System.Collections.Generic;
30using Tools;
31
32namespace 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 = new Dictionary<string, string>();
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 try
49 {
50 m_datatypeLSL2OpenSim.Add("integer", "LSL_Types.LSLInteger");
51 m_datatypeLSL2OpenSim.Add("float", "LSL_Types.LSLFloat");
52 //m_datatypeLSL2OpenSim.Add("key", "LSL_Types.key"); // key doesn't seem to be used
53 m_datatypeLSL2OpenSim.Add("key", "LSL_Types.LSLString");
54 m_datatypeLSL2OpenSim.Add("string", "LSL_Types.LSLString");
55 m_datatypeLSL2OpenSim.Add("vector", "LSL_Types.Vector3");
56 m_datatypeLSL2OpenSim.Add("rotation", "LSL_Types.Quaternion");
57 m_datatypeLSL2OpenSim.Add("list", "LSL_Types.list");
58 }
59 catch
60 {
61 // temporary workaround since we are adding to a static datatype
62 }
63 }
64
65 /// <summary>
66 /// Transform the code in the AST we have.
67 /// </summary>
68 /// <returns>The root node of the transformed AST</returns>
69 public SYMBOL Transform()
70 {
71 foreach (SYMBOL s in m_astRoot.kids)
72 TransformNode(s);
73
74 return m_astRoot;
75 }
76
77 /// <summary>
78 /// Recursively called to transform each type of node. Will transform this
79 /// node, then all it's children.
80 /// </summary>
81 /// <param name="s">The current node to transform.</param>
82 private void TransformNode(SYMBOL s)
83 {
84 // make sure to put type lower in the inheritance hierarchy first
85 // ie: since IdentConstant and StringConstant inherit from Constant,
86 // put IdentConstant and StringConstant before Constant
87 if (s is Declaration)
88 ((Declaration) s).Datatype = m_datatypeLSL2OpenSim[((Declaration) s).Datatype];
89 else if (s is Constant)
90 ((Constant) s).Type = m_datatypeLSL2OpenSim[((Constant) s).Type];
91 else if (s is TypecastExpression)
92 ((TypecastExpression) s).TypecastType = m_datatypeLSL2OpenSim[((TypecastExpression) s).TypecastType];
93 else if (s is GlobalFunctionDefinition && "void" != ((GlobalFunctionDefinition) s).ReturnType) // we don't need to translate "void"
94 ((GlobalFunctionDefinition) s).ReturnType = m_datatypeLSL2OpenSim[((GlobalFunctionDefinition) s).ReturnType];
95
96 for (int i = 0; i < s.kids.Count; i++)
97 {
98 if (!(s is Assignment || s is ArgumentDeclarationList) && s.kids[i] is Declaration)
99 AddImplicitInitialization(s, i);
100
101 TransformNode((SYMBOL) s.kids[i]);
102 }
103 }
104
105 /// <summary>
106 /// Replaces an instance of the node at s.kids[didx] with an assignment
107 /// node. The assignment node has the Declaration node on the left hand
108 /// side and a default initializer on the right hand side.
109 /// </summary>
110 /// <param name="s">
111 /// The node containing the Declaration node that needs replacing.
112 /// </param>
113 /// <param name="didx">Index of the Declaration node to replace.</param>
114 private void AddImplicitInitialization(SYMBOL s, int didx)
115 {
116 // We take the kids for a while to play with them.
117 int sKidSize = s.kids.Count;
118 object [] sKids = new object[sKidSize];
119 for (int i = 0; i < sKidSize; i++)
120 sKids[i] = s.kids.Pop();
121
122 // The child to be changed.
123 Declaration currentDeclaration = (Declaration) sKids[didx];
124
125 // We need an assignment node.
126 Assignment newAssignment = new Assignment(currentDeclaration.yyps,
127 currentDeclaration,
128 GetZeroConstant(currentDeclaration.yyps, currentDeclaration.Datatype),
129 "=");
130 sKids[didx] = newAssignment;
131
132 // Put the kids back where they belong.
133 for (int i = 0; i < sKidSize; i++)
134 s.kids.Add(sKids[i]);
135 }
136
137 /// <summary>
138 /// Generates the node structure required to generate a default
139 /// initialization.
140 /// </summary>
141 /// <param name="p">
142 /// Tools.Parser instance to use when instantiating nodes.
143 /// </param>
144 /// <param name="constantType">String describing the datatype.</param>
145 /// <returns>
146 /// A SYMBOL node conaining the appropriate structure for intializing a
147 /// constantType.
148 /// </returns>
149 private SYMBOL GetZeroConstant(Parser p, string constantType)
150 {
151 switch (constantType)
152 {
153 case "integer":
154 return new Constant(p, constantType, "0");
155 case "float":
156 return new Constant(p, constantType, "0.0");
157 case "string":
158 case "key":
159 return new Constant(p, constantType, "");
160 case "list":
161 ArgumentList al = new ArgumentList(p);
162 return new ListConstant(p, al);
163 case "vector":
164 Constant vca = new Constant(p, "float", "0.0");
165 Constant vcb = new Constant(p, "float", "0.0");
166 Constant vcc = new Constant(p, "float", "0.0");
167 ConstantExpression vcea = new ConstantExpression(p, vca);
168 ConstantExpression vceb = new ConstantExpression(p, vcb);
169 ConstantExpression vcec = new ConstantExpression(p, vcc);
170 return new VectorConstant(p, vcea, vceb, vcec);
171 case "rotation":
172 Constant rca = new Constant(p, "float", "0.0");
173 Constant rcb = new Constant(p, "float", "0.0");
174 Constant rcc = new Constant(p, "float", "0.0");
175 Constant rcd = new Constant(p, "float", "0.0");
176 ConstantExpression rcea = new ConstantExpression(p, rca);
177 ConstantExpression rceb = new ConstantExpression(p, rcb);
178 ConstantExpression rcec = new ConstantExpression(p, rcc);
179 ConstantExpression rced = new ConstantExpression(p, rcd);
180 return new RotationConstant(p, rcea, rceb, rcec, rced);
181 default:
182 return null; // this will probably break stuff
183 }
184 }
185 }
186}