aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Grid/ScriptEngine/DotNetEngine/Compiler/LSL/LSL2CSConverter.cs
diff options
context:
space:
mode:
authorTedd Hansen2007-10-05 19:56:44 +0000
committerTedd Hansen2007-10-05 19:56:44 +0000
commit6dd923b01d6864ffcb17030c9de17224f45b4c2a (patch)
tree83d00d90a13f6803b38988049096296caf9f4c17 /OpenSim/Grid/ScriptEngine/DotNetEngine/Compiler/LSL/LSL2CSConverter.cs
parentCode from Illumious Beltran (IBM) implementing more LSL (diff)
downloadopensim-SC_OLD-6dd923b01d6864ffcb17030c9de17224f45b4c2a.zip
opensim-SC_OLD-6dd923b01d6864ffcb17030c9de17224f45b4c2a.tar.gz
opensim-SC_OLD-6dd923b01d6864ffcb17030c9de17224f45b4c2a.tar.bz2
opensim-SC_OLD-6dd923b01d6864ffcb17030c9de17224f45b4c2a.tar.xz
Some more work on new ScriptEngine.
Diffstat (limited to 'OpenSim/Grid/ScriptEngine/DotNetEngine/Compiler/LSL/LSL2CSConverter.cs')
-rw-r--r--OpenSim/Grid/ScriptEngine/DotNetEngine/Compiler/LSL/LSL2CSConverter.cs266
1 files changed, 266 insertions, 0 deletions
diff --git a/OpenSim/Grid/ScriptEngine/DotNetEngine/Compiler/LSL/LSL2CSConverter.cs b/OpenSim/Grid/ScriptEngine/DotNetEngine/Compiler/LSL/LSL2CSConverter.cs
new file mode 100644
index 0000000..61a87d4
--- /dev/null
+++ b/OpenSim/Grid/ScriptEngine/DotNetEngine/Compiler/LSL/LSL2CSConverter.cs
@@ -0,0 +1,266 @@
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Text.RegularExpressions;
5
6namespace OpenSim.Grid.ScriptEngine.DotNetEngine.Compiler.LSL
7{
8 public class LSL2CSConverter
9 {
10 //private Regex rnw = new Regex(@"[a-zA-Z0-9_\-]", RegexOptions.Compiled);
11 private Dictionary<string, string> dataTypes = new Dictionary<string, string>();
12 private Dictionary<string, string> quotes = new Dictionary<string, string>();
13
14 public LSL2CSConverter()
15 {
16 // Only the types we need to convert
17 dataTypes.Add("void", "void");
18 dataTypes.Add("integer", "int");
19 dataTypes.Add("float", "double");
20 dataTypes.Add("string", "string");
21 dataTypes.Add("key", "string");
22 dataTypes.Add("vector", "LSL_Types.Vector3");
23 dataTypes.Add("rotation", "LSL_Types.Quaternion");
24 dataTypes.Add("list", "list");
25 dataTypes.Add("null", "null");
26
27 }
28
29 public string Convert(string Script)
30 {
31 string Return = "";
32 Script = " \r\n" + Script;
33
34 //
35 // Prepare script for processing
36 //
37
38 // Clean up linebreaks
39 Script = Regex.Replace(Script, @"\r\n", "\n");
40 Script = Regex.Replace(Script, @"\n", "\r\n");
41
42
43 // QUOTE REPLACEMENT
44 // temporarily replace quotes so we can work our magic on the script without
45 // always considering if we are inside our outside ""'s
46 string _Script = "";
47 string C;
48 bool in_quote = false;
49 bool quote_replaced = false;
50 string quote_replacement_string = "Q_U_O_T_E_REPLACEMENT_";
51 string quote = "";
52 bool last_was_escape = false;
53 int quote_replaced_count = 0;
54 for (int p = 0; p < Script.Length; p++)
55 {
56
57 C = Script.Substring(p, 1);
58 while (true)
59 {
60 // found " and last was not \ so this is not an escaped \"
61 if (C == "\"" && last_was_escape == false)
62 {
63 // Toggle inside/outside quote
64 in_quote = !in_quote;
65 if (in_quote)
66 {
67 quote_replaced_count++;
68 }
69 else
70 {
71 if (quote == "")
72 {
73 // We didn't replace quote, probably because of empty string?
74 _Script += quote_replacement_string + quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]);
75 }
76 // We just left a quote
77 quotes.Add(quote_replacement_string + quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]), quote);
78 quote = "";
79 }
80 break;
81 }
82
83 if (!in_quote)
84 {
85 // We are not inside a quote
86 quote_replaced = false;
87
88 }
89 else
90 {
91 // We are inside a quote
92 if (!quote_replaced)
93 {
94 // Replace quote
95 _Script += quote_replacement_string + quote_replaced_count.ToString().PadLeft(5, "0".ToCharArray()[0]);
96 quote_replaced = true;
97 }
98 quote += C;
99 break;
100 }
101 _Script += C;
102 break;
103 }
104 last_was_escape = false;
105 if (C == @"\")
106 {
107 last_was_escape = true;
108 }
109 }
110 Script = _Script;
111 //
112 // END OF QUOTE REPLACEMENT
113 //
114
115
116
117 //
118 // PROCESS STATES
119 // Remove state definitions and add state names to start of each event within state
120 //
121 int ilevel = 0;
122 int lastlevel = 0;
123 string ret = "";
124 string cache = "";
125 bool in_state = false;
126 string current_statename = "";
127 for (int p = 0; p < Script.Length; p++)
128 {
129 C = Script.Substring(p, 1);
130 while (true)
131 {
132 // inc / dec level
133 if (C == @"{")
134 ilevel++;
135 if (C == @"}")
136 ilevel--;
137 if (ilevel < 0)
138 ilevel = 0;
139 cache += C;
140
141 // if level == 0, add to return
142 if (ilevel == 1 && lastlevel == 0)
143 {
144 // 0 => 1: Get last
145 Match m = Regex.Match(cache, @"(?![a-zA-Z_]+)\s*([a-zA-Z_]+)[^a-zA-Z_\(\)]*{", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
146
147 in_state = false;
148 if (m.Success)
149 {
150 // Go back to level 0, this is not a state
151 in_state = true;
152 current_statename = m.Groups[1].Captures[0].Value;
153 //Console.WriteLine("Current statename: " + current_statename);
154 cache = Regex.Replace(cache, @"(?<s1>(?![a-zA-Z_]+)\s*)" + @"([a-zA-Z_]+)(?<s2>[^a-zA-Z_\(\)]*){", "${s1}${s2}", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
155 }
156 ret += cache;
157 cache = "";
158 }
159 if (ilevel == 0 && lastlevel == 1)
160 {
161 // 1 => 0: Remove last }
162 if (in_state == true)
163 {
164 cache = cache.Remove(cache.Length - 1, 1);
165 //cache = Regex.Replace(cache, "}$", "", RegexOptions.Multiline | RegexOptions.Singleline);
166
167 //Replace function names
168 // void dataserver(key query_id, string data) {
169 //cache = Regex.Replace(cache, @"([^a-zA-Z_]\s*)((?!if|switch|for)[a-zA-Z_]+\s*\([^\)]*\)[^{]*{)", "$1" + "<STATE>" + "$2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
170 //Console.WriteLine("Replacing using statename: " + current_statename);
171 cache = Regex.Replace(cache, @"^(\s*)((?!(if|switch|for)[^a-zA-Z0-9_])[a-zA-Z0-9_]*\s*\([^\)]*\)[^;]*\{)", @"$1public " + current_statename + "_event_$2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
172 }
173
174 ret += cache;
175 cache = "";
176 in_state = true;
177 current_statename = "";
178 }
179
180 break;
181 }
182 lastlevel = ilevel;
183 }
184 ret += cache;
185 cache = "";
186
187 Script = ret;
188 ret = "";
189
190
191
192 foreach (string key in dataTypes.Keys)
193 {
194 string val;
195 dataTypes.TryGetValue(key, out val);
196
197 // Replace CAST - (integer) with (int)
198 Script = Regex.Replace(Script, @"\(" + key + @"\)", @"(" + val + ")", RegexOptions.Compiled | RegexOptions.Multiline);
199 // Replace return types and function variables - integer a() and f(integer a, integer a)
200 Script = Regex.Replace(Script, @"(^|;|}|[\(,])(\s*)" + key + @"(\s*)", @"$1$2" + val + "$3", RegexOptions.Compiled | RegexOptions.Multiline);
201 }
202
203 // Add "void" in front of functions that needs it
204 Script = Regex.Replace(Script, @"^(\s*public\s+)((?!(if|switch|for)[^a-zA-Z0-9_])[a-zA-Z0-9_]*\s*\([^\)]*\)[^;]*\{)", @"$1void $2", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
205
206 // Replace <x,y,z> and <x,y,z,r>
207 Script = Regex.Replace(Script, @"<([^,>]*,[^,>]*,[^,>]*,[^,>]*)>", @"new LSL_Types.Quaternion($1)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
208 Script = Regex.Replace(Script, @"<([^,>]*,[^,>]*,[^,>]*)>", @"new LSL_Types.Vector3($1)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
209
210 // Replace List []'s
211 Script = Regex.Replace(Script, @"\[([^\]]*)\]", @"List.Parse($1)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
212
213
214 // Replace (string) to .ToString() //
215 Script = Regex.Replace(Script, @"\(string\)\s*([a-zA-Z0-9_]+(\s*\([^\)]*\))?)", @"$1.ToString()", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
216 Script = Regex.Replace(Script, @"\((float|int)\)\s*([a-zA-Z0-9_]+(\s*\([^\)]*\))?)", @"$1.Parse($2)", RegexOptions.Compiled | RegexOptions.Multiline | RegexOptions.Singleline);
217
218
219 // REPLACE BACK QUOTES
220 foreach (string key in quotes.Keys)
221 {
222 string val;
223 quotes.TryGetValue(key, out val);
224 Script = Script.Replace(key, "\"" + val + "\"");
225 }
226
227
228 // Add namespace, class name and inheritance
229
230 Return = "" +
231 "using OpenSim.Region.ScriptEngine.Common;";
232 //"using System; " +
233 //"using System.Collections.Generic; " +
234 //"using System.Text; " +
235 //"using OpenSim.Region.ScriptEngine.Common; " +
236 //"using integer = System.Int32; " +
237 //"using key = System.String; ";
238
239 //// Make a Using out of DataTypes
240 //// Using integer = System.Int32;
241 //string _val;
242 //foreach (string key in DataTypes.Keys)
243 //{
244 // DataTypes.TryGetValue(key, out _val);
245 // if (key != _val)
246 // {
247 // Return += "using " + key + " = " + _val + "; ";
248 // }
249 //}
250
251
252 Return += "" +
253 "namespace SecondLife { ";
254 Return += "" +
255 //"[Serializable] " +
256 "public class Script : OpenSim.Grid.ScriptEngine.DotNetEngine.Compiler.LSL.LSL_BaseClass { ";
257 Return += @"public Script() { } ";
258 Return += Script;
259 Return += "} }\r\n";
260
261 return Return;
262 }
263
264
265 }
266}