/* * Copyright (c) Contributors, http://opensimulator.org/ * See CONTRIBUTORS.TXT for a full list of copyright holders. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * Neither the name of the OpenSimulator Project nor the * names of its contributors may be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ using System; using System.IO; using System.Collections.Generic; using Tools; namespace OpenSim.Region.ScriptEngine.Shared.CodeTools { public class CSCodeGenerator : ICodeConverter { private SYMBOL m_astRoot = null; private Dictionary, KeyValuePair> m_positionMap; private int m_indentWidth = 4; // for indentation private int m_braceCount; // for indentation private int m_CSharpLine; // the current line of generated C# code private int m_CSharpCol; // the current column of generated C# code private List m_warnings = new List(); /// /// Creates an 'empty' CSCodeGenerator instance. /// public CSCodeGenerator() { ResetCounters(); } /// /// Get the mapping between LSL and C# line/column number. /// /// Dictionary\, KeyValuePair\\>. public Dictionary, KeyValuePair> PositionMap { get { return m_positionMap; } } /// /// Get the mapping between LSL and C# line/column number. /// /// SYMBOL pointing to root of the abstract syntax tree. public SYMBOL ASTRoot { get { return m_astRoot; } } /// /// Resets various counters and metadata. /// private void ResetCounters() { m_braceCount = 0; m_CSharpLine = 0; m_CSharpCol = 1; m_positionMap = new Dictionary, KeyValuePair>(); m_astRoot = null; } /// /// Generate the code from the AST we have. /// /// The LSL source as a string. /// String containing the generated C# code. public string Convert(string script) { m_warnings.Clear(); ResetCounters(); Parser p = new LSLSyntax(new yyLSLSyntax(), new ErrorHandler(true)); // Obviously this needs to be in a try/except block. LSL2CSCodeTransformer codeTransformer; try { codeTransformer = new LSL2CSCodeTransformer(p.Parse(script)); } catch (CSToolsException e) { string message; // LL start numbering lines at 0 - geeks! // Also need to subtract one line we prepend! // string emessage = e.Message; string slinfo = e.slInfo.ToString(); // Remove wrong line number info // if (emessage.StartsWith(slinfo+": ")) emessage = emessage.Substring(slinfo.Length+2); message = String.Format("Line ({0},{1}) {2}", e.slInfo.lineNumber - 2, e.slInfo.charPosition - 1, emessage); throw new Exception(message); } m_astRoot = codeTransformer.Transform(); string retstr = String.Empty; // standard preamble //retstr = GenerateLine("using OpenSim.Region.ScriptEngine.Common;"); //retstr += GenerateLine("using System.Collections.Generic;"); //retstr += GenerateLine(""); //retstr += GenerateLine("namespace SecondLife"); //retstr += GenerateLine("{"); m_braceCount++; //retstr += GenerateIndentedLine("public class Script : OpenSim.Region.ScriptEngine.Common"); //retstr += GenerateIndentedLine("{"); m_braceCount++; // line number m_CSharpLine += 3; // here's the payload retstr += GenerateLine(); foreach (SYMBOL s in m_astRoot.kids) retstr += GenerateNode(s); // close braces! m_braceCount--; //retstr += GenerateIndentedLine("}"); m_braceCount--; //retstr += GenerateLine("}"); // Removes all carriage return characters which may be generated in Windows platform. Is there // cleaner way of doing this? retstr=retstr.Replace("\r", ""); return retstr; } /// /// Get the set of warnings generated during compilation. /// /// public string[] GetWarnings() { return m_warnings.ToArray(); } private void AddWarning(string warning) { if (!m_warnings.Contains(warning)) { m_warnings.Add(warning); } } /// /// Recursively called to generate each type of node. Will generate this /// node, then all it's children. /// /// The current node to generate code for. /// String containing C# code for SYMBOL s. private string GenerateNode(SYMBOL s) { string retstr = String.Empty; // make sure to put type lower in the inheritance hierarchy first // ie: since IdentArgument and ExpressionArgument inherit from // Argument, put IdentArgument and ExpressionArgument before Argument if (s is GlobalFunctionDefinition) retstr += GenerateGlobalFunctionDefinition((GlobalFunctionDefinition) s); else if (s is GlobalVariableDeclaration) retstr += GenerateGlobalVariableDeclaration((GlobalVariableDeclaration) s); else if (s is State) retstr += GenerateState((State) s); else if (s is CompoundStatement) retstr += GenerateCompoundStatement((CompoundStatement) s); else if (s is Declaration) retstr += GenerateDeclaration((Declaration) s); else if (s is Statement) retstr += GenerateStatement((Statement) s); else if (s is ReturnStatement) retstr += GenerateReturnStatement((ReturnStatement) s); else if (s is JumpLabel) retstr += GenerateJumpLabel((JumpLabel) s); else if (s is JumpStatement) retstr += GenerateJumpStatement((JumpStatement) s); else if (s is StateChange) retstr += GenerateStateChange((StateChange) s); else if (s is IfStatement) retstr += GenerateIfStatement((IfStatement) s); else if (s is WhileStatement) retstr += GenerateWhileStatement((WhileStatement) s); else if (s is DoWhileStatement) retstr += GenerateDoWhileStatement((DoWhileStatement) s); else if (s is ForLoop) retstr += GenerateForLoop((ForLoop) s); else if (s is ArgumentList) retstr += GenerateArgumentList((ArgumentList) s); else if (s is Assignment) retstr += GenerateAssignment((Assignment) s); else if (s is BinaryExpression) retstr += GenerateBinaryExpression((BinaryExpression) s); else if (s is ParenthesisExpression) retstr += GenerateParenthesisExpression((ParenthesisExpression) s); else if (s is UnaryExpression) retstr += GenerateUnaryExpression((UnaryExpression) s); else if (s is IncrementDecrementExpression) retstr += GenerateIncrementDecrementExpression((IncrementDecrementExpression) s); else if (s is TypecastExpression) retstr += GenerateTypecastExpression((TypecastExpression) s); else if (s is FunctionCall) retstr += GenerateFunctionCall((FunctionCall) s); else if (s is VectorConstant) retstr += GenerateVectorConstant((VectorConstant) s); else if (s is RotationConstant) retstr += GenerateRotationConstant((RotationConstant) s); else if (s is ListConstant) retstr += GenerateListConstant((ListConstant) s); else if (s is Constant) retstr += GenerateConstant((Constant) s); else if (s is IdentDotExpression) retstr += Generate(CheckName(((IdentDotExpression) s).Name) + "." + ((IdentDotExpression) s).Member, s); else if (s is IdentExpression) retstr += Generate(CheckName(((IdentExpression) s).Name), s); else if (s is IDENT) retstr += Generate(CheckName(((TOKEN) s).yytext), s); else { foreach (SYMBOL kid in s.kids) retstr += GenerateNode(kid); } return retstr; } /// /// Generates the code for a GlobalFunctionDefinition node. /// /// The GlobalFunctionDefinition node. /// String containing C# code for GlobalFunctionDefinition gf. private string GenerateGlobalFunctionDefinition(GlobalFunctionDefinition gf) { string retstr = String.Empty; // we need to separate the argument declaration list from other kids List argumentDeclarationListKids = new List(); List remainingKids = new List(); foreach (SYMBOL kid in gf.kids) if (kid is ArgumentDeclarationList) argumentDeclarationListKids.Add(kid); else remainingKids.Add(kid); retstr += GenerateIndented(String.Format("{0} {1}(", gf.ReturnType, CheckName(gf.Name)), gf); // print the state arguments, if any foreach (SYMBOL kid in argumentDeclarationListKids) retstr += GenerateArgumentDeclarationList((ArgumentDeclarationList) kid); retstr += GenerateLine(")"); foreach (SYMBOL kid in remainingKids) retstr += GenerateNode(kid); return retstr; } /// /// Generates the code for a GlobalVariableDeclaration node. /// /// The GlobalVariableDeclaration node. /// String containing C# code for GlobalVariableDeclaration gv. private string GenerateGlobalVariableDeclaration(GlobalVariableDeclaration gv) { string retstr = String.Empty; foreach (SYMBOL s in gv.kids) { retstr += Indent(); retstr += GenerateNode(s); retstr += GenerateLine(";"); } return retstr; } /// /// Generates the code for a State node. /// /// The State node. /// String containing C# code for State s. private string GenerateState(State s) { string retstr = String.Empty; foreach (SYMBOL kid in s.kids) if (kid is StateEvent) retstr += GenerateStateEvent((StateEvent) kid, s.Name); return retstr; } /// /// Generates the code for a StateEvent node. /// /// The StateEvent node. /// The name of the parent state. /// String containing C# code for StateEvent se. private string GenerateStateEvent(StateEvent se, string parentStateName) { string retstr = String.Empty; // we need to separate the argument declaration list from other kids List argumentDeclarationListKids = new List(); List remainingKids = new List(); foreach (SYMBOL kid in se.kids) if (kid is ArgumentDeclarationList) argumentDeclarationListKids.Add(kid); else remainingKids.Add(kid); // "state" (function) declaration retstr += GenerateIndented(String.Format("public void {0}_event_{1}(", parentStateName, se.Name), se); // print the state arguments, if any foreach (SYMBOL kid in argumentDeclarationListKids) retstr += GenerateArgumentDeclarationList((ArgumentDeclarationList) kid); retstr += GenerateLine(")"); foreach (SYMBOL kid in remainingKids) retstr += GenerateNode(kid); return retstr; } /// /// Generates the code for an ArgumentDeclarationList node. /// /// The ArgumentDeclarationList node. /// String containing C# code for ArgumentDeclarationList adl. private string GenerateArgumentDeclarationList(ArgumentDeclarationList adl) { string retstr = String.Empty; int comma = adl.kids.Count - 1; // tells us whether to print a comma foreach (Declaration d in adl.kids) { retstr += Generate(String.Format("{0} {1}", d.Datatype, CheckName(d.Id)), d); if (0 < comma--) retstr += Generate(", "); } return retstr; } /// /// Generates the code for an ArgumentList node. /// /// The ArgumentList node. /// String containing C# code for ArgumentList al. private string GenerateArgumentList(ArgumentList al) { string retstr = String.Empty; int comma = al.kids.Count - 1; // tells us whether to print a comma foreach (SYMBOL s in al.kids) { retstr += GenerateNode(s); if (0 < comma--) retstr += Generate(", "); } return retstr; } /// /// Generates the code for a CompoundStatement node. /// /// The CompoundStatement node. /// String containing C# code for CompoundStatement cs. private string GenerateCompoundStatement(CompoundStatement cs) { string retstr = String.Empty; // opening brace retstr += GenerateIndentedLine("{"); m_braceCount++; foreach (SYMBOL kid in cs.kids) retstr += GenerateNode(kid); // closing brace m_braceCount--; retstr += GenerateIndentedLine("}"); return retstr; } /// /// Generates the code for a Declaration node. /// /// The Declaration node. /// String containing C# code for Declaration d. private string GenerateDeclaration(Declaration d) { return Generate(String.Format("{0} {1}", d.Datatype, CheckName(d.Id)), d); } /// /// Generates the code for a Statement node. /// /// The Statement node. /// String containing C# code for Statement s. private string GenerateStatement(Statement s) { string retstr = String.Empty; bool printSemicolon = true; retstr += Indent(); if (0 < s.kids.Count) { // Jump label prints its own colon, we don't need a semicolon. printSemicolon = !(s.kids.Top is JumpLabel); foreach (SYMBOL kid in s.kids) retstr += GenerateNode(kid); } if (printSemicolon) retstr += GenerateLine(";"); return retstr; } /// /// Generates the code for an Assignment node. /// /// The Assignment node. /// String containing C# code for Assignment a. private string GenerateAssignment(Assignment a) { string retstr = String.Empty; List identifiers = new List(); checkForMultipleAssignments(identifiers, a); retstr += GenerateNode((SYMBOL) a.kids.Pop()); retstr += Generate(String.Format(" {0} ", a.AssignmentType), a); foreach (SYMBOL kid in a.kids) retstr += GenerateNode(kid); return retstr; } // This code checks for LSL of the following forms, and generates a // warning if it finds them. // // list l = [ "foo" ]; // l = (l=[]) + l + ["bar"]; // (produces l=["foo","bar"] in SL but l=["bar"] in OS) // // integer i; // integer j; // i = (j = 3) + (j = 4) + (j = 5); // (produces j=3 in SL but j=5 in OS) // // Without this check, that code passes compilation, but does not do what // the end user expects, because LSL in SL evaluates right to left instead // of left to right. // // The theory here is that producing an error and alerting the end user that // something needs to change is better than silently generating incorrect code. private void checkForMultipleAssignments(List identifiers, SYMBOL s) { if (s is Assignment) { Assignment a = (Assignment)s; string newident = null; if (a.kids[0] is Declaration) { newident = ((Declaration)a.kids[0]).Id; } else if (a.kids[0] is IDENT) { newident = ((IDENT)a.kids[0]).yytext; } else if (a.kids[0] is IdentDotExpression) { newident = ((IdentDotExpression)a.kids[0]).Name; // +"." + ((IdentDotExpression)a.kids[0]).Member; } else { 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)); } if (identifiers.Contains(newident)) { 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)); } identifiers.Add(newident); } int index; for (index = 0; index < s.kids.Count; index++) { checkForMultipleAssignments(identifiers, (SYMBOL) s.kids[index]); } } /// /// Generates the code for a ReturnStatement node. /// /// The ReturnStatement node. /// String containing C# code for ReturnStatement rs. private string GenerateReturnStatement(ReturnStatement rs) { string retstr = String.Empty; retstr += Generate("return ", rs); foreach (SYMBOL kid in rs.kids) retstr += GenerateNode(kid); return retstr; } /// /// Generates the code for a JumpLabel node. /// /// The JumpLabel node. /// String containing C# code for JumpLabel jl. private string GenerateJumpLabel(JumpLabel jl) { return Generate(String.Format("{0}:", CheckName(jl.LabelName)), jl) + " NoOp();\n"; } /// /// Generates the code for a JumpStatement node. /// /// The JumpStatement node. /// String containing C# code for JumpStatement js. private string GenerateJumpStatement(JumpStatement js) { return Generate(String.Format("goto {0}", CheckName(js.TargetName)), js); } /// /// Generates the code for an IfStatement node. /// /// The IfStatement node. /// String containing C# code for IfStatement ifs. private string GenerateIfStatement(IfStatement ifs) { string retstr = String.Empty; retstr += GenerateIndented("if (", ifs); retstr += GenerateNode((SYMBOL) ifs.kids.Pop()); retstr += GenerateLine(")"); // CompoundStatement handles indentation itself but we need to do it // otherwise. bool indentHere = ifs.kids.Top is Statement; if (indentHere) m_braceCount++; retstr += GenerateNode((SYMBOL) ifs.kids.Pop()); if (indentHere) m_braceCount--; if (0 < ifs.kids.Count) // do it again for an else { retstr += GenerateIndentedLine("else", ifs); indentHere = ifs.kids.Top is Statement; if (indentHere) m_braceCount++; retstr += GenerateNode((SYMBOL) ifs.kids.Pop()); if (indentHere) m_braceCount--; } return retstr; } /// /// Generates the code for a StateChange node. /// /// The StateChange node. /// String containing C# code for StateChange sc. private string GenerateStateChange(StateChange sc) { return Generate(String.Format("state(\"{0}\")", sc.NewState), sc); } /// /// Generates the code for a WhileStatement node. /// /// The WhileStatement node. /// String containing C# code for WhileStatement ws. private string GenerateWhileStatement(WhileStatement ws) { string retstr = String.Empty; retstr += GenerateIndented("while (", ws); retstr += GenerateNode((SYMBOL) ws.kids.Pop()); retstr += GenerateLine(")"); // CompoundStatement handles indentation itself but we need to do it // otherwise. bool indentHere = ws.kids.Top is Statement; if (indentHere) m_braceCount++; retstr += GenerateNode((SYMBOL) ws.kids.Pop()); if (indentHere) m_braceCount--; return retstr; } /// /// Generates the code for a DoWhileStatement node. /// /// The DoWhileStatement node. /// String containing C# code for DoWhileStatement dws. private string GenerateDoWhileStatement(DoWhileStatement dws) { string retstr = String.Empty; retstr += GenerateIndentedLine("do", dws); // CompoundStatement handles indentation itself but we need to do it // otherwise. bool indentHere = dws.kids.Top is Statement; if (indentHere) m_braceCount++; retstr += GenerateNode((SYMBOL) dws.kids.Pop()); if (indentHere) m_braceCount--; retstr += GenerateIndented("while (", dws); retstr += GenerateNode((SYMBOL) dws.kids.Pop()); retstr += GenerateLine(");"); return retstr; } /// /// Generates the code for a ForLoop node. /// /// The ForLoop node. /// String containing C# code for ForLoop fl. private string GenerateForLoop(ForLoop fl) { string retstr = String.Empty; retstr += GenerateIndented("for (", fl); // for ( x = 0 ; x < 10 ; x++ ) // ^^^^^^^ retstr += GenerateForLoopStatement((ForLoopStatement) fl.kids.Pop()); retstr += Generate("; "); // for ( x = 0 ; x < 10 ; x++ ) // ^^^^^^^^ retstr += GenerateNode((SYMBOL) fl.kids.Pop()); retstr += Generate("; "); // for ( x = 0 ; x < 10 ; x++ ) // ^^^^^ retstr += GenerateForLoopStatement((ForLoopStatement) fl.kids.Pop()); retstr += GenerateLine(")"); // CompoundStatement handles indentation itself but we need to do it // otherwise. bool indentHere = fl.kids.Top is Statement; if (indentHere) m_braceCount++; retstr += GenerateNode((SYMBOL) fl.kids.Pop()); if (indentHere) m_braceCount--; return retstr; } /// /// Generates the code for a ForLoopStatement node. /// /// The ForLoopStatement node. /// String containing C# code for ForLoopStatement fls. private string GenerateForLoopStatement(ForLoopStatement fls) { string retstr = String.Empty; int comma = fls.kids.Count - 1; // tells us whether to print a comma foreach (SYMBOL s in fls.kids) { retstr += GenerateNode(s); if (0 < comma--) retstr += Generate(", "); } return retstr; } /// /// Generates the code for a BinaryExpression node. /// /// The BinaryExpression node. /// String containing C# code for BinaryExpression be. private string GenerateBinaryExpression(BinaryExpression be) { string retstr = String.Empty; if (be.ExpressionSymbol.Equals("&&") || be.ExpressionSymbol.Equals("||")) { // special case handling for logical and/or, see Mantis 3174 retstr += "((bool)("; retstr += GenerateNode((SYMBOL)be.kids.Pop()); retstr += "))"; retstr += Generate(String.Format(" {0} ", be.ExpressionSymbol.Substring(0,1)), be); retstr += "((bool)("; foreach (SYMBOL kid in be.kids) retstr += GenerateNode(kid); retstr += "))"; } else { retstr += GenerateNode((SYMBOL)be.kids.Pop()); retstr += Generate(String.Format(" {0} ", be.ExpressionSymbol), be); foreach (SYMBOL kid in be.kids) retstr += GenerateNode(kid); } return retstr; } /// /// Generates the code for a UnaryExpression node. /// /// The UnaryExpression node. /// String containing C# code for UnaryExpression ue. private string GenerateUnaryExpression(UnaryExpression ue) { string retstr = String.Empty; retstr += Generate(ue.UnarySymbol, ue); retstr += GenerateNode((SYMBOL) ue.kids.Pop()); return retstr; } /// /// Generates the code for a ParenthesisExpression node. /// /// The ParenthesisExpression node. /// String containing C# code for ParenthesisExpression pe. private string GenerateParenthesisExpression(ParenthesisExpression pe) { string retstr = String.Empty; retstr += Generate("("); foreach (SYMBOL kid in pe.kids) retstr += GenerateNode(kid); retstr += Generate(")"); return retstr; } /// /// Generates the code for a IncrementDecrementExpression node. /// /// The IncrementDecrementExpression node. /// String containing C# code for IncrementDecrementExpression ide. private string GenerateIncrementDecrementExpression(IncrementDecrementExpression ide) { string retstr = String.Empty; if (0 < ide.kids.Count) { IdentDotExpression dot = (IdentDotExpression) ide.kids.Top; retstr += Generate(String.Format("{0}", ide.PostOperation ? CheckName(dot.Name) + "." + dot.Member + ide.Operation : ide.Operation + CheckName(dot.Name) + "." + dot.Member), ide); } else retstr += Generate(String.Format("{0}", ide.PostOperation ? CheckName(ide.Name) + ide.Operation : ide.Operation + CheckName(ide.Name)), ide); return retstr; } /// /// Generates the code for a TypecastExpression node. /// /// The TypecastExpression node. /// String containing C# code for TypecastExpression te. private string GenerateTypecastExpression(TypecastExpression te) { string retstr = String.Empty; // we wrap all typecasted statements in parentheses retstr += Generate(String.Format("({0}) (", te.TypecastType), te); retstr += GenerateNode((SYMBOL) te.kids.Pop()); retstr += Generate(")"); return retstr; } /// /// Generates the code for a FunctionCall node. /// /// The FunctionCall node. /// String containing C# code for FunctionCall fc. private string GenerateFunctionCall(FunctionCall fc) { string retstr = String.Empty; retstr += Generate(String.Format("{0}(", CheckName(fc.Id)), fc); foreach (SYMBOL kid in fc.kids) retstr += GenerateNode(kid); retstr += Generate(")"); return retstr; } /// /// Generates the code for a Constant node. /// /// The Constant node. /// String containing C# code for Constant c. private string GenerateConstant(Constant c) { string retstr = String.Empty; // Supprt LSL's weird acceptance of floats with no trailing digits // after the period. Turn float x = 10.; into float x = 10.0; if ("LSL_Types.LSLFloat" == c.Type) { int dotIndex = c.Value.IndexOf('.') + 1; if (0 < dotIndex && (dotIndex == c.Value.Length || !Char.IsDigit(c.Value[dotIndex]))) c.Value = c.Value.Insert(dotIndex, "0"); c.Value = "new LSL_Types.LSLFloat("+c.Value+")"; } else if ("LSL_Types.LSLInteger" == c.Type) { c.Value = "new LSL_Types.LSLInteger("+c.Value+")"; } else if ("LSL_Types.LSLString" == c.Type) { c.Value = "new LSL_Types.LSLString(\""+c.Value+"\")"; } retstr += Generate(c.Value, c); return retstr; } /// /// Generates the code for a VectorConstant node. /// /// The VectorConstant node. /// String containing C# code for VectorConstant vc. private string GenerateVectorConstant(VectorConstant vc) { string retstr = String.Empty; retstr += Generate(String.Format("new {0}(", vc.Type), vc); retstr += GenerateNode((SYMBOL) vc.kids.Pop()); retstr += Generate(", "); retstr += GenerateNode((SYMBOL) vc.kids.Pop()); retstr += Generate(", "); retstr += GenerateNode((SYMBOL) vc.kids.Pop()); retstr += Generate(")"); return retstr; } /// /// Generates the code for a RotationConstant node. /// /// The RotationConstant node. /// String containing C# code for RotationConstant rc. private string GenerateRotationConstant(RotationConstant rc) { string retstr = String.Empty; retstr += Generate(String.Format("new {0}(", rc.Type), rc); retstr += GenerateNode((SYMBOL) rc.kids.Pop()); retstr += Generate(", "); retstr += GenerateNode((SYMBOL) rc.kids.Pop()); retstr += Generate(", "); retstr += GenerateNode((SYMBOL) rc.kids.Pop()); retstr += Generate(", "); retstr += GenerateNode((SYMBOL) rc.kids.Pop()); retstr += Generate(")"); return retstr; } /// /// Generates the code for a ListConstant node. /// /// The ListConstant node. /// String containing C# code for ListConstant lc. private string GenerateListConstant(ListConstant lc) { string retstr = String.Empty; retstr += Generate(String.Format("new {0}(", lc.Type), lc); foreach (SYMBOL kid in lc.kids) retstr += GenerateNode(kid); retstr += Generate(")"); return retstr; } /// /// Prints a newline. /// /// A newline. private string GenerateLine() { return GenerateLine(""); } /// /// Prints text, followed by a newline. /// /// String of text to print. /// String s followed by newline. private string GenerateLine(string s) { return GenerateLine(s, null); } /// /// Prints text, followed by a newline. /// /// String of text to print. /// Symbol being generated to extract original line /// number and column from. /// String s followed by newline. private string GenerateLine(string s, SYMBOL sym) { string retstr = Generate(s, sym) + "\n"; m_CSharpLine++; m_CSharpCol = 1; return retstr; } /// /// Prints text. /// /// String of text to print. /// String s. private string Generate(string s) { return Generate(s, null); } /// /// Prints text. /// /// String of text to print. /// Symbol being generated to extract original line /// number and column from. /// String s. private string Generate(string s, SYMBOL sym) { if (null != sym) m_positionMap.Add(new KeyValuePair(m_CSharpLine, m_CSharpCol), new KeyValuePair(sym.Line, sym.Position)); m_CSharpCol += s.Length; return s; } /// /// Prints text correctly indented, followed by a newline. /// /// String of text to print. /// Properly indented string s followed by newline. private string GenerateIndentedLine(string s) { return GenerateIndentedLine(s, null); } /// /// Prints text correctly indented, followed by a newline. /// /// String of text to print. /// Symbol being generated to extract original line /// number and column from. /// Properly indented string s followed by newline. private string GenerateIndentedLine(string s, SYMBOL sym) { string retstr = GenerateIndented(s, sym) + "\n"; m_CSharpLine++; m_CSharpCol = 1; return retstr; } /// /// Prints text correctly indented. /// /// String of text to print. /// Properly indented string s. //private string GenerateIndented(string s) //{ // return GenerateIndented(s, null); //} // THIS FUNCTION IS COMMENTED OUT TO SUPPRESS WARNINGS /// /// Prints text correctly indented. /// /// String of text to print. /// Symbol being generated to extract original line /// number and column from. /// Properly indented string s. private string GenerateIndented(string s, SYMBOL sym) { string retstr = Indent() + s; if (null != sym) m_positionMap.Add(new KeyValuePair(m_CSharpLine, m_CSharpCol), new KeyValuePair(sym.Line, sym.Position)); m_CSharpCol += s.Length; return retstr; } /// /// Prints correct indentation. /// /// Indentation based on brace count. private string Indent() { string retstr = String.Empty; for (int i = 0; i < m_braceCount; i++) for (int j = 0; j < m_indentWidth; j++) { retstr += " "; m_CSharpCol++; } return retstr; } /// /// Returns the passed name with an underscore prepended if that name is a reserved word in C# /// and not resevered in LSL otherwise it just returns the passed name. /// /// This makes no attempt to cache the results to minimise future lookups. For a non trivial /// scripts the number of unique identifiers could easily grow to the size of the reserved word /// list so maintaining a list or dictionary and doing the lookup there firstwould probably not /// give any real speed advantage. /// /// I believe there is a class Microsoft.CSharp.CSharpCodeProvider that has a function /// CreateValidIdentifier(str) that will return either the value of str if it is not a C# /// key word or "_"+str if it is. But availability under Mono? /// private string CheckName(string s) { if (CSReservedWords.IsReservedWord(s)) return "@" + s; else return s; } } }