CSCodeGeneratorTest.cs 64 KB

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