aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs')
-rw-r--r--OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs584
1 files changed, 252 insertions, 332 deletions
diff --git a/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs b/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs
index 4e0c273..031f00a 100644
--- a/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs
+++ b/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs
@@ -26,6 +26,7 @@
26 */ 26 */
27 27
28using System; 28using System;
29using System.Text;
29using System.IO; 30using System.IO;
30using System.Collections.Generic; 31using System.Collections.Generic;
31using System.Reflection; 32using System.Reflection;
@@ -39,9 +40,9 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
39 { 40 {
40// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); 41// private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
41 42
43 private static yyLSLSyntax yyLSL = new yyLSLSyntax();
42 private SYMBOL m_astRoot = null; 44 private SYMBOL m_astRoot = null;
43 private Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> m_positionMap; 45 private Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> m_positionMap;
44 private int m_indentWidth = 4; // for indentation
45 private int m_braceCount; // for indentation 46 private int m_braceCount; // for indentation
46 private int m_CSharpLine; // the current line of generated C# code 47 private int m_CSharpLine; // the current line of generated C# code
47 private int m_CSharpCol; // the current column of generated C# code 48 private int m_CSharpCol; // the current column of generated C# code
@@ -94,6 +95,16 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
94 get { return m_astRoot; } 95 get { return m_astRoot; }
95 } 96 }
96 97
98 public void Clear()
99 {
100 m_astRoot.kids = null;
101 m_astRoot.yylx = null;
102 m_astRoot.yyps = null;
103 m_astRoot = null;
104 m_positionMap = null;
105 m_warnings.Clear();
106 m_comms = null;
107 }
97 /// <summary> 108 /// <summary>
98 /// Resets various counters and metadata. 109 /// Resets various counters and metadata.
99 /// </summary> 110 /// </summary>
@@ -106,18 +117,26 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
106 m_astRoot = null; 117 m_astRoot = null;
107 } 118 }
108 119
120 public string Convert(string script)
121 {
122 StringBuilder sb = new StringBuilder(4096);
123 Convert(script, sb);
124 return sb.ToString();
125 }
126
109 /// <summary> 127 /// <summary>
110 /// Generate the code from the AST we have. 128 /// Generate the code from the AST we have.
111 /// </summary> 129 /// </summary>
112 /// <param name="script">The LSL source as a string.</param> 130 /// <param name="script">The LSL source as a string.</param>
113 /// <returns>String containing the generated C# code.</returns> 131 /// <returns>String containing the generated C# code.</returns>
114 public string Convert(string script) 132 public void Convert(string script, StringBuilder sb)
115 { 133 {
116// m_log.DebugFormat("[CS CODE GENERATOR]: Converting to C#\n{0}", script); 134// m_log.DebugFormat("[CS CODE GENERATOR]: Converting to C#\n{0}", script);
117 135
118 m_warnings.Clear(); 136 m_warnings.Clear();
119 ResetCounters(); 137 ResetCounters();
120 Parser p = new LSLSyntax(new yyLSLSyntax(), new ErrorHandler(true)); 138 ErrorHandler errorHandler = new ErrorHandler(true);
139 Parser p = new LSLSyntax(yyLSL, errorHandler);
121 140
122 LSL2CSCodeTransformer codeTransformer; 141 LSL2CSCodeTransformer codeTransformer;
123 try 142 try
@@ -148,38 +167,39 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
148 167
149 m_astRoot = codeTransformer.Transform(); 168 m_astRoot = codeTransformer.Transform();
150 169
151 string retstr = String.Empty;
152 170
153 // standard preamble 171 // standard preamble
154 //retstr = GenerateLine("using OpenSim.Region.ScriptEngine.Common;"); 172
155 //retstr += GenerateLine("using System.Collections.Generic;"); 173
156 //retstr += GenerateLine(""); 174
157 //retstr += GenerateLine("namespace SecondLife");
158 //retstr += GenerateLine("{");
159 m_braceCount++; 175 m_braceCount++;
160 //retstr += GenerateIndentedLine("public class Script : OpenSim.Region.ScriptEngine.Common");
161 //retstr += GenerateIndentedLine("{");
162 m_braceCount++; 176 m_braceCount++;
163 177
164 // line number 178 // line number
165 m_CSharpLine += 9; 179 m_CSharpLine += 10;
166 180
167 // here's the payload 181 // here's the payload
168 retstr += GenerateLine(); 182 sb.Append("\n");
169 foreach (SYMBOL s in m_astRoot.kids) 183 foreach (SYMBOL s in m_astRoot.kids)
170 retstr += GenerateNode(m_astRoot, s); 184 GenerateNodeToSB(m_astRoot, s, sb);
185
186 codeTransformer = null;
187 p.m_lexer.m_buf=null;
188 p.m_lexer.yytext = null;
189 p.m_lexer = null;
190 p.m_symbols = null;
191 p = null;
192 errorHandler = null;
171 193
172 // close braces! 194 // close braces!
173 m_braceCount--; 195// m_braceCount--;
174 //retstr += GenerateIndentedLine("}"); 196 //retstr += GenerateIndentedLine("}");
175 m_braceCount--; 197// m_braceCount--;
176 //retstr += GenerateLine("}"); 198 //retstr += GenerateLine("}");
177 199
178 // Removes all carriage return characters which may be generated in Windows platform. Is there 200 // Removes all carriage return characters which may be generated in Windows platform. Is there
179 // cleaner way of doing this? 201 // cleaner way of doing this?
180 retstr = retstr.Replace("\r", ""); 202// sb.Replace("\r", "");
181
182 return retstr;
183 } 203 }
184 204
185 /// <summary> 205 /// <summary>
@@ -206,78 +226,76 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
206 /// <param name="previousSymbol">The parent node.</param> 226 /// <param name="previousSymbol">The parent node.</param>
207 /// <param name="s">The current node to generate code for.</param> 227 /// <param name="s">The current node to generate code for.</param>
208 /// <returns>String containing C# code for SYMBOL s.</returns> 228 /// <returns>String containing C# code for SYMBOL s.</returns>
209 private string GenerateNode(SYMBOL previousSymbol, SYMBOL s) 229 private void GenerateNodeToSB(SYMBOL previousSymbol, SYMBOL s, StringBuilder sb)
210 { 230 {
211 string retstr = String.Empty;
212
213 // make sure to put type lower in the inheritance hierarchy first 231 // make sure to put type lower in the inheritance hierarchy first
214 // ie: since IdentArgument and ExpressionArgument inherit from 232 // ie: since IdentArgument and ExpressionArgument inherit from
215 // Argument, put IdentArgument and ExpressionArgument before Argument 233 // Argument, put IdentArgument and ExpressionArgument before Argument
216 if (s is GlobalFunctionDefinition) 234 if (s is GlobalFunctionDefinition)
217 retstr += GenerateGlobalFunctionDefinition((GlobalFunctionDefinition) s); 235 GenerateGlobalFunctionDefinition((GlobalFunctionDefinition) s, sb);
218 else if (s is GlobalVariableDeclaration) 236 else if (s is GlobalVariableDeclaration)
219 retstr += GenerateGlobalVariableDeclaration((GlobalVariableDeclaration) s); 237 GenerateGlobalVariableDeclaration((GlobalVariableDeclaration) s , sb);
220 else if (s is State) 238 else if (s is State)
221 retstr += GenerateState((State) s); 239 GenerateState((State) s, sb);
222 else if (s is CompoundStatement) 240 else if (s is CompoundStatement)
223 retstr += GenerateCompoundStatement(previousSymbol, (CompoundStatement) s); 241 GenerateCompoundStatement(previousSymbol, (CompoundStatement) s, sb);
224 else if (s is Declaration) 242 else if (s is Declaration)
225 retstr += GenerateDeclaration((Declaration) s); 243 GenerateDeclaration((Declaration) s, sb);
226 else if (s is Statement) 244 else if (s is Statement)
227 retstr += GenerateStatement(previousSymbol, (Statement) s); 245 GenerateStatement(previousSymbol, (Statement) s, sb);
228 else if (s is ReturnStatement) 246 else if (s is ReturnStatement)
229 retstr += GenerateReturnStatement((ReturnStatement) s); 247 GenerateReturnStatement((ReturnStatement) s, sb);
230 else if (s is JumpLabel) 248 else if (s is JumpLabel)
231 retstr += GenerateJumpLabel((JumpLabel) s); 249 GenerateJumpLabel((JumpLabel) s, sb);
232 else if (s is JumpStatement) 250 else if (s is JumpStatement)
233 retstr += GenerateJumpStatement((JumpStatement) s); 251 GenerateJumpStatement((JumpStatement) s, sb);
234 else if (s is StateChange) 252 else if (s is StateChange)
235 retstr += GenerateStateChange((StateChange) s); 253 GenerateStateChange((StateChange) s, sb);
236 else if (s is IfStatement) 254 else if (s is IfStatement)
237 retstr += GenerateIfStatement((IfStatement) s); 255 GenerateIfStatement((IfStatement) s, sb);
238 else if (s is WhileStatement) 256 else if (s is WhileStatement)
239 retstr += GenerateWhileStatement((WhileStatement) s); 257 GenerateWhileStatement((WhileStatement) s, sb);
240 else if (s is DoWhileStatement) 258 else if (s is DoWhileStatement)
241 retstr += GenerateDoWhileStatement((DoWhileStatement) s); 259 GenerateDoWhileStatement((DoWhileStatement) s, sb);
242 else if (s is ForLoop) 260 else if (s is ForLoop)
243 retstr += GenerateForLoop((ForLoop) s); 261 GenerateForLoop((ForLoop) s, sb);
244 else if (s is ArgumentList) 262 else if (s is ArgumentList)
245 retstr += GenerateArgumentList((ArgumentList) s); 263 GenerateArgumentList((ArgumentList) s, sb);
246 else if (s is Assignment) 264 else if (s is Assignment)
247 retstr += GenerateAssignment((Assignment) s); 265 GenerateAssignment((Assignment) s, sb);
248 else if (s is BinaryExpression) 266 else if (s is BinaryExpression)
249 retstr += GenerateBinaryExpression((BinaryExpression) s); 267 GenerateBinaryExpression((BinaryExpression) s, sb);
250 else if (s is ParenthesisExpression) 268 else if (s is ParenthesisExpression)
251 retstr += GenerateParenthesisExpression((ParenthesisExpression) s); 269 GenerateParenthesisExpression((ParenthesisExpression) s, sb);
252 else if (s is UnaryExpression) 270 else if (s is UnaryExpression)
253 retstr += GenerateUnaryExpression((UnaryExpression) s); 271 GenerateUnaryExpression((UnaryExpression) s, sb);
254 else if (s is IncrementDecrementExpression) 272 else if (s is IncrementDecrementExpression)
255 retstr += GenerateIncrementDecrementExpression((IncrementDecrementExpression) s); 273 GenerateIncrementDecrementExpression((IncrementDecrementExpression) s, sb);
256 else if (s is TypecastExpression) 274 else if (s is TypecastExpression)
257 retstr += GenerateTypecastExpression((TypecastExpression) s); 275 GenerateTypecastExpression((TypecastExpression) s, sb);
258 else if (s is FunctionCall) 276 else if (s is FunctionCall)
259 retstr += GenerateFunctionCall((FunctionCall) s); 277 GenerateFunctionCall((FunctionCall) s, sb);
260 else if (s is VectorConstant) 278 else if (s is VectorConstant)
261 retstr += GenerateVectorConstant((VectorConstant) s); 279 GenerateVectorConstant((VectorConstant) s, sb);
262 else if (s is RotationConstant) 280 else if (s is RotationConstant)
263 retstr += GenerateRotationConstant((RotationConstant) s); 281 GenerateRotationConstant((RotationConstant) s, sb);
264 else if (s is ListConstant) 282 else if (s is ListConstant)
265 retstr += GenerateListConstant((ListConstant) s); 283 GenerateListConstant((ListConstant) s, sb);
266 else if (s is Constant) 284 else if (s is Constant)
267 retstr += GenerateConstant((Constant) s); 285 GenerateConstant((Constant) s, sb);
268 else if (s is IdentDotExpression) 286 else if (s is IdentDotExpression)
269 retstr += Generate(CheckName(((IdentDotExpression) s).Name) + "." + ((IdentDotExpression) s).Member, s); 287 Generate(CheckName(((IdentDotExpression) s).Name) + "." + ((IdentDotExpression) s).Member, s, sb);
270 else if (s is IdentExpression) 288 else if (s is IdentExpression)
271 retstr += GenerateIdentifier(((IdentExpression) s).Name, s); 289 GenerateIdentifier(((IdentExpression) s).Name, s, sb);
272 else if (s is IDENT) 290 else if (s is IDENT)
273 retstr += Generate(CheckName(((TOKEN) s).yytext), s); 291 Generate(CheckName(((TOKEN) s).yytext), s, sb);
274 else 292 else
275 { 293 {
276 foreach (SYMBOL kid in s.kids) 294 foreach (SYMBOL kid in s.kids)
277 retstr += GenerateNode(s, kid); 295 GenerateNodeToSB(s, kid,sb);
278 } 296 }
279 297
280 return retstr; 298 return;
281 } 299 }
282 300
283 /// <summary> 301 /// <summary>
@@ -285,10 +303,8 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
285 /// </summary> 303 /// </summary>
286 /// <param name="gf">The GlobalFunctionDefinition node.</param> 304 /// <param name="gf">The GlobalFunctionDefinition node.</param>
287 /// <returns>String containing C# code for GlobalFunctionDefinition gf.</returns> 305 /// <returns>String containing C# code for GlobalFunctionDefinition gf.</returns>
288 private string GenerateGlobalFunctionDefinition(GlobalFunctionDefinition gf) 306 private void GenerateGlobalFunctionDefinition(GlobalFunctionDefinition gf, StringBuilder sb)
289 { 307 {
290 string retstr = String.Empty;
291
292 // we need to separate the argument declaration list from other kids 308 // we need to separate the argument declaration list from other kids
293 List<SYMBOL> argumentDeclarationListKids = new List<SYMBOL>(); 309 List<SYMBOL> argumentDeclarationListKids = new List<SYMBOL>();
294 List<SYMBOL> remainingKids = new List<SYMBOL>(); 310 List<SYMBOL> remainingKids = new List<SYMBOL>();
@@ -299,18 +315,16 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
299 else 315 else
300 remainingKids.Add(kid); 316 remainingKids.Add(kid);
301 317
302 retstr += GenerateIndented(String.Format("{0} {1}(", gf.ReturnType, CheckName(gf.Name)), gf); 318 GenerateIndented(String.Format("{0} {1}(", gf.ReturnType, CheckName(gf.Name)), gf, sb);
303 319
304 // print the state arguments, if any 320 // print the state arguments, if any
305 foreach (SYMBOL kid in argumentDeclarationListKids) 321 foreach (SYMBOL kid in argumentDeclarationListKids)
306 retstr += GenerateArgumentDeclarationList((ArgumentDeclarationList) kid); 322 GenerateArgumentDeclarationList((ArgumentDeclarationList) kid, sb);
307 323
308 retstr += GenerateLine(")"); 324 GenerateLine(")", sb);
309 325
310 foreach (SYMBOL kid in remainingKids) 326 foreach (SYMBOL kid in remainingKids)
311 retstr += GenerateNode(gf, kid); 327 GenerateNodeToSB(gf, kid,sb);
312
313 return retstr;
314 } 328 }
315 329
316 /// <summary> 330 /// <summary>
@@ -318,18 +332,14 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
318 /// </summary> 332 /// </summary>
319 /// <param name="gv">The GlobalVariableDeclaration node.</param> 333 /// <param name="gv">The GlobalVariableDeclaration node.</param>
320 /// <returns>String containing C# code for GlobalVariableDeclaration gv.</returns> 334 /// <returns>String containing C# code for GlobalVariableDeclaration gv.</returns>
321 private string GenerateGlobalVariableDeclaration(GlobalVariableDeclaration gv) 335 private void GenerateGlobalVariableDeclaration(GlobalVariableDeclaration gv, StringBuilder sb)
322 { 336 {
323 string retstr = String.Empty;
324
325 foreach (SYMBOL s in gv.kids) 337 foreach (SYMBOL s in gv.kids)
326 { 338 {
327 retstr += Indent(); 339 Indent(sb);
328 retstr += GenerateNode(gv, s); 340 GenerateNodeToSB(gv, s ,sb);
329 retstr += GenerateLine(";"); 341 GenerateLine(";", sb);
330 } 342 }
331
332 return retstr;
333 } 343 }
334 344
335 /// <summary> 345 /// <summary>
@@ -337,15 +347,11 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
337 /// </summary> 347 /// </summary>
338 /// <param name="s">The State node.</param> 348 /// <param name="s">The State node.</param>
339 /// <returns>String containing C# code for State s.</returns> 349 /// <returns>String containing C# code for State s.</returns>
340 private string GenerateState(State s) 350 private void GenerateState(State s, StringBuilder sb)
341 { 351 {
342 string retstr = String.Empty;
343
344 foreach (SYMBOL kid in s.kids) 352 foreach (SYMBOL kid in s.kids)
345 if (kid is StateEvent) 353 if (kid is StateEvent)
346 retstr += GenerateStateEvent((StateEvent) kid, s.Name); 354 GenerateStateEvent((StateEvent) kid, s.Name, sb);
347
348 return retstr;
349 } 355 }
350 356
351 /// <summary> 357 /// <summary>
@@ -354,10 +360,8 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
354 /// <param name="se">The StateEvent node.</param> 360 /// <param name="se">The StateEvent node.</param>
355 /// <param name="parentStateName">The name of the parent state.</param> 361 /// <param name="parentStateName">The name of the parent state.</param>
356 /// <returns>String containing C# code for StateEvent se.</returns> 362 /// <returns>String containing C# code for StateEvent se.</returns>
357 private string GenerateStateEvent(StateEvent se, string parentStateName) 363 private void GenerateStateEvent(StateEvent se, string parentStateName, StringBuilder sb)
358 { 364 {
359 string retstr = String.Empty;
360
361 // we need to separate the argument declaration list from other kids 365 // we need to separate the argument declaration list from other kids
362 List<SYMBOL> argumentDeclarationListKids = new List<SYMBOL>(); 366 List<SYMBOL> argumentDeclarationListKids = new List<SYMBOL>();
363 List<SYMBOL> remainingKids = new List<SYMBOL>(); 367 List<SYMBOL> remainingKids = new List<SYMBOL>();
@@ -369,18 +373,16 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
369 remainingKids.Add(kid); 373 remainingKids.Add(kid);
370 374
371 // "state" (function) declaration 375 // "state" (function) declaration
372 retstr += GenerateIndented(String.Format("public void {0}_event_{1}(", parentStateName, se.Name), se); 376 GenerateIndented(String.Format("public void {0}_event_{1}(", parentStateName, se.Name), se , sb);
373 377
374 // print the state arguments, if any 378 // print the state arguments, if any
375 foreach (SYMBOL kid in argumentDeclarationListKids) 379 foreach (SYMBOL kid in argumentDeclarationListKids)
376 retstr += GenerateArgumentDeclarationList((ArgumentDeclarationList) kid); 380 GenerateArgumentDeclarationList((ArgumentDeclarationList) kid, sb);
377 381
378 retstr += GenerateLine(")"); 382 GenerateLine(")", sb);
379 383
380 foreach (SYMBOL kid in remainingKids) 384 foreach (SYMBOL kid in remainingKids)
381 retstr += GenerateNode(se, kid); 385 GenerateNodeToSB(se, kid, sb);
382
383 return retstr;
384 } 386 }
385 387
386 /// <summary> 388 /// <summary>
@@ -388,20 +390,16 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
388 /// </summary> 390 /// </summary>
389 /// <param name="adl">The ArgumentDeclarationList node.</param> 391 /// <param name="adl">The ArgumentDeclarationList node.</param>
390 /// <returns>String containing C# code for ArgumentDeclarationList adl.</returns> 392 /// <returns>String containing C# code for ArgumentDeclarationList adl.</returns>
391 private string GenerateArgumentDeclarationList(ArgumentDeclarationList adl) 393 private void GenerateArgumentDeclarationList(ArgumentDeclarationList adl, StringBuilder sb)
392 { 394 {
393 string retstr = String.Empty;
394
395 int comma = adl.kids.Count - 1; // tells us whether to print a comma 395 int comma = adl.kids.Count - 1; // tells us whether to print a comma
396 396
397 foreach (Declaration d in adl.kids) 397 foreach (Declaration d in adl.kids)
398 { 398 {
399 retstr += Generate(String.Format("{0} {1}", d.Datatype, CheckName(d.Id)), d); 399 Generate(String.Format("{0} {1}", d.Datatype, CheckName(d.Id)), d, sb);
400 if (0 < comma--) 400 if (0 < comma--)
401 retstr += Generate(", "); 401 Generate(", ", sb);
402 } 402 }
403
404 return retstr;
405 } 403 }
406 404
407 /// <summary> 405 /// <summary>
@@ -409,20 +407,16 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
409 /// </summary> 407 /// </summary>
410 /// <param name="al">The ArgumentList node.</param> 408 /// <param name="al">The ArgumentList node.</param>
411 /// <returns>String containing C# code for ArgumentList al.</returns> 409 /// <returns>String containing C# code for ArgumentList al.</returns>
412 private string GenerateArgumentList(ArgumentList al) 410 private void GenerateArgumentList(ArgumentList al, StringBuilder sb)
413 { 411 {
414 string retstr = String.Empty;
415
416 int comma = al.kids.Count - 1; // tells us whether to print a comma 412 int comma = al.kids.Count - 1; // tells us whether to print a comma
417 413
418 foreach (SYMBOL s in al.kids) 414 foreach (SYMBOL s in al.kids)
419 { 415 {
420 retstr += GenerateNode(al, s); 416 GenerateNodeToSB(al, s, sb);
421 if (0 < comma--) 417 if (0 < comma--)
422 retstr += Generate(", "); 418 Generate(", ", sb);
423 } 419 }
424
425 return retstr;
426 } 420 }
427 421
428 /// <summary> 422 /// <summary>
@@ -430,33 +424,29 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
430 /// </summary> 424 /// </summary>
431 /// <param name="cs">The CompoundStatement node.</param> 425 /// <param name="cs">The CompoundStatement node.</param>
432 /// <returns>String containing C# code for CompoundStatement cs.</returns> 426 /// <returns>String containing C# code for CompoundStatement cs.</returns>
433 private string GenerateCompoundStatement(SYMBOL previousSymbol, CompoundStatement cs) 427 private void GenerateCompoundStatement(SYMBOL previousSymbol, CompoundStatement cs, StringBuilder sb)
434 { 428 {
435 string retstr = String.Empty;
436
437 // opening brace 429 // opening brace
438 retstr += GenerateIndentedLine("{"); 430 GenerateIndentedLine("{", sb);
439 m_braceCount++; 431 m_braceCount++;
440 432
441 if (m_insertCoopTerminationChecks) 433 if (m_insertCoopTerminationChecks)
442 { 434 {
443 // We have to check in event functions as well because the user can manually call these. 435 // We have to check in event functions as well because the user can manually call these.
444 if (previousSymbol is GlobalFunctionDefinition 436 if (previousSymbol is GlobalFunctionDefinition
445 || previousSymbol is WhileStatement 437 || previousSymbol is WhileStatement
446 || previousSymbol is DoWhileStatement 438 || previousSymbol is DoWhileStatement
447 || previousSymbol is ForLoop 439 || previousSymbol is ForLoop
448 || previousSymbol is StateEvent) 440 || previousSymbol is StateEvent)
449 retstr += GenerateIndentedLine(m_coopTerminationCheck); 441 GenerateIndentedLine(m_coopTerminationCheck, sb);
450 } 442 }
451 443
452 foreach (SYMBOL kid in cs.kids) 444 foreach (SYMBOL kid in cs.kids)
453 retstr += GenerateNode(cs, kid); 445 GenerateNodeToSB(cs, kid, sb);
454 446
455 // closing brace 447 // closing brace
456 m_braceCount--; 448 m_braceCount--;
457 retstr += GenerateIndentedLine("}"); 449 GenerateIndentedLine("}", sb);
458
459 return retstr;
460 } 450 }
461 451
462 /// <summary> 452 /// <summary>
@@ -464,9 +454,9 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
464 /// </summary> 454 /// </summary>
465 /// <param name="d">The Declaration node.</param> 455 /// <param name="d">The Declaration node.</param>
466 /// <returns>String containing C# code for Declaration d.</returns> 456 /// <returns>String containing C# code for Declaration d.</returns>
467 private string GenerateDeclaration(Declaration d) 457 private void GenerateDeclaration(Declaration d, StringBuilder sb)
468 { 458 {
469 return Generate(String.Format("{0} {1}", d.Datatype, CheckName(d.Id)), d); 459 Generate(String.Format("{0} {1}", d.Datatype, CheckName(d.Id)), d, sb);
470 } 460 }
471 461
472 /// <summary> 462 /// <summary>
@@ -474,7 +464,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
474 /// </summary> 464 /// </summary>
475 /// <param name="s">The Statement node.</param> 465 /// <param name="s">The Statement node.</param>
476 /// <returns>String containing C# code for Statement s.</returns> 466 /// <returns>String containing C# code for Statement s.</returns>
477 private string GenerateStatement(SYMBOL previousSymbol, Statement s) 467 private void GenerateStatement(SYMBOL previousSymbol, Statement s, StringBuilder sb)
478 { 468 {
479 string retstr = String.Empty; 469 string retstr = String.Empty;
480 bool printSemicolon = true; 470 bool printSemicolon = true;
@@ -484,20 +474,20 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
484 { 474 {
485 // A non-braced single line do while structure cannot contain multiple statements. 475 // A non-braced single line do while structure cannot contain multiple statements.
486 // So to insert the termination check we change this to a braced control structure instead. 476 // So to insert the termination check we change this to a braced control structure instead.
487 if (previousSymbol is WhileStatement 477 if (previousSymbol is WhileStatement
488 || previousSymbol is DoWhileStatement 478 || previousSymbol is DoWhileStatement
489 || previousSymbol is ForLoop) 479 || previousSymbol is ForLoop)
490 { 480 {
491 transformToBlock = true; 481 transformToBlock = true;
492 482
493 // FIXME: This will be wrongly indented because the previous for/while/dowhile will have already indented. 483 // FIXME: This will be wrongly indented because the previous for/while/dowhile will have already indented.
494 retstr += GenerateIndentedLine("{"); 484 GenerateIndentedLine("{", sb);
495 485
496 retstr += GenerateIndentedLine(m_coopTerminationCheck); 486 GenerateIndentedLine(m_coopTerminationCheck, sb);
497 } 487 }
498 } 488 }
499 489
500 retstr += Indent(); 490 Indent(sb);
501 491
502 if (0 < s.kids.Count) 492 if (0 < s.kids.Count)
503 { 493 {
@@ -508,19 +498,17 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
508 // (MONO) error. 498 // (MONO) error.
509 if (!(s.kids.Top is IdentExpression && 1 == s.kids.Count)) 499 if (!(s.kids.Top is IdentExpression && 1 == s.kids.Count))
510 foreach (SYMBOL kid in s.kids) 500 foreach (SYMBOL kid in s.kids)
511 retstr += GenerateNode(s, kid); 501 GenerateNodeToSB(s, kid, sb);
512 } 502 }
513 503
514 if (printSemicolon) 504 if (printSemicolon)
515 retstr += GenerateLine(";"); 505 GenerateLine(";", sb);
516 506
517 if (transformToBlock) 507 if (transformToBlock)
518 { 508 {
519 // FIXME: This will be wrongly indented because the for/while/dowhile is currently handling the unindent 509 // FIXME: This will be wrongly indented because the for/while/dowhile is currently handling the unindent
520 retstr += GenerateIndentedLine("}"); 510 GenerateIndentedLine("}", sb);
521 } 511 }
522
523 return retstr;
524 } 512 }
525 513
526 /// <summary> 514 /// <summary>
@@ -528,25 +516,21 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
528 /// </summary> 516 /// </summary>
529 /// <param name="a">The Assignment node.</param> 517 /// <param name="a">The Assignment node.</param>
530 /// <returns>String containing C# code for Assignment a.</returns> 518 /// <returns>String containing C# code for Assignment a.</returns>
531 private string GenerateAssignment(Assignment a) 519 private void GenerateAssignment(Assignment a, StringBuilder sb)
532 { 520 {
533 string retstr = String.Empty;
534
535 List<string> identifiers = new List<string>(); 521 List<string> identifiers = new List<string>();
536 checkForMultipleAssignments(identifiers, a); 522 checkForMultipleAssignments(identifiers, a);
537 523
538 retstr += GenerateNode(a, (SYMBOL) a.kids.Pop()); 524 GenerateNodeToSB(a, (SYMBOL) a.kids.Pop(), sb);
539 retstr += Generate(String.Format(" {0} ", a.AssignmentType), a); 525 Generate(String.Format(" {0} ", a.AssignmentType), a, sb);
540 foreach (SYMBOL kid in a.kids) 526 foreach (SYMBOL kid in a.kids)
541 retstr += GenerateNode(a, kid); 527 GenerateNodeToSB(a, kid, sb);
542
543 return retstr;
544 } 528 }
545 529
546 // This code checks for LSL of the following forms, and generates a 530 // This code checks for LSL of the following forms, and generates a
547 // warning if it finds them. 531 // warning if it finds them.
548 // 532 //
549 // list l = [ "foo" ]; 533 // list l = [ "foo" ];
550 // l = (l=[]) + l + ["bar"]; 534 // l = (l=[]) + l + ["bar"];
551 // (produces l=["foo","bar"] in SL but l=["bar"] in OS) 535 // (produces l=["foo","bar"] in SL but l=["bar"] in OS)
552 // 536 //
@@ -604,16 +588,12 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
604 /// </summary> 588 /// </summary>
605 /// <param name="rs">The ReturnStatement node.</param> 589 /// <param name="rs">The ReturnStatement node.</param>
606 /// <returns>String containing C# code for ReturnStatement rs.</returns> 590 /// <returns>String containing C# code for ReturnStatement rs.</returns>
607 private string GenerateReturnStatement(ReturnStatement rs) 591 private void GenerateReturnStatement(ReturnStatement rs, StringBuilder sb)
608 { 592 {
609 string retstr = String.Empty; 593 Generate("return ", rs, sb);
610
611 retstr += Generate("return ", rs);
612 594
613 foreach (SYMBOL kid in rs.kids) 595 foreach (SYMBOL kid in rs.kids)
614 retstr += GenerateNode(rs, kid); 596 GenerateNodeToSB(rs, kid, sb);
615
616 return retstr;
617 } 597 }
618 598
619 /// <summary> 599 /// <summary>
@@ -621,7 +601,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
621 /// </summary> 601 /// </summary>
622 /// <param name="jl">The JumpLabel node.</param> 602 /// <param name="jl">The JumpLabel node.</param>
623 /// <returns>String containing C# code for JumpLabel jl.</returns> 603 /// <returns>String containing C# code for JumpLabel jl.</returns>
624 private string GenerateJumpLabel(JumpLabel jl) 604 private void GenerateJumpLabel(JumpLabel jl, StringBuilder sb)
625 { 605 {
626 string labelStatement; 606 string labelStatement;
627 607
@@ -630,7 +610,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
630 else 610 else
631 labelStatement = "NoOp();"; 611 labelStatement = "NoOp();";
632 612
633 return GenerateLine(String.Format("{0}: {1}", CheckName(jl.LabelName), labelStatement), jl); 613 GenerateLine(String.Format("{0}: {1}", CheckName(jl.LabelName), labelStatement), jl, sb);
634 } 614 }
635 615
636 /// <summary> 616 /// <summary>
@@ -638,9 +618,9 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
638 /// </summary> 618 /// </summary>
639 /// <param name="js">The JumpStatement node.</param> 619 /// <param name="js">The JumpStatement node.</param>
640 /// <returns>String containing C# code for JumpStatement js.</returns> 620 /// <returns>String containing C# code for JumpStatement js.</returns>
641 private string GenerateJumpStatement(JumpStatement js) 621 private void GenerateJumpStatement(JumpStatement js, StringBuilder sb)
642 { 622 {
643 return Generate(String.Format("goto {0}", CheckName(js.TargetName)), js); 623 Generate(String.Format("goto {0}", CheckName(js.TargetName)), js, sb);
644 } 624 }
645 625
646 /// <summary> 626 /// <summary>
@@ -648,32 +628,28 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
648 /// </summary> 628 /// </summary>
649 /// <param name="ifs">The IfStatement node.</param> 629 /// <param name="ifs">The IfStatement node.</param>
650 /// <returns>String containing C# code for IfStatement ifs.</returns> 630 /// <returns>String containing C# code for IfStatement ifs.</returns>
651 private string GenerateIfStatement(IfStatement ifs) 631 private void GenerateIfStatement(IfStatement ifs, StringBuilder sb)
652 { 632 {
653 string retstr = String.Empty; 633 GenerateIndented("if (", ifs, sb);
654 634 GenerateNodeToSB(ifs, (SYMBOL) ifs.kids.Pop(), sb);
655 retstr += GenerateIndented("if (", ifs); 635 GenerateLine(")", sb);
656 retstr += GenerateNode(ifs, (SYMBOL) ifs.kids.Pop());
657 retstr += GenerateLine(")");
658 636
659 // CompoundStatement handles indentation itself but we need to do it 637 // CompoundStatement handles indentation itself but we need to do it
660 // otherwise. 638 // otherwise.
661 bool indentHere = ifs.kids.Top is Statement; 639 bool indentHere = ifs.kids.Top is Statement;
662 if (indentHere) m_braceCount++; 640 if (indentHere) m_braceCount++;
663 retstr += GenerateNode(ifs, (SYMBOL) ifs.kids.Pop()); 641 GenerateNodeToSB(ifs, (SYMBOL) ifs.kids.Pop(), sb);
664 if (indentHere) m_braceCount--; 642 if (indentHere) m_braceCount--;
665 643
666 if (0 < ifs.kids.Count) // do it again for an else 644 if (0 < ifs.kids.Count) // do it again for an else
667 { 645 {
668 retstr += GenerateIndentedLine("else", ifs); 646 GenerateIndentedLine("else", ifs, sb);
669 647
670 indentHere = ifs.kids.Top is Statement; 648 indentHere = ifs.kids.Top is Statement;
671 if (indentHere) m_braceCount++; 649 if (indentHere) m_braceCount++;
672 retstr += GenerateNode(ifs, (SYMBOL) ifs.kids.Pop()); 650 GenerateNodeToSB(ifs, (SYMBOL) ifs.kids.Pop(), sb);
673 if (indentHere) m_braceCount--; 651 if (indentHere) m_braceCount--;
674 } 652 }
675
676 return retstr;
677 } 653 }
678 654
679 /// <summary> 655 /// <summary>
@@ -681,9 +657,9 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
681 /// </summary> 657 /// </summary>
682 /// <param name="sc">The StateChange node.</param> 658 /// <param name="sc">The StateChange node.</param>
683 /// <returns>String containing C# code for StateChange sc.</returns> 659 /// <returns>String containing C# code for StateChange sc.</returns>
684 private string GenerateStateChange(StateChange sc) 660 private void GenerateStateChange(StateChange sc, StringBuilder sb)
685 { 661 {
686 return Generate(String.Format("state(\"{0}\")", sc.NewState), sc); 662 Generate(String.Format("state(\"{0}\")", sc.NewState), sc, sb);
687 } 663 }
688 664
689 /// <summary> 665 /// <summary>
@@ -691,22 +667,18 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
691 /// </summary> 667 /// </summary>
692 /// <param name="ws">The WhileStatement node.</param> 668 /// <param name="ws">The WhileStatement node.</param>
693 /// <returns>String containing C# code for WhileStatement ws.</returns> 669 /// <returns>String containing C# code for WhileStatement ws.</returns>
694 private string GenerateWhileStatement(WhileStatement ws) 670 private void GenerateWhileStatement(WhileStatement ws, StringBuilder sb)
695 { 671 {
696 string retstr = String.Empty; 672 GenerateIndented("while (", ws, sb);
697 673 GenerateNodeToSB(ws, (SYMBOL) ws.kids.Pop(), sb);
698 retstr += GenerateIndented("while (", ws); 674 GenerateLine(")", sb);
699 retstr += GenerateNode(ws, (SYMBOL) ws.kids.Pop());
700 retstr += GenerateLine(")");
701 675
702 // CompoundStatement handles indentation itself but we need to do it 676 // CompoundStatement handles indentation itself but we need to do it
703 // otherwise. 677 // otherwise.
704 bool indentHere = ws.kids.Top is Statement; 678 bool indentHere = ws.kids.Top is Statement;
705 if (indentHere) m_braceCount++; 679 if (indentHere) m_braceCount++;
706 retstr += GenerateNode(ws, (SYMBOL) ws.kids.Pop()); 680 GenerateNodeToSB(ws, (SYMBOL) ws.kids.Pop(), sb);
707 if (indentHere) m_braceCount--; 681 if (indentHere) m_braceCount--;
708
709 return retstr;
710 } 682 }
711 683
712 /// <summary> 684 /// <summary>
@@ -714,24 +686,20 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
714 /// </summary> 686 /// </summary>
715 /// <param name="dws">The DoWhileStatement node.</param> 687 /// <param name="dws">The DoWhileStatement node.</param>
716 /// <returns>String containing C# code for DoWhileStatement dws.</returns> 688 /// <returns>String containing C# code for DoWhileStatement dws.</returns>
717 private string GenerateDoWhileStatement(DoWhileStatement dws) 689 private void GenerateDoWhileStatement(DoWhileStatement dws, StringBuilder sb)
718 { 690 {
719 string retstr = String.Empty; 691 GenerateIndentedLine("do", dws, sb);
720
721 retstr += GenerateIndentedLine("do", dws);
722 692
723 // CompoundStatement handles indentation itself but we need to do it 693 // CompoundStatement handles indentation itself but we need to do it
724 // otherwise. 694 // otherwise.
725 bool indentHere = dws.kids.Top is Statement; 695 bool indentHere = dws.kids.Top is Statement;
726 if (indentHere) m_braceCount++; 696 if (indentHere) m_braceCount++;
727 retstr += GenerateNode(dws, (SYMBOL) dws.kids.Pop()); 697 GenerateNodeToSB(dws, (SYMBOL) dws.kids.Pop(), sb);
728 if (indentHere) m_braceCount--; 698 if (indentHere) m_braceCount--;
729 699
730 retstr += GenerateIndented("while (", dws); 700 GenerateIndented("while (", dws ,sb);
731 retstr += GenerateNode(dws, (SYMBOL) dws.kids.Pop()); 701 GenerateNodeToSB(dws, (SYMBOL) dws.kids.Pop(), sb);
732 retstr += GenerateLine(");"); 702 GenerateLine(");", sb);
733
734 return retstr;
735 } 703 }
736 704
737 /// <summary> 705 /// <summary>
@@ -739,11 +707,9 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
739 /// </summary> 707 /// </summary>
740 /// <param name="fl">The ForLoop node.</param> 708 /// <param name="fl">The ForLoop node.</param>
741 /// <returns>String containing C# code for ForLoop fl.</returns> 709 /// <returns>String containing C# code for ForLoop fl.</returns>
742 private string GenerateForLoop(ForLoop fl) 710 private void GenerateForLoop(ForLoop fl, StringBuilder sb)
743 { 711 {
744 string retstr = String.Empty; 712 GenerateIndented("for (", fl, sb);
745
746 retstr += GenerateIndented("for (", fl);
747 713
748 // It's possible that we don't have an assignment, in which case 714 // It's possible that we don't have an assignment, in which case
749 // the child will be null and we only print the semicolon. 715 // the child will be null and we only print the semicolon.
@@ -752,26 +718,24 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
752 ForLoopStatement s = (ForLoopStatement) fl.kids.Pop(); 718 ForLoopStatement s = (ForLoopStatement) fl.kids.Pop();
753 if (null != s) 719 if (null != s)
754 { 720 {
755 retstr += GenerateForLoopStatement(s); 721 GenerateForLoopStatement(s, sb);
756 } 722 }
757 retstr += Generate("; "); 723 Generate("; ", sb);
758 // for (x = 0; x < 10; x++) 724 // for (x = 0; x < 10; x++)
759 // ^^^^^^ 725 // ^^^^^^
760 retstr += GenerateNode(fl, (SYMBOL) fl.kids.Pop()); 726 GenerateNodeToSB(fl, (SYMBOL) fl.kids.Pop(), sb);
761 retstr += Generate("; "); 727 Generate("; ", sb);
762 // for (x = 0; x < 10; x++) 728 // for (x = 0; x < 10; x++)
763 // ^^^ 729 // ^^^
764 retstr += GenerateForLoopStatement((ForLoopStatement) fl.kids.Pop()); 730 GenerateForLoopStatement((ForLoopStatement) fl.kids.Pop(), sb);
765 retstr += GenerateLine(")"); 731 GenerateLine(")", sb);
766 732
767 // CompoundStatement handles indentation itself but we need to do it 733 // CompoundStatement handles indentation itself but we need to do it
768 // otherwise. 734 // otherwise.
769 bool indentHere = fl.kids.Top is Statement; 735 bool indentHere = fl.kids.Top is Statement;
770 if (indentHere) m_braceCount++; 736 if (indentHere) m_braceCount++;
771 retstr += GenerateNode(fl, (SYMBOL) fl.kids.Pop()); 737 GenerateNodeToSB(fl, (SYMBOL) fl.kids.Pop(), sb);
772 if (indentHere) m_braceCount--; 738 if (indentHere) m_braceCount--;
773
774 return retstr;
775 } 739 }
776 740
777 /// <summary> 741 /// <summary>
@@ -779,10 +743,8 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
779 /// </summary> 743 /// </summary>
780 /// <param name="fls">The ForLoopStatement node.</param> 744 /// <param name="fls">The ForLoopStatement node.</param>
781 /// <returns>String containing C# code for ForLoopStatement fls.</returns> 745 /// <returns>String containing C# code for ForLoopStatement fls.</returns>
782 private string GenerateForLoopStatement(ForLoopStatement fls) 746 private void GenerateForLoopStatement(ForLoopStatement fls, StringBuilder sb)
783 { 747 {
784 string retstr = String.Empty;
785
786 int comma = fls.kids.Count - 1; // tells us whether to print a comma 748 int comma = fls.kids.Count - 1; // tells us whether to print a comma
787 749
788 // It's possible that all we have is an empty Ident, for example: 750 // It's possible that all we have is an empty Ident, for example:
@@ -791,12 +753,12 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
791 // 753 //
792 // Which is illegal in C# (MONO). We'll skip it. 754 // Which is illegal in C# (MONO). We'll skip it.
793 if (fls.kids.Top is IdentExpression && 1 == fls.kids.Count) 755 if (fls.kids.Top is IdentExpression && 1 == fls.kids.Count)
794 return retstr; 756 return;
795 757
796 for (int i = 0; i < fls.kids.Count; i++) 758 for (int i = 0; i < fls.kids.Count; i++)
797 { 759 {
798 SYMBOL s = (SYMBOL)fls.kids[i]; 760 SYMBOL s = (SYMBOL)fls.kids[i];
799 761
800 // Statements surrounded by parentheses in for loops 762 // Statements surrounded by parentheses in for loops
801 // 763 //
802 // e.g. for ((i = 0), (j = 7); (i < 10); (++i)) 764 // e.g. for ((i = 0), (j = 7); (i < 10); (++i))
@@ -812,13 +774,11 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
812 // like it would be considerably more complicated to handle). 774 // like it would be considerably more complicated to handle).
813 while (s is ParenthesisExpression) 775 while (s is ParenthesisExpression)
814 s = (SYMBOL)s.kids.Pop(); 776 s = (SYMBOL)s.kids.Pop();
815 777
816 retstr += GenerateNode(fls, s); 778 GenerateNodeToSB(fls, s, sb);
817 if (0 < comma--) 779 if (0 < comma--)
818 retstr += Generate(", "); 780 Generate(", ", sb);
819 } 781 }
820
821 return retstr;
822 } 782 }
823 783
824 /// <summary> 784 /// <summary>
@@ -826,31 +786,27 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
826 /// </summary> 786 /// </summary>
827 /// <param name="be">The BinaryExpression node.</param> 787 /// <param name="be">The BinaryExpression node.</param>
828 /// <returns>String containing C# code for BinaryExpression be.</returns> 788 /// <returns>String containing C# code for BinaryExpression be.</returns>
829 private string GenerateBinaryExpression(BinaryExpression be) 789 private void GenerateBinaryExpression(BinaryExpression be, StringBuilder sb)
830 { 790 {
831 string retstr = String.Empty;
832
833 if (be.ExpressionSymbol.Equals("&&") || be.ExpressionSymbol.Equals("||")) 791 if (be.ExpressionSymbol.Equals("&&") || be.ExpressionSymbol.Equals("||"))
834 { 792 {
835 // special case handling for logical and/or, see Mantis 3174 793 // special case handling for logical and/or, see Mantis 3174
836 retstr += "((bool)("; 794 sb.Append("((bool)(");
837 retstr += GenerateNode(be, (SYMBOL)be.kids.Pop()); 795 GenerateNodeToSB(be, (SYMBOL)be.kids.Pop(), sb);
838 retstr += "))"; 796 sb.Append("))");
839 retstr += Generate(String.Format(" {0} ", be.ExpressionSymbol.Substring(0,1)), be); 797 Generate(String.Format(" {0} ", be.ExpressionSymbol.Substring(0,1)), be, sb);
840 retstr += "((bool)("; 798 sb.Append("((bool)(");
841 foreach (SYMBOL kid in be.kids) 799 foreach (SYMBOL kid in be.kids)
842 retstr += GenerateNode(be, kid); 800 GenerateNodeToSB(be, kid, sb);
843 retstr += "))"; 801 sb.Append("))");
844 } 802 }
845 else 803 else
846 { 804 {
847 retstr += GenerateNode(be, (SYMBOL)be.kids.Pop()); 805 GenerateNodeToSB(be, (SYMBOL)be.kids.Pop(), sb);
848 retstr += Generate(String.Format(" {0} ", be.ExpressionSymbol), be); 806 Generate(String.Format(" {0} ", be.ExpressionSymbol), be, sb);
849 foreach (SYMBOL kid in be.kids) 807 foreach (SYMBOL kid in be.kids)
850 retstr += GenerateNode(be, kid); 808 GenerateNodeToSB(be, kid, sb);
851 } 809 }
852
853 return retstr;
854 } 810 }
855 811
856 /// <summary> 812 /// <summary>
@@ -858,14 +814,10 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
858 /// </summary> 814 /// </summary>
859 /// <param name="ue">The UnaryExpression node.</param> 815 /// <param name="ue">The UnaryExpression node.</param>
860 /// <returns>String containing C# code for UnaryExpression ue.</returns> 816 /// <returns>String containing C# code for UnaryExpression ue.</returns>
861 private string GenerateUnaryExpression(UnaryExpression ue) 817 private void GenerateUnaryExpression(UnaryExpression ue, StringBuilder sb)
862 { 818 {
863 string retstr = String.Empty; 819 Generate(ue.UnarySymbol, ue, sb);
864 820 GenerateNodeToSB(ue, (SYMBOL) ue.kids.Pop(), sb);
865 retstr += Generate(ue.UnarySymbol, ue);
866 retstr += GenerateNode(ue, (SYMBOL) ue.kids.Pop());
867
868 return retstr;
869 } 821 }
870 822
871 /// <summary> 823 /// <summary>
@@ -873,16 +825,14 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
873 /// </summary> 825 /// </summary>
874 /// <param name="pe">The ParenthesisExpression node.</param> 826 /// <param name="pe">The ParenthesisExpression node.</param>
875 /// <returns>String containing C# code for ParenthesisExpression pe.</returns> 827 /// <returns>String containing C# code for ParenthesisExpression pe.</returns>
876 private string GenerateParenthesisExpression(ParenthesisExpression pe) 828 private void GenerateParenthesisExpression(ParenthesisExpression pe, StringBuilder sb)
877 { 829 {
878 string retstr = String.Empty; 830 string retstr = String.Empty;
879 831
880 retstr += Generate("("); 832 Generate("(", sb);
881 foreach (SYMBOL kid in pe.kids) 833 foreach (SYMBOL kid in pe.kids)
882 retstr += GenerateNode(pe, kid); 834 GenerateNodeToSB(pe, kid, sb);
883 retstr += Generate(")"); 835 Generate(")", sb);
884
885 return retstr;
886 } 836 }
887 837
888 /// <summary> 838 /// <summary>
@@ -890,19 +840,15 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
890 /// </summary> 840 /// </summary>
891 /// <param name="ide">The IncrementDecrementExpression node.</param> 841 /// <param name="ide">The IncrementDecrementExpression node.</param>
892 /// <returns>String containing C# code for IncrementDecrementExpression ide.</returns> 842 /// <returns>String containing C# code for IncrementDecrementExpression ide.</returns>
893 private string GenerateIncrementDecrementExpression(IncrementDecrementExpression ide) 843 private void GenerateIncrementDecrementExpression(IncrementDecrementExpression ide, StringBuilder sb)
894 { 844 {
895 string retstr = String.Empty;
896
897 if (0 < ide.kids.Count) 845 if (0 < ide.kids.Count)
898 { 846 {
899 IdentDotExpression dot = (IdentDotExpression) ide.kids.Top; 847 IdentDotExpression dot = (IdentDotExpression) ide.kids.Top;
900 retstr += Generate(String.Format("{0}", ide.PostOperation ? CheckName(dot.Name) + "." + dot.Member + ide.Operation : ide.Operation + CheckName(dot.Name) + "." + dot.Member), ide); 848 Generate(String.Format("{0}", ide.PostOperation ? CheckName(dot.Name) + "." + dot.Member + ide.Operation : ide.Operation + CheckName(dot.Name) + "." + dot.Member), ide, sb);
901 } 849 }
902 else 850 else
903 retstr += Generate(String.Format("{0}", ide.PostOperation ? CheckName(ide.Name) + ide.Operation : ide.Operation + CheckName(ide.Name)), ide); 851 Generate(String.Format("{0}", ide.PostOperation ? CheckName(ide.Name) + ide.Operation : ide.Operation + CheckName(ide.Name)), ide, sb);
904
905 return retstr;
906 } 852 }
907 853
908 /// <summary> 854 /// <summary>
@@ -910,16 +856,12 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
910 /// </summary> 856 /// </summary>
911 /// <param name="te">The TypecastExpression node.</param> 857 /// <param name="te">The TypecastExpression node.</param>
912 /// <returns>String containing C# code for TypecastExpression te.</returns> 858 /// <returns>String containing C# code for TypecastExpression te.</returns>
913 private string GenerateTypecastExpression(TypecastExpression te) 859 private void GenerateTypecastExpression(TypecastExpression te, StringBuilder sb)
914 { 860 {
915 string retstr = String.Empty;
916
917 // we wrap all typecasted statements in parentheses 861 // we wrap all typecasted statements in parentheses
918 retstr += Generate(String.Format("({0}) (", te.TypecastType), te); 862 Generate(String.Format("({0}) (", te.TypecastType), te, sb);
919 retstr += GenerateNode(te, (SYMBOL) te.kids.Pop()); 863 GenerateNodeToSB(te, (SYMBOL) te.kids.Pop(), sb);
920 retstr += Generate(")"); 864 Generate(")", sb);
921
922 return retstr;
923 } 865 }
924 866
925 /// <summary> 867 /// <summary>
@@ -928,7 +870,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
928 /// <param name="id">The symbol name</param> 870 /// <param name="id">The symbol name</param>
929 /// <param name="s">The Symbol node.</param> 871 /// <param name="s">The Symbol node.</param>
930 /// <returns>String containing C# code for identifier reference.</returns> 872 /// <returns>String containing C# code for identifier reference.</returns>
931 private string GenerateIdentifier(string id, SYMBOL s) 873 private void GenerateIdentifier(string id, SYMBOL s, StringBuilder sb)
932 { 874 {
933 if (m_comms != null) 875 if (m_comms != null)
934 { 876 {
@@ -949,12 +891,14 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
949 else if (value is OpenMetaverse.Quaternion) 891 else if (value is OpenMetaverse.Quaternion)
950 retval = String.Format("new LSL_Types.Quaternion(\"{0}\")",((OpenMetaverse.Quaternion)value).ToString()); 892 retval = String.Format("new LSL_Types.Quaternion(\"{0}\")",((OpenMetaverse.Quaternion)value).ToString());
951 else retval = id; 893 else retval = id;
952 894
953 return Generate(retval, s); 895 Generate(retval, s, sb);
896 return;
954 } 897 }
955 } 898 }
956 899
957 return Generate(CheckName(id), s); 900 Generate(CheckName(id), s, sb);
901 return;
958 } 902 }
959 903
960 /// <summary> 904 /// <summary>
@@ -962,35 +906,31 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
962 /// </summary> 906 /// </summary>
963 /// <param name="fc">The FunctionCall node.</param> 907 /// <param name="fc">The FunctionCall node.</param>
964 /// <returns>String containing C# code for FunctionCall fc.</returns> 908 /// <returns>String containing C# code for FunctionCall fc.</returns>
965 private string GenerateFunctionCall(FunctionCall fc) 909 private void GenerateFunctionCall(FunctionCall fc, StringBuilder sb)
966 { 910 {
967 string retstr = String.Empty;
968
969 string modinvoke = null; 911 string modinvoke = null;
970 if (m_comms != null) 912 if (m_comms != null)
971 modinvoke = m_comms.LookupModInvocation(fc.Id); 913 modinvoke = m_comms.LookupModInvocation(fc.Id);
972 914
973 if (modinvoke != null) 915 if (modinvoke != null)
974 { 916 {
975 if (fc.kids[0] is ArgumentList) 917 if (fc.kids[0] is ArgumentList)
976 { 918 {
977 if ((fc.kids[0] as ArgumentList).kids.Count == 0) 919 if ((fc.kids[0] as ArgumentList).kids.Count == 0)
978 retstr += Generate(String.Format("{0}(\"{1}\"",modinvoke,fc.Id), fc); 920 Generate(String.Format("{0}(\"{1}\"",modinvoke,fc.Id), fc, sb);
979 else 921 else
980 retstr += Generate(String.Format("{0}(\"{1}\",",modinvoke,fc.Id), fc); 922 Generate(String.Format("{0}(\"{1}\",",modinvoke,fc.Id), fc, sb);
981 } 923 }
982 } 924 }
983 else 925 else
984 { 926 {
985 retstr += Generate(String.Format("{0}(", CheckName(fc.Id)), fc); 927 Generate(String.Format("{0}(", CheckName(fc.Id)), fc, sb);
986 } 928 }
987
988 foreach (SYMBOL kid in fc.kids)
989 retstr += GenerateNode(fc, kid);
990 929
991 retstr += Generate(")"); 930 foreach (SYMBOL kid in fc.kids)
931 GenerateNodeToSB(fc, kid, sb);
992 932
993 return retstr; 933 Generate(")", sb);
994 } 934 }
995 935
996 /// <summary> 936 /// <summary>
@@ -998,10 +938,8 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
998 /// </summary> 938 /// </summary>
999 /// <param name="c">The Constant node.</param> 939 /// <param name="c">The Constant node.</param>
1000 /// <returns>String containing C# code for Constant c.</returns> 940 /// <returns>String containing C# code for Constant c.</returns>
1001 private string GenerateConstant(Constant c) 941 private void GenerateConstant(Constant c, StringBuilder sb)
1002 { 942 {
1003 string retstr = String.Empty;
1004
1005 // Supprt LSL's weird acceptance of floats with no trailing digits 943 // Supprt LSL's weird acceptance of floats with no trailing digits
1006 // after the period. Turn float x = 10.; into float x = 10.0; 944 // after the period. Turn float x = 10.; into float x = 10.0;
1007 if ("LSL_Types.LSLFloat" == c.Type) 945 if ("LSL_Types.LSLFloat" == c.Type)
@@ -1020,9 +958,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
1020 c.Value = "new LSL_Types.LSLString(\""+c.Value+"\")"; 958 c.Value = "new LSL_Types.LSLString(\""+c.Value+"\")";
1021 } 959 }
1022 960
1023 retstr += Generate(c.Value, c); 961 Generate(c.Value, c, sb);
1024
1025 return retstr;
1026 } 962 }
1027 963
1028 /// <summary> 964 /// <summary>
@@ -1030,19 +966,15 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
1030 /// </summary> 966 /// </summary>
1031 /// <param name="vc">The VectorConstant node.</param> 967 /// <param name="vc">The VectorConstant node.</param>
1032 /// <returns>String containing C# code for VectorConstant vc.</returns> 968 /// <returns>String containing C# code for VectorConstant vc.</returns>
1033 private string GenerateVectorConstant(VectorConstant vc) 969 private void GenerateVectorConstant(VectorConstant vc, StringBuilder sb)
1034 { 970 {
1035 string retstr = String.Empty; 971 Generate(String.Format("new {0}(", vc.Type), vc, sb);
1036 972 GenerateNodeToSB(vc, (SYMBOL) vc.kids.Pop(), sb);
1037 retstr += Generate(String.Format("new {0}(", vc.Type), vc); 973 Generate(", ", sb);
1038 retstr += GenerateNode(vc, (SYMBOL) vc.kids.Pop()); 974 GenerateNodeToSB(vc, (SYMBOL) vc.kids.Pop(), sb);
1039 retstr += Generate(", "); 975 Generate(", ", sb);
1040 retstr += GenerateNode(vc, (SYMBOL) vc.kids.Pop()); 976 GenerateNodeToSB(vc, (SYMBOL) vc.kids.Pop(), sb);
1041 retstr += Generate(", "); 977 Generate(")", sb);
1042 retstr += GenerateNode(vc, (SYMBOL) vc.kids.Pop());
1043 retstr += Generate(")");
1044
1045 return retstr;
1046 } 978 }
1047 979
1048 /// <summary> 980 /// <summary>
@@ -1050,21 +982,17 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
1050 /// </summary> 982 /// </summary>
1051 /// <param name="rc">The RotationConstant node.</param> 983 /// <param name="rc">The RotationConstant node.</param>
1052 /// <returns>String containing C# code for RotationConstant rc.</returns> 984 /// <returns>String containing C# code for RotationConstant rc.</returns>
1053 private string GenerateRotationConstant(RotationConstant rc) 985 private void GenerateRotationConstant(RotationConstant rc, StringBuilder sb)
1054 { 986 {
1055 string retstr = String.Empty; 987 Generate(String.Format("new {0}(", rc.Type), rc, sb);
1056 988 GenerateNodeToSB(rc, (SYMBOL) rc.kids.Pop(), sb);
1057 retstr += Generate(String.Format("new {0}(", rc.Type), rc); 989 Generate(", ", sb);
1058 retstr += GenerateNode(rc, (SYMBOL) rc.kids.Pop()); 990 GenerateNodeToSB(rc, (SYMBOL) rc.kids.Pop(), sb);
1059 retstr += Generate(", "); 991 Generate(", ", sb);
1060 retstr += GenerateNode(rc, (SYMBOL) rc.kids.Pop()); 992 GenerateNodeToSB(rc, (SYMBOL) rc.kids.Pop(), sb);
1061 retstr += Generate(", "); 993 Generate(", ", sb);
1062 retstr += GenerateNode(rc, (SYMBOL) rc.kids.Pop()); 994 GenerateNodeToSB(rc, (SYMBOL) rc.kids.Pop(), sb);
1063 retstr += Generate(", "); 995 Generate(")", sb);
1064 retstr += GenerateNode(rc, (SYMBOL) rc.kids.Pop());
1065 retstr += Generate(")");
1066
1067 return retstr;
1068 } 996 }
1069 997
1070 /// <summary> 998 /// <summary>
@@ -1072,27 +1000,25 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
1072 /// </summary> 1000 /// </summary>
1073 /// <param name="lc">The ListConstant node.</param> 1001 /// <param name="lc">The ListConstant node.</param>
1074 /// <returns>String containing C# code for ListConstant lc.</returns> 1002 /// <returns>String containing C# code for ListConstant lc.</returns>
1075 private string GenerateListConstant(ListConstant lc) 1003 private void GenerateListConstant(ListConstant lc, StringBuilder sb)
1076 { 1004 {
1077 string retstr = String.Empty; 1005 Generate(String.Format("new {0}(", lc.Type), lc, sb);
1078
1079 retstr += Generate(String.Format("new {0}(", lc.Type), lc);
1080 1006
1081 foreach (SYMBOL kid in lc.kids) 1007 foreach (SYMBOL kid in lc.kids)
1082 retstr += GenerateNode(lc, kid); 1008 GenerateNodeToSB(lc, kid, sb);
1083
1084 retstr += Generate(")");
1085 1009
1086 return retstr; 1010 Generate(")", sb);
1087 } 1011 }
1088 1012
1089 /// <summary> 1013 /// <summary>
1090 /// Prints a newline. 1014 /// Prints a newline.
1091 /// </summary> 1015 /// </summary>
1092 /// <returns>A newline.</returns> 1016 /// <returns>A newline.</returns>
1093 private string GenerateLine() 1017 private void GenerateLine(StringBuilder sb)
1094 { 1018 {
1095 return GenerateLine(""); 1019 sb.Append("\n");
1020 m_CSharpLine++;
1021 m_CSharpCol = 1;
1096 } 1022 }
1097 1023
1098 /// <summary> 1024 /// <summary>
@@ -1100,9 +1026,12 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
1100 /// </summary> 1026 /// </summary>
1101 /// <param name="s">String of text to print.</param> 1027 /// <param name="s">String of text to print.</param>
1102 /// <returns>String s followed by newline.</returns> 1028 /// <returns>String s followed by newline.</returns>
1103 private string GenerateLine(string s) 1029 private void GenerateLine(string s, StringBuilder sb)
1104 { 1030 {
1105 return GenerateLine(s, null); 1031 sb.Append(s);
1032 sb.Append("\n");
1033 m_CSharpLine++;
1034 m_CSharpCol = 1;
1106 } 1035 }
1107 1036
1108 /// <summary> 1037 /// <summary>
@@ -1112,14 +1041,13 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
1112 /// <param name="sym">Symbol being generated to extract original line 1041 /// <param name="sym">Symbol being generated to extract original line
1113 /// number and column from.</param> 1042 /// number and column from.</param>
1114 /// <returns>String s followed by newline.</returns> 1043 /// <returns>String s followed by newline.</returns>
1115 private string GenerateLine(string s, SYMBOL sym) 1044 private void GenerateLine(string s, SYMBOL sym, StringBuilder sb)
1116 { 1045 {
1117 string retstr = Generate(s, sym) + "\n"; 1046 Generate(s, sym, sb);
1047 sb.Append("\n");
1118 1048
1119 m_CSharpLine++; 1049 m_CSharpLine++;
1120 m_CSharpCol = 1; 1050 m_CSharpCol = 1;
1121
1122 return retstr;
1123 } 1051 }
1124 1052
1125 /// <summary> 1053 /// <summary>
@@ -1127,9 +1055,10 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
1127 /// </summary> 1055 /// </summary>
1128 /// <param name="s">String of text to print.</param> 1056 /// <param name="s">String of text to print.</param>
1129 /// <returns>String s.</returns> 1057 /// <returns>String s.</returns>
1130 private string Generate(string s) 1058 private void Generate(string s, StringBuilder sb)
1131 { 1059 {
1132 return Generate(s, null); 1060 sb.Append(s);
1061 m_CSharpCol += s.Length;
1133 } 1062 }
1134 1063
1135 /// <summary> 1064 /// <summary>
@@ -1139,14 +1068,13 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
1139 /// <param name="sym">Symbol being generated to extract original line 1068 /// <param name="sym">Symbol being generated to extract original line
1140 /// number and column from.</param> 1069 /// number and column from.</param>
1141 /// <returns>String s.</returns> 1070 /// <returns>String s.</returns>
1142 private string Generate(string s, SYMBOL sym) 1071 private void Generate(string s, SYMBOL sym, StringBuilder sb)
1143 { 1072 {
1073 sb.Append(s);
1144 if (null != sym) 1074 if (null != sym)
1145 m_positionMap.Add(new KeyValuePair<int, int>(m_CSharpLine, m_CSharpCol), new KeyValuePair<int, int>(sym.Line, sym.Position)); 1075 m_positionMap.Add(new KeyValuePair<int, int>(m_CSharpLine, m_CSharpCol), new KeyValuePair<int, int>(sym.Line, sym.Position));
1146 1076
1147 m_CSharpCol += s.Length; 1077 m_CSharpCol += s.Length;
1148
1149 return s;
1150 } 1078 }
1151 1079
1152 /// <summary> 1080 /// <summary>
@@ -1154,9 +1082,9 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
1154 /// </summary> 1082 /// </summary>
1155 /// <param name="s">String of text to print.</param> 1083 /// <param name="s">String of text to print.</param>
1156 /// <returns>Properly indented string s followed by newline.</returns> 1084 /// <returns>Properly indented string s followed by newline.</returns>
1157 private string GenerateIndentedLine(string s) 1085 private void GenerateIndentedLine(string s, StringBuilder sb)
1158 { 1086 {
1159 return GenerateIndentedLine(s, null); 1087 GenerateIndentedLine(s, null, sb);
1160 } 1088 }
1161 1089
1162 /// <summary> 1090 /// <summary>
@@ -1166,14 +1094,12 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
1166 /// <param name="sym">Symbol being generated to extract original line 1094 /// <param name="sym">Symbol being generated to extract original line
1167 /// number and column from.</param> 1095 /// number and column from.</param>
1168 /// <returns>Properly indented string s followed by newline.</returns> 1096 /// <returns>Properly indented string s followed by newline.</returns>
1169 private string GenerateIndentedLine(string s, SYMBOL sym) 1097 private void GenerateIndentedLine(string s, SYMBOL sym, StringBuilder sb)
1170 { 1098 {
1171 string retstr = GenerateIndented(s, sym) + "\n"; 1099 GenerateIndented(s, sym , sb );
1172 1100 sb.Append("\n");
1173 m_CSharpLine++; 1101 m_CSharpLine++;
1174 m_CSharpCol = 1; 1102 m_CSharpCol = 1;
1175
1176 return retstr;
1177 } 1103 }
1178 1104
1179 /// <summary> 1105 /// <summary>
@@ -1194,34 +1120,28 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
1194 /// <param name="sym">Symbol being generated to extract original line 1120 /// <param name="sym">Symbol being generated to extract original line
1195 /// number and column from.</param> 1121 /// number and column from.</param>
1196 /// <returns>Properly indented string s.</returns> 1122 /// <returns>Properly indented string s.</returns>
1197 private string GenerateIndented(string s, SYMBOL sym) 1123 private void GenerateIndented(string s, SYMBOL sym, StringBuilder sb)
1198 { 1124 {
1199 string retstr = Indent() + s; 1125 Indent(sb);
1126 sb.Append(s);
1200 1127
1201 if (null != sym) 1128 if (null != sym)
1202 m_positionMap.Add(new KeyValuePair<int, int>(m_CSharpLine, m_CSharpCol), new KeyValuePair<int, int>(sym.Line, sym.Position)); 1129 m_positionMap.Add(new KeyValuePair<int, int>(m_CSharpLine, m_CSharpCol), new KeyValuePair<int, int>(sym.Line, sym.Position));
1203 1130
1204 m_CSharpCol += s.Length; 1131 m_CSharpCol += s.Length;
1205
1206 return retstr;
1207 } 1132 }
1208 1133
1209 /// <summary> 1134 /// <summary>
1210 /// Prints correct indentation. 1135 /// Prints correct indentation.
1211 /// </summary> 1136 /// </summary>
1212 /// <returns>Indentation based on brace count.</returns> 1137 /// <returns>Indentation based on brace count.</returns>
1213 private string Indent() 1138 private void Indent(StringBuilder sb)
1214 { 1139 {
1215 string retstr = String.Empty;
1216
1217 for (int i = 0; i < m_braceCount; i++) 1140 for (int i = 0; i < m_braceCount; i++)
1218 for (int j = 0; j < m_indentWidth; j++) 1141 {
1219 { 1142 sb.Append(" ");
1220 retstr += " "; 1143 m_CSharpCol += 4;
1221 m_CSharpCol++; 1144 }
1222 }
1223
1224 return retstr;
1225 } 1145 }
1226 1146
1227 /// <summary> 1147 /// <summary>