12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808 |
- /*
- * 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;
- using OpenSim.Tests.Common;
- 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 : OpenSimTestCase
- {
- [Test]
- public void TestDefaultState()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- // 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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()
- {
- TestHelpers.InMethod();
- 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]
- public void TestSyntaxError()
- {
- TestHelpers.InMethod();
- bool gotException = false;
- 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 5, char 4 (expected ';', found
- // '}').
- Assert.AreEqual("(5,4) syntax error", e.Message);
- gotException = true;
- }
- Assert.That(gotException, Is.True);
- }
- [Test]
- public void TestSyntaxErrorDeclaringVariableInForLoop()
- {
- TestHelpers.InMethod();
- bool gotException = false;
- 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 4, char 13 (Syntax error)
- Assert.AreEqual("(4,13) syntax error", e.Message);
- gotException = true;
- }
- Assert.That(gotException, Is.True);
- }
- }
- }
|