CSCodeGeneratorTest.cs 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809
  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. // TestHelpers.EnableLogging();
  682. string input = @"// let's test if statements
  683. default
  684. {
  685. touch_start(integer num_detected)
  686. {
  687. integer x = 1;
  688. if (x) llSay(0, ""Hello"");
  689. if (1)
  690. {
  691. llSay(0, ""Hi"");
  692. integer r = 3;
  693. return;
  694. }
  695. if (f(x)) llSay(0, ""f(x) is true"");
  696. else llSay(0, ""f(x) is false"");
  697. if (x + y) llSay(0, ""x + y is true"");
  698. else if (y - x) llSay(0, ""y - x is true"");
  699. else llSay(0, ""Who needs x and y anyway?"");
  700. if (x * y) llSay(0, ""x * y is true"");
  701. else if (y / x)
  702. {
  703. llSay(0, ""uh-oh, y / x is true, exiting"");
  704. return;
  705. }
  706. else llSay(0, ""Who needs x and y anyway?"");
  707. // and now for my last trick
  708. if (x % y) llSay(0, ""x is true"");
  709. else if (y & x) llSay(0, ""y is true"");
  710. else if (z | x) llSay(0, ""z is true"");
  711. else if (a * (b + x)) llSay(0, ""a is true"");
  712. else if (b) llSay(0, ""b is true"");
  713. else if (v) llSay(0, ""v is true"");
  714. else llSay(0, ""Everything is lies!"");
  715. }
  716. }
  717. ";
  718. string expected =
  719. "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
  720. "\n {" +
  721. "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
  722. "\n if (x)" +
  723. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Hello\"));" +
  724. "\n if (new LSL_Types.LSLInteger(1))" +
  725. "\n {" +
  726. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Hi\"));" +
  727. "\n LSL_Types.LSLInteger r = new LSL_Types.LSLInteger(3);" +
  728. "\n return ;" +
  729. "\n }" +
  730. "\n if (f(x))" +
  731. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"f(x) is true\"));" +
  732. "\n else" +
  733. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"f(x) is false\"));" +
  734. "\n if (x + y)" +
  735. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x + y is true\"));" +
  736. "\n else" +
  737. "\n if (y - x)" +
  738. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"y - x is true\"));" +
  739. "\n else" +
  740. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Who needs x and y anyway?\"));" +
  741. "\n if (x * y)" +
  742. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x * y is true\"));" +
  743. "\n else" +
  744. "\n if (y / x)" +
  745. "\n {" +
  746. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"uh-oh, y / x is true, exiting\"));" +
  747. "\n return ;" +
  748. "\n }" +
  749. "\n else" +
  750. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Who needs x and y anyway?\"));" +
  751. "\n if (x % y)" +
  752. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is true\"));" +
  753. "\n else" +
  754. "\n if (y & x)" +
  755. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"y is true\"));" +
  756. "\n else" +
  757. "\n if (z | x)" +
  758. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"z is true\"));" +
  759. "\n else" +
  760. "\n if (a * (b + x))" +
  761. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"a is true\"));" +
  762. "\n else" +
  763. "\n if (b)" +
  764. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"b is true\"));" +
  765. "\n else" +
  766. "\n if (v)" +
  767. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"v is true\"));" +
  768. "\n else" +
  769. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Everything is lies!\"));" +
  770. "\n }\n";
  771. CSCodeGenerator cg = new CSCodeGenerator();
  772. string output = cg.Convert(input);
  773. Assert.AreEqual(expected, output);
  774. }
  775. [Test]
  776. public void TestIfElseStatement()
  777. {
  778. TestHelpers.InMethod();
  779. string input = @"// let's test complex logical expressions
  780. default
  781. {
  782. touch_start(integer num_detected)
  783. {
  784. integer x = 1;
  785. integer y = 0;
  786. if (x && y) llSay(0, ""Hello"");
  787. if (x || y)
  788. {
  789. llSay(0, ""Hi"");
  790. integer r = 3;
  791. return;
  792. }
  793. if (x && y || z) llSay(0, ""x is true"");
  794. else llSay(0, ""x is false"");
  795. if (x == y) llSay(0, ""x is true"");
  796. else if (y < x) llSay(0, ""y is true"");
  797. else llSay(0, ""Who needs x and y anyway?"");
  798. if (x > y) llSay(0, ""x is true"");
  799. else if (y <= x)
  800. {
  801. llSay(0, ""uh-oh, y is true, exiting"");
  802. return;
  803. }
  804. else llSay(0, ""Who needs x and y anyway?"");
  805. // and now for my last trick
  806. if (x >= y) llSay(0, ""x is true"");
  807. else if (y != x) llSay(0, ""y is true"");
  808. else if (!z) llSay(0, ""z is true"");
  809. else if (!(a && b)) llSay(0, ""a is true"");
  810. else if (b) llSay(0, ""b is true"");
  811. else if (v) llSay(0, ""v is true"");
  812. else llSay(0, ""Everything is lies!"");
  813. }
  814. }
  815. ";
  816. string expected =
  817. "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
  818. "\n {" +
  819. "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
  820. "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);" +
  821. "\n if (((bool)(x)) & ((bool)(y)))" +
  822. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Hello\"));" +
  823. "\n if (((bool)(x)) | ((bool)(y)))" +
  824. "\n {" +
  825. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Hi\"));" +
  826. "\n LSL_Types.LSLInteger r = new LSL_Types.LSLInteger(3);" +
  827. "\n return ;" +
  828. "\n }" +
  829. "\n if (((bool)(((bool)(x)) & ((bool)(y)))) | ((bool)(z)))" +
  830. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is true\"));" +
  831. "\n else" +
  832. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is false\"));" +
  833. "\n if (x == y)" +
  834. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is true\"));" +
  835. "\n else" +
  836. "\n if (y < x)" +
  837. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"y is true\"));" +
  838. "\n else" +
  839. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Who needs x and y anyway?\"));" +
  840. "\n if (x > y)" +
  841. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is true\"));" +
  842. "\n else" +
  843. "\n if (y <= x)" +
  844. "\n {" +
  845. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"uh-oh, y is true, exiting\"));" +
  846. "\n return ;" +
  847. "\n }" +
  848. "\n else" +
  849. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Who needs x and y anyway?\"));" +
  850. "\n if (x >= y)" +
  851. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"x is true\"));" +
  852. "\n else" +
  853. "\n if (y != x)" +
  854. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"y is true\"));" +
  855. "\n else" +
  856. "\n if (!z)" +
  857. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"z is true\"));" +
  858. "\n else" +
  859. "\n if (!(((bool)(a)) & ((bool)(b))))" +
  860. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"a is true\"));" +
  861. "\n else" +
  862. "\n if (b)" +
  863. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"b is true\"));" +
  864. "\n else" +
  865. "\n if (v)" +
  866. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"v is true\"));" +
  867. "\n else" +
  868. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Everything is lies!\"));" +
  869. "\n }\n";
  870. CSCodeGenerator cg = new CSCodeGenerator();
  871. string output = cg.Convert(input);
  872. Assert.AreEqual(expected, output);
  873. }
  874. [Test]
  875. public void TestWhileLoop()
  876. {
  877. TestHelpers.InMethod();
  878. string input = @"// let's test while loops
  879. default
  880. {
  881. touch_start(integer num_detected)
  882. {
  883. integer x = 1;
  884. integer y = 0;
  885. while (x) llSay(0, ""To infinity, and beyond!"");
  886. while (0 || (x && 0))
  887. {
  888. llSay(0, ""Never say never."");
  889. return;
  890. }
  891. }
  892. }
  893. ";
  894. string expected =
  895. "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
  896. "\n {" +
  897. "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
  898. "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);" +
  899. "\n while (x)" +
  900. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"To infinity, and beyond!\"));" +
  901. "\n while (((bool)(new LSL_Types.LSLInteger(0))) | ((bool)((((bool)(x)) & ((bool)(new LSL_Types.LSLInteger(0)))))))" +
  902. "\n {" +
  903. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Never say never.\"));" +
  904. "\n return ;" +
  905. "\n }" +
  906. "\n }\n";
  907. CSCodeGenerator cg = new CSCodeGenerator();
  908. string output = cg.Convert(input);
  909. Assert.AreEqual(expected, output);
  910. }
  911. [Test]
  912. public void TestDoWhileLoop()
  913. {
  914. TestHelpers.InMethod();
  915. string input = @"// let's test do-while loops
  916. default
  917. {
  918. touch_start(integer num_detected)
  919. {
  920. integer x = 1;
  921. integer y = 0;
  922. do llSay(0, ""And we're doing..."");
  923. while (x);
  924. do
  925. {
  926. llSay(0, ""I like it here. I wish we could stay here forever."");
  927. y--;
  928. } while (y);
  929. }
  930. }
  931. ";
  932. string expected =
  933. "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
  934. "\n {" +
  935. "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
  936. "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);" +
  937. "\n do" +
  938. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"And we're doing...\"));" +
  939. "\n while (x);" +
  940. "\n do" +
  941. "\n {" +
  942. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"I like it here. I wish we could stay here forever.\"));" +
  943. "\n y--;" +
  944. "\n }" +
  945. "\n while (y);" +
  946. "\n }\n";
  947. CSCodeGenerator cg = new CSCodeGenerator();
  948. string output = cg.Convert(input);
  949. Assert.AreEqual(expected, output);
  950. }
  951. [Test]
  952. public void TestForLoop()
  953. {
  954. TestHelpers.InMethod();
  955. string input = @"// let's test for loops
  956. default
  957. {
  958. touch_start(integer num_detected)
  959. {
  960. integer x = 1;
  961. integer y = 0;
  962. for (x = 10; x >= 0; x--)
  963. {
  964. llOwnerSay(""Launch in T minus "" + x);
  965. IncreaseRocketPower();
  966. }
  967. for (x = 0, y = 6; y > 0 && x != y; x++, y--) llOwnerSay(""Hi "" + x + "", "" + y);
  968. for (x = 0, y = 6; ! y; x++,y--) llOwnerSay(""Hi "" + x + "", "" + y);
  969. }
  970. }
  971. ";
  972. string expected =
  973. "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
  974. "\n {" +
  975. "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
  976. "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);" +
  977. "\n for (x = new LSL_Types.LSLInteger(10); x >= new LSL_Types.LSLInteger(0); x--)" +
  978. "\n {" +
  979. "\n llOwnerSay(new LSL_Types.LSLString(\"Launch in T minus \") + x);" +
  980. "\n IncreaseRocketPower();" +
  981. "\n }" +
  982. "\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--)" +
  983. "\n llOwnerSay(new LSL_Types.LSLString(\"Hi \") + x + new LSL_Types.LSLString(\", \") + y);" +
  984. "\n for (x = new LSL_Types.LSLInteger(0), y = new LSL_Types.LSLInteger(6); !y; x++, y--)" +
  985. "\n llOwnerSay(new LSL_Types.LSLString(\"Hi \") + x + new LSL_Types.LSLString(\", \") + y);" +
  986. "\n }\n";
  987. CSCodeGenerator cg = new CSCodeGenerator();
  988. string output = cg.Convert(input);
  989. Assert.AreEqual(expected, output);
  990. }
  991. [Test]
  992. public void TestFloatsWithTrailingDecimal()
  993. {
  994. TestHelpers.InMethod();
  995. string input = @"// a curious feature of LSL that allows floats to be defined with a trailing dot
  996. default
  997. {
  998. touch_start(integer num_detected)
  999. {
  1000. float y = 1.;
  1001. y = 1.E3;
  1002. y = 1.e3;
  1003. y = 1.E+3;
  1004. y = 1.e+3;
  1005. y = 1.E-3;
  1006. y = 1.e-3;
  1007. y = -1.E3;
  1008. y = -1.e3;
  1009. y = -1.E+3;
  1010. y = -1.e+3;
  1011. y = -1.E-3;
  1012. y = -1.e-3;
  1013. y = 12. + -1.E3 - 1.e-2;
  1014. vector v = <0.,0.,0.>;
  1015. }
  1016. }
  1017. ";
  1018. string expected =
  1019. "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
  1020. "\n {" +
  1021. "\n LSL_Types.LSLFloat y = new LSL_Types.LSLFloat(1.0);" +
  1022. "\n y = new LSL_Types.LSLFloat(1.0E3);" +
  1023. "\n y = new LSL_Types.LSLFloat(1.0e3);" +
  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.0e-3);" +
  1028. "\n y = -new LSL_Types.LSLFloat(1.0E3);" +
  1029. "\n y = -new LSL_Types.LSLFloat(1.0e3);" +
  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(1.0e-3);" +
  1034. "\n y = new LSL_Types.LSLFloat(12.0) + -new LSL_Types.LSLFloat(1.0E3) - new LSL_Types.LSLFloat(1.0e-2);" +
  1035. "\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));" +
  1036. "\n }\n";
  1037. CSCodeGenerator cg = new CSCodeGenerator();
  1038. string output = cg.Convert(input);
  1039. Assert.AreEqual(expected, output);
  1040. }
  1041. [Test]
  1042. public void TestUnaryAndBinaryOperators()
  1043. {
  1044. TestHelpers.InMethod();
  1045. string input = @"// let's test a few more operators
  1046. default
  1047. {
  1048. touch_start(integer num_detected)
  1049. {
  1050. integer x = 2;
  1051. integer y = 1;
  1052. integer z = x ^ y;
  1053. x = ~ z;
  1054. x = ~(y && z);
  1055. y = x >> z;
  1056. z = y << x;
  1057. }
  1058. }
  1059. ";
  1060. string expected =
  1061. "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
  1062. "\n {" +
  1063. "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(2);" +
  1064. "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(1);" +
  1065. "\n LSL_Types.LSLInteger z = x ^ y;" +
  1066. "\n x = ~z;" +
  1067. "\n x = ~(((bool)(y)) & ((bool)(z)));" +
  1068. "\n y = x >> z;" +
  1069. "\n z = y << x;" +
  1070. "\n }\n";
  1071. CSCodeGenerator cg = new CSCodeGenerator();
  1072. string output = cg.Convert(input);
  1073. Assert.AreEqual(expected, output);
  1074. }
  1075. [Test]
  1076. public void TestTypecasts()
  1077. {
  1078. TestHelpers.InMethod();
  1079. string input = @"// let's test typecasts
  1080. default
  1081. {
  1082. touch_start(integer num_detected)
  1083. {
  1084. string s = """";
  1085. integer x = 1;
  1086. s = (string) x++;
  1087. s = (string) x;
  1088. s = (string) <0., 0., 0.>;
  1089. s = (string) <1., 1., 1., 1.>;
  1090. s = (integer) ""1"";
  1091. s = (string) llSomethingThatReturnsInteger();
  1092. s = (string) 134;
  1093. s = (string) (x ^ y | (z && l)) + (string) (x + y - 13);
  1094. llOwnerSay(""s is: "" + s);
  1095. }
  1096. }
  1097. ";
  1098. string expected =
  1099. "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
  1100. "\n {" +
  1101. "\n LSL_Types.LSLString s = new LSL_Types.LSLString(\"\");" +
  1102. "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(1);" +
  1103. "\n s = (LSL_Types.LSLString) (x++);" +
  1104. "\n s = (LSL_Types.LSLString) (x);" +
  1105. "\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)));" +
  1106. "\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)));" +
  1107. "\n s = (LSL_Types.LSLInteger) (new LSL_Types.LSLString(\"1\"));" +
  1108. "\n s = (LSL_Types.LSLString) (llSomethingThatReturnsInteger());" +
  1109. "\n s = (LSL_Types.LSLString) (new LSL_Types.LSLInteger(134));" +
  1110. "\n s = (LSL_Types.LSLString) (x ^ y | (((bool)(z)) & ((bool)(l)))) + (LSL_Types.LSLString) (x + y - new LSL_Types.LSLInteger(13));" +
  1111. "\n llOwnerSay(new LSL_Types.LSLString(\"s is: \") + s);" +
  1112. "\n }\n";
  1113. CSCodeGenerator cg = new CSCodeGenerator();
  1114. string output = cg.Convert(input);
  1115. Assert.AreEqual(expected, output);
  1116. }
  1117. [Test]
  1118. public void TestStates()
  1119. {
  1120. TestHelpers.InMethod();
  1121. string input = @"// let's test states
  1122. default
  1123. {
  1124. touch_start(integer num_detected)
  1125. {
  1126. llSay(0, ""Going to state 'statetwo'"");
  1127. state statetwo;
  1128. }
  1129. }
  1130. state statetwo
  1131. {
  1132. state_entry()
  1133. {
  1134. llSay(0, ""Going to the default state"");
  1135. state default;
  1136. }
  1137. }
  1138. ";
  1139. string expected =
  1140. "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
  1141. "\n {" +
  1142. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Going to state 'statetwo'\"));" +
  1143. "\n state(\"statetwo\");" +
  1144. "\n }" +
  1145. "\n public void statetwo_event_state_entry()" +
  1146. "\n {" +
  1147. "\n llSay(new LSL_Types.LSLInteger(0), new LSL_Types.LSLString(\"Going to the default state\"));" +
  1148. "\n state(\"default\");" +
  1149. "\n }\n";
  1150. CSCodeGenerator cg = new CSCodeGenerator();
  1151. string output = cg.Convert(input);
  1152. Assert.AreEqual(expected, output);
  1153. }
  1154. [Test]
  1155. public void TestHexIntegerConstants()
  1156. {
  1157. TestHelpers.InMethod();
  1158. string input = @"// let's test hex integers
  1159. default
  1160. {
  1161. touch_start(integer num_detected)
  1162. {
  1163. integer x = 0x23;
  1164. integer x = 0x2f34B;
  1165. integer x = 0x2F34b;
  1166. integer x = 0x2F34B;
  1167. integer x = 0x2f34b;
  1168. }
  1169. }
  1170. ";
  1171. string expected =
  1172. "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
  1173. "\n {" +
  1174. "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0x23);" +
  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 LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0x2f34b);" +
  1179. "\n }\n";
  1180. CSCodeGenerator cg = new CSCodeGenerator();
  1181. string output = cg.Convert(input);
  1182. Assert.AreEqual(expected, output);
  1183. }
  1184. [Test]
  1185. public void TestJumps()
  1186. {
  1187. TestHelpers.InMethod();
  1188. string input = @"// let's test jumps
  1189. default
  1190. {
  1191. touch_start(integer num_detected)
  1192. {
  1193. jump here;
  1194. llOwnerSay(""Uh oh, the jump didn't work"");
  1195. @here;
  1196. llOwnerSay(""After the jump"");
  1197. }
  1198. }
  1199. ";
  1200. string expected =
  1201. "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
  1202. "\n {" +
  1203. "\n goto here;" +
  1204. "\n llOwnerSay(new LSL_Types.LSLString(\"Uh oh, the jump didn't work\"));" +
  1205. "\n here: NoOp();" +
  1206. "\n llOwnerSay(new LSL_Types.LSLString(\"After the jump\"));" +
  1207. "\n }\n";
  1208. CSCodeGenerator cg = new CSCodeGenerator();
  1209. string output = cg.Convert(input);
  1210. Assert.AreEqual(expected, output);
  1211. }
  1212. [Test]
  1213. public void TestImplicitVariableInitialization()
  1214. {
  1215. TestHelpers.InMethod();
  1216. string input = @"// let's test implicitly initializing variables
  1217. default
  1218. {
  1219. touch_start(integer num_detected)
  1220. {
  1221. integer i; integer j = 14;
  1222. float f; float g = 14.0;
  1223. string s; string t = ""Hi there"";
  1224. list l; list m = [1, 2, 3];
  1225. vector v; vector w = <1.0, 0.1, 0.5>;
  1226. rotation r; rotation u = <0.8, 0.7, 0.6, llSomeFunc()>;
  1227. key k; key n = ""ping"";
  1228. }
  1229. }
  1230. ";
  1231. string expected =
  1232. "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
  1233. "\n {" +
  1234. "\n LSL_Types.LSLInteger i = new LSL_Types.LSLInteger(0);" +
  1235. "\n LSL_Types.LSLInteger j = new LSL_Types.LSLInteger(14);" +
  1236. "\n LSL_Types.LSLFloat f = new LSL_Types.LSLFloat(0.0);" +
  1237. "\n LSL_Types.LSLFloat g = new LSL_Types.LSLFloat(14.0);" +
  1238. "\n LSL_Types.LSLString s = new LSL_Types.LSLString(\"\");" +
  1239. "\n LSL_Types.LSLString t = new LSL_Types.LSLString(\"Hi there\");" +
  1240. "\n LSL_Types.list l = new LSL_Types.list();" +
  1241. "\n LSL_Types.list m = new LSL_Types.list(new LSL_Types.LSLInteger(1), new LSL_Types.LSLInteger(2), new LSL_Types.LSLInteger(3));" +
  1242. "\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));" +
  1243. "\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));" +
  1244. "\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));" +
  1245. "\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());" +
  1246. "\n LSL_Types.LSLString k = new LSL_Types.LSLString(\"\");" +
  1247. "\n LSL_Types.LSLString n = new LSL_Types.LSLString(\"ping\");" +
  1248. "\n }\n";
  1249. CSCodeGenerator cg = new CSCodeGenerator();
  1250. string output = cg.Convert(input);
  1251. Assert.AreEqual(expected, output);
  1252. }
  1253. [Test]
  1254. public void TestMultipleEqualsExpression()
  1255. {
  1256. TestHelpers.InMethod();
  1257. string input = @"// let's test x = y = 5 type expressions
  1258. default
  1259. {
  1260. touch_start(integer num_detected)
  1261. {
  1262. integer x;
  1263. integer y;
  1264. x = y = 5;
  1265. x += y -= 5;
  1266. llOwnerSay(""x is: "" + (string) x + "", y is: "" + (string) y);
  1267. }
  1268. }
  1269. ";
  1270. string expected =
  1271. "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" +
  1272. "\n {" +
  1273. "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0);" +
  1274. "\n LSL_Types.LSLInteger y = new LSL_Types.LSLInteger(0);" +
  1275. "\n x = y = new LSL_Types.LSLInteger(5);" +
  1276. "\n x += y -= new LSL_Types.LSLInteger(5);" +
  1277. "\n llOwnerSay(new LSL_Types.LSLString(\"x is: \") + (LSL_Types.LSLString) (x) + new LSL_Types.LSLString(\", y is: \") + (LSL_Types.LSLString) (y));" +
  1278. "\n }\n";
  1279. CSCodeGenerator cg = new CSCodeGenerator();
  1280. string output = cg.Convert(input);
  1281. Assert.AreEqual(expected, output);
  1282. }
  1283. [Test]
  1284. public void TestUnaryExpressionLastInVectorConstant()
  1285. {
  1286. TestHelpers.InMethod();
  1287. string input = @"// let's test unary expressions some more
  1288. default
  1289. {
  1290. state_entry()
  1291. {
  1292. vector v = <x,y,-0.5>;
  1293. }
  1294. }
  1295. ";
  1296. string expected =
  1297. "\n public void default_event_state_entry()" +
  1298. "\n {" +
  1299. "\n LSL_Types.Vector3 v = new LSL_Types.Vector3(x, y, -new LSL_Types.LSLFloat(0.5));" +
  1300. "\n }\n";
  1301. CSCodeGenerator cg = new CSCodeGenerator();
  1302. string output = cg.Convert(input);
  1303. Assert.AreEqual(expected, output);
  1304. }
  1305. [Test]
  1306. public void TestVectorMemberPlusEquals()
  1307. {
  1308. TestHelpers.InMethod();
  1309. string input = @"// let's test unary expressions some more
  1310. default
  1311. {
  1312. state_entry()
  1313. {
  1314. vector v = llGetPos();
  1315. v.z += 4;
  1316. v.z -= 4;
  1317. v.z *= 4;
  1318. v.z /= 4;
  1319. v.z %= 4;
  1320. }
  1321. }
  1322. ";
  1323. string expected =
  1324. "\n public void default_event_state_entry()" +
  1325. "\n {" +
  1326. "\n LSL_Types.Vector3 v = llGetPos();" +
  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 v.z %= new LSL_Types.LSLInteger(4);" +
  1332. "\n }\n";
  1333. CSCodeGenerator cg = new CSCodeGenerator();
  1334. string output = cg.Convert(input);
  1335. Assert.AreEqual(expected, output);
  1336. }
  1337. [Test]
  1338. public void TestWhileLoopWithNoBody()
  1339. {
  1340. TestHelpers.InMethod();
  1341. string input = @"default
  1342. {
  1343. state_entry()
  1344. {
  1345. while (1<0);
  1346. }
  1347. }";
  1348. string expected =
  1349. "\n public void default_event_state_entry()" +
  1350. "\n {" +
  1351. "\n while (new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0))" +
  1352. "\n ;" +
  1353. "\n }\n";
  1354. CSCodeGenerator cg = new CSCodeGenerator();
  1355. string output = cg.Convert(input);
  1356. Assert.AreEqual(expected, output);
  1357. }
  1358. [Test]
  1359. public void TestDoWhileLoopWithNoBody()
  1360. {
  1361. TestHelpers.InMethod();
  1362. string input = @"default
  1363. {
  1364. state_entry()
  1365. {
  1366. do;
  1367. while (1<0);
  1368. }
  1369. }";
  1370. string expected =
  1371. "\n public void default_event_state_entry()" +
  1372. "\n {" +
  1373. "\n do" +
  1374. "\n ;" +
  1375. "\n while (new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0));" +
  1376. "\n }\n";
  1377. CSCodeGenerator cg = new CSCodeGenerator();
  1378. string output = cg.Convert(input);
  1379. Assert.AreEqual(expected, output);
  1380. }
  1381. [Test]
  1382. public void TestIfWithNoBody()
  1383. {
  1384. TestHelpers.InMethod();
  1385. string input = @"default
  1386. {
  1387. state_entry()
  1388. {
  1389. if (1<0);
  1390. }
  1391. }";
  1392. string expected =
  1393. "\n public void default_event_state_entry()" +
  1394. "\n {" +
  1395. "\n if (new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0))" +
  1396. "\n ;" +
  1397. "\n }\n";
  1398. CSCodeGenerator cg = new CSCodeGenerator();
  1399. string output = cg.Convert(input);
  1400. Assert.AreEqual(expected, output);
  1401. }
  1402. [Test]
  1403. public void TestIfElseWithNoBody()
  1404. {
  1405. TestHelpers.InMethod();
  1406. string input = @"default
  1407. {
  1408. state_entry()
  1409. {
  1410. if (1<0);
  1411. else;
  1412. }
  1413. }";
  1414. string expected =
  1415. "\n public void default_event_state_entry()" +
  1416. "\n {" +
  1417. "\n if (new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0))" +
  1418. "\n ;" +
  1419. "\n else" +
  1420. "\n ;" +
  1421. "\n }\n";
  1422. CSCodeGenerator cg = new CSCodeGenerator();
  1423. string output = cg.Convert(input);
  1424. Assert.AreEqual(expected, output);
  1425. }
  1426. [Test]
  1427. public void TestForLoopWithNoBody()
  1428. {
  1429. TestHelpers.InMethod();
  1430. string input = @"default
  1431. {
  1432. state_entry()
  1433. {
  1434. for (x = 4; 1<0; x += 2);
  1435. }
  1436. }";
  1437. string expected =
  1438. "\n public void default_event_state_entry()" +
  1439. "\n {" +
  1440. "\n for (x = new LSL_Types.LSLInteger(4); new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0); x += new LSL_Types.LSLInteger(2))" +
  1441. "\n ;" +
  1442. "\n }\n";
  1443. CSCodeGenerator cg = new CSCodeGenerator();
  1444. string output = cg.Convert(input);
  1445. Assert.AreEqual(expected, output);
  1446. }
  1447. [Test]
  1448. public void TestForLoopWithNoAssignment()
  1449. {
  1450. TestHelpers.InMethod();
  1451. string input = @"default
  1452. {
  1453. state_entry()
  1454. {
  1455. integer x = 4;
  1456. for (; 1<0; x += 2);
  1457. }
  1458. }";
  1459. string expected =
  1460. "\n public void default_event_state_entry()" +
  1461. "\n {" +
  1462. "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(4);" +
  1463. "\n for (; new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0); x += new LSL_Types.LSLInteger(2))" +
  1464. "\n ;" +
  1465. "\n }\n";
  1466. CSCodeGenerator cg = new CSCodeGenerator();
  1467. string output = cg.Convert(input);
  1468. Assert.AreEqual(expected, output);
  1469. }
  1470. [Test]
  1471. public void TestForLoopWithOnlyIdentInAssignment()
  1472. {
  1473. TestHelpers.InMethod();
  1474. string input = @"default
  1475. {
  1476. state_entry()
  1477. {
  1478. integer x = 4;
  1479. for (x; 1<0; x += 2);
  1480. }
  1481. }";
  1482. string expected =
  1483. "\n public void default_event_state_entry()" +
  1484. "\n {" +
  1485. "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(4);" +
  1486. "\n for (; new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0); x += new LSL_Types.LSLInteger(2))" +
  1487. "\n ;" +
  1488. "\n }\n";
  1489. CSCodeGenerator cg = new CSCodeGenerator();
  1490. string output = cg.Convert(input);
  1491. Assert.AreEqual(expected, output);
  1492. }
  1493. [Test]
  1494. public void TestAssignmentInIfWhileDoWhile()
  1495. {
  1496. TestHelpers.InMethod();
  1497. string input = @"default
  1498. {
  1499. state_entry()
  1500. {
  1501. integer x;
  1502. while (x = 14) llOwnerSay(""x is: "" + (string) x);
  1503. if (x = 24) llOwnerSay(""x is: "" + (string) x);
  1504. do
  1505. llOwnerSay(""x is: "" + (string) x);
  1506. while (x = 44);
  1507. }
  1508. }";
  1509. string expected =
  1510. "\n public void default_event_state_entry()" +
  1511. "\n {" +
  1512. "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0);" +
  1513. "\n while (x = new LSL_Types.LSLInteger(14))" +
  1514. "\n llOwnerSay(new LSL_Types.LSLString(\"x is: \") + (LSL_Types.LSLString) (x));" +
  1515. "\n if (x = new LSL_Types.LSLInteger(24))" +
  1516. "\n llOwnerSay(new LSL_Types.LSLString(\"x is: \") + (LSL_Types.LSLString) (x));" +
  1517. "\n do" +
  1518. "\n llOwnerSay(new LSL_Types.LSLString(\"x is: \") + (LSL_Types.LSLString) (x));" +
  1519. "\n while (x = new LSL_Types.LSLInteger(44));" +
  1520. "\n }\n";
  1521. CSCodeGenerator cg = new CSCodeGenerator();
  1522. string output = cg.Convert(input);
  1523. Assert.AreEqual(expected, output);
  1524. }
  1525. [Test]
  1526. public void TestLSLListHack()
  1527. {
  1528. TestHelpers.InMethod();
  1529. string input = @"default
  1530. {
  1531. state_entry()
  1532. {
  1533. list l = [""hello""];
  1534. l = (l=[]) + l + ""world"";
  1535. }
  1536. }";
  1537. string expected =
  1538. "\n public void default_event_state_entry()" +
  1539. "\n {" +
  1540. "\n LSL_Types.list l = new LSL_Types.list(new LSL_Types.LSLString(\"hello\"));" +
  1541. "\n l = (l = new LSL_Types.list()) + l + new LSL_Types.LSLString(\"world\");" +
  1542. "\n }\n";
  1543. CSCodeGenerator cg = new CSCodeGenerator();
  1544. string output = cg.Convert(input);
  1545. Assert.AreEqual(expected, output);
  1546. }
  1547. [Test]
  1548. public void TestSyntaxError()
  1549. {
  1550. TestHelpers.InMethod();
  1551. bool gotException = false;
  1552. string input = @"default
  1553. {
  1554. state_entry()
  1555. {
  1556. integer y
  1557. }
  1558. }
  1559. ";
  1560. try
  1561. {
  1562. CSCodeGenerator cg = new CSCodeGenerator();
  1563. cg.Convert(input);
  1564. }
  1565. catch (System.Exception e)
  1566. {
  1567. // The syntax error is on line 5, char 4 (expected ';', found
  1568. // '}').
  1569. Assert.AreEqual("(5,4) syntax error", e.Message);
  1570. gotException = true;
  1571. }
  1572. Assert.That(gotException, Is.True);
  1573. }
  1574. [Test]
  1575. public void TestSyntaxErrorDeclaringVariableInForLoop()
  1576. {
  1577. TestHelpers.InMethod();
  1578. bool gotException = false;
  1579. string input = @"default
  1580. {
  1581. state_entry()
  1582. {
  1583. for (integer x = 0; x < 10; x++) llOwnerSay(""x is: "" + (string) x);
  1584. }
  1585. }
  1586. ";
  1587. try
  1588. {
  1589. CSCodeGenerator cg = new CSCodeGenerator();
  1590. cg.Convert(input);
  1591. }
  1592. catch (System.Exception e)
  1593. {
  1594. // The syntax error is on line 4, char 13 (Syntax error)
  1595. Assert.AreEqual("(4,13) syntax error", e.Message);
  1596. gotException = true;
  1597. }
  1598. Assert.That(gotException, Is.True);
  1599. }
  1600. }
  1601. }