aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Tests/Region/ScriptEngine/Shared/CodeTools
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Tests/Region/ScriptEngine/Shared/CodeTools')
-rw-r--r--OpenSim/Tests/Region/ScriptEngine/Shared/CodeTools/CSCodeGeneratorTest.cs1644
-rw-r--r--OpenSim/Tests/Region/ScriptEngine/Shared/CodeTools/CompilerTest.cs153
2 files changed, 1797 insertions, 0 deletions
diff --git a/OpenSim/Tests/Region/ScriptEngine/Shared/CodeTools/CSCodeGeneratorTest.cs b/OpenSim/Tests/Region/ScriptEngine/Shared/CodeTools/CSCodeGeneratorTest.cs
new file mode 100644
index 0000000..10dd3fe
--- /dev/null
+++ b/OpenSim/Tests/Region/ScriptEngine/Shared/CodeTools/CSCodeGeneratorTest.cs
@@ -0,0 +1,1644 @@
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.Collections.Generic;
29using System.Text.RegularExpressions;
30using NUnit.Framework;
31using OpenSim.Region.ScriptEngine.Shared.CodeTools;
32
33namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests
34{
35 /// <summary>
36 /// Tests the LSL compiler, both the code generation and transformation.
37 /// Each test has some LSL code as input and C# code as expected output.
38 /// The generated C# code is compared against the expected C# code.
39 /// </summary>
40 [TestFixture]
41 public class CSCodeGeneratorTest
42 {
43 [Test]
44 public void TestDefaultState()
45 {
46 string input = @"default
47{
48 state_entry()
49 {
50 }
51}
52";
53 string expected = @"
54 public void default_event_state_entry()
55 {
56 }
57";
58
59 CSCodeGenerator cg = new CSCodeGenerator();
60 string output = cg.Convert(input);
61 Assert.AreEqual(expected, output);
62 }
63
64 [Test]
65 public void TestCustomState()
66 {
67 string input = @"default
68{
69 state_entry()
70 {
71 }
72}
73
74state another_state
75{
76 no_sensor()
77 {
78 }
79}
80";
81 string expected = @"
82 public void default_event_state_entry()
83 {
84 }
85 public void another_state_event_no_sensor()
86 {
87 }
88";
89
90 CSCodeGenerator cg = new CSCodeGenerator();
91 string output = cg.Convert(input);
92 Assert.AreEqual(expected, output);
93 }
94
95 [Test]
96 public void TestEventWithArguments()
97 {
98 string input = @"default
99{
100 at_rot_target(integer tnum, rotation targetrot, rotation ourrot)
101 {
102 }
103}
104";
105 string expected = @"
106 public void default_event_at_rot_target(LSL_Types.LSLInteger tnum, LSL_Types.Quaternion targetrot, LSL_Types.Quaternion ourrot)
107 {
108 }
109";
110
111 CSCodeGenerator cg = new CSCodeGenerator();
112 string output = cg.Convert(input);
113 Assert.AreEqual(expected, output);
114 }
115
116 [Test]
117 public void TestIntegerDeclaration()
118 {
119 string input = @"default
120{
121 touch_start(integer num_detected)
122 {
123 integer x;
124 }
125}
126";
127 string expected = @"
128 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
129 {
130 LSL_Types.LSLInteger x = 0;
131 }
132";
133
134 CSCodeGenerator cg = new CSCodeGenerator();
135 string output = cg.Convert(input);
136 Assert.AreEqual(expected, output);
137 }
138
139 [Test]
140 public void TestAssignments()
141 {
142 string input = @"default
143{
144 touch_start(integer num_detected)
145 {
146 string y;
147 integer x = 14;
148 y = ""Hello"";
149 }
150}
151";
152 string expected = @"
153 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
154 {
155 LSL_Types.LSLString y = """";
156 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(14);
157 y = new LSL_Types.LSLString(""Hello"");
158 }
159";
160
161 CSCodeGenerator cg = new CSCodeGenerator();
162 string output = cg.Convert(input);
163 Assert.AreEqual(expected, output);
164 }
165
166 [Test]
167 public void TestAdditionSubtractionOperator()
168 {
169 string input = @"default
170{
171 touch_start(integer num_detected)
172 {
173 integer y = -3;
174 integer x = 14 + 6;
175 y = 12 +45+20+x + 23 + 1 + x + y;
176 y = 12 + -45 + - 20 + x + 23 + -1 + x + y;
177 }
178}
179";
180 string expected = @"
181 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
182 {
183 LSL_Types.LSLInteger y = -new LSL_Types.LSLInteger(3);
184 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(14) + new LSL_Types.LSLInteger(6);
185 y = new LSL_Types.LSLInteger(12) + new LSL_Types.LSLInteger(45) + new LSL_Types.LSLInteger(20) + x + new LSL_Types.LSLInteger(23) + new LSL_Types.LSLInteger(1) + x + y;
186 y = new LSL_Types.LSLInteger(12) + -new LSL_Types.LSLInteger(45) + -new LSL_Types.LSLInteger(20) + x + new LSL_Types.LSLInteger(23) + -new LSL_Types.LSLInteger(1) + x + y;
187 }
188";
189
190 CSCodeGenerator cg = new CSCodeGenerator();
191 string output = cg.Convert(input);
192 Assert.AreEqual(expected, output);
193 }
194
195 [Test]
196 public void TestStrings()
197 {
198 string input = @"default
199{
200 touch_start(integer num_detected)
201 {
202 llOwnerSay(""Testing, 1, 2, 3"");
203 llSay(0, ""I can hear you!"");
204 some_custom_function(1, 2, 3 + x, 4, ""five"", ""arguments"");
205 }
206}
207";
208 string expected = @"
209 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
210 {
211 llOwnerSay(new LSL_Types.LSLString(""Testing, 1, 2, 3""));
212 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""I can hear you!""));
213 some_custom_function(new LSL_Types.LSLInteger(1), new LSL_Types.LSLInteger(2), new LSL_Types.LSLInteger(3) + x, new LSL_Types.LSLInteger(4), new LSL_Types.LSLString(""five""), new LSL_Types.LSLString(""arguments""));
214 }
215";
216
217 CSCodeGenerator cg = new CSCodeGenerator();
218 string output = cg.Convert(input);
219 Assert.AreEqual(expected, output);
220 }
221
222 [Test]
223 public void TestBinaryExpression()
224 {
225 string input = @"default
226{
227 touch_start(integer num_detected)
228 {
229 integer y;
230 integer x = 14 + 6;
231 y = 12 - 3;
232 y = 12 * 3;
233 y = 12 / 3;
234 y = 12 | 3;
235 y = 12 & 3;
236 y = 12 % 3;
237 y = 12 + 45 - 20 * x / 23 | 1 & x + y;
238 }
239}
240";
241 string expected = @"
242 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
243 {
244 LSL_Types.LSLInteger y = 0;
245 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(14) + new LSL_Types.LSLInteger(6);
246 y = new LSL_Types.LSLInteger(12) - new LSL_Types.LSLInteger(3);
247 y = new LSL_Types.LSLInteger(12) * new LSL_Types.LSLInteger(3);
248 y = new LSL_Types.LSLInteger(12) / new LSL_Types.LSLInteger(3);
249 y = new LSL_Types.LSLInteger(12) | new LSL_Types.LSLInteger(3);
250 y = new LSL_Types.LSLInteger(12) & new LSL_Types.LSLInteger(3);
251 y = new LSL_Types.LSLInteger(12) % new LSL_Types.LSLInteger(3);
252 y = new LSL_Types.LSLInteger(12) + new LSL_Types.LSLInteger(45) - new LSL_Types.LSLInteger(20) * x / new LSL_Types.LSLInteger(23) | new LSL_Types.LSLInteger(1) & x + y;
253 }
254";
255
256 CSCodeGenerator cg = new CSCodeGenerator();
257 string output = cg.Convert(input);
258 Assert.AreEqual(expected, output);
259 }
260
261 [Test]
262 public void TestFloatConstants()
263 {
264 string input = @"default
265{
266 touch_start(integer num_detected)
267 {
268 float y = 1.1;
269 y = 1.123E3;
270 y = 1.123e3;
271 y = 1.123E+3;
272 y = 1.123e+3;
273 y = 1.123E-3;
274 y = 1.123e-3;
275 y = .4;
276 y = -1.123E3;
277 y = -1.123e3;
278 y = -1.123E+3;
279 y = -1.123e+3;
280 y = -1.123E-3;
281 y = -1.123e-3;
282 y = -.4;
283 y = 12.3 + -1.45E3 - 1.20e-2;
284 }
285}
286";
287 string expected = @"
288 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
289 {
290 LSL_Types.LSLFloat y = new LSL_Types.LSLFloat(1.1);
291 y = new LSL_Types.LSLFloat(1.123E3);
292 y = new LSL_Types.LSLFloat(1.123e3);
293 y = new LSL_Types.LSLFloat(1.123E+3);
294 y = new LSL_Types.LSLFloat(1.123e+3);
295 y = new LSL_Types.LSLFloat(1.123E-3);
296 y = new LSL_Types.LSLFloat(1.123e-3);
297 y = new LSL_Types.LSLFloat(.4);
298 y = -new LSL_Types.LSLFloat(1.123E3);
299 y = -new LSL_Types.LSLFloat(1.123e3);
300 y = -new LSL_Types.LSLFloat(1.123E+3);
301 y = -new LSL_Types.LSLFloat(1.123e+3);
302 y = -new LSL_Types.LSLFloat(1.123E-3);
303 y = -new LSL_Types.LSLFloat(1.123e-3);
304 y = -new LSL_Types.LSLFloat(.4);
305 y = new LSL_Types.LSLFloat(12.3) + -new LSL_Types.LSLFloat(1.45E3) - new LSL_Types.LSLFloat(1.20e-2);
306 }
307";
308
309 CSCodeGenerator cg = new CSCodeGenerator();
310 string output = cg.Convert(input);
311 Assert.AreEqual(expected, output);
312 }
313
314 [Test]
315 public void TestComments()
316 {
317 string input = @"// this test tests comments
318default
319{
320 touch_start(integer num_detected) // this should be stripped
321 {
322 // fill in code here...
323 }
324}
325";
326 string expected = @"
327 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
328 {
329 }
330";
331
332 CSCodeGenerator cg = new CSCodeGenerator();
333 string output = cg.Convert(input);
334 Assert.AreEqual(expected, output);
335 }
336
337 [Test]
338 public void TestStringsWithEscapedQuotesAndComments()
339 {
340 string input = @"// this test tests strings, with escaped quotes and comments in strings
341default
342{
343 touch_start(integer num_detected)
344 {
345 string s1 = ""this is a string."";
346 string s2 = ""this is a string ""+""with an escaped \"" inside it."";
347 s1 = s2+"" and this ""+""is a string with // comments."";
348
349 string onemore = ""[\^@]"";
350
351 string multiline = ""Good evening Sir,
352 my name is Steve.
353 I come from a rough area.
354 I used to be addicted to crack
355 but now I am off it and trying to stay clean.
356 That is why I am selling magazine subscriptions.""; // http://www.imdb.com/title/tt0151804/quotes
357 }
358}
359";
360 string expected = @"
361 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
362 {
363 LSL_Types.LSLString s1 = new LSL_Types.LSLString(""this is a string."");
364 LSL_Types.LSLString s2 = new LSL_Types.LSLString(""this is a string "") + new LSL_Types.LSLString(""with an escaped \"" inside it."");
365 s1 = s2 + new LSL_Types.LSLString("" and this "") + new LSL_Types.LSLString(""is a string with // comments."");
366 LSL_Types.LSLString onemore = new LSL_Types.LSLString(""[\^@]"");
367 LSL_Types.LSLString multiline = new LSL_Types.LSLString(""Good evening Sir,\n my name is Steve.\n I come from a rough area.\n I used to be addicted to crack\n but now I am off it and trying to stay clean.\n That is why I am selling magazine subscriptions."");
368 }
369";
370
371 CSCodeGenerator cg = new CSCodeGenerator();
372 string output = cg.Convert(input);
373 Assert.AreEqual(expected, output);
374 }
375
376 [Test]
377 public void TestGlobalDefinedFunctions()
378 {
379 string input = @"// this test tests custom defined functions
380
381string onefunc()
382{
383 return ""Hi from onefunc()!"";
384}
385
386twofunc(string s)
387{
388 llSay(1000, s);
389}
390
391default
392{
393 touch_start(integer num_detected)
394 {
395 llSay(2000, onefunc());
396 twofunc();
397 }
398}
399";
400 string expected = @"
401 LSL_Types.LSLString onefunc()
402 {
403 return new LSL_Types.LSLString(""Hi from onefunc()!"");
404 }
405 void twofunc(LSL_Types.LSLString s)
406 {
407 llSay(new LSL_Types.LSLInteger(1000), s);
408 }
409 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
410 {
411 llSay(new LSL_Types.LSLInteger(2000), onefunc());
412 twofunc();
413 }
414";
415
416 CSCodeGenerator cg = new CSCodeGenerator();
417 string output = cg.Convert(input);
418 Assert.AreEqual(expected, output);
419 }
420
421 [Test]
422 public void TestGlobalDeclaredVariables()
423 {
424 string input = @"// this test tests custom defined functions and global variables
425
426string globalString;
427integer globalInt = 14;
428integer anotherGlobal = 20 * globalInt;
429
430string onefunc()
431{
432 globalString = "" ...and the global!"";
433 return ""Hi "" +
434 ""from "" +
435 ""onefunc()!"" + globalString;
436}
437
438twofunc(string s)
439{
440 llSay(1000, s);
441}
442
443default
444{
445 touch_start(integer num_detected)
446 {
447 llSay(2000, onefunc());
448 twofunc();
449 }
450}
451";
452 string expected = @"
453 LSL_Types.LSLString globalString = """";
454 LSL_Types.LSLInteger globalInt = new LSL_Types.LSLInteger(14);
455 LSL_Types.LSLInteger anotherGlobal = new LSL_Types.LSLInteger(20) * globalInt;
456 LSL_Types.LSLString onefunc()
457 {
458 globalString = new LSL_Types.LSLString("" ...and the global!"");
459 return new LSL_Types.LSLString(""Hi "") + new LSL_Types.LSLString(""from "") + new LSL_Types.LSLString(""onefunc()!"") + globalString;
460 }
461 void twofunc(LSL_Types.LSLString s)
462 {
463 llSay(new LSL_Types.LSLInteger(1000), s);
464 }
465 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
466 {
467 llSay(new LSL_Types.LSLInteger(2000), onefunc());
468 twofunc();
469 }
470";
471
472 CSCodeGenerator cg = new CSCodeGenerator();
473 string output = cg.Convert(input);
474 Assert.AreEqual(expected, output);
475 }
476
477 [Test]
478 public void TestMoreAssignments()
479 {
480 string input = @"// this test tests +=, -=, *=, /=, %=
481
482string globalString;
483integer globalInt = 14;
484
485string onefunc(string addition)
486{
487 globalInt -= 2;
488
489 globalString += addition;
490 return ""Hi "" +
491 ""from "" +
492 ""onefunc()! "" + globalString;
493}
494
495default
496{
497 touch_start(integer num_detected)
498 {
499 llSay(2000, onefunc());
500
501 integer x = 2;
502 x *= 3;
503 x /= 14 + -2;
504 x %= 10;
505 }
506}
507";
508 string expected = @"
509 LSL_Types.LSLString globalString = """";
510 LSL_Types.LSLInteger globalInt = new LSL_Types.LSLInteger(14);
511 LSL_Types.LSLString onefunc(LSL_Types.LSLString addition)
512 {
513 globalInt -= new LSL_Types.LSLInteger(2);
514 globalString += addition;
515 return new LSL_Types.LSLString(""Hi "") + new LSL_Types.LSLString(""from "") + new LSL_Types.LSLString(""onefunc()! "") + globalString;
516 }
517 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
518 {
519 llSay(new LSL_Types.LSLInteger(2000), onefunc());
520 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(2);
521 x *= new LSL_Types.LSLInteger(3);
522 x /= new LSL_Types.LSLInteger(14) + -new LSL_Types.LSLInteger(2);
523 x %= new LSL_Types.LSLInteger(10);
524 }
525";
526
527 CSCodeGenerator cg = new CSCodeGenerator();
528 string output = cg.Convert(input);
529 Assert.AreEqual(expected, output);
530 }
531
532 [Test]
533 public void TestVectorConstantNotation()
534 {
535 string input = @"default
536{
537 touch_start(integer num_detected)
538 {
539 vector y = <1.2, llGetMeAFloat(), 4.4>;
540 rotation x = <0.1, 0.1, one + 2, 0.9>;
541
542 y = <0.1, 0.1, 1.1 - three - two+eight*8>;
543 }
544}
545";
546 string expected = @"
547 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
548 {
549 LSL_Types.Vector3 y = new LSL_Types.Vector3(new LSL_Types.LSLFloat(1.2), llGetMeAFloat(), new LSL_Types.LSLFloat(4.4));
550 LSL_Types.Quaternion x = new LSL_Types.Quaternion(new LSL_Types.LSLFloat(0.1), new LSL_Types.LSLFloat(0.1), one + new LSL_Types.LSLInteger(2), new LSL_Types.LSLFloat(0.9));
551 y = new LSL_Types.Vector3(new LSL_Types.LSLFloat(0.1), new LSL_Types.LSLFloat(0.1), new LSL_Types.LSLFloat(1.1) - three - two + eight * new LSL_Types.LSLInteger(8));
552 }
553";
554
555 CSCodeGenerator cg = new CSCodeGenerator();
556 string output = cg.Convert(input);
557 Assert.AreEqual(expected, output);
558 }
559
560 [Test]
561 public void TestVectorMemberAccess()
562 {
563 string input = @"default
564{
565 touch_start(integer num_detected)
566 {
567 vector y = <1.2, llGetMeAFloat(), 4.4>;
568 x = y.x + 1.1;
569 y.x = 1.1;
570 }
571}
572";
573 string expected = @"
574 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
575 {
576 LSL_Types.Vector3 y = new LSL_Types.Vector3(new LSL_Types.LSLFloat(1.2), llGetMeAFloat(), new LSL_Types.LSLFloat(4.4));
577 x = y.x + new LSL_Types.LSLFloat(1.1);
578 y.x = new LSL_Types.LSLFloat(1.1);
579 }
580";
581
582 CSCodeGenerator cg = new CSCodeGenerator();
583 string output = cg.Convert(input);
584 Assert.AreEqual(expected, output);
585 }
586
587 [Test]
588 public void TestExpressionInParentheses()
589 {
590 string input = @"default
591{
592 touch_start(integer num_detected)
593 {
594 integer y = -3;
595 integer x = 14 + 6;
596 y = 12 +45+20+x + (23 + 1) + x + y;
597 y = (12 + -45 + -20 + x + 23 )+ -1 + x + y;
598 }
599}
600";
601 string expected = @"
602 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
603 {
604 LSL_Types.LSLInteger y = -new LSL_Types.LSLInteger(3);
605 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(14) + new LSL_Types.LSLInteger(6);
606 y = new LSL_Types.LSLInteger(12) + new LSL_Types.LSLInteger(45) + new LSL_Types.LSLInteger(20) + x + (new LSL_Types.LSLInteger(23) + new LSL_Types.LSLInteger(1)) + x + y;
607 y = (new LSL_Types.LSLInteger(12) + -new LSL_Types.LSLInteger(45) + -new LSL_Types.LSLInteger(20) + x + new LSL_Types.LSLInteger(23)) + -new LSL_Types.LSLInteger(1) + x + y;
608 }
609";
610
611 CSCodeGenerator cg = new CSCodeGenerator();
612 string output = cg.Convert(input);
613 Assert.AreEqual(expected, output);
614 }
615
616 [Test]
617 public void TestIncrementDecrementOperator()
618 {
619 string input = @"// here we'll test the ++ and -- operators
620
621default
622{
623 touch_start(integer num_detected)
624 {
625 integer y = -3;
626 integer x = 14 + 6;
627 y = 12 +45+20+x++ + (23 + 1) + ++x + -- y;
628 y = (12 + -45 + -20 + x-- + 23 )+ -1 + x -- + ++y;
629 }
630}
631";
632 string expected = @"
633 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
634 {
635 LSL_Types.LSLInteger y = -new LSL_Types.LSLInteger(3);
636 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(14) + new LSL_Types.LSLInteger(6);
637 y = new LSL_Types.LSLInteger(12) + new LSL_Types.LSLInteger(45) + new LSL_Types.LSLInteger(20) + x++ + (new LSL_Types.LSLInteger(23) + new LSL_Types.LSLInteger(1)) + ++x + --y;
638 y = (new LSL_Types.LSLInteger(12) + -new LSL_Types.LSLInteger(45) + -new LSL_Types.LSLInteger(20) + x-- + new LSL_Types.LSLInteger(23)) + -new LSL_Types.LSLInteger(1) + x-- + ++y;
639 }
640";
641
642 CSCodeGenerator cg = new CSCodeGenerator();
643 string output = cg.Convert(input);
644 Assert.AreEqual(expected, output);
645 }
646
647 [Test]
648 public void TestLists()
649 {
650 string input = @"// testing lists
651
652default
653{
654 touch_start(integer num_detected)
655 {
656 list l = [];
657 list m = [1, two, ""three"", <4.0, 4.0, 4.0>, 5 + 5];
658 llCallSomeFunc(1, llAnotherFunc(), [1, 2, 3]);
659 }
660}
661";
662 string expected = @"
663 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
664 {
665 LSL_Types.list l = new LSL_Types.list();
666 LSL_Types.list m = new LSL_Types.list(new LSL_Types.LSLInteger(1), two, new LSL_Types.LSLString(""three""), new LSL_Types.Vector3(new LSL_Types.LSLFloat(4.0), new LSL_Types.LSLFloat(4.0), new LSL_Types.LSLFloat(4.0)), new LSL_Types.LSLInteger(5) + new LSL_Types.LSLInteger(5));
667 llCallSomeFunc(new LSL_Types.LSLInteger(1), llAnotherFunc(), new LSL_Types.list(new LSL_Types.LSLInteger(1), new LSL_Types.LSLInteger(2), new LSL_Types.LSLInteger(3)));
668 }
669";
670
671 CSCodeGenerator cg = new CSCodeGenerator();
672 string output = cg.Convert(input);
673 Assert.AreEqual(expected, output);
674 }
675
676 [Test]
677 public void TestIfStatement()
678 {
679 string input = @"// let's test if statements
680
681default
682{
683 touch_start(integer num_detected)
684 {
685 integer x = 1;
686
687 if (x) llSay(0, ""Hello"");
688 if (1)
689 {
690 llSay(0, ""Hi"");
691 integer r = 3;
692 return;
693 }
694
695 if (f(x)) llSay(0, ""f(x) is true"");
696 else llSay(0, ""f(x) is false"");
697
698 if (x + y) llSay(0, ""x + y is true"");
699 else if (y - x) llSay(0, ""y - x is true"");
700 else llSay(0, ""Who needs x and y anyway?"");
701
702 if (x * y) llSay(0, ""x * y is true"");
703 else if (y / x)
704 {
705 llSay(0, ""uh-oh, y / x is true, exiting"");
706 return;
707 }
708 else llSay(0, ""Who needs x and y anyway?"");
709
710 // and now for my last trick
711 if (x % y) llSay(0, ""x is true"");
712 else if (y & x) llSay(0, ""y is true"");
713 else if (z | x) llSay(0, ""z is true"");
714 else if (a * (b + x)) llSay(0, ""a is true"");
715 else if (b) llSay(0, ""b is true"");
716 else if (v) llSay(0, ""v is true"");
717 else llSay(0, ""Everything is lies!"");
718 }
719}
720";
721 string expected = @"
722 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
723 {
724 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);
725 if (x)
726 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""Hello""));
727 if (new LSL_Types.LSLInteger(1))
728 {
729 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""Hi""));
730 LSL_Types.LSLInteger r = new LSL_Types.LSLInteger(3);
731 return ;
732 }
733 if (f(x))
734 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""f(x) is true""));
735 else
736 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""f(x) is false""));
737 if (x + y)
738 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""x + y is true""));
739 else
740 if (y - x)
741 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""y - x is true""));
742 else
743 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""Who needs x and y anyway?""));
744 if (x * y)
745 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""x * y is true""));
746 else
747 if (y / x)
748 {
749 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""uh-oh, y / x is true, exiting""));
750 return ;
751 }
752 else
753 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""Who needs x and y anyway?""));
754 if (x % y)
755 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""x is true""));
756 else
757 if (y & x)
758 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""y is true""));
759 else
760 if (z | x)
761 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""z is true""));
762 else
763 if (a * (b + x))
764 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""a is true""));
765 else
766 if (b)
767 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""b is true""));
768 else
769 if (v)
770 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""v is true""));
771 else
772 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""Everything is lies!""));
773 }
774";
775
776 CSCodeGenerator cg = new CSCodeGenerator();
777 string output = cg.Convert(input);
778 Assert.AreEqual(expected, output);
779 }
780
781 [Test]
782 public void TestIfElseStatement()
783 {
784 string input = @"// let's test complex logical expressions
785
786default
787{
788 touch_start(integer num_detected)
789 {
790 integer x = 1;
791 integer y = 0;
792
793 if (x && y) llSay(0, ""Hello"");
794 if (x || y)
795 {
796 llSay(0, ""Hi"");
797 integer r = 3;
798 return;
799 }
800
801 if (x && y || z) llSay(0, ""x is true"");
802 else llSay(0, ""x is false"");
803
804 if (x == y) llSay(0, ""x is true"");
805 else if (y < x) llSay(0, ""y is true"");
806 else llSay(0, ""Who needs x and y anyway?"");
807
808 if (x > y) llSay(0, ""x is true"");
809 else if (y <= x)
810 {
811 llSay(0, ""uh-oh, y is true, exiting"");
812 return;
813 }
814 else llSay(0, ""Who needs x and y anyway?"");
815
816 // and now for my last trick
817 if (x >= y) llSay(0, ""x is true"");
818 else if (y != x) llSay(0, ""y is true"");
819 else if (!z) llSay(0, ""z is true"");
820 else if (!(a && b)) llSay(0, ""a is true"");
821 else if (b) llSay(0, ""b is true"");
822 else if (v) llSay(0, ""v is true"");
823 else llSay(0, ""Everything is lies!"");
824 }
825}
826";
827 string expected = @"
828 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
829 {
830 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);
831 LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);
832 if (x && y)
833 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""Hello""));
834 if (x || y)
835 {
836 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""Hi""));
837 LSL_Types.LSLInteger r = new LSL_Types.LSLInteger(3);
838 return ;
839 }
840 if (x && y || z)
841 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""x is true""));
842 else
843 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""x is false""));
844 if (x == y)
845 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""x is true""));
846 else
847 if (y < x)
848 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""y is true""));
849 else
850 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""Who needs x and y anyway?""));
851 if (x > y)
852 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""x is true""));
853 else
854 if (y <= x)
855 {
856 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""uh-oh, y is true, exiting""));
857 return ;
858 }
859 else
860 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""Who needs x and y anyway?""));
861 if (x >= y)
862 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""x is true""));
863 else
864 if (y != x)
865 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""y is true""));
866 else
867 if (!z)
868 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""z is true""));
869 else
870 if (!(a && b))
871 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""a is true""));
872 else
873 if (b)
874 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""b is true""));
875 else
876 if (v)
877 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""v is true""));
878 else
879 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""Everything is lies!""));
880 }
881";
882
883 CSCodeGenerator cg = new CSCodeGenerator();
884 string output = cg.Convert(input);
885 Assert.AreEqual(expected, output);
886 }
887
888 [Test]
889 public void TestWhileLoop()
890 {
891 string input = @"// let's test while loops
892
893default
894{
895 touch_start(integer num_detected)
896 {
897 integer x = 1;
898 integer y = 0;
899
900 while (x) llSay(0, ""To infinity, and beyond!"");
901 while (0 || (x && 0))
902 {
903 llSay(0, ""Never say never."");
904 return;
905 }
906 }
907}
908";
909 string expected = @"
910 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
911 {
912 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);
913 LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);
914 while (x)
915 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""To infinity, and beyond!""));
916 while (new LSL_Types.LSLInteger(0) || (x && new LSL_Types.LSLInteger(0)))
917 {
918 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""Never say never.""));
919 return ;
920 }
921 }
922";
923
924 CSCodeGenerator cg = new CSCodeGenerator();
925 string output = cg.Convert(input);
926 Assert.AreEqual(expected, output);
927 }
928
929 [Test]
930 public void TestDoWhileLoop()
931 {
932 string input = @"// let's test do-while loops
933
934default
935{
936 touch_start(integer num_detected)
937 {
938 integer x = 1;
939 integer y = 0;
940
941 do llSay(0, ""And we're doing..."");
942 while (x);
943
944 do
945 {
946 llSay(0, ""I like it here. I wish we could stay here forever."");
947 y--;
948 } while (y);
949 }
950}
951";
952 string expected = @"
953 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
954 {
955 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);
956 LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);
957 do
958 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""And we're doing...""));
959 while (x);
960 do
961 {
962 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""I like it here. I wish we could stay here forever.""));
963 y--;
964 }
965 while (y);
966 }
967";
968
969 CSCodeGenerator cg = new CSCodeGenerator();
970 string output = cg.Convert(input);
971 Assert.AreEqual(expected, output);
972 }
973
974 [Test]
975 public void TestForLoop()
976 {
977 string input = @"// let's test for loops
978
979default
980{
981 touch_start(integer num_detected)
982 {
983 integer x = 1;
984 integer y = 0;
985
986 for (x = 10; x >= 0; x--)
987 {
988 llOwnerSay(""Launch in T minus "" + x);
989 IncreaseRocketPower();
990 }
991
992 for (x = 0, y = 6; y > 0 && x != y; x++, y--) llOwnerSay(""Hi "" + x + "", "" + y);
993 for (x = 0, y = 6; ! y; x++,y--) llOwnerSay(""Hi "" + x + "", "" + y);
994 }
995}
996";
997 string expected = @"
998 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
999 {
1000 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);
1001 LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);
1002 for (x = new LSL_Types.LSLInteger(10); x >= new LSL_Types.LSLInteger(0); x--)
1003 {
1004 llOwnerSay(new LSL_Types.LSLString(""Launch in T minus "") + x);
1005 IncreaseRocketPower();
1006 }
1007 for (x = new LSL_Types.LSLInteger(0), y = new LSL_Types.LSLInteger(6); y > new LSL_Types.LSLInteger(0) && x != y; x++, y--)
1008 llOwnerSay(new LSL_Types.LSLString(""Hi "") + x + new LSL_Types.LSLString("", "") + y);
1009 for (x = new LSL_Types.LSLInteger(0), y = new LSL_Types.LSLInteger(6); !y; x++, y--)
1010 llOwnerSay(new LSL_Types.LSLString(""Hi "") + x + new LSL_Types.LSLString("", "") + y);
1011 }
1012";
1013
1014 CSCodeGenerator cg = new CSCodeGenerator();
1015 string output = cg.Convert(input);
1016 Assert.AreEqual(expected, output);
1017 }
1018
1019 [Test]
1020 public void TestFloatsWithTrailingDecimal()
1021 {
1022 string input = @"// a curious feature of LSL that allows floats to be defined with a trailing dot
1023
1024default
1025{
1026 touch_start(integer num_detected)
1027 {
1028 float y = 1.;
1029 y = 1.E3;
1030 y = 1.e3;
1031 y = 1.E+3;
1032 y = 1.e+3;
1033 y = 1.E-3;
1034 y = 1.e-3;
1035 y = -1.E3;
1036 y = -1.e3;
1037 y = -1.E+3;
1038 y = -1.e+3;
1039 y = -1.E-3;
1040 y = -1.e-3;
1041 y = 12. + -1.E3 - 1.e-2;
1042 vector v = <0.,0.,0.>;
1043 }
1044}
1045";
1046 string expected = @"
1047 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
1048 {
1049 LSL_Types.LSLFloat y = new LSL_Types.LSLFloat(1.0);
1050 y = new LSL_Types.LSLFloat(1.0E3);
1051 y = new LSL_Types.LSLFloat(1.0e3);
1052 y = new LSL_Types.LSLFloat(1.0E+3);
1053 y = new LSL_Types.LSLFloat(1.0e+3);
1054 y = new LSL_Types.LSLFloat(1.0E-3);
1055 y = new LSL_Types.LSLFloat(1.0e-3);
1056 y = -new LSL_Types.LSLFloat(1.0E3);
1057 y = -new LSL_Types.LSLFloat(1.0e3);
1058 y = -new LSL_Types.LSLFloat(1.0E+3);
1059 y = -new LSL_Types.LSLFloat(1.0e+3);
1060 y = -new LSL_Types.LSLFloat(1.0E-3);
1061 y = -new LSL_Types.LSLFloat(1.0e-3);
1062 y = new LSL_Types.LSLFloat(12.0) + -new LSL_Types.LSLFloat(1.0E3) - new LSL_Types.LSLFloat(1.0e-2);
1063 LSL_Types.Vector3 v = new LSL_Types.Vector3(new LSL_Types.LSLFloat(0.0), new LSL_Types.LSLFloat(0.0), new LSL_Types.LSLFloat(0.0));
1064 }
1065";
1066
1067 CSCodeGenerator cg = new CSCodeGenerator();
1068 string output = cg.Convert(input);
1069 Assert.AreEqual(expected, output);
1070 }
1071
1072 [Test]
1073 public void TestUnaryAndBinaryOperators()
1074 {
1075 string input = @"// let's test a few more operators
1076
1077default
1078{
1079 touch_start(integer num_detected)
1080 {
1081 integer x = 2;
1082 integer y = 1;
1083 integer z = x ^ y;
1084 x = ~ z;
1085 x = ~(y && z);
1086 y = x >> z;
1087 z = y << x;
1088 }
1089}
1090";
1091 string expected = @"
1092 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
1093 {
1094 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(2);
1095 LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(1);
1096 LSL_Types.LSLInteger z = x ^ y;
1097 x = ~z;
1098 x = ~(y && z);
1099 y = x >> z;
1100 z = y << x;
1101 }
1102";
1103
1104 CSCodeGenerator cg = new CSCodeGenerator();
1105 string output = cg.Convert(input);
1106 Assert.AreEqual(expected, output);
1107 }
1108
1109 [Test]
1110 public void TestTypecasts()
1111 {
1112 string input = @"// let's test typecasts
1113
1114default
1115{
1116 touch_start(integer num_detected)
1117 {
1118 string s = """";
1119 integer x = 1;
1120
1121 s = (string) x++;
1122 s = (string) x;
1123 s = (string) <0., 0., 0.>;
1124 s = (string) <1., 1., 1., 1.>;
1125 s = (integer) ""1"";
1126 s = (string) llSomethingThatReturnsInteger();
1127 s = (string) 134;
1128 s = (string) (x ^ y | (z && l)) + (string) (x + y - 13);
1129 llOwnerSay(""s is: "" + s);
1130 }
1131}
1132";
1133 string expected = @"
1134 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
1135 {
1136 LSL_Types.LSLString s = new LSL_Types.LSLString("""");
1137 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);
1138 s = (LSL_Types.LSLString) (x++);
1139 s = (LSL_Types.LSLString) (x);
1140 s = (LSL_Types.LSLString) (new LSL_Types.Vector3(new LSL_Types.LSLFloat(0.0), new LSL_Types.LSLFloat(0.0), new LSL_Types.LSLFloat(0.0)));
1141 s = (LSL_Types.LSLString) (new LSL_Types.Quaternion(new LSL_Types.LSLFloat(1.0), new LSL_Types.LSLFloat(1.0), new LSL_Types.LSLFloat(1.0), new LSL_Types.LSLFloat(1.0)));
1142 s = (LSL_Types.LSLInteger) (new LSL_Types.LSLString(""1""));
1143 s = (LSL_Types.LSLString) (llSomethingThatReturnsInteger());
1144 s = (LSL_Types.LSLString) (new LSL_Types.LSLInteger(134));
1145 s = (LSL_Types.LSLString) (x ^ y | (z && l)) + (LSL_Types.LSLString) (x + y - new LSL_Types.LSLInteger(13));
1146 llOwnerSay(new LSL_Types.LSLString(""s is: "") + s);
1147 }
1148";
1149
1150 CSCodeGenerator cg = new CSCodeGenerator();
1151 string output = cg.Convert(input);
1152 Assert.AreEqual(expected, output);
1153 }
1154
1155 [Test]
1156 public void TestStates()
1157 {
1158 string input = @"// let's test states
1159
1160default
1161{
1162 touch_start(integer num_detected)
1163 {
1164 llSay(0, ""Going to state 'statetwo'"");
1165 state statetwo;
1166 }
1167}
1168
1169state statetwo
1170{
1171 state_entry()
1172 {
1173 llSay(0, ""Going to the default state"");
1174 state default;
1175 }
1176}
1177";
1178 string expected = @"
1179 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
1180 {
1181 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""Going to state 'statetwo'""));
1182 state(""statetwo"");
1183 }
1184 public void statetwo_event_state_entry()
1185 {
1186 llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(""Going to the default state""));
1187 state(""default"");
1188 }
1189";
1190
1191 CSCodeGenerator cg = new CSCodeGenerator();
1192 string output = cg.Convert(input);
1193 Assert.AreEqual(expected, output);
1194 }
1195
1196 [Test]
1197 public void TestHexIntegerConstants()
1198 {
1199 string input = @"// let's test hex integers
1200
1201default
1202{
1203 touch_start(integer num_detected)
1204 {
1205 integer x = 0x23;
1206 integer x = 0x2f34B;
1207 integer x = 0x2F34b;
1208 integer x = 0x2F34B;
1209 integer x = 0x2f34b;
1210 }
1211}
1212";
1213 string expected = @"
1214 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
1215 {
1216 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0x23);
1217 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0x2f34B);
1218 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0x2F34b);
1219 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0x2F34B);
1220 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0x2f34b);
1221 }
1222";
1223
1224 CSCodeGenerator cg = new CSCodeGenerator();
1225 string output = cg.Convert(input);
1226 Assert.AreEqual(expected, output);
1227 }
1228
1229 [Test]
1230 public void TestJumps()
1231 {
1232 string input = @"// let's test jumps
1233
1234default
1235{
1236 touch_start(integer num_detected)
1237 {
1238 jump here;
1239 llOwnerSay(""Uh oh, the jump didn't work"");
1240 @here;
1241 llOwnerSay(""After the jump"");
1242 }
1243}
1244";
1245 string expected = @"
1246 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
1247 {
1248 goto here;
1249 llOwnerSay(new LSL_Types.LSLString(""Uh oh, the jump didn't work""));
1250 here:
1251 llOwnerSay(new LSL_Types.LSLString(""After the jump""));
1252 }
1253";
1254
1255 CSCodeGenerator cg = new CSCodeGenerator();
1256 string output = cg.Convert(input);
1257 Assert.AreEqual(expected, output);
1258 }
1259
1260 [Test]
1261 public void TestImplicitVariableInitialization()
1262 {
1263 string input = @"// let's test implicitly initializing variables
1264
1265default
1266{
1267 touch_start(integer num_detected)
1268 {
1269 integer i; integer j = 14;
1270 float f; float g = 14.0;
1271 string s; string t = ""Hi there"";
1272 list l; list m = [1, 2, 3];
1273 vector v; vector w = <1.0, 0.1, 0.5>;
1274 rotation r; rotation u = <0.8, 0.7, 0.6, llSomeFunc()>;
1275 key k; key n = ""ping"";
1276 }
1277}
1278";
1279 string expected = @"
1280 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
1281 {
1282 LSL_Types.LSLInteger i = 0;
1283 LSL_Types.LSLInteger j = new LSL_Types.LSLInteger(14);
1284 LSL_Types.LSLFloat f = 0.0;
1285 LSL_Types.LSLFloat g = new LSL_Types.LSLFloat(14.0);
1286 LSL_Types.LSLString s = """";
1287 LSL_Types.LSLString t = new LSL_Types.LSLString(""Hi there"");
1288 LSL_Types.list l = new LSL_Types.list();
1289 LSL_Types.list m = new LSL_Types.list(new LSL_Types.LSLInteger(1), new LSL_Types.LSLInteger(2), new LSL_Types.LSLInteger(3));
1290 LSL_Types.Vector3 v = new LSL_Types.Vector3(0.0, 0.0, 0.0);
1291 LSL_Types.Vector3 w = new LSL_Types.Vector3(new LSL_Types.LSLFloat(1.0), new LSL_Types.LSLFloat(0.1), new LSL_Types.LSLFloat(0.5));
1292 LSL_Types.Quaternion r = new LSL_Types.Quaternion(0.0, 0.0, 0.0, 0.0);
1293 LSL_Types.Quaternion u = new LSL_Types.Quaternion(new LSL_Types.LSLFloat(0.8), new LSL_Types.LSLFloat(0.7), new LSL_Types.LSLFloat(0.6), llSomeFunc());
1294 LSL_Types.LSLString k = """";
1295 LSL_Types.LSLString n = new LSL_Types.LSLString(""ping"");
1296 }
1297";
1298
1299 CSCodeGenerator cg = new CSCodeGenerator();
1300 string output = cg.Convert(input);
1301 Assert.AreEqual(expected, output);
1302 }
1303
1304 [Test]
1305 public void TestMultipleEqualsExpression()
1306 {
1307 string input = @"// let's test x = y = 5 type expressions
1308
1309default
1310{
1311 touch_start(integer num_detected)
1312 {
1313 integer x;
1314 integer y;
1315 x = y = 5;
1316 x += y -= 5;
1317 llOwnerSay(""x is: "" + (string) x + "", y is: "" + (string) y);
1318 }
1319}
1320";
1321 string expected = @"
1322 public void default_event_touch_start(LSL_Types.LSLInteger num_detected)
1323 {
1324 LSL_Types.LSLInteger x = 0;
1325 LSL_Types.LSLInteger y = 0;
1326 x = y = new LSL_Types.LSLInteger(5);
1327 x += y -= new LSL_Types.LSLInteger(5);
1328 llOwnerSay(new LSL_Types.LSLString(""x is: "") + (LSL_Types.LSLString) (x) + new LSL_Types.LSLString("", y is: "") + (LSL_Types.LSLString) (y));
1329 }
1330";
1331
1332 CSCodeGenerator cg = new CSCodeGenerator();
1333 string output = cg.Convert(input);
1334 Assert.AreEqual(expected, output);
1335 }
1336
1337 [Test]
1338 public void TestUnaryExpressionLastInVectorConstant()
1339 {
1340 string input = @"// let's test unary expressions some more
1341
1342default
1343{
1344 state_entry()
1345 {
1346 vector v = <x,y,-0.5>;
1347 }
1348}
1349";
1350 string expected = @"
1351 public void default_event_state_entry()
1352 {
1353 LSL_Types.Vector3 v = new LSL_Types.Vector3(x, y, -new LSL_Types.LSLFloat(0.5));
1354 }
1355";
1356
1357 CSCodeGenerator cg = new CSCodeGenerator();
1358 string output = cg.Convert(input);
1359 Assert.AreEqual(expected, output);
1360 }
1361
1362 [Test]
1363 public void TestVectorMemberPlusEquals()
1364 {
1365 string input = @"// let's test unary expressions some more
1366
1367default
1368{
1369 state_entry()
1370 {
1371 vector v = llGetPos();
1372 v.z += 4;
1373 v.z -= 4;
1374 v.z *= 4;
1375 v.z /= 4;
1376 v.z %= 4;
1377 }
1378}
1379";
1380 string expected = @"
1381 public void default_event_state_entry()
1382 {
1383 LSL_Types.Vector3 v = llGetPos();
1384 v.z += new LSL_Types.LSLInteger(4);
1385 v.z -= new LSL_Types.LSLInteger(4);
1386 v.z *= new LSL_Types.LSLInteger(4);
1387 v.z /= new LSL_Types.LSLInteger(4);
1388 v.z %= new LSL_Types.LSLInteger(4);
1389 }
1390";
1391
1392 CSCodeGenerator cg = new CSCodeGenerator();
1393 string output = cg.Convert(input);
1394 Assert.AreEqual(expected, output);
1395 }
1396
1397 [Test]
1398 public void TestWhileLoopWithNoBody()
1399 {
1400 string input = @"default
1401{
1402 state_entry()
1403 {
1404 while (1<0);
1405 }
1406}";
1407
1408 string expected = @"
1409 public void default_event_state_entry()
1410 {
1411 while (new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0))
1412 ;
1413 }
1414";
1415
1416 CSCodeGenerator cg = new CSCodeGenerator();
1417 string output = cg.Convert(input);
1418 Assert.AreEqual(expected, output);
1419 }
1420
1421 [Test]
1422 public void TestDoWhileLoopWithNoBody()
1423 {
1424 string input = @"default
1425{
1426 state_entry()
1427 {
1428 do;
1429 while (1<0);
1430 }
1431}";
1432
1433 string expected = @"
1434 public void default_event_state_entry()
1435 {
1436 do
1437 ;
1438 while (new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0));
1439 }
1440";
1441
1442 CSCodeGenerator cg = new CSCodeGenerator();
1443 string output = cg.Convert(input);
1444 Assert.AreEqual(expected, output);
1445 }
1446
1447 [Test]
1448 public void TestIfWithNoBody()
1449 {
1450 string input = @"default
1451{
1452 state_entry()
1453 {
1454 if (1<0);
1455 }
1456}";
1457
1458 string expected = @"
1459 public void default_event_state_entry()
1460 {
1461 if (new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0))
1462 ;
1463 }
1464";
1465
1466 CSCodeGenerator cg = new CSCodeGenerator();
1467 string output = cg.Convert(input);
1468 Assert.AreEqual(expected, output);
1469 }
1470
1471 [Test]
1472 public void TestIfElseWithNoBody()
1473 {
1474 string input = @"default
1475{
1476 state_entry()
1477 {
1478 if (1<0);
1479 else;
1480 }
1481}";
1482
1483 string expected = @"
1484 public void default_event_state_entry()
1485 {
1486 if (new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0))
1487 ;
1488 else
1489 ;
1490 }
1491";
1492
1493 CSCodeGenerator cg = new CSCodeGenerator();
1494 string output = cg.Convert(input);
1495 Assert.AreEqual(expected, output);
1496 }
1497
1498 [Test]
1499 public void TestForLoopWithNoBody()
1500 {
1501 string input = @"default
1502{
1503 state_entry()
1504 {
1505 for (x = 4; 1<0; x += 2);
1506 }
1507}";
1508
1509 string expected = @"
1510 public void default_event_state_entry()
1511 {
1512 for (x = new LSL_Types.LSLInteger(4); new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0); x += new LSL_Types.LSLInteger(2))
1513 ;
1514 }
1515";
1516
1517 CSCodeGenerator cg = new CSCodeGenerator();
1518 string output = cg.Convert(input);
1519 Assert.AreEqual(expected, output);
1520 }
1521
1522 [Test]
1523 public void TestAssignmentInIfWhileDoWhile()
1524 {
1525 string input = @"default
1526{
1527 state_entry()
1528 {
1529 integer x;
1530
1531 while (x = 14) llOwnerSay(""x is: "" + (string) x);
1532
1533 if (x = 24) llOwnerSay(""x is: "" + (string) x);
1534
1535 do
1536 llOwnerSay(""x is: "" + (string) x);
1537 while (x = 44);
1538 }
1539}";
1540
1541 string expected = @"
1542 public void default_event_state_entry()
1543 {
1544 LSL_Types.LSLInteger x = 0;
1545 while (x = new LSL_Types.LSLInteger(14))
1546 llOwnerSay(new LSL_Types.LSLString(""x is: "") + (LSL_Types.LSLString) (x));
1547 if (x = new LSL_Types.LSLInteger(24))
1548 llOwnerSay(new LSL_Types.LSLString(""x is: "") + (LSL_Types.LSLString) (x));
1549 do
1550 llOwnerSay(new LSL_Types.LSLString(""x is: "") + (LSL_Types.LSLString) (x));
1551 while (x = new LSL_Types.LSLInteger(44));
1552 }
1553";
1554
1555 CSCodeGenerator cg = new CSCodeGenerator();
1556 string output = cg.Convert(input);
1557 Assert.AreEqual(expected, output);
1558 }
1559
1560 [Test]
1561 public void TestLSLListHack()
1562 {
1563 string input = @"default
1564{
1565 state_entry()
1566 {
1567 list l = [""hello""];
1568 l = (l=[]) + l + ""world"";
1569 }
1570}";
1571
1572 string expected = @"
1573 public void default_event_state_entry()
1574 {
1575 LSL_Types.list l = new LSL_Types.list(new LSL_Types.LSLString(""hello""));
1576 l = (l = new LSL_Types.list()) + l + new LSL_Types.LSLString(""world"");
1577 }
1578";
1579
1580 CSCodeGenerator cg = new CSCodeGenerator();
1581 string output = cg.Convert(input);
1582 Assert.AreEqual(expected, output);
1583 }
1584
1585 [Test]
1586 [ExpectedException("Tools.CSToolsException")]
1587 public void TestSyntaxError()
1588 {
1589 string input = @"default
1590{
1591 state_entry()
1592 {
1593 integer y
1594 }
1595}
1596";
1597 try
1598 {
1599 CSCodeGenerator cg = new CSCodeGenerator();
1600 cg.Convert(input);
1601 }
1602 catch (Tools.CSToolsException e)
1603 {
1604 // The syntax error is on line 6, char 5 (expected ';', found
1605 // '}').
1606 Regex r = new Regex("Line ([0-9]+), char ([0-9]+)");
1607 Match m = r.Match(e.Message);
1608 Assert.AreEqual("6", m.Groups[1].Value);
1609 Assert.AreEqual("5", m.Groups[2].Value);
1610
1611 throw;
1612 }
1613 }
1614
1615 [Test]
1616 [ExpectedException("Tools.CSToolsException")]
1617 public void TestSyntaxErrorDeclaringVariableInForLoop()
1618 {
1619 string input = @"default
1620{
1621 state_entry()
1622 {
1623 for (integer x = 0; x < 10; x++) llOwnerSay(""x is: "" + (string) x);
1624 }
1625}
1626";
1627 try
1628 {
1629 CSCodeGenerator cg = new CSCodeGenerator();
1630 cg.Convert(input);
1631 }
1632 catch (Tools.CSToolsException e)
1633 {
1634 // The syntax error is on line 5, char 14 (Syntax error)
1635 Regex r = new Regex("Line ([0-9]+), char ([0-9]+)");
1636 Match m = r.Match(e.Message);
1637 Assert.AreEqual("5", m.Groups[1].Value);
1638 Assert.AreEqual("14", m.Groups[2].Value);
1639
1640 throw;
1641 }
1642 }
1643 }
1644}
diff --git a/OpenSim/Tests/Region/ScriptEngine/Shared/CodeTools/CompilerTest.cs b/OpenSim/Tests/Region/ScriptEngine/Shared/CodeTools/CompilerTest.cs
new file mode 100644
index 0000000..7725d8d
--- /dev/null
+++ b/OpenSim/Tests/Region/ScriptEngine/Shared/CodeTools/CompilerTest.cs
@@ -0,0 +1,153 @@
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.IO;
29using System.CodeDom.Compiler;
30using System.Collections.Generic;
31using Microsoft.CSharp;
32using NUnit.Framework;
33using OpenSim.Region.ScriptEngine.Shared.CodeTools;
34
35namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests
36{
37 /// <summary>
38 /// Tests the LSL compiler. Among other things, test that error messages
39 /// generated by the C# compiler can be mapped to prper lines/columns in
40 /// the LSL source.
41 /// </summary>
42 [TestFixture]
43 public class CompilerTest
44 {
45 private string m_testDir;
46 private CSharpCodeProvider m_CSCodeProvider;
47 private CompilerParameters m_compilerParameters;
48 private CompilerResults m_compilerResults;
49
50 /// <summary>
51 /// Creates a temporary directory where build artifacts are stored.
52 /// </summary>
53 [TestFixtureSetUp]
54 public void Init()
55 {
56 m_testDir = Path.Combine(Path.GetTempPath(), "opensim_compilerTest_" + Path.GetRandomFileName());
57
58 if (!Directory.Exists(m_testDir))
59 {
60 // Create the temporary directory for housing build artifacts.
61 Directory.CreateDirectory(m_testDir);
62 }
63
64 // Create a CSCodeProvider and CompilerParameters.
65 m_CSCodeProvider = new CSharpCodeProvider();
66 m_compilerParameters = new CompilerParameters();
67
68 string rootPath = Path.Combine(Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory), "bin");
69 m_compilerParameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.dll"));
70 m_compilerParameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.Api.Runtime.dll"));
71 m_compilerParameters.GenerateExecutable = false;
72 }
73
74 /// <summary>
75 /// Removes the temporary build directory and any build artifacts
76 /// inside it.
77 /// </summary>
78 [TestFixtureTearDown]
79 public void CleanUp()
80 {
81 if (Directory.Exists(m_testDir))
82 {
83 // Blow away the temporary directory with artifacts.
84 Directory.Delete(m_testDir, true);
85 }
86 }
87
88 /// <summary>
89 /// Test the C# compiler error message can be mapped to the correct
90 /// line/column in the LSL source when an undeclared variable is used.
91 /// </summary>
92 //[Test]
93 public void TestUseUndeclaredVariable()
94 {
95 m_compilerParameters.OutputAssembly = Path.Combine(m_testDir, Path.GetRandomFileName() + ".dll");
96
97 string input = @"default
98{
99 state_entry()
100 {
101 integer y = x + 3;
102 }
103}";
104
105 CSCodeGenerator cg = new CSCodeGenerator();
106 string output = "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\n" +
107 "namespace SecondLife { " +
108 "public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass {\n" +
109 "public Script() { } " +
110 cg.Convert(input) +
111 "} }\n";
112 Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> positionMap = cg.PositionMap;
113
114 m_compilerResults = m_CSCodeProvider.CompileAssemblyFromSource(m_compilerParameters, output);
115
116 Assert.AreEqual(new KeyValuePair<int, int>(5, 21),
117 positionMap[new KeyValuePair<int, int>(m_compilerResults.Errors[0].Line, m_compilerResults.Errors[0].Column)]);
118 }
119
120 /// <summary>
121 /// Test that a string can be cast to string and another string
122 /// concatenated.
123 /// </summary>
124 //[Test]
125 public void TestCastAndConcatString()
126 {
127 m_compilerParameters.OutputAssembly = Path.Combine(m_testDir, Path.GetRandomFileName() + ".dll");
128
129 string input = @"string s = "" a string"";
130
131default
132{
133 state_entry()
134 {
135 key gAvatarKey = llDetectedKey(0);
136 string tmp = (string) gAvatarKey + s;
137 llSay(0, tmp);
138 }
139}";
140
141 CSCodeGenerator cg = new CSCodeGenerator();
142 string output = "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\n" +
143 "namespace SecondLife { " +
144 "public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass {\n" +
145 "public Script() { } " +
146 cg.Convert(input) +
147 "} }\n";
148 m_compilerResults = m_CSCodeProvider.CompileAssemblyFromSource(m_compilerParameters, output);
149
150 Assert.AreEqual(0, m_compilerResults.Errors.Count);
151 }
152 }
153}