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