CSCodeGeneratorTest.cs 65 KB

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