1
0

CSCodeGenerator.cs 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107
  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;
  28. using System.IO;
  29. using System.Collections.Generic;
  30. using Tools;
  31. namespace OpenSim.Region.ScriptEngine.Shared.CodeTools
  32. {
  33. public class CSCodeGenerator : ICodeConverter
  34. {
  35. private SYMBOL m_astRoot = null;
  36. private Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> m_positionMap;
  37. private int m_indentWidth = 4; // for indentation
  38. private int m_braceCount; // for indentation
  39. private int m_CSharpLine; // the current line of generated C# code
  40. private int m_CSharpCol; // the current column of generated C# code
  41. private List<string> m_warnings = new List<string>();
  42. /// <summary>
  43. /// Creates an 'empty' CSCodeGenerator instance.
  44. /// </summary>
  45. public CSCodeGenerator()
  46. {
  47. ResetCounters();
  48. }
  49. /// <summary>
  50. /// Get the mapping between LSL and C# line/column number.
  51. /// </summary>
  52. /// <returns>Dictionary\<KeyValuePair\<int, int\>, KeyValuePair\<int, int\>\>.</returns>
  53. public Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>> PositionMap
  54. {
  55. get { return m_positionMap; }
  56. }
  57. /// <summary>
  58. /// Get the mapping between LSL and C# line/column number.
  59. /// </summary>
  60. /// <returns>SYMBOL pointing to root of the abstract syntax tree.</returns>
  61. public SYMBOL ASTRoot
  62. {
  63. get { return m_astRoot; }
  64. }
  65. /// <summary>
  66. /// Resets various counters and metadata.
  67. /// </summary>
  68. private void ResetCounters()
  69. {
  70. m_braceCount = 0;
  71. m_CSharpLine = 0;
  72. m_CSharpCol = 1;
  73. m_positionMap = new Dictionary<KeyValuePair<int, int>, KeyValuePair<int, int>>();
  74. m_astRoot = null;
  75. }
  76. /// <summary>
  77. /// Generate the code from the AST we have.
  78. /// </summary>
  79. /// <param name="script">The LSL source as a string.</param>
  80. /// <returns>String containing the generated C# code.</returns>
  81. public string Convert(string script)
  82. {
  83. m_warnings.Clear();
  84. ResetCounters();
  85. Parser p = new LSLSyntax(new yyLSLSyntax(), new ErrorHandler(true));
  86. LSL2CSCodeTransformer codeTransformer;
  87. try
  88. {
  89. codeTransformer = new LSL2CSCodeTransformer(p.Parse(script));
  90. }
  91. catch (CSToolsException e)
  92. {
  93. string message;
  94. // LL start numbering lines at 0 - geeks!
  95. // Also need to subtract one line we prepend!
  96. //
  97. string emessage = e.Message;
  98. string slinfo = e.slInfo.ToString();
  99. // Remove wrong line number info
  100. //
  101. if (emessage.StartsWith(slinfo+": "))
  102. emessage = emessage.Substring(slinfo.Length+2);
  103. message = String.Format("Line ({0},{1}) {2}",
  104. e.slInfo.lineNumber - 2,
  105. e.slInfo.charPosition - 1, emessage);
  106. throw new Exception(message);
  107. }
  108. m_astRoot = codeTransformer.Transform();
  109. string retstr = String.Empty;
  110. // standard preamble
  111. //retstr = GenerateLine("using OpenSim.Region.ScriptEngine.Common;");
  112. //retstr += GenerateLine("using System.Collections.Generic;");
  113. //retstr += GenerateLine("");
  114. //retstr += GenerateLine("namespace SecondLife");
  115. //retstr += GenerateLine("{");
  116. m_braceCount++;
  117. //retstr += GenerateIndentedLine("public class Script : OpenSim.Region.ScriptEngine.Common");
  118. //retstr += GenerateIndentedLine("{");
  119. m_braceCount++;
  120. // line number
  121. m_CSharpLine += 3;
  122. // here's the payload
  123. retstr += GenerateLine();
  124. foreach (SYMBOL s in m_astRoot.kids)
  125. retstr += GenerateNode(s);
  126. // close braces!
  127. m_braceCount--;
  128. //retstr += GenerateIndentedLine("}");
  129. m_braceCount--;
  130. //retstr += GenerateLine("}");
  131. // Removes all carriage return characters which may be generated in Windows platform. Is there
  132. // cleaner way of doing this?
  133. retstr=retstr.Replace("\r", "");
  134. return retstr;
  135. }
  136. /// <summary>
  137. /// Get the set of warnings generated during compilation.
  138. /// </summary>
  139. /// <returns></returns>
  140. public string[] GetWarnings()
  141. {
  142. return m_warnings.ToArray();
  143. }
  144. private void AddWarning(string warning)
  145. {
  146. if (!m_warnings.Contains(warning))
  147. {
  148. m_warnings.Add(warning);
  149. }
  150. }
  151. /// <summary>
  152. /// Recursively called to generate each type of node. Will generate this
  153. /// node, then all it's children.
  154. /// </summary>
  155. /// <param name="s">The current node to generate code for.</param>
  156. /// <returns>String containing C# code for SYMBOL s.</returns>
  157. private string GenerateNode(SYMBOL s)
  158. {
  159. string retstr = String.Empty;
  160. // make sure to put type lower in the inheritance hierarchy first
  161. // ie: since IdentArgument and ExpressionArgument inherit from
  162. // Argument, put IdentArgument and ExpressionArgument before Argument
  163. if (s is GlobalFunctionDefinition)
  164. retstr += GenerateGlobalFunctionDefinition((GlobalFunctionDefinition) s);
  165. else if (s is GlobalVariableDeclaration)
  166. retstr += GenerateGlobalVariableDeclaration((GlobalVariableDeclaration) s);
  167. else if (s is State)
  168. retstr += GenerateState((State) s);
  169. else if (s is CompoundStatement)
  170. retstr += GenerateCompoundStatement((CompoundStatement) s);
  171. else if (s is Declaration)
  172. retstr += GenerateDeclaration((Declaration) s);
  173. else if (s is Statement)
  174. retstr += GenerateStatement((Statement) s);
  175. else if (s is ReturnStatement)
  176. retstr += GenerateReturnStatement((ReturnStatement) s);
  177. else if (s is JumpLabel)
  178. retstr += GenerateJumpLabel((JumpLabel) s);
  179. else if (s is JumpStatement)
  180. retstr += GenerateJumpStatement((JumpStatement) s);
  181. else if (s is StateChange)
  182. retstr += GenerateStateChange((StateChange) s);
  183. else if (s is IfStatement)
  184. retstr += GenerateIfStatement((IfStatement) s);
  185. else if (s is WhileStatement)
  186. retstr += GenerateWhileStatement((WhileStatement) s);
  187. else if (s is DoWhileStatement)
  188. retstr += GenerateDoWhileStatement((DoWhileStatement) s);
  189. else if (s is ForLoop)
  190. retstr += GenerateForLoop((ForLoop) s);
  191. else if (s is ArgumentList)
  192. retstr += GenerateArgumentList((ArgumentList) s);
  193. else if (s is Assignment)
  194. retstr += GenerateAssignment((Assignment) s);
  195. else if (s is BinaryExpression)
  196. retstr += GenerateBinaryExpression((BinaryExpression) s);
  197. else if (s is ParenthesisExpression)
  198. retstr += GenerateParenthesisExpression((ParenthesisExpression) s);
  199. else if (s is UnaryExpression)
  200. retstr += GenerateUnaryExpression((UnaryExpression) s);
  201. else if (s is IncrementDecrementExpression)
  202. retstr += GenerateIncrementDecrementExpression((IncrementDecrementExpression) s);
  203. else if (s is TypecastExpression)
  204. retstr += GenerateTypecastExpression((TypecastExpression) s);
  205. else if (s is FunctionCall)
  206. retstr += GenerateFunctionCall((FunctionCall) s);
  207. else if (s is VectorConstant)
  208. retstr += GenerateVectorConstant((VectorConstant) s);
  209. else if (s is RotationConstant)
  210. retstr += GenerateRotationConstant((RotationConstant) s);
  211. else if (s is ListConstant)
  212. retstr += GenerateListConstant((ListConstant) s);
  213. else if (s is Constant)
  214. retstr += GenerateConstant((Constant) s);
  215. else if (s is IdentDotExpression)
  216. retstr += Generate(CheckName(((IdentDotExpression) s).Name) + "." + ((IdentDotExpression) s).Member, s);
  217. else if (s is IdentExpression)
  218. retstr += Generate(CheckName(((IdentExpression) s).Name), s);
  219. else if (s is IDENT)
  220. retstr += Generate(CheckName(((TOKEN) s).yytext), s);
  221. else
  222. {
  223. foreach (SYMBOL kid in s.kids)
  224. retstr += GenerateNode(kid);
  225. }
  226. return retstr;
  227. }
  228. /// <summary>
  229. /// Generates the code for a GlobalFunctionDefinition node.
  230. /// </summary>
  231. /// <param name="gf">The GlobalFunctionDefinition node.</param>
  232. /// <returns>String containing C# code for GlobalFunctionDefinition gf.</returns>
  233. private string GenerateGlobalFunctionDefinition(GlobalFunctionDefinition gf)
  234. {
  235. string retstr = String.Empty;
  236. // we need to separate the argument declaration list from other kids
  237. List<SYMBOL> argumentDeclarationListKids = new List<SYMBOL>();
  238. List<SYMBOL> remainingKids = new List<SYMBOL>();
  239. foreach (SYMBOL kid in gf.kids)
  240. if (kid is ArgumentDeclarationList)
  241. argumentDeclarationListKids.Add(kid);
  242. else
  243. remainingKids.Add(kid);
  244. retstr += GenerateIndented(String.Format("{0} {1}(", gf.ReturnType, CheckName(gf.Name)), gf);
  245. // print the state arguments, if any
  246. foreach (SYMBOL kid in argumentDeclarationListKids)
  247. retstr += GenerateArgumentDeclarationList((ArgumentDeclarationList) kid);
  248. retstr += GenerateLine(")");
  249. foreach (SYMBOL kid in remainingKids)
  250. retstr += GenerateNode(kid);
  251. return retstr;
  252. }
  253. /// <summary>
  254. /// Generates the code for a GlobalVariableDeclaration node.
  255. /// </summary>
  256. /// <param name="gv">The GlobalVariableDeclaration node.</param>
  257. /// <returns>String containing C# code for GlobalVariableDeclaration gv.</returns>
  258. private string GenerateGlobalVariableDeclaration(GlobalVariableDeclaration gv)
  259. {
  260. string retstr = String.Empty;
  261. foreach (SYMBOL s in gv.kids)
  262. {
  263. retstr += Indent();
  264. retstr += GenerateNode(s);
  265. retstr += GenerateLine(";");
  266. }
  267. return retstr;
  268. }
  269. /// <summary>
  270. /// Generates the code for a State node.
  271. /// </summary>
  272. /// <param name="s">The State node.</param>
  273. /// <returns>String containing C# code for State s.</returns>
  274. private string GenerateState(State s)
  275. {
  276. string retstr = String.Empty;
  277. foreach (SYMBOL kid in s.kids)
  278. if (kid is StateEvent)
  279. retstr += GenerateStateEvent((StateEvent) kid, s.Name);
  280. return retstr;
  281. }
  282. /// <summary>
  283. /// Generates the code for a StateEvent node.
  284. /// </summary>
  285. /// <param name="se">The StateEvent node.</param>
  286. /// <param name="parentStateName">The name of the parent state.</param>
  287. /// <returns>String containing C# code for StateEvent se.</returns>
  288. private string GenerateStateEvent(StateEvent se, string parentStateName)
  289. {
  290. string retstr = String.Empty;
  291. // we need to separate the argument declaration list from other kids
  292. List<SYMBOL> argumentDeclarationListKids = new List<SYMBOL>();
  293. List<SYMBOL> remainingKids = new List<SYMBOL>();
  294. foreach (SYMBOL kid in se.kids)
  295. if (kid is ArgumentDeclarationList)
  296. argumentDeclarationListKids.Add(kid);
  297. else
  298. remainingKids.Add(kid);
  299. // "state" (function) declaration
  300. retstr += GenerateIndented(String.Format("public void {0}_event_{1}(", parentStateName, se.Name), se);
  301. // print the state arguments, if any
  302. foreach (SYMBOL kid in argumentDeclarationListKids)
  303. retstr += GenerateArgumentDeclarationList((ArgumentDeclarationList) kid);
  304. retstr += GenerateLine(")");
  305. foreach (SYMBOL kid in remainingKids)
  306. retstr += GenerateNode(kid);
  307. return retstr;
  308. }
  309. /// <summary>
  310. /// Generates the code for an ArgumentDeclarationList node.
  311. /// </summary>
  312. /// <param name="adl">The ArgumentDeclarationList node.</param>
  313. /// <returns>String containing C# code for ArgumentDeclarationList adl.</returns>
  314. private string GenerateArgumentDeclarationList(ArgumentDeclarationList adl)
  315. {
  316. string retstr = String.Empty;
  317. int comma = adl.kids.Count - 1; // tells us whether to print a comma
  318. foreach (Declaration d in adl.kids)
  319. {
  320. retstr += Generate(String.Format("{0} {1}", d.Datatype, CheckName(d.Id)), d);
  321. if (0 < comma--)
  322. retstr += Generate(", ");
  323. }
  324. return retstr;
  325. }
  326. /// <summary>
  327. /// Generates the code for an ArgumentList node.
  328. /// </summary>
  329. /// <param name="al">The ArgumentList node.</param>
  330. /// <returns>String containing C# code for ArgumentList al.</returns>
  331. private string GenerateArgumentList(ArgumentList al)
  332. {
  333. string retstr = String.Empty;
  334. int comma = al.kids.Count - 1; // tells us whether to print a comma
  335. foreach (SYMBOL s in al.kids)
  336. {
  337. retstr += GenerateNode(s);
  338. if (0 < comma--)
  339. retstr += Generate(", ");
  340. }
  341. return retstr;
  342. }
  343. /// <summary>
  344. /// Generates the code for a CompoundStatement node.
  345. /// </summary>
  346. /// <param name="cs">The CompoundStatement node.</param>
  347. /// <returns>String containing C# code for CompoundStatement cs.</returns>
  348. private string GenerateCompoundStatement(CompoundStatement cs)
  349. {
  350. string retstr = String.Empty;
  351. // opening brace
  352. retstr += GenerateIndentedLine("{");
  353. m_braceCount++;
  354. foreach (SYMBOL kid in cs.kids)
  355. retstr += GenerateNode(kid);
  356. // closing brace
  357. m_braceCount--;
  358. retstr += GenerateIndentedLine("}");
  359. return retstr;
  360. }
  361. /// <summary>
  362. /// Generates the code for a Declaration node.
  363. /// </summary>
  364. /// <param name="d">The Declaration node.</param>
  365. /// <returns>String containing C# code for Declaration d.</returns>
  366. private string GenerateDeclaration(Declaration d)
  367. {
  368. return Generate(String.Format("{0} {1}", d.Datatype, CheckName(d.Id)), d);
  369. }
  370. /// <summary>
  371. /// Generates the code for a Statement node.
  372. /// </summary>
  373. /// <param name="s">The Statement node.</param>
  374. /// <returns>String containing C# code for Statement s.</returns>
  375. private string GenerateStatement(Statement s)
  376. {
  377. string retstr = String.Empty;
  378. bool printSemicolon = true;
  379. retstr += Indent();
  380. if (0 < s.kids.Count)
  381. {
  382. // Jump label prints its own colon, we don't need a semicolon.
  383. printSemicolon = !(s.kids.Top is JumpLabel);
  384. // If we encounter a lone Ident, we skip it, since that's a C#
  385. // (MONO) error.
  386. if (!(s.kids.Top is IdentExpression && 1 == s.kids.Count))
  387. foreach (SYMBOL kid in s.kids)
  388. retstr += GenerateNode(kid);
  389. }
  390. if (printSemicolon)
  391. retstr += GenerateLine(";");
  392. return retstr;
  393. }
  394. /// <summary>
  395. /// Generates the code for an Assignment node.
  396. /// </summary>
  397. /// <param name="a">The Assignment node.</param>
  398. /// <returns>String containing C# code for Assignment a.</returns>
  399. private string GenerateAssignment(Assignment a)
  400. {
  401. string retstr = String.Empty;
  402. List<string> identifiers = new List<string>();
  403. checkForMultipleAssignments(identifiers, a);
  404. retstr += GenerateNode((SYMBOL) a.kids.Pop());
  405. retstr += Generate(String.Format(" {0} ", a.AssignmentType), a);
  406. foreach (SYMBOL kid in a.kids)
  407. retstr += GenerateNode(kid);
  408. return retstr;
  409. }
  410. // This code checks for LSL of the following forms, and generates a
  411. // warning if it finds them.
  412. //
  413. // list l = [ "foo" ];
  414. // l = (l=[]) + l + ["bar"];
  415. // (produces l=["foo","bar"] in SL but l=["bar"] in OS)
  416. //
  417. // integer i;
  418. // integer j;
  419. // i = (j = 3) + (j = 4) + (j = 5);
  420. // (produces j=3 in SL but j=5 in OS)
  421. //
  422. // Without this check, that code passes compilation, but does not do what
  423. // the end user expects, because LSL in SL evaluates right to left instead
  424. // of left to right.
  425. //
  426. // The theory here is that producing an error and alerting the end user that
  427. // something needs to change is better than silently generating incorrect code.
  428. private void checkForMultipleAssignments(List<string> identifiers, SYMBOL s)
  429. {
  430. if (s is Assignment)
  431. {
  432. Assignment a = (Assignment)s;
  433. string newident = null;
  434. if (a.kids[0] is Declaration)
  435. {
  436. newident = ((Declaration)a.kids[0]).Id;
  437. }
  438. else if (a.kids[0] is IDENT)
  439. {
  440. newident = ((IDENT)a.kids[0]).yytext;
  441. }
  442. else if (a.kids[0] is IdentDotExpression)
  443. {
  444. newident = ((IdentDotExpression)a.kids[0]).Name; // +"." + ((IdentDotExpression)a.kids[0]).Member;
  445. }
  446. else
  447. {
  448. AddWarning(String.Format("Multiple assignments checker internal error '{0}' at line {1} column {2}.", a.kids[0].GetType(), ((SYMBOL)a.kids[0]).Line - 1, ((SYMBOL)a.kids[0]).Position));
  449. }
  450. if (identifiers.Contains(newident))
  451. {
  452. AddWarning(String.Format("Multiple assignments to '{0}' at line {1} column {2}; results may differ between LSL and OSSL.", newident, ((SYMBOL)a.kids[0]).Line - 1, ((SYMBOL)a.kids[0]).Position));
  453. }
  454. identifiers.Add(newident);
  455. }
  456. int index;
  457. for (index = 0; index < s.kids.Count; index++)
  458. {
  459. checkForMultipleAssignments(identifiers, (SYMBOL) s.kids[index]);
  460. }
  461. }
  462. /// <summary>
  463. /// Generates the code for a ReturnStatement node.
  464. /// </summary>
  465. /// <param name="rs">The ReturnStatement node.</param>
  466. /// <returns>String containing C# code for ReturnStatement rs.</returns>
  467. private string GenerateReturnStatement(ReturnStatement rs)
  468. {
  469. string retstr = String.Empty;
  470. retstr += Generate("return ", rs);
  471. foreach (SYMBOL kid in rs.kids)
  472. retstr += GenerateNode(kid);
  473. return retstr;
  474. }
  475. /// <summary>
  476. /// Generates the code for a JumpLabel node.
  477. /// </summary>
  478. /// <param name="jl">The JumpLabel node.</param>
  479. /// <returns>String containing C# code for JumpLabel jl.</returns>
  480. private string GenerateJumpLabel(JumpLabel jl)
  481. {
  482. return Generate(String.Format("{0}:", CheckName(jl.LabelName)), jl) + " NoOp();\n";
  483. }
  484. /// <summary>
  485. /// Generates the code for a JumpStatement node.
  486. /// </summary>
  487. /// <param name="js">The JumpStatement node.</param>
  488. /// <returns>String containing C# code for JumpStatement js.</returns>
  489. private string GenerateJumpStatement(JumpStatement js)
  490. {
  491. return Generate(String.Format("goto {0}", CheckName(js.TargetName)), js);
  492. }
  493. /// <summary>
  494. /// Generates the code for an IfStatement node.
  495. /// </summary>
  496. /// <param name="ifs">The IfStatement node.</param>
  497. /// <returns>String containing C# code for IfStatement ifs.</returns>
  498. private string GenerateIfStatement(IfStatement ifs)
  499. {
  500. string retstr = String.Empty;
  501. retstr += GenerateIndented("if (", ifs);
  502. retstr += GenerateNode((SYMBOL) ifs.kids.Pop());
  503. retstr += GenerateLine(")");
  504. // CompoundStatement handles indentation itself but we need to do it
  505. // otherwise.
  506. bool indentHere = ifs.kids.Top is Statement;
  507. if (indentHere) m_braceCount++;
  508. retstr += GenerateNode((SYMBOL) ifs.kids.Pop());
  509. if (indentHere) m_braceCount--;
  510. if (0 < ifs.kids.Count) // do it again for an else
  511. {
  512. retstr += GenerateIndentedLine("else", ifs);
  513. indentHere = ifs.kids.Top is Statement;
  514. if (indentHere) m_braceCount++;
  515. retstr += GenerateNode((SYMBOL) ifs.kids.Pop());
  516. if (indentHere) m_braceCount--;
  517. }
  518. return retstr;
  519. }
  520. /// <summary>
  521. /// Generates the code for a StateChange node.
  522. /// </summary>
  523. /// <param name="sc">The StateChange node.</param>
  524. /// <returns>String containing C# code for StateChange sc.</returns>
  525. private string GenerateStateChange(StateChange sc)
  526. {
  527. return Generate(String.Format("state(\"{0}\")", sc.NewState), sc);
  528. }
  529. /// <summary>
  530. /// Generates the code for a WhileStatement node.
  531. /// </summary>
  532. /// <param name="ws">The WhileStatement node.</param>
  533. /// <returns>String containing C# code for WhileStatement ws.</returns>
  534. private string GenerateWhileStatement(WhileStatement ws)
  535. {
  536. string retstr = String.Empty;
  537. retstr += GenerateIndented("while (", ws);
  538. retstr += GenerateNode((SYMBOL) ws.kids.Pop());
  539. retstr += GenerateLine(")");
  540. // CompoundStatement handles indentation itself but we need to do it
  541. // otherwise.
  542. bool indentHere = ws.kids.Top is Statement;
  543. if (indentHere) m_braceCount++;
  544. retstr += GenerateNode((SYMBOL) ws.kids.Pop());
  545. if (indentHere) m_braceCount--;
  546. return retstr;
  547. }
  548. /// <summary>
  549. /// Generates the code for a DoWhileStatement node.
  550. /// </summary>
  551. /// <param name="dws">The DoWhileStatement node.</param>
  552. /// <returns>String containing C# code for DoWhileStatement dws.</returns>
  553. private string GenerateDoWhileStatement(DoWhileStatement dws)
  554. {
  555. string retstr = String.Empty;
  556. retstr += GenerateIndentedLine("do", dws);
  557. // CompoundStatement handles indentation itself but we need to do it
  558. // otherwise.
  559. bool indentHere = dws.kids.Top is Statement;
  560. if (indentHere) m_braceCount++;
  561. retstr += GenerateNode((SYMBOL) dws.kids.Pop());
  562. if (indentHere) m_braceCount--;
  563. retstr += GenerateIndented("while (", dws);
  564. retstr += GenerateNode((SYMBOL) dws.kids.Pop());
  565. retstr += GenerateLine(");");
  566. return retstr;
  567. }
  568. /// <summary>
  569. /// Generates the code for a ForLoop node.
  570. /// </summary>
  571. /// <param name="fl">The ForLoop node.</param>
  572. /// <returns>String containing C# code for ForLoop fl.</returns>
  573. private string GenerateForLoop(ForLoop fl)
  574. {
  575. string retstr = String.Empty;
  576. retstr += GenerateIndented("for (", fl);
  577. // It's possible that we don't have an assignment, in which case
  578. // the child will be null and we only print the semicolon.
  579. // for (x = 0; x < 10; x++)
  580. // ^^^^^
  581. ForLoopStatement s = (ForLoopStatement) fl.kids.Pop();
  582. if (null != s)
  583. {
  584. retstr += GenerateForLoopStatement(s);
  585. }
  586. retstr += Generate("; ");
  587. // for (x = 0; x < 10; x++)
  588. // ^^^^^^
  589. retstr += GenerateNode((SYMBOL) fl.kids.Pop());
  590. retstr += Generate("; ");
  591. // for (x = 0; x < 10; x++)
  592. // ^^^
  593. retstr += GenerateForLoopStatement((ForLoopStatement) fl.kids.Pop());
  594. retstr += GenerateLine(")");
  595. // CompoundStatement handles indentation itself but we need to do it
  596. // otherwise.
  597. bool indentHere = fl.kids.Top is Statement;
  598. if (indentHere) m_braceCount++;
  599. retstr += GenerateNode((SYMBOL) fl.kids.Pop());
  600. if (indentHere) m_braceCount--;
  601. return retstr;
  602. }
  603. /// <summary>
  604. /// Generates the code for a ForLoopStatement node.
  605. /// </summary>
  606. /// <param name="fls">The ForLoopStatement node.</param>
  607. /// <returns>String containing C# code for ForLoopStatement fls.</returns>
  608. private string GenerateForLoopStatement(ForLoopStatement fls)
  609. {
  610. string retstr = String.Empty;
  611. int comma = fls.kids.Count - 1; // tells us whether to print a comma
  612. // It's possible that all we have is an empty Ident, for example:
  613. //
  614. // for (x; x < 10; x++) { ... }
  615. //
  616. // Which is illegal in C# (MONO). We'll skip it.
  617. if (fls.kids.Top is IdentExpression && 1 == fls.kids.Count)
  618. return retstr;
  619. foreach (SYMBOL s in fls.kids)
  620. {
  621. retstr += GenerateNode(s);
  622. if (0 < comma--)
  623. retstr += Generate(", ");
  624. }
  625. return retstr;
  626. }
  627. /// <summary>
  628. /// Generates the code for a BinaryExpression node.
  629. /// </summary>
  630. /// <param name="be">The BinaryExpression node.</param>
  631. /// <returns>String containing C# code for BinaryExpression be.</returns>
  632. private string GenerateBinaryExpression(BinaryExpression be)
  633. {
  634. string retstr = String.Empty;
  635. if (be.ExpressionSymbol.Equals("&&") || be.ExpressionSymbol.Equals("||"))
  636. {
  637. // special case handling for logical and/or, see Mantis 3174
  638. retstr += "((bool)(";
  639. retstr += GenerateNode((SYMBOL)be.kids.Pop());
  640. retstr += "))";
  641. retstr += Generate(String.Format(" {0} ", be.ExpressionSymbol.Substring(0,1)), be);
  642. retstr += "((bool)(";
  643. foreach (SYMBOL kid in be.kids)
  644. retstr += GenerateNode(kid);
  645. retstr += "))";
  646. }
  647. else
  648. {
  649. retstr += GenerateNode((SYMBOL)be.kids.Pop());
  650. retstr += Generate(String.Format(" {0} ", be.ExpressionSymbol), be);
  651. foreach (SYMBOL kid in be.kids)
  652. retstr += GenerateNode(kid);
  653. }
  654. return retstr;
  655. }
  656. /// <summary>
  657. /// Generates the code for a UnaryExpression node.
  658. /// </summary>
  659. /// <param name="ue">The UnaryExpression node.</param>
  660. /// <returns>String containing C# code for UnaryExpression ue.</returns>
  661. private string GenerateUnaryExpression(UnaryExpression ue)
  662. {
  663. string retstr = String.Empty;
  664. retstr += Generate(ue.UnarySymbol, ue);
  665. retstr += GenerateNode((SYMBOL) ue.kids.Pop());
  666. return retstr;
  667. }
  668. /// <summary>
  669. /// Generates the code for a ParenthesisExpression node.
  670. /// </summary>
  671. /// <param name="pe">The ParenthesisExpression node.</param>
  672. /// <returns>String containing C# code for ParenthesisExpression pe.</returns>
  673. private string GenerateParenthesisExpression(ParenthesisExpression pe)
  674. {
  675. string retstr = String.Empty;
  676. retstr += Generate("(");
  677. foreach (SYMBOL kid in pe.kids)
  678. retstr += GenerateNode(kid);
  679. retstr += Generate(")");
  680. return retstr;
  681. }
  682. /// <summary>
  683. /// Generates the code for a IncrementDecrementExpression node.
  684. /// </summary>
  685. /// <param name="ide">The IncrementDecrementExpression node.</param>
  686. /// <returns>String containing C# code for IncrementDecrementExpression ide.</returns>
  687. private string GenerateIncrementDecrementExpression(IncrementDecrementExpression ide)
  688. {
  689. string retstr = String.Empty;
  690. if (0 < ide.kids.Count)
  691. {
  692. IdentDotExpression dot = (IdentDotExpression) ide.kids.Top;
  693. retstr += Generate(String.Format("{0}", ide.PostOperation ? CheckName(dot.Name) + "." + dot.Member + ide.Operation : ide.Operation + CheckName(dot.Name) + "." + dot.Member), ide);
  694. }
  695. else
  696. retstr += Generate(String.Format("{0}", ide.PostOperation ? CheckName(ide.Name) + ide.Operation : ide.Operation + CheckName(ide.Name)), ide);
  697. return retstr;
  698. }
  699. /// <summary>
  700. /// Generates the code for a TypecastExpression node.
  701. /// </summary>
  702. /// <param name="te">The TypecastExpression node.</param>
  703. /// <returns>String containing C# code for TypecastExpression te.</returns>
  704. private string GenerateTypecastExpression(TypecastExpression te)
  705. {
  706. string retstr = String.Empty;
  707. // we wrap all typecasted statements in parentheses
  708. retstr += Generate(String.Format("({0}) (", te.TypecastType), te);
  709. retstr += GenerateNode((SYMBOL) te.kids.Pop());
  710. retstr += Generate(")");
  711. return retstr;
  712. }
  713. /// <summary>
  714. /// Generates the code for a FunctionCall node.
  715. /// </summary>
  716. /// <param name="fc">The FunctionCall node.</param>
  717. /// <returns>String containing C# code for FunctionCall fc.</returns>
  718. private string GenerateFunctionCall(FunctionCall fc)
  719. {
  720. string retstr = String.Empty;
  721. retstr += Generate(String.Format("{0}(", CheckName(fc.Id)), fc);
  722. foreach (SYMBOL kid in fc.kids)
  723. retstr += GenerateNode(kid);
  724. retstr += Generate(")");
  725. return retstr;
  726. }
  727. /// <summary>
  728. /// Generates the code for a Constant node.
  729. /// </summary>
  730. /// <param name="c">The Constant node.</param>
  731. /// <returns>String containing C# code for Constant c.</returns>
  732. private string GenerateConstant(Constant c)
  733. {
  734. string retstr = String.Empty;
  735. // Supprt LSL's weird acceptance of floats with no trailing digits
  736. // after the period. Turn float x = 10.; into float x = 10.0;
  737. if ("LSL_Types.LSLFloat" == c.Type)
  738. {
  739. int dotIndex = c.Value.IndexOf('.') + 1;
  740. if (0 < dotIndex && (dotIndex == c.Value.Length || !Char.IsDigit(c.Value[dotIndex])))
  741. c.Value = c.Value.Insert(dotIndex, "0");
  742. c.Value = "new LSL_Types.LSLFloat("+c.Value+")";
  743. }
  744. else if ("LSL_Types.LSLInteger" == c.Type)
  745. {
  746. c.Value = "new LSL_Types.LSLInteger("+c.Value+")";
  747. }
  748. else if ("LSL_Types.LSLString" == c.Type)
  749. {
  750. c.Value = "new LSL_Types.LSLString(\""+c.Value+"\")";
  751. }
  752. retstr += Generate(c.Value, c);
  753. return retstr;
  754. }
  755. /// <summary>
  756. /// Generates the code for a VectorConstant node.
  757. /// </summary>
  758. /// <param name="vc">The VectorConstant node.</param>
  759. /// <returns>String containing C# code for VectorConstant vc.</returns>
  760. private string GenerateVectorConstant(VectorConstant vc)
  761. {
  762. string retstr = String.Empty;
  763. retstr += Generate(String.Format("new {0}(", vc.Type), vc);
  764. retstr += GenerateNode((SYMBOL) vc.kids.Pop());
  765. retstr += Generate(", ");
  766. retstr += GenerateNode((SYMBOL) vc.kids.Pop());
  767. retstr += Generate(", ");
  768. retstr += GenerateNode((SYMBOL) vc.kids.Pop());
  769. retstr += Generate(")");
  770. return retstr;
  771. }
  772. /// <summary>
  773. /// Generates the code for a RotationConstant node.
  774. /// </summary>
  775. /// <param name="rc">The RotationConstant node.</param>
  776. /// <returns>String containing C# code for RotationConstant rc.</returns>
  777. private string GenerateRotationConstant(RotationConstant rc)
  778. {
  779. string retstr = String.Empty;
  780. retstr += Generate(String.Format("new {0}(", rc.Type), rc);
  781. retstr += GenerateNode((SYMBOL) rc.kids.Pop());
  782. retstr += Generate(", ");
  783. retstr += GenerateNode((SYMBOL) rc.kids.Pop());
  784. retstr += Generate(", ");
  785. retstr += GenerateNode((SYMBOL) rc.kids.Pop());
  786. retstr += Generate(", ");
  787. retstr += GenerateNode((SYMBOL) rc.kids.Pop());
  788. retstr += Generate(")");
  789. return retstr;
  790. }
  791. /// <summary>
  792. /// Generates the code for a ListConstant node.
  793. /// </summary>
  794. /// <param name="lc">The ListConstant node.</param>
  795. /// <returns>String containing C# code for ListConstant lc.</returns>
  796. private string GenerateListConstant(ListConstant lc)
  797. {
  798. string retstr = String.Empty;
  799. retstr += Generate(String.Format("new {0}(", lc.Type), lc);
  800. foreach (SYMBOL kid in lc.kids)
  801. retstr += GenerateNode(kid);
  802. retstr += Generate(")");
  803. return retstr;
  804. }
  805. /// <summary>
  806. /// Prints a newline.
  807. /// </summary>
  808. /// <returns>A newline.</returns>
  809. private string GenerateLine()
  810. {
  811. return GenerateLine("");
  812. }
  813. /// <summary>
  814. /// Prints text, followed by a newline.
  815. /// </summary>
  816. /// <param name="s">String of text to print.</param>
  817. /// <returns>String s followed by newline.</returns>
  818. private string GenerateLine(string s)
  819. {
  820. return GenerateLine(s, null);
  821. }
  822. /// <summary>
  823. /// Prints text, followed by a newline.
  824. /// </summary>
  825. /// <param name="s">String of text to print.</param>
  826. /// <param name="sym">Symbol being generated to extract original line
  827. /// number and column from.</param>
  828. /// <returns>String s followed by newline.</returns>
  829. private string GenerateLine(string s, SYMBOL sym)
  830. {
  831. string retstr = Generate(s, sym) + "\n";
  832. m_CSharpLine++;
  833. m_CSharpCol = 1;
  834. return retstr;
  835. }
  836. /// <summary>
  837. /// Prints text.
  838. /// </summary>
  839. /// <param name="s">String of text to print.</param>
  840. /// <returns>String s.</returns>
  841. private string Generate(string s)
  842. {
  843. return Generate(s, null);
  844. }
  845. /// <summary>
  846. /// Prints text.
  847. /// </summary>
  848. /// <param name="s">String of text to print.</param>
  849. /// <param name="sym">Symbol being generated to extract original line
  850. /// number and column from.</param>
  851. /// <returns>String s.</returns>
  852. private string Generate(string s, SYMBOL sym)
  853. {
  854. if (null != sym)
  855. m_positionMap.Add(new KeyValuePair<int, int>(m_CSharpLine, m_CSharpCol), new KeyValuePair<int, int>(sym.Line, sym.Position));
  856. m_CSharpCol += s.Length;
  857. return s;
  858. }
  859. /// <summary>
  860. /// Prints text correctly indented, followed by a newline.
  861. /// </summary>
  862. /// <param name="s">String of text to print.</param>
  863. /// <returns>Properly indented string s followed by newline.</returns>
  864. private string GenerateIndentedLine(string s)
  865. {
  866. return GenerateIndentedLine(s, null);
  867. }
  868. /// <summary>
  869. /// Prints text correctly indented, followed by a newline.
  870. /// </summary>
  871. /// <param name="s">String of text to print.</param>
  872. /// <param name="sym">Symbol being generated to extract original line
  873. /// number and column from.</param>
  874. /// <returns>Properly indented string s followed by newline.</returns>
  875. private string GenerateIndentedLine(string s, SYMBOL sym)
  876. {
  877. string retstr = GenerateIndented(s, sym) + "\n";
  878. m_CSharpLine++;
  879. m_CSharpCol = 1;
  880. return retstr;
  881. }
  882. /// <summary>
  883. /// Prints text correctly indented.
  884. /// </summary>
  885. /// <param name="s">String of text to print.</param>
  886. /// <returns>Properly indented string s.</returns>
  887. //private string GenerateIndented(string s)
  888. //{
  889. // return GenerateIndented(s, null);
  890. //}
  891. // THIS FUNCTION IS COMMENTED OUT TO SUPPRESS WARNINGS
  892. /// <summary>
  893. /// Prints text correctly indented.
  894. /// </summary>
  895. /// <param name="s">String of text to print.</param>
  896. /// <param name="sym">Symbol being generated to extract original line
  897. /// number and column from.</param>
  898. /// <returns>Properly indented string s.</returns>
  899. private string GenerateIndented(string s, SYMBOL sym)
  900. {
  901. string retstr = Indent() + s;
  902. if (null != sym)
  903. m_positionMap.Add(new KeyValuePair<int, int>(m_CSharpLine, m_CSharpCol), new KeyValuePair<int, int>(sym.Line, sym.Position));
  904. m_CSharpCol += s.Length;
  905. return retstr;
  906. }
  907. /// <summary>
  908. /// Prints correct indentation.
  909. /// </summary>
  910. /// <returns>Indentation based on brace count.</returns>
  911. private string Indent()
  912. {
  913. string retstr = String.Empty;
  914. for (int i = 0; i < m_braceCount; i++)
  915. for (int j = 0; j < m_indentWidth; j++)
  916. {
  917. retstr += " ";
  918. m_CSharpCol++;
  919. }
  920. return retstr;
  921. }
  922. /// <summary>
  923. /// Returns the passed name with an underscore prepended if that name is a reserved word in C#
  924. /// and not resevered in LSL otherwise it just returns the passed name.
  925. ///
  926. /// This makes no attempt to cache the results to minimise future lookups. For a non trivial
  927. /// scripts the number of unique identifiers could easily grow to the size of the reserved word
  928. /// list so maintaining a list or dictionary and doing the lookup there firstwould probably not
  929. /// give any real speed advantage.
  930. ///
  931. /// I believe there is a class Microsoft.CSharp.CSharpCodeProvider that has a function
  932. /// CreateValidIdentifier(str) that will return either the value of str if it is not a C#
  933. /// key word or "_"+str if it is. But availability under Mono?
  934. /// </summary>
  935. private string CheckName(string s)
  936. {
  937. if (CSReservedWords.IsReservedWord(s))
  938. return "@" + s;
  939. else
  940. return s;
  941. }
  942. }
  943. }