123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707 |
- /*
- * Copyright (c) Contributors, http://opensimulator.org/
- * See CONTRIBUTORS.TXT for a full list of copyright holders.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the OpenSimulator Project nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
- using System.Collections.Generic;
- using System.Text.RegularExpressions;
- using NUnit.Framework;
- using OpenSim.Region.ScriptEngine.Shared.CodeTools;
- namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests
- {
- /// <summary>
- /// Tests the LSL compiler, both the code generation and transformation.
- /// Each test has some LSL code as input and C# code as expected output.
- /// The generated C# code is compared against the expected C# code.
- /// </summary>
- [TestFixture]
- public class CSCodeGeneratorTest
- {
- [Test]
- public void TestDefaultState()
- {
- string input = @"default
- {
- state_entry()
- {
- }
- }
- ";
- string expected =
- "\n public void default_event_state_entry()" +
- "\n {" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestCustomState()
- {
- string input = @"default
- {
- state_entry()
- {
- }
- }
- state another_state
- {
- no_sensor()
- {
- }
- }
- ";
- string expected =
- "\n public void default_event_state_entry()" +
- "\n {" +
- "\n }" +
- "\n public void another_state_event_no_sensor()" +
- "\n {" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestEventWithArguments()
- {
- string input = @"default
- {
- at_rot_target(integer tnum, rotation targetrot, rotation ourrot)
- {
- }
- }
- ";
- string expected =
- "\n public void default_event_at_rot_target(LSL_Types.LSLInteger tnum, LSL_Types.Quaternion targetrot, LSL_Types.Quaternion ourrot)" +
- "\n {" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestIntegerDeclaration()
- {
- string input = @"default
- {
- touch_start(integer num_detected)
- {
- integer x;
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0);" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestLoneIdent()
- {
- // A lone ident should be removed completely as it's an error in C#
- // (MONO at least).
- string input = @"default
- {
- touch_start(integer num_detected)
- {
- integer x;
- x;
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0);" +
- "\n ;" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestAssignments()
- {
- string input = @"default
- {
- touch_start(integer num_detected)
- {
- string y;
- integer x = 14;
- y = ""Hello"";
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLString y = new LSL_Types.LSLString(\"\");" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(14);" +
- "\n y = new LSL_Types.LSLString(\"Hello\");" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestAdditionSubtractionOperator()
- {
- string input = @"default
- {
- touch_start(integer num_detected)
- {
- integer y = -3;
- integer x = 14 + 6;
- y = 12 +45+20+x + 23 + 1 + x + y;
- y = 12 + -45 + - 20 + x + 23 + -1 + x + y;
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)\n" +
- " {\n" +
- " LSL_Types.LSLInteger y = -new LSL_Types.LSLInteger(3);\n" +
- " LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(14) + new LSL_Types.LSLInteger(6);\n" +
- " 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;\n" +
- " 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;\n" +
- " }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestStrings()
- {
- string input = @"default
- {
- touch_start(integer num_detected)
- {
- llOwnerSay(""Testing, 1, 2, 3"");
- llSay(0, ""I can hear you!"");
- some_custom_function(1, 2, 3 +x, 4, ""five"", ""arguments"");
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n llOwnerSay(new LSL_Types.LSLString(\"Testing, 1, 2, 3\"));" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"I can hear you!\"));" +
- "\n 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\"));" +
- "\n }" +
- "\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestBinaryExpression()
- {
- string input = @"default
- {
- touch_start(integer num_detected)
- {
- integer y;
- integer x = 14 + 6;
- y = 12 - 3;
- y = 12 && 3;
- y = 12 || 3;
- y = 12 * 3;
- y = 12 / 3;
- y = 12 | 3;
- y = 12 & 3;
- y = 12 % 3;
- y = 12 + 45 - 20 * x / 23 | 1 & x + y;
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(14) + new LSL_Types.LSLInteger(6);" +
- "\n y = new LSL_Types.LSLInteger(12) - new LSL_Types.LSLInteger(3);" +
- "\n y = ((bool)(new LSL_Types.LSLInteger(12))) & ((bool)(new LSL_Types.LSLInteger(3)));" +
- "\n y = ((bool)(new LSL_Types.LSLInteger(12))) | ((bool)(new LSL_Types.LSLInteger(3)));" +
- "\n y = new LSL_Types.LSLInteger(12) * new LSL_Types.LSLInteger(3);" +
- "\n y = new LSL_Types.LSLInteger(12) / new LSL_Types.LSLInteger(3);" +
- "\n y = new LSL_Types.LSLInteger(12) | new LSL_Types.LSLInteger(3);" +
- "\n y = new LSL_Types.LSLInteger(12) & new LSL_Types.LSLInteger(3);" +
- "\n y = new LSL_Types.LSLInteger(12) % new LSL_Types.LSLInteger(3);" +
- "\n 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;" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestFloatConstants()
- {
- string input = @"default
- {
- touch_start(integer num_detected)
- {
- float y = 1.1;
- y = 1.123E3;
- y = 1.123e3;
- y = 1.123E+3;
- y = 1.123e+3;
- y = 1.123E-3;
- y = 1.123e-3;
- y = .4;
- y = -1.123E3;
- y = -1.123e3;
- y = -1.123E+3;
- y = -1.123e+3;
- y = -1.123E-3;
- y = -1.123e-3;
- y = -.4;
- y = 12.3 + -1.45E3 - 1.20e-2;
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLFloat y = new LSL_Types.LSLFloat(1.1);" +
- "\n y = new LSL_Types.LSLFloat(1.123E3);" +
- "\n y = new LSL_Types.LSLFloat(1.123e3);" +
- "\n y = new LSL_Types.LSLFloat(1.123E+3);" +
- "\n y = new LSL_Types.LSLFloat(1.123e+3);" +
- "\n y = new LSL_Types.LSLFloat(1.123E-3);" +
- "\n y = new LSL_Types.LSLFloat(1.123e-3);" +
- "\n y = new LSL_Types.LSLFloat(.4);" +
- "\n y = -new LSL_Types.LSLFloat(1.123E3);" +
- "\n y = -new LSL_Types.LSLFloat(1.123e3);" +
- "\n y = -new LSL_Types.LSLFloat(1.123E+3);" +
- "\n y = -new LSL_Types.LSLFloat(1.123e+3);" +
- "\n y = -new LSL_Types.LSLFloat(1.123E-3);" +
- "\n y = -new LSL_Types.LSLFloat(1.123e-3);" +
- "\n y = -new LSL_Types.LSLFloat(.4);" +
- "\n y = new LSL_Types.LSLFloat(12.3) + -new LSL_Types.LSLFloat(1.45E3) - new LSL_Types.LSLFloat(1.20e-2);" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestComments()
- {
- string input = @"// this test tests comments
- default
- {
- touch_start(integer num_detected) // this should be stripped
- {
- // fill in code here...
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestStringsWithEscapedQuotesAndComments()
- {
- string input = @"// this test tests strings, with escaped quotes and comments in strings
- default
- {
- touch_start(integer num_detected)
- {
- string s1 = ""this is a string."";
- string s2 = ""this is a string ""+""with an escaped \"" inside it."";
- s1 = s2+"" and this ""+""is a string with // comments."";
- string onemore = ""[\^@]"";
- string multiline = ""Good evening Sir,
- my name is Steve.
- I come from a rough area.
- I used to be addicted to crack
- but now I am off it and trying to stay clean.
- That is why I am selling magazine subscriptions.""; // http://www.imdb.com/title/tt0151804/quotes
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLString s1 = new LSL_Types.LSLString(\"this is a string.\");" +
- "\n LSL_Types.LSLString s2 = new LSL_Types.LSLString(\"this is a string \") + new LSL_Types.LSLString(\"with an escaped \\\" inside it.\");" +
- "\n s1 = s2 + new LSL_Types.LSLString(\" and this \") + new LSL_Types.LSLString(\"is a string with // comments.\");" +
- "\n LSL_Types.LSLString onemore = new LSL_Types.LSLString(\"[\\^@]\");" +
- "\n 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.\");" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestCStyleComments()
- {
- string input = @"/* this test tests comments
- of the C variety
- */
- default
- {
- touch_start(integer /* you can't see me! */ num_detected) /* this should be stripped */
- {
- /*
- * fill
- * in
- * code
- * here...
- */
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestGlobalDefinedFunctions()
- {
- string input = @"// this test tests custom defined functions
- string onefunc()
- {
- return ""Hi from onefunc()!"";
- }
- twofunc(string s)
- {
- llSay(1000, s);
- }
- default
- {
- touch_start(integer num_detected)
- {
- llSay(2000, onefunc());
- twofunc();
- }
- }
- ";
- string expected =
- "\n LSL_Types.LSLString onefunc()" +
- "\n {" +
- "\n return new LSL_Types.LSLString(\"Hi from onefunc()!\");" +
- "\n }" +
- "\n void twofunc(LSL_Types.LSLString s)" +
- "\n {" +
- "\n llSay(new LSL_Types.LSLInteger(1000), s);" +
- "\n }" +
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n llSay(new LSL_Types.LSLInteger(2000), onefunc());" +
- "\n twofunc();" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestGlobalDeclaredVariables()
- {
- string input = @"// this test tests custom defined functions and global variables
- string globalString;
- integer globalInt = 14;
- integer anotherGlobal = 20 * globalInt;
- string onefunc()
- {
- globalString = "" ...and the global!"";
- return ""Hi "" +
- ""from "" +
- ""onefunc()!"" + globalString;
- }
- twofunc(string s)
- {
- llSay(1000, s);
- }
- default
- {
- touch_start(integer num_detected)
- {
- llSay(2000, onefunc());
- twofunc();
- }
- }
- ";
- string expected =
- "\n LSL_Types.LSLString globalString = new LSL_Types.LSLString(\"\");" +
- "\n LSL_Types.LSLInteger globalInt = new LSL_Types.LSLInteger(14);" +
- "\n LSL_Types.LSLInteger anotherGlobal = new LSL_Types.LSLInteger(20) * globalInt;" +
- "\n LSL_Types.LSLString onefunc()" +
- "\n {" +
- "\n globalString = new LSL_Types.LSLString(\" ...and the global!\");" +
- "\n return new LSL_Types.LSLString(\"Hi \") + new LSL_Types.LSLString(\"from \") + new LSL_Types.LSLString(\"onefunc()!\") + globalString;" +
- "\n }" +
- "\n void twofunc(LSL_Types.LSLString s)" +
- "\n {" +
- "\n llSay(new LSL_Types.LSLInteger(1000), s);" +
- "\n }" +
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n llSay(new LSL_Types.LSLInteger(2000), onefunc());" +
- "\n twofunc();" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestMoreAssignments()
- {
- string input = @"// this test tests +=, -=, *=, /=, %=
- string globalString;
- integer globalInt = 14;
- string onefunc(string addition)
- {
- globalInt -= 2;
- globalString += addition;
- return ""Hi "" +
- ""from "" +
- ""onefunc()! "" + globalString;
- }
- default
- {
- touch_start(integer num_detected)
- {
- llSay(2000, onefunc());
- integer x = 2;
- x *= 3;
- x /= 14 + -2;
- x %= 10;
- }
- }
- ";
- string expected =
- "\n LSL_Types.LSLString globalString = new LSL_Types.LSLString(\"\");" +
- "\n LSL_Types.LSLInteger globalInt = new LSL_Types.LSLInteger(14);" +
- "\n LSL_Types.LSLString onefunc(LSL_Types.LSLString addition)" +
- "\n {" +
- "\n globalInt -= new LSL_Types.LSLInteger(2);" +
- "\n globalString += addition;" +
- "\n return new LSL_Types.LSLString(\"Hi \") + new LSL_Types.LSLString(\"from \") + new LSL_Types.LSLString(\"onefunc()! \") + globalString;" +
- "\n }" +
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n llSay(new LSL_Types.LSLInteger(2000), onefunc());" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(2);" +
- "\n x *= new LSL_Types.LSLInteger(3);" +
- "\n x /= new LSL_Types.LSLInteger(14) + -new LSL_Types.LSLInteger(2);" +
- "\n x %= new LSL_Types.LSLInteger(10);" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestVectorConstantNotation()
- {
- string input = @"default
- {
- touch_start(integer num_detected)
- {
- vector y = <1.2, llGetMeAFloat(), 4.4>;
- rotation x = <0.1, 0.1, one + 2, 0.9>;
- y = <0.1, 0.1, 1.1 - three - two+eight*8>;
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.Vector3 y = new LSL_Types.Vector3(new LSL_Types.LSLFloat(1.2), llGetMeAFloat(), new LSL_Types.LSLFloat(4.4));" +
- "\n 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));" +
- "\n 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));" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestVectorMemberAccess()
- {
- string input = @"default
- {
- touch_start(integer num_detected)
- {
- vector y = <1.2, llGetMeAFloat(), 4.4>;
- x = y.x + 1.1;
- y.x = 1.1;
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.Vector3 y = new LSL_Types.Vector3(new LSL_Types.LSLFloat(1.2), llGetMeAFloat(), new LSL_Types.LSLFloat(4.4));" +
- "\n x = y.x + new LSL_Types.LSLFloat(1.1);" +
- "\n y.x = new LSL_Types.LSLFloat(1.1);" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestExpressionInParentheses()
- {
- string input = @"default
- {
- touch_start(integer num_detected)
- {
- integer y = -3;
- integer x = 14 + 6;
- y = 12 +45+20+x + (23 + 1) + x + y;
- y = (12 + -45 + -20 + x + 23 )+ -1 + x + y;
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLInteger y = -new LSL_Types.LSLInteger(3);" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(14) + new LSL_Types.LSLInteger(6);" +
- "\n 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;" +
- "\n 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;" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestIncrementDecrementOperator()
- {
- string input = @"// here we'll test the ++ and -- operators
- default
- {
- touch_start(integer num_detected)
- {
- integer y = -3;
- integer x = 14 + 6;
- y = 12 +45+20+x++ + (23 + 1) + ++x + -- y;
- y = (12 + -45 + -20 + x-- + 23 )+ -1 + x -- + ++y;
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLInteger y = -new LSL_Types.LSLInteger(3);" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(14) + new LSL_Types.LSLInteger(6);" +
- "\n 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;" +
- "\n 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;" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestLists()
- {
- string input = @"// testing lists
- default
- {
- touch_start(integer num_detected)
- {
- list l = [];
- list m = [1, two, ""three"", <4.0, 4.0, 4.0>, 5 + 5];
- llCallSomeFunc(1, llAnotherFunc(), [1, 2, 3]);
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.list l = new LSL_Types.list();" +
- "\n 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));" +
- "\n 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)));" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestIfStatement()
- {
- string input = @"// let's test if statements
- default
- {
- touch_start(integer num_detected)
- {
- integer x = 1;
- if (x) llSay(0, ""Hello"");
- if (1)
- {
- llSay(0, ""Hi"");
- integer r = 3;
- return;
- }
- if (f(x)) llSay(0, ""f(x) is true"");
- else llSay(0, ""f(x) is false"");
- if (x + y) llSay(0, ""x + y is true"");
- else if (y - x) llSay(0, ""y - x is true"");
- else llSay(0, ""Who needs x and y anyway?"");
- if (x * y) llSay(0, ""x * y is true"");
- else if (y / x)
- {
- llSay(0, ""uh-oh, y / x is true, exiting"");
- return;
- }
- else llSay(0, ""Who needs x and y anyway?"");
- // and now for my last trick
- if (x % y) llSay(0, ""x is true"");
- else if (y & x) llSay(0, ""y is true"");
- else if (z | x) llSay(0, ""z is true"");
- else if (a * (b + x)) llSay(0, ""a is true"");
- else if (b) llSay(0, ""b is true"");
- else if (v) llSay(0, ""v is true"");
- else llSay(0, ""Everything is lies!"");
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
- "\n if (x)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Hello\"));" +
- "\n if (new LSL_Types.LSLInteger(1))" +
- "\n {" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Hi\"));" +
- "\n LSL_Types.LSLInteger r = new LSL_Types.LSLInteger(3);" +
- "\n return ;" +
- "\n }" +
- "\n if (f(x))" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"f(x) is true\"));" +
- "\n else" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"f(x) is false\"));" +
- "\n if (x + y)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x + y is true\"));" +
- "\n else" +
- "\n if (y - x)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"y - x is true\"));" +
- "\n else" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Who needs x and y anyway?\"));" +
- "\n if (x * y)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x * y is true\"));" +
- "\n else" +
- "\n if (y / x)" +
- "\n {" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"uh-oh, y / x is true, exiting\"));" +
- "\n return ;" +
- "\n }" +
- "\n else" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Who needs x and y anyway?\"));" +
- "\n if (x % y)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is true\"));" +
- "\n else" +
- "\n if (y & x)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"y is true\"));" +
- "\n else" +
- "\n if (z | x)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"z is true\"));" +
- "\n else" +
- "\n if (a * (b + x))" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"a is true\"));" +
- "\n else" +
- "\n if (b)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"b is true\"));" +
- "\n else" +
- "\n if (v)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"v is true\"));" +
- "\n else" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Everything is lies!\"));" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestIfElseStatement()
- {
- string input = @"// let's test complex logical expressions
- default
- {
- touch_start(integer num_detected)
- {
- integer x = 1;
- integer y = 0;
- if (x && y) llSay(0, ""Hello"");
- if (x || y)
- {
- llSay(0, ""Hi"");
- integer r = 3;
- return;
- }
- if (x && y || z) llSay(0, ""x is true"");
- else llSay(0, ""x is false"");
- if (x == y) llSay(0, ""x is true"");
- else if (y < x) llSay(0, ""y is true"");
- else llSay(0, ""Who needs x and y anyway?"");
- if (x > y) llSay(0, ""x is true"");
- else if (y <= x)
- {
- llSay(0, ""uh-oh, y is true, exiting"");
- return;
- }
- else llSay(0, ""Who needs x and y anyway?"");
- // and now for my last trick
- if (x >= y) llSay(0, ""x is true"");
- else if (y != x) llSay(0, ""y is true"");
- else if (!z) llSay(0, ""z is true"");
- else if (!(a && b)) llSay(0, ""a is true"");
- else if (b) llSay(0, ""b is true"");
- else if (v) llSay(0, ""v is true"");
- else llSay(0, ""Everything is lies!"");
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
- "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);" +
- "\n if (((bool)(x)) & ((bool)(y)))" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Hello\"));" +
- "\n if (((bool)(x)) | ((bool)(y)))" +
- "\n {" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Hi\"));" +
- "\n LSL_Types.LSLInteger r = new LSL_Types.LSLInteger(3);" +
- "\n return ;" +
- "\n }" +
- "\n if (((bool)(((bool)(x)) & ((bool)(y)))) | ((bool)(z)))" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is true\"));" +
- "\n else" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is false\"));" +
- "\n if (x == y)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is true\"));" +
- "\n else" +
- "\n if (y < x)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"y is true\"));" +
- "\n else" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Who needs x and y anyway?\"));" +
- "\n if (x > y)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is true\"));" +
- "\n else" +
- "\n if (y <= x)" +
- "\n {" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"uh-oh, y is true, exiting\"));" +
- "\n return ;" +
- "\n }" +
- "\n else" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Who needs x and y anyway?\"));" +
- "\n if (x >= y)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is true\"));" +
- "\n else" +
- "\n if (y != x)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"y is true\"));" +
- "\n else" +
- "\n if (!z)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"z is true\"));" +
- "\n else" +
- "\n if (!(((bool)(a)) & ((bool)(b))))" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"a is true\"));" +
- "\n else" +
- "\n if (b)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"b is true\"));" +
- "\n else" +
- "\n if (v)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"v is true\"));" +
- "\n else" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Everything is lies!\"));" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestWhileLoop()
- {
- string input = @"// let's test while loops
- default
- {
- touch_start(integer num_detected)
- {
- integer x = 1;
- integer y = 0;
- while (x) llSay(0, ""To infinity, and beyond!"");
- while (0 || (x && 0))
- {
- llSay(0, ""Never say never."");
- return;
- }
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
- "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);" +
- "\n while (x)" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"To infinity, and beyond!\"));" +
- "\n while (((bool)(new LSL_Types.LSLInteger(0))) | ((bool)((((bool)(x)) & ((bool)(new LSL_Types.LSLInteger(0)))))))" +
- "\n {" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Never say never.\"));" +
- "\n return ;" +
- "\n }" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestDoWhileLoop()
- {
- string input = @"// let's test do-while loops
- default
- {
- touch_start(integer num_detected)
- {
- integer x = 1;
- integer y = 0;
- do llSay(0, ""And we're doing..."");
- while (x);
- do
- {
- llSay(0, ""I like it here. I wish we could stay here forever."");
- y--;
- } while (y);
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
- "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);" +
- "\n do" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"And we're doing...\"));" +
- "\n while (x);" +
- "\n do" +
- "\n {" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"I like it here. I wish we could stay here forever.\"));" +
- "\n y--;" +
- "\n }" +
- "\n while (y);" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestForLoop()
- {
- string input = @"// let's test for loops
- default
- {
- touch_start(integer num_detected)
- {
- integer x = 1;
- integer y = 0;
- for (x = 10; x >= 0; x--)
- {
- llOwnerSay(""Launch in T minus "" + x);
- IncreaseRocketPower();
- }
- for (x = 0, y = 6; y > 0 && x != y; x++, y--) llOwnerSay(""Hi "" + x + "", "" + y);
- for (x = 0, y = 6; ! y; x++,y--) llOwnerSay(""Hi "" + x + "", "" + y);
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
- "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);" +
- "\n for (x = new LSL_Types.LSLInteger(10); x >= new LSL_Types.LSLInteger(0); x--)" +
- "\n {" +
- "\n llOwnerSay(new LSL_Types.LSLString(\"Launch in T minus \") + x);" +
- "\n IncreaseRocketPower();" +
- "\n }" +
- "\n for (x = new LSL_Types.LSLInteger(0), y = new LSL_Types.LSLInteger(6); ((bool)(y > new LSL_Types.LSLInteger(0))) & ((bool)(x != y)); x++, y--)" +
- "\n llOwnerSay(new LSL_Types.LSLString(\"Hi \") + x + new LSL_Types.LSLString(\", \") + y);" +
- "\n for (x = new LSL_Types.LSLInteger(0), y = new LSL_Types.LSLInteger(6); !y; x++, y--)" +
- "\n llOwnerSay(new LSL_Types.LSLString(\"Hi \") + x + new LSL_Types.LSLString(\", \") + y);" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestFloatsWithTrailingDecimal()
- {
- string input = @"// a curious feature of LSL that allows floats to be defined with a trailing dot
- default
- {
- touch_start(integer num_detected)
- {
- float y = 1.;
- y = 1.E3;
- y = 1.e3;
- y = 1.E+3;
- y = 1.e+3;
- y = 1.E-3;
- y = 1.e-3;
- y = -1.E3;
- y = -1.e3;
- y = -1.E+3;
- y = -1.e+3;
- y = -1.E-3;
- y = -1.e-3;
- y = 12. + -1.E3 - 1.e-2;
- vector v = <0.,0.,0.>;
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLFloat y = new LSL_Types.LSLFloat(1.0);" +
- "\n y = new LSL_Types.LSLFloat(1.0E3);" +
- "\n y = new LSL_Types.LSLFloat(1.0e3);" +
- "\n y = new LSL_Types.LSLFloat(1.0E+3);" +
- "\n y = new LSL_Types.LSLFloat(1.0e+3);" +
- "\n y = new LSL_Types.LSLFloat(1.0E-3);" +
- "\n y = new LSL_Types.LSLFloat(1.0e-3);" +
- "\n y = -new LSL_Types.LSLFloat(1.0E3);" +
- "\n y = -new LSL_Types.LSLFloat(1.0e3);" +
- "\n y = -new LSL_Types.LSLFloat(1.0E+3);" +
- "\n y = -new LSL_Types.LSLFloat(1.0e+3);" +
- "\n y = -new LSL_Types.LSLFloat(1.0E-3);" +
- "\n y = -new LSL_Types.LSLFloat(1.0e-3);" +
- "\n y = new LSL_Types.LSLFloat(12.0) + -new LSL_Types.LSLFloat(1.0E3) - new LSL_Types.LSLFloat(1.0e-2);" +
- "\n 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));" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestUnaryAndBinaryOperators()
- {
- string input = @"// let's test a few more operators
- default
- {
- touch_start(integer num_detected)
- {
- integer x = 2;
- integer y = 1;
- integer z = x ^ y;
- x = ~ z;
- x = ~(y && z);
- y = x >> z;
- z = y << x;
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(2);" +
- "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(1);" +
- "\n LSL_Types.LSLInteger z = x ^ y;" +
- "\n x = ~z;" +
- "\n x = ~(((bool)(y)) & ((bool)(z)));" +
- "\n y = x >> z;" +
- "\n z = y << x;" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestTypecasts()
- {
- string input = @"// let's test typecasts
- default
- {
- touch_start(integer num_detected)
- {
- string s = """";
- integer x = 1;
- s = (string) x++;
- s = (string) x;
- s = (string) <0., 0., 0.>;
- s = (string) <1., 1., 1., 1.>;
- s = (integer) ""1"";
- s = (string) llSomethingThatReturnsInteger();
- s = (string) 134;
- s = (string) (x ^ y | (z && l)) + (string) (x + y - 13);
- llOwnerSay(""s is: "" + s);
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLString s = new LSL_Types.LSLString(\"\");" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
- "\n s = (LSL_Types.LSLString) (x++);" +
- "\n s = (LSL_Types.LSLString) (x);" +
- "\n 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)));" +
- "\n 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)));" +
- "\n s = (LSL_Types.LSLInteger) (new LSL_Types.LSLString(\"1\"));" +
- "\n s = (LSL_Types.LSLString) (llSomethingThatReturnsInteger());" +
- "\n s = (LSL_Types.LSLString) (new LSL_Types.LSLInteger(134));" +
- "\n s = (LSL_Types.LSLString) (x ^ y | (((bool)(z)) & ((bool)(l)))) + (LSL_Types.LSLString) (x + y - new LSL_Types.LSLInteger(13));" +
- "\n llOwnerSay(new LSL_Types.LSLString(\"s is: \") + s);" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestStates()
- {
- string input = @"// let's test states
- default
- {
- touch_start(integer num_detected)
- {
- llSay(0, ""Going to state 'statetwo'"");
- state statetwo;
- }
- }
- state statetwo
- {
- state_entry()
- {
- llSay(0, ""Going to the default state"");
- state default;
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Going to state 'statetwo'\"));" +
- "\n state(\"statetwo\");" +
- "\n }" +
- "\n public void statetwo_event_state_entry()" +
- "\n {" +
- "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Going to the default state\"));" +
- "\n state(\"default\");" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestHexIntegerConstants()
- {
- string input = @"// let's test hex integers
- default
- {
- touch_start(integer num_detected)
- {
- integer x = 0x23;
- integer x = 0x2f34B;
- integer x = 0x2F34b;
- integer x = 0x2F34B;
- integer x = 0x2f34b;
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0x23);" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0x2f34B);" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0x2F34b);" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0x2F34B);" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0x2f34b);" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestJumps()
- {
- string input = @"// let's test jumps
- default
- {
- touch_start(integer num_detected)
- {
- jump here;
- llOwnerSay(""Uh oh, the jump didn't work"");
- @here;
- llOwnerSay(""After the jump"");
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n goto here;" +
- "\n llOwnerSay(new LSL_Types.LSLString(\"Uh oh, the jump didn't work\"));" +
- "\n here: NoOp();" +
- "\n llOwnerSay(new LSL_Types.LSLString(\"After the jump\"));" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestImplicitVariableInitialization()
- {
- string input = @"// let's test implicitly initializing variables
- default
- {
- touch_start(integer num_detected)
- {
- integer i; integer j = 14;
- float f; float g = 14.0;
- string s; string t = ""Hi there"";
- list l; list m = [1, 2, 3];
- vector v; vector w = <1.0, 0.1, 0.5>;
- rotation r; rotation u = <0.8, 0.7, 0.6, llSomeFunc()>;
- key k; key n = ""ping"";
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLInteger i = new LSL_Types.LSLInteger(0);" +
- "\n LSL_Types.LSLInteger j = new LSL_Types.LSLInteger(14);" +
- "\n LSL_Types.LSLFloat f = new LSL_Types.LSLFloat(0.0);" +
- "\n LSL_Types.LSLFloat g = new LSL_Types.LSLFloat(14.0);" +
- "\n LSL_Types.LSLString s = new LSL_Types.LSLString(\"\");" +
- "\n LSL_Types.LSLString t = new LSL_Types.LSLString(\"Hi there\");" +
- "\n LSL_Types.list l = new LSL_Types.list();" +
- "\n LSL_Types.list m = new LSL_Types.list(new LSL_Types.LSLInteger(1), new LSL_Types.LSLInteger(2), new LSL_Types.LSLInteger(3));" +
- "\n 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));" +
- "\n 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));" +
- "\n LSL_Types.Quaternion r = new LSL_Types.Quaternion(new LSL_Types.LSLFloat(0.0), new LSL_Types.LSLFloat(0.0), new LSL_Types.LSLFloat(0.0), new LSL_Types.LSLFloat(0.0));" +
- "\n 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());" +
- "\n LSL_Types.LSLString k = new LSL_Types.LSLString(\"\");" +
- "\n LSL_Types.LSLString n = new LSL_Types.LSLString(\"ping\");" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestMultipleEqualsExpression()
- {
- string input = @"// let's test x = y = 5 type expressions
- default
- {
- touch_start(integer num_detected)
- {
- integer x;
- integer y;
- x = y = 5;
- x += y -= 5;
- llOwnerSay(""x is: "" + (string) x + "", y is: "" + (string) y);
- }
- }
- ";
- string expected =
- "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
- "\n {" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0);" +
- "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);" +
- "\n x = y = new LSL_Types.LSLInteger(5);" +
- "\n x += y -= new LSL_Types.LSLInteger(5);" +
- "\n llOwnerSay(new LSL_Types.LSLString(\"x is: \") + (LSL_Types.LSLString) (x) + new LSL_Types.LSLString(\", y is: \") + (LSL_Types.LSLString) (y));" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestUnaryExpressionLastInVectorConstant()
- {
- string input = @"// let's test unary expressions some more
- default
- {
- state_entry()
- {
- vector v = <x,y,-0.5>;
- }
- }
- ";
- string expected =
- "\n public void default_event_state_entry()" +
- "\n {" +
- "\n LSL_Types.Vector3 v = new LSL_Types.Vector3(x, y, -new LSL_Types.LSLFloat(0.5));" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestVectorMemberPlusEquals()
- {
- string input = @"// let's test unary expressions some more
- default
- {
- state_entry()
- {
- vector v = llGetPos();
- v.z += 4;
- v.z -= 4;
- v.z *= 4;
- v.z /= 4;
- v.z %= 4;
- }
- }
- ";
- string expected =
- "\n public void default_event_state_entry()" +
- "\n {" +
- "\n LSL_Types.Vector3 v = llGetPos();" +
- "\n v.z += new LSL_Types.LSLInteger(4);" +
- "\n v.z -= new LSL_Types.LSLInteger(4);" +
- "\n v.z *= new LSL_Types.LSLInteger(4);" +
- "\n v.z /= new LSL_Types.LSLInteger(4);" +
- "\n v.z %= new LSL_Types.LSLInteger(4);" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestWhileLoopWithNoBody()
- {
- string input = @"default
- {
- state_entry()
- {
- while (1<0);
- }
- }";
- string expected =
- "\n public void default_event_state_entry()" +
- "\n {" +
- "\n while (new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0))" +
- "\n ;" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestDoWhileLoopWithNoBody()
- {
- string input = @"default
- {
- state_entry()
- {
- do;
- while (1<0);
- }
- }";
- string expected =
- "\n public void default_event_state_entry()" +
- "\n {" +
- "\n do" +
- "\n ;" +
- "\n while (new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0));" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestIfWithNoBody()
- {
- string input = @"default
- {
- state_entry()
- {
- if (1<0);
- }
- }";
- string expected =
- "\n public void default_event_state_entry()" +
- "\n {" +
- "\n if (new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0))" +
- "\n ;" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestIfElseWithNoBody()
- {
- string input = @"default
- {
- state_entry()
- {
- if (1<0);
- else;
- }
- }";
- string expected =
- "\n public void default_event_state_entry()" +
- "\n {" +
- "\n if (new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0))" +
- "\n ;" +
- "\n else" +
- "\n ;" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestForLoopWithNoBody()
- {
- string input = @"default
- {
- state_entry()
- {
- for (x = 4; 1<0; x += 2);
- }
- }";
- string expected =
- "\n public void default_event_state_entry()" +
- "\n {" +
- "\n for (x = new LSL_Types.LSLInteger(4); new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0); x += new LSL_Types.LSLInteger(2))" +
- "\n ;" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestForLoopWithNoAssignment()
- {
- string input = @"default
- {
- state_entry()
- {
- integer x = 4;
- for (; 1<0; x += 2);
- }
- }";
- string expected =
- "\n public void default_event_state_entry()" +
- "\n {" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(4);" +
- "\n for (; new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0); x += new LSL_Types.LSLInteger(2))" +
- "\n ;" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestForLoopWithOnlyIdentInAssignment()
- {
- string input = @"default
- {
- state_entry()
- {
- integer x = 4;
- for (x; 1<0; x += 2);
- }
- }";
- string expected =
- "\n public void default_event_state_entry()" +
- "\n {" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(4);" +
- "\n for (; new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0); x += new LSL_Types.LSLInteger(2))" +
- "\n ;" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestAssignmentInIfWhileDoWhile()
- {
- string input = @"default
- {
- state_entry()
- {
- integer x;
- while (x = 14) llOwnerSay(""x is: "" + (string) x);
- if (x = 24) llOwnerSay(""x is: "" + (string) x);
- do
- llOwnerSay(""x is: "" + (string) x);
- while (x = 44);
- }
- }";
- string expected =
- "\n public void default_event_state_entry()" +
- "\n {" +
- "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0);" +
- "\n while (x = new LSL_Types.LSLInteger(14))" +
- "\n llOwnerSay(new LSL_Types.LSLString(\"x is: \") + (LSL_Types.LSLString) (x));" +
- "\n if (x = new LSL_Types.LSLInteger(24))" +
- "\n llOwnerSay(new LSL_Types.LSLString(\"x is: \") + (LSL_Types.LSLString) (x));" +
- "\n do" +
- "\n llOwnerSay(new LSL_Types.LSLString(\"x is: \") + (LSL_Types.LSLString) (x));" +
- "\n while (x = new LSL_Types.LSLInteger(44));" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- public void TestLSLListHack()
- {
- string input = @"default
- {
- state_entry()
- {
- list l = [""hello""];
- l = (l=[]) + l + ""world"";
- }
- }";
- string expected =
- "\n public void default_event_state_entry()" +
- "\n {" +
- "\n LSL_Types.list l = new LSL_Types.list(new LSL_Types.LSLString(\"hello\"));" +
- "\n l = (l = new LSL_Types.list()) + l + new LSL_Types.LSLString(\"world\");" +
- "\n }\n";
- CSCodeGenerator cg = new CSCodeGenerator();
- string output = cg.Convert(input);
- Assert.AreEqual(expected, output);
- }
- [Test]
- [ExpectedException(typeof(System.Exception))]
- public void TestSyntaxError()
- {
- string input = @"default
- {
- state_entry()
- {
- integer y
- }
- }
- ";
- try
- {
- CSCodeGenerator cg = new CSCodeGenerator();
- cg.Convert(input);
- }
- catch (System.Exception e)
- {
- // The syntax error is on line 6, char 5 (expected ';', found
- // '}').
- Assert.AreEqual("Line (4,4) syntax error", e.Message);
- throw;
- }
- }
- [Test]
- [ExpectedException(typeof(System.Exception))]
- public void TestSyntaxErrorDeclaringVariableInForLoop()
- {
- string input = @"default
- {
- state_entry()
- {
- for (integer x = 0; x < 10; x++) llOwnerSay(""x is: "" + (string) x);
- }
- }
- ";
- try
- {
- CSCodeGenerator cg = new CSCodeGenerator();
- cg.Convert(input);
- }
- catch (System.Exception e)
- {
- // The syntax error is on line 5, char 14 (Syntax error)
- Assert.AreEqual("Line (3,13) syntax error", e.Message);
- throw;
- }
- }
- }
- }
|