public static void Main(string[] argv) { StreamReader s = new StreamReader(argv[0]); string source = s.ReadToEnd(); CSCodeGenerator cscg = new CSCodeGenerator(); string output = cscg.Convert(source); if (1 < argv.Length && "-t" == argv[1]) { Parser p = new LSLSyntax(); LSL2CSCodeTransformer codeTransformer = new LSL2CSCodeTransformer(p.Parse(source)); SYMBOL ast = codeTransformer.Transform(); printThisTree(ast); } else { Console.Write(output); } }
/// <summary> /// Generate the code from the AST we have. /// </summary> /// <param name="script">The LSL source as a string.</param> /// <returns>String containing the generated C# code.</returns> 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 Simian.Scripting.Linden;"); //retstr += GenerateLine("using System.Collections.Generic;"); //retstr += GenerateLine(""); //retstr += GenerateLine("namespace SecondLife"); //retstr += GenerateLine("{"); m_braceCount++; //retstr += GenerateIndentedLine("public class Script : LSLScriptBase"); //retstr += GenerateIndentedLine("{"); m_braceCount++; // line number m_CSharpLine += 3; // here's the payload retstr += GenerateLine(String.Empty); foreach (SYMBOL s in m_astRoot.kids) { retstr += GenerateNode(s); } // close braces! m_braceCount--; //retstr += GenerateIndentedLine("}"); m_braceCount--; //retstr += GenerateLine("}"); return(retstr); }
/// <summary> /// Generate the code from the AST we have. /// </summary> /// <param name="script">The LSL source as a string.</param> /// <returns>String containing the generated C# code.</returns> public string Convert(string script) { // m_log.DebugFormat("[CS CODE GENERATOR]: Converting to C#\n{0}", script); m_warnings.Clear(); ResetCounters(); Parser p = new LSLSyntax(new yyLSLSyntax(), new ErrorHandler(true)); 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("({0},{1}) {2}", e.slInfo.lineNumber - 1, 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); }
/// <summary> /// Generate the code from the AST we have. /// </summary> /// <param name="script">The LSL source as a string.</param> /// <returns>String containing the generated C# code.</returns> public void Convert(string script, StringBuilder sb) { // m_log.DebugFormat("[CS CODE GENERATOR]: Converting to C#\n{0}", script); m_warnings.Clear(); ResetCounters(); ErrorHandler errorHandler = new ErrorHandler(true); Parser p = new LSLSyntax(yyLSL, errorHandler); 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("({0},{1}) {2}", e.slInfo.lineNumber - 1, e.slInfo.charPosition - 1, emessage); throw new Exception(message); } m_astRoot = codeTransformer.Transform(); // standard preamble m_braceCount++; m_braceCount++; // line number m_CSharpLine += 10; // here's the payload sb.Append("\n"); foreach (SYMBOL s in m_astRoot.kids) { GenerateNodeToSB(m_astRoot, s, sb); } codeTransformer = null; p.m_lexer.m_buf = null; p.m_lexer.yytext = null; p.m_lexer = null; p.m_symbols = null; p = null; errorHandler = null; // 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? // sb.Replace("\r", ""); }