From ba17b0df273c87edf30151f3c470780cdf5f997e Mon Sep 17 00:00:00 2001 From: Mike Mazur Date: Wed, 23 Jul 2008 02:51:45 +0000 Subject: First version of position mapping between LSL <-> C# implemented. --- .../DotNetEngine/Compiler/LSL/CSCodeGenerator.cs | 369 +++-- .../DotNetEngine/Compiler/LSL/Compiler.cs | 5 +- .../Shared/CodeTools/CSCodeGenerator.cs | 357 +++-- .../Shared/CodeTools/CSCodeGeneratorTest.cs | 1427 ++++++++++++++++++++ .../ScriptEngine/Shared/CodeTools/CompilerTest.cs | 120 ++ .../Shared/CodeTools/LSLCompilerTest.cs | 1427 -------------------- 6 files changed, 2047 insertions(+), 1658 deletions(-) create mode 100644 OpenSim/Tests/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGeneratorTest.cs create mode 100644 OpenSim/Tests/OpenSim/Region/ScriptEngine/Shared/CodeTools/CompilerTest.cs delete mode 100644 OpenSim/Tests/OpenSim/Region/ScriptEngine/Shared/CodeTools/LSLCompilerTest.cs (limited to 'OpenSim') diff --git a/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/LSL/CSCodeGenerator.cs b/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/LSL/CSCodeGenerator.cs index f7aee66..f129859 100644 --- a/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/LSL/CSCodeGenerator.cs +++ b/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/LSL/CSCodeGenerator.cs @@ -35,56 +35,89 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL public class CSCodeGenerator { private SYMBOL m_astRoot = null; - private int m_braceCount; // for indentation + 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 /// - /// Pass the new CodeGenerator a string containing the LSL source. + /// Creates an 'empty' CSCodeGenerator instance. /// - /// String containing LSL source. - public CSCodeGenerator(string script) + public CSCodeGenerator() { - Parser p = new LSLSyntax(new yyLSLSyntax(), new ErrorHandler(true)); - // Obviously this needs to be in a try/except block. - LSL2CSCodeTransformer codeTransformer = new LSL2CSCodeTransformer(p.Parse(script)); - m_astRoot = codeTransformer.Transform(); + 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; } } /// - /// Pass the new CodeGenerator an abstract syntax tree. + /// Resets various counters and metadata. /// - /// The root node of the AST. - public CSCodeGenerator(SYMBOL astRoot) + private void ResetCounters() { m_braceCount = 0; - m_astRoot = astRoot; + 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 Generate() + public string Convert(string script) { + ResetCounters(); + Parser p = new LSLSyntax(new yyLSLSyntax(), new ErrorHandler(true)); + // Obviously this needs to be in a try/except block. + LSL2CSCodeTransformer codeTransformer = new LSL2CSCodeTransformer(p.Parse(script)); + m_astRoot = codeTransformer.Transform(); + string retstr = String.Empty; // standard preamble - //retstr = "using OpenSim.Region.ScriptEngine.Common;\n"; - //retstr += "using System.Collections.Generic;\n\n"; - //retstr += "namespace SecondLife\n"; - //retstr += "{\n"; - //retstr += " public class Script : OpenSim.Region.ScriptEngine.Common\n"; - //retstr += " {\n"; + //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 - m_braceCount += 2; - retstr += "\n"; + retstr += GenerateLine(); foreach (SYMBOL s in m_astRoot.kids) retstr += GenerateNode(s); // close braces! - //retstr += " }\n"; - //retstr += "}\n"; - m_braceCount -= 2; + m_braceCount--; + //retstr += GenerateIndentedLine("}"); + m_braceCount--; + //retstr += GenerateLine("}"); return retstr; } @@ -155,11 +188,11 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL else if (s is Constant) retstr += GenerateConstant((Constant) s); else if (s is IdentDotExpression) - retstr += ((IdentDotExpression) s).Name + "." + ((IdentDotExpression) s).Member; + retstr += Generate(((IdentDotExpression) s).Name + "." + ((IdentDotExpression) s).Member, s); else if (s is IdentExpression) - retstr += ((IdentExpression) s).Name; + retstr += Generate(((IdentExpression) s).Name, s); else if (s is IDENT) - retstr += ((TOKEN) s).yytext; + retstr += Generate(((TOKEN) s).yytext, s); else { foreach (SYMBOL kid in s.kids) @@ -188,13 +221,13 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL else remainingKids.Add(kid); - retstr += WriteIndented(String.Format("{0} {1}(", gf.ReturnType, gf.Name)); + retstr += GenerateIndented(String.Format("{0} {1}(", gf.ReturnType, gf.Name), gf); // print the state arguments, if any foreach (SYMBOL kid in argumentDeclarationListKids) retstr += GenerateArgumentDeclarationList((ArgumentDeclarationList) kid); - retstr += ")\n"; + retstr += GenerateLine(")"); foreach (SYMBOL kid in remainingKids) retstr += GenerateNode(kid); @@ -215,7 +248,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL { retstr += Indent(); retstr += GenerateNode(s); - retstr += ";\n"; + retstr += GenerateLine(";"); } return retstr; @@ -233,8 +266,6 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL foreach (SYMBOL kid in s.kids) if (kid is StateEvent) retstr += GenerateStateEvent((StateEvent) kid, s.Name); - else - retstr += String.Format("ERROR: State '{0}' contains a '{1}\n", s.Name, kid.GetType()); return retstr; } @@ -260,13 +291,13 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL remainingKids.Add(kid); // "state" (function) declaration - retstr += WriteIndented(String.Format("public void {0}_event_{1}(", parentStateName, se.Name)); + 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 += ")\n"; + retstr += GenerateLine(")"); foreach (SYMBOL kid in remainingKids) retstr += GenerateNode(kid); @@ -278,7 +309,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for an ArgumentDeclarationList node. /// /// The ArgumentDeclarationList node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for ArgumentDeclarationList adl. private string GenerateArgumentDeclarationList(ArgumentDeclarationList adl) { string retstr = String.Empty; @@ -287,9 +318,9 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL foreach (Declaration d in adl.kids) { - retstr += String.Format("{0} {1}", d.Datatype, d.Id); + retstr += Generate(String.Format("{0} {1}", d.Datatype, d.Id), d); if (0 < comma--) - retstr += ", "; + retstr += Generate(", "); } return retstr; @@ -299,7 +330,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for an ArgumentList node. /// /// The ArgumentList node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for ArgumentList al. private string GenerateArgumentList(ArgumentList al) { string retstr = String.Empty; @@ -310,7 +341,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL { retstr += GenerateNode(s); if (0 < comma--) - retstr += ", "; + retstr += Generate(", "); } return retstr; @@ -320,13 +351,13 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a CompoundStatement node. /// /// The CompoundStatement node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for CompoundStatement cs. private string GenerateCompoundStatement(CompoundStatement cs) { string retstr = String.Empty; // opening brace - retstr += WriteIndentedLine("{"); + retstr += GenerateIndentedLine("{"); m_braceCount++; foreach (SYMBOL kid in cs.kids) @@ -334,7 +365,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL // closing brace m_braceCount--; - retstr += WriteIndentedLine("}"); + retstr += GenerateIndentedLine("}"); return retstr; } @@ -343,17 +374,17 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a Declaration node. /// /// The Declaration node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for Declaration d. private string GenerateDeclaration(Declaration d) { - return String.Format("{0} {1}", d.Datatype, d.Id); + return Generate(String.Format("{0} {1}", d.Datatype, d.Id), d); } /// /// Generates the code for a Statement node. /// /// The Statement node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for Statement s. private string GenerateStatement(Statement s) { string retstr = String.Empty; @@ -367,7 +398,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL retstr += GenerateNode(kid); if (printSemicolon) - retstr += ";\n"; + retstr += GenerateLine(";"); return retstr; } @@ -376,13 +407,13 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for an Assignment node. /// /// The Assignment node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for Assignment a. private string GenerateAssignment(Assignment a) { string retstr = String.Empty; retstr += GenerateNode((SYMBOL) a.kids.Pop()); - retstr +=String.Format(" {0} ", a.AssignmentType); + retstr += Generate(String.Format(" {0} ", a.AssignmentType), a); foreach (SYMBOL kid in a.kids) retstr += GenerateNode(kid); @@ -393,12 +424,12 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a ReturnStatement node. /// /// The ReturnStatement node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for ReturnStatement rs. private string GenerateReturnStatement(ReturnStatement rs) { string retstr = String.Empty; - retstr += "return "; + retstr += Generate("return ", rs); foreach (SYMBOL kid in rs.kids) retstr += GenerateNode(kid); @@ -410,34 +441,34 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a JumpLabel node. /// /// The JumpLabel node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for JumpLabel jl. private string GenerateJumpLabel(JumpLabel jl) { - return String.Format("{0}:\n", jl.LabelName); + return Generate(String.Format("{0}:\n", jl.LabelName), jl); } /// /// Generates the code for a JumpStatement node. /// /// The JumpStatement node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for JumpStatement js. private string GenerateJumpStatement(JumpStatement js) { - return String.Format("goto {0}", js.TargetName); + return Generate(String.Format("goto {0}", js.TargetName), js); } /// - /// Generates the code for a IfStatement node. + /// Generates the code for an IfStatement node. /// /// The IfStatement node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for IfStatement ifs. private string GenerateIfStatement(IfStatement ifs) { string retstr = String.Empty; - retstr += WriteIndented("if ("); + retstr += GenerateIndented("if (", ifs); retstr += GenerateNode((SYMBOL) ifs.kids.Pop()); - retstr += ")\n"; + retstr += GenerateLine(")"); // CompoundStatement handles indentation itself but we need to do it // otherwise. @@ -448,7 +479,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL if (0 < ifs.kids.Count) // do it again for an else { - retstr += WriteIndentedLine("else"); + retstr += GenerateIndentedLine("else", ifs); indentHere = ifs.kids.Top is Statement; if (indentHere) m_braceCount++; @@ -463,24 +494,24 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a StateChange node. /// /// The StateChange node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for StateChange sc. private string GenerateStateChange(StateChange sc) { - return String.Format("state(\"{0}\")", sc.NewState); + return Generate(String.Format("state(\"{0}\")", sc.NewState), sc); } /// /// Generates the code for a WhileStatement node. /// /// The WhileStatement node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for WhileStatement ws. private string GenerateWhileStatement(WhileStatement ws) { string retstr = String.Empty; - retstr += WriteIndented("while ("); + retstr += GenerateIndented("while (", ws); retstr += GenerateNode((SYMBOL) ws.kids.Pop()); - retstr += ")\n"; + retstr += GenerateLine(")"); // CompoundStatement handles indentation itself but we need to do it // otherwise. @@ -496,12 +527,12 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a DoWhileStatement node. /// /// The DoWhileStatement node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for DoWhileStatement dws. private string GenerateDoWhileStatement(DoWhileStatement dws) { string retstr = String.Empty; - retstr += WriteIndentedLine("do"); + retstr += GenerateIndentedLine("do", dws); // CompoundStatement handles indentation itself but we need to do it // otherwise. @@ -510,9 +541,9 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL retstr += GenerateNode((SYMBOL) dws.kids.Pop()); if (indentHere) m_braceCount--; - retstr += WriteIndented("while ("); + retstr += GenerateIndented("while (", dws); retstr += GenerateNode((SYMBOL) dws.kids.Pop()); - retstr += ");\n"; + retstr += GenerateLine(");"); return retstr; } @@ -521,25 +552,25 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a ForLoop node. /// /// The ForLoop node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for ForLoop fl. private string GenerateForLoop(ForLoop fl) { string retstr = String.Empty; - retstr += WriteIndented("for ("); + retstr += GenerateIndented("for (", fl); // for ( x = 0 ; x < 10 ; x++ ) // ^^^^^^^ retstr += GenerateForLoopStatement((ForLoopStatement) fl.kids.Pop()); - retstr += "; "; + retstr += Generate("; "); // for ( x = 0 ; x < 10 ; x++ ) // ^^^^^^^^ retstr += GenerateNode((SYMBOL) fl.kids.Pop()); - retstr += "; "; + retstr += Generate("; "); // for ( x = 0 ; x < 10 ; x++ ) // ^^^^^ retstr += GenerateForLoopStatement((ForLoopStatement) fl.kids.Pop()); - retstr += ")\n"; + retstr += GenerateLine(")"); // CompoundStatement handles indentation itself but we need to do it // otherwise. @@ -555,7 +586,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a ForLoopStatement node. /// /// The ForLoopStatement node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for ForLoopStatement fls. private string GenerateForLoopStatement(ForLoopStatement fls) { string retstr = String.Empty; @@ -566,7 +597,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL { retstr += GenerateNode(s); if (0 < comma--) - retstr += ", "; + retstr += Generate(", "); } return retstr; @@ -576,13 +607,13 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a BinaryExpression node. /// /// The BinaryExpression node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for BinaryExpression be. private string GenerateBinaryExpression(BinaryExpression be) { string retstr = String.Empty; retstr += GenerateNode((SYMBOL) be.kids.Pop()); - retstr += String.Format(" {0} ", be.ExpressionSymbol); + retstr += Generate(String.Format(" {0} ", be.ExpressionSymbol), be); foreach (SYMBOL kid in be.kids) retstr += GenerateNode(kid); @@ -593,12 +624,12 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a UnaryExpression node. /// /// The UnaryExpression node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for UnaryExpression ue. private string GenerateUnaryExpression(UnaryExpression ue) { string retstr = String.Empty; - retstr += ue.UnarySymbol; + retstr += Generate(ue.UnarySymbol, ue); retstr += GenerateNode((SYMBOL) ue.kids.Pop()); return retstr; @@ -608,15 +639,15 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a ParenthesisExpression node. /// /// The ParenthesisExpression node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for ParenthesisExpression pe. private string GenerateParenthesisExpression(ParenthesisExpression pe) { string retstr = String.Empty; - retstr += "("; + retstr += Generate("("); foreach (SYMBOL kid in pe.kids) retstr += GenerateNode(kid); - retstr += ")"; + retstr += Generate(")"); return retstr; } @@ -625,7 +656,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a IncrementDecrementExpression node. /// /// The IncrementDecrementExpression node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for IncrementDecrementExpression ide. private string GenerateIncrementDecrementExpression(IncrementDecrementExpression ide) { string retstr = String.Empty; @@ -633,10 +664,10 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL if (0 < ide.kids.Count) { IdentDotExpression dot = (IdentDotExpression) ide.kids.Top; - retstr += String.Format("{0}", ide.PostOperation ? dot.Name + "." + dot.Member + ide.Operation : ide.Operation + dot.Name + "." + dot.Member); + retstr += Generate(String.Format("{0}", ide.PostOperation ? dot.Name + "." + dot.Member + ide.Operation : ide.Operation + dot.Name + "." + dot.Member), ide); } else - retstr += String.Format("{0}", ide.PostOperation ? ide.Name + ide.Operation : ide.Operation + ide.Name); + retstr += Generate(String.Format("{0}", ide.PostOperation ? ide.Name + ide.Operation : ide.Operation + ide.Name), ide); return retstr; } @@ -645,15 +676,15 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a TypecastExpression node. /// /// The TypecastExpression node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for TypecastExpression te. private string GenerateTypecastExpression(TypecastExpression te) { string retstr = String.Empty; // we wrap all typecasted statements in parentheses - retstr += String.Format("({0}) (", te.TypecastType); + retstr += Generate(String.Format("({0}) (", te.TypecastType), te); retstr += GenerateNode((SYMBOL) te.kids.Pop()); - retstr += ")"; + retstr += Generate(")"); return retstr; } @@ -662,17 +693,17 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a FunctionCall node. /// /// The FunctionCall node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for FunctionCall fc. private string GenerateFunctionCall(FunctionCall fc) { string retstr = String.Empty; - retstr += String.Format("{0}(", fc.Id); + retstr += Generate(String.Format("{0}(", fc.Id), fc); foreach (SYMBOL kid in fc.kids) retstr += GenerateNode(kid); - retstr += ")"; + retstr += Generate(")"); return retstr; } @@ -681,7 +712,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a Constant node. /// /// The Constant node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for Constant c. private string GenerateConstant(Constant c) { string retstr = String.Empty; @@ -697,10 +728,10 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL // need to quote strings if ("LSL_Types.LSLString" == c.Type) - retstr += "\""; - retstr += c.Value; + retstr += Generate("\""); + retstr += Generate(c.Value, c); if ("LSL_Types.LSLString" == c.Type) - retstr += "\""; + retstr += Generate("\""); return retstr; } @@ -709,18 +740,18 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a VectorConstant node. /// /// The VectorConstant node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for VectorConstant vc. private string GenerateVectorConstant(VectorConstant vc) { string retstr = String.Empty; - retstr += String.Format("new {0}(", vc.Type); + retstr += Generate(String.Format("new {0}(", vc.Type), vc); retstr += GenerateNode((SYMBOL) vc.kids.Pop()); - retstr += ", "; + retstr += Generate(", "); retstr += GenerateNode((SYMBOL) vc.kids.Pop()); - retstr += ", "; + retstr += Generate(", "); retstr += GenerateNode((SYMBOL) vc.kids.Pop()); - retstr += ")"; + retstr += Generate(")"); return retstr; } @@ -729,20 +760,20 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a RotationConstant node. /// /// The RotationConstant node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for RotationConstant rc. private string GenerateRotationConstant(RotationConstant rc) { string retstr = String.Empty; - retstr += String.Format("new {0}(", rc.Type); + retstr += Generate(String.Format("new {0}(", rc.Type), rc); retstr += GenerateNode((SYMBOL) rc.kids.Pop()); - retstr += ", "; + retstr += Generate(", "); retstr += GenerateNode((SYMBOL) rc.kids.Pop()); - retstr += ", "; + retstr += Generate(", "); retstr += GenerateNode((SYMBOL) rc.kids.Pop()); - retstr += ", "; + retstr += Generate(", "); retstr += GenerateNode((SYMBOL) rc.kids.Pop()); - retstr += ")"; + retstr += Generate(")"); return retstr; } @@ -751,51 +782,155 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL /// Generates the code for a ListConstant node. /// /// The ListConstant node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for ListConstant lc. private string GenerateListConstant(ListConstant lc) { string retstr = String.Empty; - retstr += String.Format("new {0}(", lc.Type); + retstr += Generate(String.Format("new {0}(", lc.Type), lc); foreach (SYMBOL kid in lc.kids) retstr += GenerateNode(kid); - retstr += ")"; + 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. - /// String containing C# code for SYMBOL s. - private string WriteIndentedLine(string s) + /// Properly indented string s followed by newline. + private string GenerateIndentedLine(string s) { - return WriteIndented(s) + "\n"; + 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. - /// String containing C# code for SYMBOL s. - private string WriteIndented(string s) + /// 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) { - return Indent() + s; + 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. /// - /// String containing C# code for SYMBOL s. + /// Indentation based on brace count. private string Indent() { string retstr = String.Empty; for (int i = 0; i < m_braceCount; i++) - retstr += " "; + for (int j = 0; j < m_indentWidth; j++) + { + retstr += " "; + m_CSharpCol++; + } return retstr; } diff --git a/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/LSL/Compiler.cs b/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/LSL/Compiler.cs index ed6e783..73894d5 100644 --- a/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/LSL/Compiler.cs +++ b/OpenSim/Region/ScriptEngine/DotNetEngine/Compiler/LSL/Compiler.cs @@ -73,7 +73,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL private string ScriptEnginesPath = "ScriptEngines"; private static LSL2CSConverter LSL_Converter = new LSL2CSConverter(); - //private static CSCodeGenerator LSL_Converter; + //private static CSCodeGenerator LSL_Converter = new CSCodeGenerator(); private static CSharpCodeProvider CScodeProvider = new CSharpCodeProvider(); private static VBCodeProvider VBcodeProvider = new VBCodeProvider(); private static JScriptCodeProvider JScodeProvider = new JScriptCodeProvider(); @@ -276,8 +276,7 @@ namespace OpenSim.Region.ScriptEngine.DotNetEngine.Compiler.LSL { // Its LSL, convert it to C# compileScript = LSL_Converter.Convert(Script); - //LSL_Converter = new CSCodeGenerator(Script); - //compileScript = LSL_Converter.Generate(); + //compileScript = LSL_Converter.Convert(Script); l = enumCompileType.cs; } diff --git a/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs b/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs index 7d7384e..3bdad4d 100644 --- a/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs +++ b/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs @@ -35,56 +35,89 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools public class CSCodeGenerator : ICodeConverter { private SYMBOL m_astRoot = null; - private int m_braceCount; // for indentation + 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 /// - /// Pass the new CodeGenerator a string containing the LSL source. + /// Creates an 'empty' CSCodeGenerator instance. /// - /// String containing LSL source. public CSCodeGenerator() { + ResetCounters(); } /// - /// Pass the new CodeGenerator an abstract syntax tree. + /// Get the mapping between LSL and C# line/column number. /// - /// The root node of the AST. - public CSCodeGenerator(SYMBOL astRoot) + /// 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_astRoot = astRoot; + 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) { + ResetCounters(); Parser p = new LSLSyntax(new yyLSLSyntax(), new ErrorHandler(true)); // Obviously this needs to be in a try/except block. LSL2CSCodeTransformer codeTransformer = new LSL2CSCodeTransformer(p.Parse(script)); m_astRoot = codeTransformer.Transform(); + string retstr = String.Empty; // standard preamble - //retstr = "using OpenSim.Region.ScriptEngine.Common;\n"; - //retstr += "using System.Collections.Generic;\n\n"; - //retstr += "namespace SecondLife\n"; - //retstr += "{\n"; - //retstr += " public class Script : OpenSim.Region.ScriptEngine.Common\n"; - //retstr += " {\n"; + //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 - m_braceCount += 2; - retstr += "\n"; + retstr += GenerateLine(); foreach (SYMBOL s in m_astRoot.kids) retstr += GenerateNode(s); // close braces! - //retstr += " }\n"; - //retstr += "}\n"; - m_braceCount -= 2; + m_braceCount--; + //retstr += GenerateIndentedLine("}"); + m_braceCount--; + //retstr += GenerateLine("}"); return retstr; } @@ -155,11 +188,11 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools else if (s is Constant) retstr += GenerateConstant((Constant) s); else if (s is IdentDotExpression) - retstr += ((IdentDotExpression) s).Name + "." + ((IdentDotExpression) s).Member; + retstr += Generate(((IdentDotExpression) s).Name + "." + ((IdentDotExpression) s).Member, s); else if (s is IdentExpression) - retstr += ((IdentExpression) s).Name; + retstr += Generate(((IdentExpression) s).Name, s); else if (s is IDENT) - retstr += ((TOKEN) s).yytext; + retstr += Generate(((TOKEN) s).yytext, s); else { foreach (SYMBOL kid in s.kids) @@ -188,13 +221,13 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools else remainingKids.Add(kid); - retstr += WriteIndented(String.Format("{0} {1}(", gf.ReturnType, gf.Name)); + retstr += GenerateIndented(String.Format("{0} {1}(", gf.ReturnType, gf.Name), gf); // print the state arguments, if any foreach (SYMBOL kid in argumentDeclarationListKids) retstr += GenerateArgumentDeclarationList((ArgumentDeclarationList) kid); - retstr += ")\n"; + retstr += GenerateLine(")"); foreach (SYMBOL kid in remainingKids) retstr += GenerateNode(kid); @@ -215,7 +248,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools { retstr += Indent(); retstr += GenerateNode(s); - retstr += ";\n"; + retstr += GenerateLine(";"); } return retstr; @@ -233,8 +266,6 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools foreach (SYMBOL kid in s.kids) if (kid is StateEvent) retstr += GenerateStateEvent((StateEvent) kid, s.Name); - else - retstr += String.Format("ERROR: State '{0}' contains a '{1}\n", s.Name, kid.GetType()); return retstr; } @@ -260,13 +291,13 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools remainingKids.Add(kid); // "state" (function) declaration - retstr += WriteIndented(String.Format("public void {0}_event_{1}(", parentStateName, se.Name)); + 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 += ")\n"; + retstr += GenerateLine(")"); foreach (SYMBOL kid in remainingKids) retstr += GenerateNode(kid); @@ -278,7 +309,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for an ArgumentDeclarationList node. /// /// The ArgumentDeclarationList node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for ArgumentDeclarationList adl. private string GenerateArgumentDeclarationList(ArgumentDeclarationList adl) { string retstr = String.Empty; @@ -287,9 +318,9 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools foreach (Declaration d in adl.kids) { - retstr += String.Format("{0} {1}", d.Datatype, d.Id); + retstr += Generate(String.Format("{0} {1}", d.Datatype, d.Id), d); if (0 < comma--) - retstr += ", "; + retstr += Generate(", "); } return retstr; @@ -299,7 +330,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for an ArgumentList node. /// /// The ArgumentList node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for ArgumentList al. private string GenerateArgumentList(ArgumentList al) { string retstr = String.Empty; @@ -310,7 +341,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools { retstr += GenerateNode(s); if (0 < comma--) - retstr += ", "; + retstr += Generate(", "); } return retstr; @@ -320,13 +351,13 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a CompoundStatement node. /// /// The CompoundStatement node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for CompoundStatement cs. private string GenerateCompoundStatement(CompoundStatement cs) { string retstr = String.Empty; // opening brace - retstr += WriteIndentedLine("{"); + retstr += GenerateIndentedLine("{"); m_braceCount++; foreach (SYMBOL kid in cs.kids) @@ -334,7 +365,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools // closing brace m_braceCount--; - retstr += WriteIndentedLine("}"); + retstr += GenerateIndentedLine("}"); return retstr; } @@ -343,17 +374,17 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a Declaration node. /// /// The Declaration node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for Declaration d. private string GenerateDeclaration(Declaration d) { - return String.Format("{0} {1}", d.Datatype, d.Id); + return Generate(String.Format("{0} {1}", d.Datatype, d.Id), d); } /// /// Generates the code for a Statement node. /// /// The Statement node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for Statement s. private string GenerateStatement(Statement s) { string retstr = String.Empty; @@ -367,7 +398,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools retstr += GenerateNode(kid); if (printSemicolon) - retstr += ";\n"; + retstr += GenerateLine(";"); return retstr; } @@ -376,13 +407,13 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for an Assignment node. /// /// The Assignment node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for Assignment a. private string GenerateAssignment(Assignment a) { string retstr = String.Empty; retstr += GenerateNode((SYMBOL) a.kids.Pop()); - retstr +=String.Format(" {0} ", a.AssignmentType); + retstr += Generate(String.Format(" {0} ", a.AssignmentType), a); foreach (SYMBOL kid in a.kids) retstr += GenerateNode(kid); @@ -393,12 +424,12 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a ReturnStatement node. /// /// The ReturnStatement node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for ReturnStatement rs. private string GenerateReturnStatement(ReturnStatement rs) { string retstr = String.Empty; - retstr += "return "; + retstr += Generate("return ", rs); foreach (SYMBOL kid in rs.kids) retstr += GenerateNode(kid); @@ -410,34 +441,34 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a JumpLabel node. /// /// The JumpLabel node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for JumpLabel jl. private string GenerateJumpLabel(JumpLabel jl) { - return String.Format("{0}:\n", jl.LabelName); + return Generate(String.Format("{0}:\n", jl.LabelName), jl); } /// /// Generates the code for a JumpStatement node. /// /// The JumpStatement node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for JumpStatement js. private string GenerateJumpStatement(JumpStatement js) { - return String.Format("goto {0}", js.TargetName); + return Generate(String.Format("goto {0}", js.TargetName), js); } /// - /// Generates the code for a IfStatement node. + /// Generates the code for an IfStatement node. /// /// The IfStatement node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for IfStatement ifs. private string GenerateIfStatement(IfStatement ifs) { string retstr = String.Empty; - retstr += WriteIndented("if ("); + retstr += GenerateIndented("if (", ifs); retstr += GenerateNode((SYMBOL) ifs.kids.Pop()); - retstr += ")\n"; + retstr += GenerateLine(")"); // CompoundStatement handles indentation itself but we need to do it // otherwise. @@ -448,7 +479,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools if (0 < ifs.kids.Count) // do it again for an else { - retstr += WriteIndentedLine("else"); + retstr += GenerateIndentedLine("else", ifs); indentHere = ifs.kids.Top is Statement; if (indentHere) m_braceCount++; @@ -463,24 +494,24 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a StateChange node. /// /// The StateChange node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for StateChange sc. private string GenerateStateChange(StateChange sc) { - return String.Format("state(\"{0}\")", sc.NewState); + return Generate(String.Format("state(\"{0}\")", sc.NewState), sc); } /// /// Generates the code for a WhileStatement node. /// /// The WhileStatement node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for WhileStatement ws. private string GenerateWhileStatement(WhileStatement ws) { string retstr = String.Empty; - retstr += WriteIndented("while ("); + retstr += GenerateIndented("while (", ws); retstr += GenerateNode((SYMBOL) ws.kids.Pop()); - retstr += ")\n"; + retstr += GenerateLine(")"); // CompoundStatement handles indentation itself but we need to do it // otherwise. @@ -496,12 +527,12 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a DoWhileStatement node. /// /// The DoWhileStatement node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for DoWhileStatement dws. private string GenerateDoWhileStatement(DoWhileStatement dws) { string retstr = String.Empty; - retstr += WriteIndentedLine("do"); + retstr += GenerateIndentedLine("do", dws); // CompoundStatement handles indentation itself but we need to do it // otherwise. @@ -510,9 +541,9 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools retstr += GenerateNode((SYMBOL) dws.kids.Pop()); if (indentHere) m_braceCount--; - retstr += WriteIndented("while ("); + retstr += GenerateIndented("while (", dws); retstr += GenerateNode((SYMBOL) dws.kids.Pop()); - retstr += ");\n"; + retstr += GenerateLine(");"); return retstr; } @@ -521,25 +552,25 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a ForLoop node. /// /// The ForLoop node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for ForLoop fl. private string GenerateForLoop(ForLoop fl) { string retstr = String.Empty; - retstr += WriteIndented("for ("); + retstr += GenerateIndented("for (", fl); // for ( x = 0 ; x < 10 ; x++ ) // ^^^^^^^ retstr += GenerateForLoopStatement((ForLoopStatement) fl.kids.Pop()); - retstr += "; "; + retstr += Generate("; "); // for ( x = 0 ; x < 10 ; x++ ) // ^^^^^^^^ retstr += GenerateNode((SYMBOL) fl.kids.Pop()); - retstr += "; "; + retstr += Generate("; "); // for ( x = 0 ; x < 10 ; x++ ) // ^^^^^ retstr += GenerateForLoopStatement((ForLoopStatement) fl.kids.Pop()); - retstr += ")\n"; + retstr += GenerateLine(")"); // CompoundStatement handles indentation itself but we need to do it // otherwise. @@ -555,7 +586,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a ForLoopStatement node. /// /// The ForLoopStatement node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for ForLoopStatement fls. private string GenerateForLoopStatement(ForLoopStatement fls) { string retstr = String.Empty; @@ -566,7 +597,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools { retstr += GenerateNode(s); if (0 < comma--) - retstr += ", "; + retstr += Generate(", "); } return retstr; @@ -576,13 +607,13 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a BinaryExpression node. /// /// The BinaryExpression node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for BinaryExpression be. private string GenerateBinaryExpression(BinaryExpression be) { string retstr = String.Empty; retstr += GenerateNode((SYMBOL) be.kids.Pop()); - retstr += String.Format(" {0} ", be.ExpressionSymbol); + retstr += Generate(String.Format(" {0} ", be.ExpressionSymbol), be); foreach (SYMBOL kid in be.kids) retstr += GenerateNode(kid); @@ -593,12 +624,12 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a UnaryExpression node. /// /// The UnaryExpression node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for UnaryExpression ue. private string GenerateUnaryExpression(UnaryExpression ue) { string retstr = String.Empty; - retstr += ue.UnarySymbol; + retstr += Generate(ue.UnarySymbol, ue); retstr += GenerateNode((SYMBOL) ue.kids.Pop()); return retstr; @@ -608,15 +639,15 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a ParenthesisExpression node. /// /// The ParenthesisExpression node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for ParenthesisExpression pe. private string GenerateParenthesisExpression(ParenthesisExpression pe) { string retstr = String.Empty; - retstr += "("; + retstr += Generate("("); foreach (SYMBOL kid in pe.kids) retstr += GenerateNode(kid); - retstr += ")"; + retstr += Generate(")"); return retstr; } @@ -625,7 +656,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a IncrementDecrementExpression node. /// /// The IncrementDecrementExpression node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for IncrementDecrementExpression ide. private string GenerateIncrementDecrementExpression(IncrementDecrementExpression ide) { string retstr = String.Empty; @@ -633,10 +664,10 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools if (0 < ide.kids.Count) { IdentDotExpression dot = (IdentDotExpression) ide.kids.Top; - retstr += String.Format("{0}", ide.PostOperation ? dot.Name + "." + dot.Member + ide.Operation : ide.Operation + dot.Name + "." + dot.Member); + retstr += Generate(String.Format("{0}", ide.PostOperation ? dot.Name + "." + dot.Member + ide.Operation : ide.Operation + dot.Name + "." + dot.Member), ide); } else - retstr += String.Format("{0}", ide.PostOperation ? ide.Name + ide.Operation : ide.Operation + ide.Name); + retstr += Generate(String.Format("{0}", ide.PostOperation ? ide.Name + ide.Operation : ide.Operation + ide.Name), ide); return retstr; } @@ -645,15 +676,15 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a TypecastExpression node. /// /// The TypecastExpression node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for TypecastExpression te. private string GenerateTypecastExpression(TypecastExpression te) { string retstr = String.Empty; // we wrap all typecasted statements in parentheses - retstr += String.Format("({0}) (", te.TypecastType); + retstr += Generate(String.Format("({0}) (", te.TypecastType), te); retstr += GenerateNode((SYMBOL) te.kids.Pop()); - retstr += ")"; + retstr += Generate(")"); return retstr; } @@ -662,17 +693,17 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a FunctionCall node. /// /// The FunctionCall node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for FunctionCall fc. private string GenerateFunctionCall(FunctionCall fc) { string retstr = String.Empty; - retstr += String.Format("{0}(", fc.Id); + retstr += Generate(String.Format("{0}(", fc.Id), fc); foreach (SYMBOL kid in fc.kids) retstr += GenerateNode(kid); - retstr += ")"; + retstr += Generate(")"); return retstr; } @@ -681,7 +712,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a Constant node. /// /// The Constant node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for Constant c. private string GenerateConstant(Constant c) { string retstr = String.Empty; @@ -697,10 +728,10 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools // need to quote strings if ("LSL_Types.LSLString" == c.Type) - retstr += "\""; - retstr += c.Value; + retstr += Generate("\""); + retstr += Generate(c.Value, c); if ("LSL_Types.LSLString" == c.Type) - retstr += "\""; + retstr += Generate("\""); return retstr; } @@ -709,18 +740,18 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a VectorConstant node. /// /// The VectorConstant node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for VectorConstant vc. private string GenerateVectorConstant(VectorConstant vc) { string retstr = String.Empty; - retstr += String.Format("new {0}(", vc.Type); + retstr += Generate(String.Format("new {0}(", vc.Type), vc); retstr += GenerateNode((SYMBOL) vc.kids.Pop()); - retstr += ", "; + retstr += Generate(", "); retstr += GenerateNode((SYMBOL) vc.kids.Pop()); - retstr += ", "; + retstr += Generate(", "); retstr += GenerateNode((SYMBOL) vc.kids.Pop()); - retstr += ")"; + retstr += Generate(")"); return retstr; } @@ -729,20 +760,20 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a RotationConstant node. /// /// The RotationConstant node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for RotationConstant rc. private string GenerateRotationConstant(RotationConstant rc) { string retstr = String.Empty; - retstr += String.Format("new {0}(", rc.Type); + retstr += Generate(String.Format("new {0}(", rc.Type), rc); retstr += GenerateNode((SYMBOL) rc.kids.Pop()); - retstr += ", "; + retstr += Generate(", "); retstr += GenerateNode((SYMBOL) rc.kids.Pop()); - retstr += ", "; + retstr += Generate(", "); retstr += GenerateNode((SYMBOL) rc.kids.Pop()); - retstr += ", "; + retstr += Generate(", "); retstr += GenerateNode((SYMBOL) rc.kids.Pop()); - retstr += ")"; + retstr += Generate(")"); return retstr; } @@ -751,51 +782,155 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools /// Generates the code for a ListConstant node. /// /// The ListConstant node. - /// String containing C# code for SYMBOL s. + /// String containing C# code for ListConstant lc. private string GenerateListConstant(ListConstant lc) { string retstr = String.Empty; - retstr += String.Format("new {0}(", lc.Type); + retstr += Generate(String.Format("new {0}(", lc.Type), lc); foreach (SYMBOL kid in lc.kids) retstr += GenerateNode(kid); - retstr += ")"; + 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. - /// String containing C# code for SYMBOL s. - private string WriteIndentedLine(string s) + /// 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) { - return WriteIndented(s) + "\n"; + string retstr = GenerateIndented(s, sym) + "\n"; + + m_CSharpLine++; + m_CSharpCol = 1; + + return retstr; } /// /// Prints text correctly indented. /// /// String of text to print. - /// String containing C# code for SYMBOL s. - private string WriteIndented(string s) + /// 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) { - return Indent() + s; + 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. /// - /// String containing C# code for SYMBOL s. + /// Indentation based on brace count. private string Indent() { string retstr = String.Empty; for (int i = 0; i < m_braceCount; i++) - retstr += " "; + for (int j = 0; j < m_indentWidth; j++) + { + retstr += " "; + m_CSharpCol++; + } return retstr; } diff --git a/OpenSim/Tests/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGeneratorTest.cs b/OpenSim/Tests/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGeneratorTest.cs new file mode 100644 index 0000000..0b07b30 --- /dev/null +++ b/OpenSim/Tests/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGeneratorTest.cs @@ -0,0 +1,1427 @@ +/* + * 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 OpenSim 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.Collections.Generic; +using System.Text.RegularExpressions; +using NUnit.Framework; +using OpenSim.Region.ScriptEngine.Shared.CodeTools; + +namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests +{ + /// + /// Tests the LSL compiler, both the code generation and transformation. + /// Each test has some LSL code as input and C# code as expected output. + /// The generated C# code is compared against the expected C# code. + /// + [TestFixture] + public class CSCodeGeneratorTest + { + [Test] + public void TestDefaultState() + { + string input = @"default +{ + state_entry() + { + } +} +"; + string expected = @" + public void default_event_state_entry() + { + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestCustomState() + { + string input = @"default +{ + state_entry() + { + } +} + +state another_state +{ + no_sensor() + { + } +} +"; + string expected = @" + public void default_event_state_entry() + { + } + public void another_state_event_no_sensor() + { + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestEventWithArguments() + { + string input = @"default +{ + at_rot_target(integer tnum, rotation targetrot, rotation ourrot) + { + } +} +"; + string expected = @" + public void default_event_at_rot_target(LSL_Types.LSLInteger tnum, LSL_Types.Quaternion targetrot, LSL_Types.Quaternion ourrot) + { + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestIntegerDeclaration() + { + string input = @"default +{ + touch_start(integer num_detected) + { + integer x; + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLInteger x = 0; + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestAssignments() + { + string input = @"default +{ + touch_start(integer num_detected) + { + string y; + integer x = 14; + y = ""Hello""; + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLString y = """"; + LSL_Types.LSLInteger x = 14; + y = ""Hello""; + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestAdditionSubtractionOperator() + { + string input = @"default +{ + touch_start(integer num_detected) + { + integer y = -3; + integer x = 14 + 6; + y = 12 +45+20+x + 23 + 1 + x + y; + y = 12 + -45 + - 20 + x + 23 + -1 + x + y; + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLInteger y = -3; + LSL_Types.LSLInteger x = 14 + 6; + y = 12 + 45 + 20 + x + 23 + 1 + x + y; + y = 12 + -45 + -20 + x + 23 + -1 + x + y; + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestStrings() + { + string input = @"default +{ + touch_start(integer num_detected) + { + llOwnerSay(""Testing, 1, 2, 3""); + llSay(0, ""I can hear you!""); + some_custom_function(1, 2, 3 +x, 4, ""five"", ""arguments""); + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + llOwnerSay(""Testing, 1, 2, 3""); + llSay(0, ""I can hear you!""); + some_custom_function(1, 2, 3 + x, 4, ""five"", ""arguments""); + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestBinaryExpression() + { + string input = @"default +{ + touch_start(integer num_detected) + { + integer y; + integer x = 14 + 6; + y = 12 - 3; + y = 12 * 3; + y = 12 / 3; + y = 12 | 3; + y = 12 & 3; + y = 12 % 3; + y = 12 + 45 - 20 * x / 23 | 1 & x + y; + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLInteger y = 0; + LSL_Types.LSLInteger x = 14 + 6; + y = 12 - 3; + y = 12 * 3; + y = 12 / 3; + y = 12 | 3; + y = 12 & 3; + y = 12 % 3; + y = 12 + 45 - 20 * x / 23 | 1 & x + y; + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestFloatConstants() + { + string input = @"default +{ + touch_start(integer num_detected) + { + float y = 1.1; + y = 1.123E3; + y = 1.123e3; + y = 1.123E+3; + y = 1.123e+3; + y = 1.123E-3; + y = 1.123e-3; + y = .4; + y = -1.123E3; + y = -1.123e3; + y = -1.123E+3; + y = -1.123e+3; + y = -1.123E-3; + y = -1.123e-3; + y = -.4; + y = 12.3 + -1.45E3 - 1.20e-2; + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLFloat y = 1.1; + y = 1.123E3; + y = 1.123e3; + y = 1.123E+3; + y = 1.123e+3; + y = 1.123E-3; + y = 1.123e-3; + y = .4; + y = -1.123E3; + y = -1.123e3; + y = -1.123E+3; + y = -1.123e+3; + y = -1.123E-3; + y = -1.123e-3; + y = -.4; + y = 12.3 + -1.45E3 - 1.20e-2; + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestComments() + { + string input = @"// this test tests comments +default +{ + touch_start(integer num_detected) // this should be stripped + { + // fill in code here... + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestStringsWithEscapedQuotesAndComments() + { + string input = @"// this test tests strings, with escaped quotes and comments in strings +default +{ + touch_start(integer num_detected) + { + string s1 = ""this is a string.""; + string s2 = ""this is a string ""+""with an escaped \"" inside it.""; + s1 = s2+"" and this ""+""is a string with // comments.""; + + string onemore = ""[\^@]""; + + string multiline = ""Good evening Sir, + my name is Steve. + I come from a rough area. + I used to be addicted to crack + but now I am off it and trying to stay clean. + That is why I am selling magazine subscriptions.""; // http://www.imdb.com/title/tt0151804/quotes + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLString s1 = ""this is a string.""; + LSL_Types.LSLString s2 = ""this is a string "" + ""with an escaped \"" inside it.""; + s1 = s2 + "" and this "" + ""is a string with // comments.""; + LSL_Types.LSLString onemore = ""[\^@]""; + LSL_Types.LSLString multiline = ""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.""; + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestGlobalDefinedFunctions() + { + string input = @"// this test tests custom defined functions + +string onefunc() +{ + return ""Hi from onefunc()!""; +} + +twofunc(string s) +{ + llSay(1000, s); +} + +default +{ + touch_start(integer num_detected) + { + llSay(2000, onefunc()); + twofunc(); + } +} +"; + string expected = @" + LSL_Types.LSLString onefunc() + { + return ""Hi from onefunc()!""; + } + void twofunc(LSL_Types.LSLString s) + { + llSay(1000, s); + } + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + llSay(2000, onefunc()); + twofunc(); + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestGlobalDeclaredVariables() + { + string input = @"// this test tests custom defined functions and global variables + +string globalString; +integer globalInt = 14; +integer anotherGlobal = 20 * globalInt; + +string onefunc() +{ + globalString = "" ...and the global!""; + return ""Hi "" + + ""from "" + + ""onefunc()!"" + globalString; +} + +twofunc(string s) +{ + llSay(1000, s); +} + +default +{ + touch_start(integer num_detected) + { + llSay(2000, onefunc()); + twofunc(); + } +} +"; + string expected = @" + LSL_Types.LSLString globalString = """"; + LSL_Types.LSLInteger globalInt = 14; + LSL_Types.LSLInteger anotherGlobal = 20 * globalInt; + LSL_Types.LSLString onefunc() + { + globalString = "" ...and the global!""; + return ""Hi "" + ""from "" + ""onefunc()!"" + globalString; + } + void twofunc(LSL_Types.LSLString s) + { + llSay(1000, s); + } + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + llSay(2000, onefunc()); + twofunc(); + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestMoreAssignments() + { + string input = @"// this test tests +=, -=, *=, /=, %= + +string globalString; +integer globalInt = 14; + +string onefunc(string addition) +{ + globalInt -= 2; + + globalString += addition; + return ""Hi "" + + ""from "" + + ""onefunc()! "" + globalString; +} + +default +{ + touch_start(integer num_detected) + { + llSay(2000, onefunc()); + + integer x = 2; + x *= 3; + x /= 14 + -2; + x %= 10; + } +} +"; + string expected = @" + LSL_Types.LSLString globalString = """"; + LSL_Types.LSLInteger globalInt = 14; + LSL_Types.LSLString onefunc(LSL_Types.LSLString addition) + { + globalInt -= 2; + globalString += addition; + return ""Hi "" + ""from "" + ""onefunc()! "" + globalString; + } + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + llSay(2000, onefunc()); + LSL_Types.LSLInteger x = 2; + x *= 3; + x /= 14 + -2; + x %= 10; + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestVectorConstantNotation() + { + string input = @"default +{ + touch_start(integer num_detected) + { + vector y = <1.2, llGetMeAFloat(), 4.4>; + rotation x = <0.1, 0.1, one + 2, 0.9>; + + y = <0.1, 0.1, 1.1 - three - two+eight*8>; + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.Vector3 y = new LSL_Types.Vector3(1.2, llGetMeAFloat(), 4.4); + LSL_Types.Quaternion x = new LSL_Types.Quaternion(0.1, 0.1, one + 2, 0.9); + y = new LSL_Types.Vector3(0.1, 0.1, 1.1 - three - two + eight * 8); + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestVectorMemberAccess() + { + string input = @"default +{ + touch_start(integer num_detected) + { + vector y = <1.2, llGetMeAFloat(), 4.4>; + x = y.x + 1.1; + y.x = 1.1; + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.Vector3 y = new LSL_Types.Vector3(1.2, llGetMeAFloat(), 4.4); + x = y.x + 1.1; + y.x = 1.1; + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestExpressionInParentheses() + { + string input = @"default +{ + touch_start(integer num_detected) + { + integer y = -3; + integer x = 14 + 6; + y = 12 +45+20+x + (23 + 1) + x + y; + y = (12 + -45 + -20 + x + 23 )+ -1 + x + y; + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLInteger y = -3; + LSL_Types.LSLInteger x = 14 + 6; + y = 12 + 45 + 20 + x + (23 + 1) + x + y; + y = (12 + -45 + -20 + x + 23) + -1 + x + y; + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestIncrementDecrementOperator() + { + string input = @"// here we'll test the ++ and -- operators + +default +{ + touch_start(integer num_detected) + { + integer y = -3; + integer x = 14 + 6; + y = 12 +45+20+x++ + (23 + 1) + ++x + -- y; + y = (12 + -45 + -20 + x-- + 23 )+ -1 + x -- + ++y; + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLInteger y = -3; + LSL_Types.LSLInteger x = 14 + 6; + y = 12 + 45 + 20 + x++ + (23 + 1) + ++x + --y; + y = (12 + -45 + -20 + x-- + 23) + -1 + x-- + ++y; + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestLists() + { + string input = @"// testing lists + +default +{ + touch_start(integer num_detected) + { + list l = []; + list m = [1, two, ""three"", <4.0, 4.0, 4.0>, 5 + 5]; + llCallSomeFunc(1, llAnotherFunc(), [1, 2, 3]); + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.list l = new LSL_Types.list(); + LSL_Types.list m = new LSL_Types.list(1, two, ""three"", new LSL_Types.Vector3(4.0, 4.0, 4.0), 5 + 5); + llCallSomeFunc(1, llAnotherFunc(), new LSL_Types.list(1, 2, 3)); + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestIfStatement() + { + string input = @"// let's test if statements + +default +{ + touch_start(integer num_detected) + { + integer x = 1; + + if(x) llSay(0, ""Hello""); + if(1) + { + llSay(0, ""Hi""); + integer r = 3; + return; + } + + if (f(x)) llSay(0, ""f(x) is true""); + else llSay(0, ""f(x) is false""); + + if (x + y) llSay(0, ""x + y is true""); + else if (y - x) llSay(0, ""y - x is true""); + else llSay(0, ""Who needs x and y anyway?""); + + if (x * y) llSay(0, ""x * y is true""); + else if (y / x) + { + llSay(0, ""uh-oh, y / x is true, exiting""); + return; + } + else llSay(0, ""Who needs x and y anyway?""); + + // and now for my last trick + if (x % y) llSay(0, ""x is true""); + else if (y & x) llSay(0, ""y is true""); + else if (z | x) llSay(0, ""z is true""); + else if (a * (b + x)) llSay(0, ""a is true""); + else if (b) llSay(0, ""b is true""); + else if (v) llSay(0, ""v is true""); + else llSay(0, ""Everything is lies!""); + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLInteger x = 1; + if (x) + llSay(0, ""Hello""); + if (1) + { + llSay(0, ""Hi""); + LSL_Types.LSLInteger r = 3; + return ; + } + if (f(x)) + llSay(0, ""f(x) is true""); + else + llSay(0, ""f(x) is false""); + if (x + y) + llSay(0, ""x + y is true""); + else + if (y - x) + llSay(0, ""y - x is true""); + else + llSay(0, ""Who needs x and y anyway?""); + if (x * y) + llSay(0, ""x * y is true""); + else + if (y / x) + { + llSay(0, ""uh-oh, y / x is true, exiting""); + return ; + } + else + llSay(0, ""Who needs x and y anyway?""); + if (x % y) + llSay(0, ""x is true""); + else + if (y & x) + llSay(0, ""y is true""); + else + if (z | x) + llSay(0, ""z is true""); + else + if (a * (b + x)) + llSay(0, ""a is true""); + else + if (b) + llSay(0, ""b is true""); + else + if (v) + llSay(0, ""v is true""); + else + llSay(0, ""Everything is lies!""); + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestIfElseStatement() + { + string input = @"// let's test complex logical expressions + +default +{ + touch_start(integer num_detected) + { + integer x = 1; + integer y = 0; + + if(x && y) llSay(0, ""Hello""); + if(x || y) + { + llSay(0, ""Hi""); + integer r = 3; + return; + } + + if (x && y || z) llSay(0, ""x is true""); + else llSay(0, ""x is false""); + + if (x == y) llSay(0, ""x is true""); + else if (y < x) llSay(0, ""y is true""); + else llSay(0, ""Who needs x and y anyway?""); + + if (x > y) llSay(0, ""x is true""); + else if (y <= x) + { + llSay(0, ""uh-oh, y is true, exiting""); + return; + } + else llSay(0, ""Who needs x and y anyway?""); + + // and now for my last trick + if (x >= y) llSay(0, ""x is true""); + else if (y != x) llSay(0, ""y is true""); + else if (!z) llSay(0, ""z is true""); + else if (!(a && b)) llSay(0, ""a is true""); + else if (b) llSay(0, ""b is true""); + else if (v) llSay(0, ""v is true""); + else llSay(0, ""Everything is lies!""); + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLInteger x = 1; + LSL_Types.LSLInteger y = 0; + if (x && y) + llSay(0, ""Hello""); + if (x || y) + { + llSay(0, ""Hi""); + LSL_Types.LSLInteger r = 3; + return ; + } + if (x && y || z) + llSay(0, ""x is true""); + else + llSay(0, ""x is false""); + if (x == y) + llSay(0, ""x is true""); + else + if (y < x) + llSay(0, ""y is true""); + else + llSay(0, ""Who needs x and y anyway?""); + if (x > y) + llSay(0, ""x is true""); + else + if (y <= x) + { + llSay(0, ""uh-oh, y is true, exiting""); + return ; + } + else + llSay(0, ""Who needs x and y anyway?""); + if (x >= y) + llSay(0, ""x is true""); + else + if (y != x) + llSay(0, ""y is true""); + else + if (!z) + llSay(0, ""z is true""); + else + if (!(a && b)) + llSay(0, ""a is true""); + else + if (b) + llSay(0, ""b is true""); + else + if (v) + llSay(0, ""v is true""); + else + llSay(0, ""Everything is lies!""); + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestWhileLoop() + { + string input = @"// let's test while loops + +default +{ + touch_start(integer num_detected) + { + integer x = 1; + integer y = 0; + + while (x) llSay(0, ""To infinity, and beyond!""); + while (0 || (x && 0)) + { + llSay(0, ""Never say never.""); + return; + } + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLInteger x = 1; + LSL_Types.LSLInteger y = 0; + while (x) + llSay(0, ""To infinity, and beyond!""); + while (0 || (x && 0)) + { + llSay(0, ""Never say never.""); + return ; + } + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestDoWhileLoop() + { + string input = @"// let's test do-while loops + +default +{ + touch_start(integer num_detected) + { + integer x = 1; + integer y = 0; + + do llSay(0, ""And we're doing...""); + while (x); + + do + { + llSay(0, ""I like it here. I wish we could stay here forever.""); + y--; + } while (y); + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLInteger x = 1; + LSL_Types.LSLInteger y = 0; + do + llSay(0, ""And we're doing...""); + while (x); + do + { + llSay(0, ""I like it here. I wish we could stay here forever.""); + y--; + } + while (y); + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestForLoop() + { + string input = @"// let's test for loops + +default +{ + touch_start(integer num_detected) + { + integer x = 1; + integer y = 0; + + for(x = 10; x >= 0; x--) + { + llOwnerSay(""Launch in T minus "" + x); + IncreaseRocketPower(); + } + + for(x = 0, y = 6; y > 0 && x != y; x++, y--) llOwnerSay(""Hi "" + x + "", "" + y); + for(x = 0, y = 6; ! y; x++,y--) llOwnerSay(""Hi "" + x + "", "" + y); + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLInteger x = 1; + LSL_Types.LSLInteger y = 0; + for (x = 10; x >= 0; x--) + { + llOwnerSay(""Launch in T minus "" + x); + IncreaseRocketPower(); + } + for (x = 0, y = 6; y > 0 && x != y; x++, y--) + llOwnerSay(""Hi "" + x + "", "" + y); + for (x = 0, y = 6; !y; x++, y--) + llOwnerSay(""Hi "" + x + "", "" + y); + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestFloatsWithTrailingDecimal() + { + string input = @"// a curious feature of LSL that allows floats to be defined with a trailing dot + +default +{ + touch_start(integer num_detected) + { + float y = 1.; + y = 1.E3; + y = 1.e3; + y = 1.E+3; + y = 1.e+3; + y = 1.E-3; + y = 1.e-3; + y = -1.E3; + y = -1.e3; + y = -1.E+3; + y = -1.e+3; + y = -1.E-3; + y = -1.e-3; + y = 12. + -1.E3 - 1.e-2; + vector v = <0.,0.,0.>; + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLFloat y = 1.0; + y = 1.0E3; + y = 1.0e3; + y = 1.0E+3; + y = 1.0e+3; + y = 1.0E-3; + y = 1.0e-3; + y = -1.0E3; + y = -1.0e3; + y = -1.0E+3; + y = -1.0e+3; + y = -1.0E-3; + y = -1.0e-3; + y = 12.0 + -1.0E3 - 1.0e-2; + LSL_Types.Vector3 v = new LSL_Types.Vector3(0.0, 0.0, 0.0); + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestUnaryAndBinaryOperators() + { + string input = @"// let's test a few more operators + +default +{ + touch_start(integer num_detected) + { + integer x = 2; + integer y = 1; + integer z = x ^ y; + x = ~ z; + x = ~(y && z); + y = x >> z; + z = y << x; + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLInteger x = 2; + LSL_Types.LSLInteger y = 1; + LSL_Types.LSLInteger z = x ^ y; + x = ~z; + x = ~(y && z); + y = x >> z; + z = y << x; + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestTypecasts() + { + string input = @"// let's test typecasts + +default +{ + touch_start(integer num_detected) + { + string s = """"; + integer x = 1; + + s = (string) x++; + s = (string) x; + s = (string) <0., 0., 0.>; + s = (string) <1., 1., 1., 1.>; + s = (integer) ""1""; + s = (string) llSomethingThatReturnsInteger(); + s = (string) 134; + s = (string) (x ^ y | (z && l)) + (string) (x + y - 13); + llOwnerSay(""s is: "" + s); + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLString s = """"; + LSL_Types.LSLInteger x = 1; + s = (LSL_Types.LSLString) (x++); + s = (LSL_Types.LSLString) (x); + s = (LSL_Types.LSLString) (new LSL_Types.Vector3(0.0, 0.0, 0.0)); + s = (LSL_Types.LSLString) (new LSL_Types.Quaternion(1.0, 1.0, 1.0, 1.0)); + s = (LSL_Types.LSLInteger) (""1""); + s = (LSL_Types.LSLString) (llSomethingThatReturnsInteger()); + s = (LSL_Types.LSLString) (134); + s = (LSL_Types.LSLString) (x ^ y | (z && l)) + (LSL_Types.LSLString) (x + y - 13); + llOwnerSay(""s is: "" + s); + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestStates() + { + string input = @"// let's test states + +default +{ + touch_start(integer num_detected) + { + llSay(0, ""Going to state 'statetwo'""); + state statetwo; + } +} + +state statetwo +{ + state_entry() + { + llSay(0, ""Going to the default state""); + state default; + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + llSay(0, ""Going to state 'statetwo'""); + state(""statetwo""); + } + public void statetwo_event_state_entry() + { + llSay(0, ""Going to the default state""); + state(""default""); + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestHexIntegerConstants() + { + string input = @"// let's test hex integers + +default +{ + touch_start(integer num_detected) + { + integer x = 0x23; + integer x = 0x2f34B; + integer x = 0x2F34b; + integer x = 0x2F34B; + integer x = 0x2f34b; + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLInteger x = 0x23; + LSL_Types.LSLInteger x = 0x2f34B; + LSL_Types.LSLInteger x = 0x2F34b; + LSL_Types.LSLInteger x = 0x2F34B; + LSL_Types.LSLInteger x = 0x2f34b; + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestJumps() + { + string input = @"// let's test jumps + +default +{ + touch_start(integer num_detected) + { + jump here; + llOwnerSay(""Uh oh, the jump didn't work""); + @here; + llOwnerSay(""After the jump""); + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + goto here; + llOwnerSay(""Uh oh, the jump didn't work""); + here: + llOwnerSay(""After the jump""); + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestImplicitVariableInitialization() + { + string input = @"// let's test implicitly initializing variables + +default +{ + touch_start(integer num_detected) + { + integer i; integer j = 14; + float f; float g = 14.0; + string s; string t = ""Hi there""; + list l; list m = [1, 2, 3]; + vector v; vector w = <1.0, 0.1, 0.5>; + rotation r; rotation u = <0.8, 0.7, 0.6, llSomeFunc()>; + key k; key n = ""ping""; + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLInteger i = 0; + LSL_Types.LSLInteger j = 14; + LSL_Types.LSLFloat f = 0.0; + LSL_Types.LSLFloat g = 14.0; + LSL_Types.LSLString s = """"; + LSL_Types.LSLString t = ""Hi there""; + LSL_Types.list l = new LSL_Types.list(); + LSL_Types.list m = new LSL_Types.list(1, 2, 3); + LSL_Types.Vector3 v = new LSL_Types.Vector3(0.0, 0.0, 0.0); + LSL_Types.Vector3 w = new LSL_Types.Vector3(1.0, 0.1, 0.5); + LSL_Types.Quaternion r = new LSL_Types.Quaternion(0.0, 0.0, 0.0, 0.0); + LSL_Types.Quaternion u = new LSL_Types.Quaternion(0.8, 0.7, 0.6, llSomeFunc()); + LSL_Types.LSLString k = """"; + LSL_Types.LSLString n = ""ping""; + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestMultipleEqualsExpression() + { + string input = @"// let's test x = y = 5 type expressions + +default +{ + touch_start(integer num_detected) + { + integer x; + integer y; + x = y = 5; + x += y -= 5; + llOwnerSay(""x is: "" + (string) x + "", y is: "" + (string) y); + } +} +"; + string expected = @" + public void default_event_touch_start(LSL_Types.LSLInteger num_detected) + { + LSL_Types.LSLInteger x = 0; + LSL_Types.LSLInteger y = 0; + x = y = 5; + x += y -= 5; + llOwnerSay(""x is: "" + (LSL_Types.LSLString) (x) + "", y is: "" + (LSL_Types.LSLString) (y)); + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestUnaryExpressionLastInVectorConstant() + { + string input = @"// let's test unary expressions some more + +default +{ + state_entry() + { + vector v = ; + } +} +"; + string expected = @" + public void default_event_state_entry() + { + LSL_Types.Vector3 v = new LSL_Types.Vector3(x, y, -0.5); + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + public void TestVectorMemberPlusEquals() + { + string input = @"// let's test unary expressions some more + +default +{ + state_entry() + { + vector v = llGetPos(); + v.z += 4; + v.z -= 4; + v.z *= 4; + v.z /= 4; + v.z %= 4; + } +} +"; + string expected = @" + public void default_event_state_entry() + { + LSL_Types.Vector3 v = llGetPos(); + v.z += 4; + v.z -= 4; + v.z *= 4; + v.z /= 4; + v.z %= 4; + } +"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] + [ExpectedException("Tools.CSToolsException")] + public void TestSyntaxError() + { + string input = @"default +{ + state_entry() + { + integer y + } +} +"; + try + { + CSCodeGenerator cg = new CSCodeGenerator(); + cg.Convert(input); + } + catch (Tools.CSToolsException e) + { + // The syntax error is on line 6, char 5 (expected ';', found + // '}'). + Regex r = new Regex("Line ([0-9]+), char ([0-9]+)"); + Match m = r.Match(e.Message); + Assert.AreEqual("6", m.Groups[1].Value); + Assert.AreEqual("5", m.Groups[2].Value); + + throw; + } + } + } +} diff --git a/OpenSim/Tests/OpenSim/Region/ScriptEngine/Shared/CodeTools/CompilerTest.cs b/OpenSim/Tests/OpenSim/Region/ScriptEngine/Shared/CodeTools/CompilerTest.cs new file mode 100644 index 0000000..7a11d33 --- /dev/null +++ b/OpenSim/Tests/OpenSim/Region/ScriptEngine/Shared/CodeTools/CompilerTest.cs @@ -0,0 +1,120 @@ +/* + * 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 OpenSim 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.IO; +using System.CodeDom.Compiler; +using System.Collections.Generic; +using Microsoft.CSharp; +using NUnit.Framework; +using OpenSim.Region.ScriptEngine.Shared.CodeTools; + +namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests +{ + /// + /// Tests the LSL compiler. Among other things, test that error messages + /// generated by the C# compiler can be mapped to prper lines/columns in + /// the LSL source. + /// + [TestFixture] + public class CompilerTest + { + private string m_testDir; + private CSharpCodeProvider m_CSCodeProvider; + private CompilerParameters m_compilerParameters; + private CompilerResults m_compilerResults; + + /// + /// Creates a temporary directory where build artifacts are stored. + /// + [TestFixtureSetUp] + public void Init() + { + m_testDir = Path.Combine(Path.GetTempPath(), "opensim_compilerTest_" + Path.GetRandomFileName()); + + if (!Directory.Exists(m_testDir)) + { + // Create the temporary directory for housing build artifacts. + Directory.CreateDirectory(m_testDir); + } + + // Create a CSCodeProvider and CompilerParameters. + m_CSCodeProvider = new CSharpCodeProvider(); + m_compilerParameters = new CompilerParameters(); + + string rootPath = Path.Combine(Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory), "bin"); + m_compilerParameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.dll")); + m_compilerParameters.ReferencedAssemblies.Add(Path.Combine(rootPath, "OpenSim.Region.ScriptEngine.Shared.Api.Runtime.dll")); + m_compilerParameters.GenerateExecutable = false; + } + + /// + /// Removes the temporary build directory and any build artifacts + /// inside it. + /// + [TestFixtureTearDown] + public void CleanUp() + { + if (Directory.Exists(m_testDir)) + { + // Blow away the temporary directory with artifacts. + Directory.Delete(m_testDir, true); + } + } + + [Test] + /// + /// Test the C# compiler error message can be mapped to the correct + /// line/column in the LSL source when an undeclared variable is used. + /// + public void TestUseUndeclaredVariable() + { + m_compilerParameters.OutputAssembly = Path.Combine(m_testDir, Path.GetRandomFileName() + ".dll"); + + string input = @"default +{ + state_entry() + { + integer y = x + 3; + } +}"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = "using OpenSim.Region.ScriptEngine.Shared; using System.Collections.Generic;\n" + + "namespace SecondLife { " + + "public class Script : OpenSim.Region.ScriptEngine.Shared.ScriptBase.ScriptBaseClass {\n" + + "public Script() { } " + + cg.Convert(input) + + "} }\n"; + Dictionary, KeyValuePair> positionMap = cg.PositionMap; + + m_compilerResults = m_CSCodeProvider.CompileAssemblyFromSource(m_compilerParameters, output); + + Assert.AreEqual(new KeyValuePair(5, 21), + positionMap[new KeyValuePair(m_compilerResults.Errors[0].Line, m_compilerResults.Errors[0].Column)]); + } + } +} diff --git a/OpenSim/Tests/OpenSim/Region/ScriptEngine/Shared/CodeTools/LSLCompilerTest.cs b/OpenSim/Tests/OpenSim/Region/ScriptEngine/Shared/CodeTools/LSLCompilerTest.cs deleted file mode 100644 index 70472cb..0000000 --- a/OpenSim/Tests/OpenSim/Region/ScriptEngine/Shared/CodeTools/LSLCompilerTest.cs +++ /dev/null @@ -1,1427 +0,0 @@ -/* - * 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 OpenSim 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.Collections.Generic; -using System.Text.RegularExpressions; -using NUnit.Framework; -using OpenSim.Region.ScriptEngine.Shared.CodeTools; - -namespace OpenSim.Region.ScriptEngine.Shared.CodeTools.Tests -{ - /// - /// Tests the LSL compiler, both the code generation and transformation. - /// Each test has some LSL code as input and C# code as expected output. - /// The generated C# code is compared against the expected C# code. - /// - [TestFixture] - public class LSLCompilerTest - { - [Test] - public void TestDefaultState() - { - string input = @"default -{ - state_entry() - { - } -} -"; - string expected = @" - public void default_event_state_entry() - { - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestCustomState() - { - string input = @"default -{ - state_entry() - { - } -} - -state another_state -{ - no_sensor() - { - } -} -"; - string expected = @" - public void default_event_state_entry() - { - } - public void another_state_event_no_sensor() - { - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestEventWithArguments() - { - string input = @"default -{ - at_rot_target(integer tnum, rotation targetrot, rotation ourrot) - { - } -} -"; - string expected = @" - public void default_event_at_rot_target(LSL_Types.LSLInteger tnum, LSL_Types.Quaternion targetrot, LSL_Types.Quaternion ourrot) - { - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestIntegerDeclaration() - { - string input = @"default -{ - touch_start(integer num_detected) - { - integer x; - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLInteger x = 0; - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestAssignments() - { - string input = @"default -{ - touch_start(integer num_detected) - { - string y; - integer x = 14; - y = ""Hello""; - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLString y = """"; - LSL_Types.LSLInteger x = 14; - y = ""Hello""; - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestAdditionSubtractionOperator() - { - string input = @"default -{ - touch_start(integer num_detected) - { - integer y = -3; - integer x = 14 + 6; - y = 12 +45+20+x + 23 + 1 + x + y; - y = 12 + -45 + - 20 + x + 23 + -1 + x + y; - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLInteger y = -3; - LSL_Types.LSLInteger x = 14 + 6; - y = 12 + 45 + 20 + x + 23 + 1 + x + y; - y = 12 + -45 + -20 + x + 23 + -1 + x + y; - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestStrings() - { - string input = @"default -{ - touch_start(integer num_detected) - { - llOwnerSay(""Testing, 1, 2, 3""); - llSay(0, ""I can hear you!""); - some_custom_function(1, 2, 3 +x, 4, ""five"", ""arguments""); - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - llOwnerSay(""Testing, 1, 2, 3""); - llSay(0, ""I can hear you!""); - some_custom_function(1, 2, 3 + x, 4, ""five"", ""arguments""); - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestBinaryExpression() - { - string input = @"default -{ - touch_start(integer num_detected) - { - integer y; - integer x = 14 + 6; - y = 12 - 3; - y = 12 * 3; - y = 12 / 3; - y = 12 | 3; - y = 12 & 3; - y = 12 % 3; - y = 12 + 45 - 20 * x / 23 | 1 & x + y; - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLInteger y = 0; - LSL_Types.LSLInteger x = 14 + 6; - y = 12 - 3; - y = 12 * 3; - y = 12 / 3; - y = 12 | 3; - y = 12 & 3; - y = 12 % 3; - y = 12 + 45 - 20 * x / 23 | 1 & x + y; - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestFloatConstants() - { - string input = @"default -{ - touch_start(integer num_detected) - { - float y = 1.1; - y = 1.123E3; - y = 1.123e3; - y = 1.123E+3; - y = 1.123e+3; - y = 1.123E-3; - y = 1.123e-3; - y = .4; - y = -1.123E3; - y = -1.123e3; - y = -1.123E+3; - y = -1.123e+3; - y = -1.123E-3; - y = -1.123e-3; - y = -.4; - y = 12.3 + -1.45E3 - 1.20e-2; - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLFloat y = 1.1; - y = 1.123E3; - y = 1.123e3; - y = 1.123E+3; - y = 1.123e+3; - y = 1.123E-3; - y = 1.123e-3; - y = .4; - y = -1.123E3; - y = -1.123e3; - y = -1.123E+3; - y = -1.123e+3; - y = -1.123E-3; - y = -1.123e-3; - y = -.4; - y = 12.3 + -1.45E3 - 1.20e-2; - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestComments() - { - string input = @"// this test tests comments -default -{ - touch_start(integer num_detected) // this should be stripped - { - // fill in code here... - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestStringsWithEscapedQuotesAndComments() - { - string input = @"// this test tests strings, with escaped quotes and comments in strings -default -{ - touch_start(integer num_detected) - { - string s1 = ""this is a string.""; - string s2 = ""this is a string ""+""with an escaped \"" inside it.""; - s1 = s2+"" and this ""+""is a string with // comments.""; - - string onemore = ""[\^@]""; - - string multiline = ""Good evening Sir, - my name is Steve. - I come from a rough area. - I used to be addicted to crack - but now I am off it and trying to stay clean. - That is why I am selling magazine subscriptions.""; // http://www.imdb.com/title/tt0151804/quotes - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLString s1 = ""this is a string.""; - LSL_Types.LSLString s2 = ""this is a string "" + ""with an escaped \"" inside it.""; - s1 = s2 + "" and this "" + ""is a string with // comments.""; - LSL_Types.LSLString onemore = ""[\^@]""; - LSL_Types.LSLString multiline = ""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.""; - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestGlobalDefinedFunctions() - { - string input = @"// this test tests custom defined functions - -string onefunc() -{ - return ""Hi from onefunc()!""; -} - -twofunc(string s) -{ - llSay(1000, s); -} - -default -{ - touch_start(integer num_detected) - { - llSay(2000, onefunc()); - twofunc(); - } -} -"; - string expected = @" - LSL_Types.LSLString onefunc() - { - return ""Hi from onefunc()!""; - } - void twofunc(LSL_Types.LSLString s) - { - llSay(1000, s); - } - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - llSay(2000, onefunc()); - twofunc(); - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestGlobalDeclaredVariables() - { - string input = @"// this test tests custom defined functions and global variables - -string globalString; -integer globalInt = 14; -integer anotherGlobal = 20 * globalInt; - -string onefunc() -{ - globalString = "" ...and the global!""; - return ""Hi "" + - ""from "" + - ""onefunc()!"" + globalString; -} - -twofunc(string s) -{ - llSay(1000, s); -} - -default -{ - touch_start(integer num_detected) - { - llSay(2000, onefunc()); - twofunc(); - } -} -"; - string expected = @" - LSL_Types.LSLString globalString = """"; - LSL_Types.LSLInteger globalInt = 14; - LSL_Types.LSLInteger anotherGlobal = 20 * globalInt; - LSL_Types.LSLString onefunc() - { - globalString = "" ...and the global!""; - return ""Hi "" + ""from "" + ""onefunc()!"" + globalString; - } - void twofunc(LSL_Types.LSLString s) - { - llSay(1000, s); - } - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - llSay(2000, onefunc()); - twofunc(); - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestMoreAssignments() - { - string input = @"// this test tests +=, -=, *=, /=, %= - -string globalString; -integer globalInt = 14; - -string onefunc(string addition) -{ - globalInt -= 2; - - globalString += addition; - return ""Hi "" + - ""from "" + - ""onefunc()! "" + globalString; -} - -default -{ - touch_start(integer num_detected) - { - llSay(2000, onefunc()); - - integer x = 2; - x *= 3; - x /= 14 + -2; - x %= 10; - } -} -"; - string expected = @" - LSL_Types.LSLString globalString = """"; - LSL_Types.LSLInteger globalInt = 14; - LSL_Types.LSLString onefunc(LSL_Types.LSLString addition) - { - globalInt -= 2; - globalString += addition; - return ""Hi "" + ""from "" + ""onefunc()! "" + globalString; - } - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - llSay(2000, onefunc()); - LSL_Types.LSLInteger x = 2; - x *= 3; - x /= 14 + -2; - x %= 10; - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestVectorConstantNotation() - { - string input = @"default -{ - touch_start(integer num_detected) - { - vector y = <1.2, llGetMeAFloat(), 4.4>; - rotation x = <0.1, 0.1, one + 2, 0.9>; - - y = <0.1, 0.1, 1.1 - three - two+eight*8>; - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.Vector3 y = new LSL_Types.Vector3(1.2, llGetMeAFloat(), 4.4); - LSL_Types.Quaternion x = new LSL_Types.Quaternion(0.1, 0.1, one + 2, 0.9); - y = new LSL_Types.Vector3(0.1, 0.1, 1.1 - three - two + eight * 8); - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestVectorMemberAccess() - { - string input = @"default -{ - touch_start(integer num_detected) - { - vector y = <1.2, llGetMeAFloat(), 4.4>; - x = y.x + 1.1; - y.x = 1.1; - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.Vector3 y = new LSL_Types.Vector3(1.2, llGetMeAFloat(), 4.4); - x = y.x + 1.1; - y.x = 1.1; - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestExpressionInParentheses() - { - string input = @"default -{ - touch_start(integer num_detected) - { - integer y = -3; - integer x = 14 + 6; - y = 12 +45+20+x + (23 + 1) + x + y; - y = (12 + -45 + -20 + x + 23 )+ -1 + x + y; - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLInteger y = -3; - LSL_Types.LSLInteger x = 14 + 6; - y = 12 + 45 + 20 + x + (23 + 1) + x + y; - y = (12 + -45 + -20 + x + 23) + -1 + x + y; - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestIncrementDecrementOperator() - { - string input = @"// here we'll test the ++ and -- operators - -default -{ - touch_start(integer num_detected) - { - integer y = -3; - integer x = 14 + 6; - y = 12 +45+20+x++ + (23 + 1) + ++x + -- y; - y = (12 + -45 + -20 + x-- + 23 )+ -1 + x -- + ++y; - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLInteger y = -3; - LSL_Types.LSLInteger x = 14 + 6; - y = 12 + 45 + 20 + x++ + (23 + 1) + ++x + --y; - y = (12 + -45 + -20 + x-- + 23) + -1 + x-- + ++y; - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestLists() - { - string input = @"// testing lists - -default -{ - touch_start(integer num_detected) - { - list l = []; - list m = [1, two, ""three"", <4.0, 4.0, 4.0>, 5 + 5]; - llCallSomeFunc(1, llAnotherFunc(), [1, 2, 3]); - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.list l = new LSL_Types.list(); - LSL_Types.list m = new LSL_Types.list(1, two, ""three"", new LSL_Types.Vector3(4.0, 4.0, 4.0), 5 + 5); - llCallSomeFunc(1, llAnotherFunc(), new LSL_Types.list(1, 2, 3)); - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestIfStatement() - { - string input = @"// let's test if statements - -default -{ - touch_start(integer num_detected) - { - integer x = 1; - - if(x) llSay(0, ""Hello""); - if(1) - { - llSay(0, ""Hi""); - integer r = 3; - return; - } - - if (f(x)) llSay(0, ""f(x) is true""); - else llSay(0, ""f(x) is false""); - - if (x + y) llSay(0, ""x + y is true""); - else if (y - x) llSay(0, ""y - x is true""); - else llSay(0, ""Who needs x and y anyway?""); - - if (x * y) llSay(0, ""x * y is true""); - else if (y / x) - { - llSay(0, ""uh-oh, y / x is true, exiting""); - return; - } - else llSay(0, ""Who needs x and y anyway?""); - - // and now for my last trick - if (x % y) llSay(0, ""x is true""); - else if (y & x) llSay(0, ""y is true""); - else if (z | x) llSay(0, ""z is true""); - else if (a * (b + x)) llSay(0, ""a is true""); - else if (b) llSay(0, ""b is true""); - else if (v) llSay(0, ""v is true""); - else llSay(0, ""Everything is lies!""); - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLInteger x = 1; - if (x) - llSay(0, ""Hello""); - if (1) - { - llSay(0, ""Hi""); - LSL_Types.LSLInteger r = 3; - return ; - } - if (f(x)) - llSay(0, ""f(x) is true""); - else - llSay(0, ""f(x) is false""); - if (x + y) - llSay(0, ""x + y is true""); - else - if (y - x) - llSay(0, ""y - x is true""); - else - llSay(0, ""Who needs x and y anyway?""); - if (x * y) - llSay(0, ""x * y is true""); - else - if (y / x) - { - llSay(0, ""uh-oh, y / x is true, exiting""); - return ; - } - else - llSay(0, ""Who needs x and y anyway?""); - if (x % y) - llSay(0, ""x is true""); - else - if (y & x) - llSay(0, ""y is true""); - else - if (z | x) - llSay(0, ""z is true""); - else - if (a * (b + x)) - llSay(0, ""a is true""); - else - if (b) - llSay(0, ""b is true""); - else - if (v) - llSay(0, ""v is true""); - else - llSay(0, ""Everything is lies!""); - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestIfElseStatement() - { - string input = @"// let's test complex logical expressions - -default -{ - touch_start(integer num_detected) - { - integer x = 1; - integer y = 0; - - if(x && y) llSay(0, ""Hello""); - if(x || y) - { - llSay(0, ""Hi""); - integer r = 3; - return; - } - - if (x && y || z) llSay(0, ""x is true""); - else llSay(0, ""x is false""); - - if (x == y) llSay(0, ""x is true""); - else if (y < x) llSay(0, ""y is true""); - else llSay(0, ""Who needs x and y anyway?""); - - if (x > y) llSay(0, ""x is true""); - else if (y <= x) - { - llSay(0, ""uh-oh, y is true, exiting""); - return; - } - else llSay(0, ""Who needs x and y anyway?""); - - // and now for my last trick - if (x >= y) llSay(0, ""x is true""); - else if (y != x) llSay(0, ""y is true""); - else if (!z) llSay(0, ""z is true""); - else if (!(a && b)) llSay(0, ""a is true""); - else if (b) llSay(0, ""b is true""); - else if (v) llSay(0, ""v is true""); - else llSay(0, ""Everything is lies!""); - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLInteger x = 1; - LSL_Types.LSLInteger y = 0; - if (x && y) - llSay(0, ""Hello""); - if (x || y) - { - llSay(0, ""Hi""); - LSL_Types.LSLInteger r = 3; - return ; - } - if (x && y || z) - llSay(0, ""x is true""); - else - llSay(0, ""x is false""); - if (x == y) - llSay(0, ""x is true""); - else - if (y < x) - llSay(0, ""y is true""); - else - llSay(0, ""Who needs x and y anyway?""); - if (x > y) - llSay(0, ""x is true""); - else - if (y <= x) - { - llSay(0, ""uh-oh, y is true, exiting""); - return ; - } - else - llSay(0, ""Who needs x and y anyway?""); - if (x >= y) - llSay(0, ""x is true""); - else - if (y != x) - llSay(0, ""y is true""); - else - if (!z) - llSay(0, ""z is true""); - else - if (!(a && b)) - llSay(0, ""a is true""); - else - if (b) - llSay(0, ""b is true""); - else - if (v) - llSay(0, ""v is true""); - else - llSay(0, ""Everything is lies!""); - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestWhileLoop() - { - string input = @"// let's test while loops - -default -{ - touch_start(integer num_detected) - { - integer x = 1; - integer y = 0; - - while (x) llSay(0, ""To infinity, and beyond!""); - while (0 || (x && 0)) - { - llSay(0, ""Never say never.""); - return; - } - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLInteger x = 1; - LSL_Types.LSLInteger y = 0; - while (x) - llSay(0, ""To infinity, and beyond!""); - while (0 || (x && 0)) - { - llSay(0, ""Never say never.""); - return ; - } - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestDoWhileLoop() - { - string input = @"// let's test do-while loops - -default -{ - touch_start(integer num_detected) - { - integer x = 1; - integer y = 0; - - do llSay(0, ""And we're doing...""); - while (x); - - do - { - llSay(0, ""I like it here. I wish we could stay here forever.""); - y--; - } while (y); - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLInteger x = 1; - LSL_Types.LSLInteger y = 0; - do - llSay(0, ""And we're doing...""); - while (x); - do - { - llSay(0, ""I like it here. I wish we could stay here forever.""); - y--; - } - while (y); - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestForLoop() - { - string input = @"// let's test for loops - -default -{ - touch_start(integer num_detected) - { - integer x = 1; - integer y = 0; - - for(x = 10; x >= 0; x--) - { - llOwnerSay(""Launch in T minus "" + x); - IncreaseRocketPower(); - } - - for(x = 0, y = 6; y > 0 && x != y; x++, y--) llOwnerSay(""Hi "" + x + "", "" + y); - for(x = 0, y = 6; ! y; x++,y--) llOwnerSay(""Hi "" + x + "", "" + y); - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLInteger x = 1; - LSL_Types.LSLInteger y = 0; - for (x = 10; x >= 0; x--) - { - llOwnerSay(""Launch in T minus "" + x); - IncreaseRocketPower(); - } - for (x = 0, y = 6; y > 0 && x != y; x++, y--) - llOwnerSay(""Hi "" + x + "", "" + y); - for (x = 0, y = 6; !y; x++, y--) - llOwnerSay(""Hi "" + x + "", "" + y); - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestFloatsWithTrailingDecimal() - { - string input = @"// a curious feature of LSL that allows floats to be defined with a trailing dot - -default -{ - touch_start(integer num_detected) - { - float y = 1.; - y = 1.E3; - y = 1.e3; - y = 1.E+3; - y = 1.e+3; - y = 1.E-3; - y = 1.e-3; - y = -1.E3; - y = -1.e3; - y = -1.E+3; - y = -1.e+3; - y = -1.E-3; - y = -1.e-3; - y = 12. + -1.E3 - 1.e-2; - vector v = <0.,0.,0.>; - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLFloat y = 1.0; - y = 1.0E3; - y = 1.0e3; - y = 1.0E+3; - y = 1.0e+3; - y = 1.0E-3; - y = 1.0e-3; - y = -1.0E3; - y = -1.0e3; - y = -1.0E+3; - y = -1.0e+3; - y = -1.0E-3; - y = -1.0e-3; - y = 12.0 + -1.0E3 - 1.0e-2; - LSL_Types.Vector3 v = new LSL_Types.Vector3(0.0, 0.0, 0.0); - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestUnaryAndBinaryOperators() - { - string input = @"// let's test a few more operators - -default -{ - touch_start(integer num_detected) - { - integer x = 2; - integer y = 1; - integer z = x ^ y; - x = ~ z; - x = ~(y && z); - y = x >> z; - z = y << x; - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLInteger x = 2; - LSL_Types.LSLInteger y = 1; - LSL_Types.LSLInteger z = x ^ y; - x = ~z; - x = ~(y && z); - y = x >> z; - z = y << x; - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestTypecasts() - { - string input = @"// let's test typecasts - -default -{ - touch_start(integer num_detected) - { - string s = """"; - integer x = 1; - - s = (string) x++; - s = (string) x; - s = (string) <0., 0., 0.>; - s = (string) <1., 1., 1., 1.>; - s = (integer) ""1""; - s = (string) llSomethingThatReturnsInteger(); - s = (string) 134; - s = (string) (x ^ y | (z && l)) + (string) (x + y - 13); - llOwnerSay(""s is: "" + s); - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLString s = """"; - LSL_Types.LSLInteger x = 1; - s = (LSL_Types.LSLString) (x++); - s = (LSL_Types.LSLString) (x); - s = (LSL_Types.LSLString) (new LSL_Types.Vector3(0.0, 0.0, 0.0)); - s = (LSL_Types.LSLString) (new LSL_Types.Quaternion(1.0, 1.0, 1.0, 1.0)); - s = (LSL_Types.LSLInteger) (""1""); - s = (LSL_Types.LSLString) (llSomethingThatReturnsInteger()); - s = (LSL_Types.LSLString) (134); - s = (LSL_Types.LSLString) (x ^ y | (z && l)) + (LSL_Types.LSLString) (x + y - 13); - llOwnerSay(""s is: "" + s); - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestStates() - { - string input = @"// let's test states - -default -{ - touch_start(integer num_detected) - { - llSay(0, ""Going to state 'statetwo'""); - state statetwo; - } -} - -state statetwo -{ - state_entry() - { - llSay(0, ""Going to the default state""); - state default; - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - llSay(0, ""Going to state 'statetwo'""); - state(""statetwo""); - } - public void statetwo_event_state_entry() - { - llSay(0, ""Going to the default state""); - state(""default""); - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestHexIntegerConstants() - { - string input = @"// let's test hex integers - -default -{ - touch_start(integer num_detected) - { - integer x = 0x23; - integer x = 0x2f34B; - integer x = 0x2F34b; - integer x = 0x2F34B; - integer x = 0x2f34b; - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLInteger x = 0x23; - LSL_Types.LSLInteger x = 0x2f34B; - LSL_Types.LSLInteger x = 0x2F34b; - LSL_Types.LSLInteger x = 0x2F34B; - LSL_Types.LSLInteger x = 0x2f34b; - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestJumps() - { - string input = @"// let's test jumps - -default -{ - touch_start(integer num_detected) - { - jump here; - llOwnerSay(""Uh oh, the jump didn't work""); - @here; - llOwnerSay(""After the jump""); - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - goto here; - llOwnerSay(""Uh oh, the jump didn't work""); - here: - llOwnerSay(""After the jump""); - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestImplicitVariableInitialization() - { - string input = @"// let's test implicitly initializing variables - -default -{ - touch_start(integer num_detected) - { - integer i; integer j = 14; - float f; float g = 14.0; - string s; string t = ""Hi there""; - list l; list m = [1, 2, 3]; - vector v; vector w = <1.0, 0.1, 0.5>; - rotation r; rotation u = <0.8, 0.7, 0.6, llSomeFunc()>; - key k; key n = ""ping""; - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLInteger i = 0; - LSL_Types.LSLInteger j = 14; - LSL_Types.LSLFloat f = 0.0; - LSL_Types.LSLFloat g = 14.0; - LSL_Types.LSLString s = """"; - LSL_Types.LSLString t = ""Hi there""; - LSL_Types.list l = new LSL_Types.list(); - LSL_Types.list m = new LSL_Types.list(1, 2, 3); - LSL_Types.Vector3 v = new LSL_Types.Vector3(0.0, 0.0, 0.0); - LSL_Types.Vector3 w = new LSL_Types.Vector3(1.0, 0.1, 0.5); - LSL_Types.Quaternion r = new LSL_Types.Quaternion(0.0, 0.0, 0.0, 0.0); - LSL_Types.Quaternion u = new LSL_Types.Quaternion(0.8, 0.7, 0.6, llSomeFunc()); - LSL_Types.LSLString k = """"; - LSL_Types.LSLString n = ""ping""; - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestMultipleEqualsExpression() - { - string input = @"// let's test x = y = 5 type expressions - -default -{ - touch_start(integer num_detected) - { - integer x; - integer y; - x = y = 5; - x += y -= 5; - llOwnerSay(""x is: "" + (string) x + "", y is: "" + (string) y); - } -} -"; - string expected = @" - public void default_event_touch_start(LSL_Types.LSLInteger num_detected) - { - LSL_Types.LSLInteger x = 0; - LSL_Types.LSLInteger y = 0; - x = y = 5; - x += y -= 5; - llOwnerSay(""x is: "" + (LSL_Types.LSLString) (x) + "", y is: "" + (LSL_Types.LSLString) (y)); - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestUnaryExpressionLastInVectorConstant() - { - string input = @"// let's test unary expressions some more - -default -{ - state_entry() - { - vector v = ; - } -} -"; - string expected = @" - public void default_event_state_entry() - { - LSL_Types.Vector3 v = new LSL_Types.Vector3(x, y, -0.5); - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - public void TestVectorMemberPlusEquals() - { - string input = @"// let's test unary expressions some more - -default -{ - state_entry() - { - vector v = llGetPos(); - v.z += 4; - v.z -= 4; - v.z *= 4; - v.z /= 4; - v.z %= 4; - } -} -"; - string expected = @" - public void default_event_state_entry() - { - LSL_Types.Vector3 v = llGetPos(); - v.z += 4; - v.z -= 4; - v.z *= 4; - v.z /= 4; - v.z %= 4; - } -"; - - CSCodeGenerator cg = new CSCodeGenerator(); - string output = cg.Convert(input); - Assert.AreEqual(expected, output); - } - - [Test] - [ExpectedException("Tools.CSToolsException")] - public void TestSyntaxError() - { - string input = @"default -{ - state_entry() - { - integer y - } -} -"; - try - { - CSCodeGenerator cg = new CSCodeGenerator(); - cg.Convert(input); - } - catch (Tools.CSToolsException e) - { - // The syntax error is on line 6, char 5 (expected ';', found - // '}'). - Regex r = new Regex("Line ([0-9]+), char ([0-9]+)"); - Match m = r.Match(e.Message); - Assert.AreEqual("6", m.Groups[1].Value); - Assert.AreEqual("5", m.Groups[2].Value); - - throw; - } - } - } -} -- cgit v1.1