From cda6b2466868cc74367da1b17d676f7168b84f6d Mon Sep 17 00:00:00 2001 From: Mike Mazur Date: Sun, 7 Jun 2009 10:22:55 +0000 Subject: Skip lone ident statments or for-loop assignments SL's LSL supports lone idents: integer x; x; as well as lone idents in for-loop assignments: for (x; x < 10; x++) { ... } while those are errors in C# (MONO at least). This patch skips lone idents in such places. Fixes Mantis #3042. --- .../Shared/CodeTools/CSCodeGenerator.cs | 17 ++++++-- .../Shared/CodeTools/Tests/CSCodeGeneratorTest.cs | 51 ++++++++++++++++++++++ 2 files changed, 65 insertions(+), 3 deletions(-) (limited to 'OpenSim/Region/ScriptEngine/Shared') diff --git a/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs b/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs index ab9968f..91747af 100644 --- a/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs +++ b/OpenSim/Region/ScriptEngine/Shared/CodeTools/CSCodeGenerator.cs @@ -90,7 +90,7 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools m_warnings.Clear(); ResetCounters(); Parser p = new LSLSyntax(new yyLSLSyntax(), new ErrorHandler(true)); - // Obviously this needs to be in a try/except block. + LSL2CSCodeTransformer codeTransformer; try { @@ -446,8 +446,11 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools // Jump label prints its own colon, we don't need a semicolon. printSemicolon = !(s.kids.Top is JumpLabel); - foreach (SYMBOL kid in s.kids) - retstr += GenerateNode(kid); + // If we encounter a lone Ident, we skip it, since that's a C# + // (MONO) error. + if (!(s.kids.Top is IdentExpression && 1 == s.kids.Count)) + foreach (SYMBOL kid in s.kids) + retstr += GenerateNode(kid); } if (printSemicolon) @@ -711,6 +714,14 @@ namespace OpenSim.Region.ScriptEngine.Shared.CodeTools int comma = fls.kids.Count - 1; // tells us whether to print a comma + // It's possible that all we have is an empty Ident, for example: + // + // for (x; x < 10; x++) { ... } + // + // Which is illegal in C# (MONO). We'll skip it. + if (fls.kids.Top is IdentExpression && 1 == fls.kids.Count) + return retstr; + foreach (SYMBOL s in fls.kids) { retstr += GenerateNode(s); diff --git a/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CSCodeGeneratorTest.cs b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CSCodeGeneratorTest.cs index a7d4fa9..5dfbdbc 100644 --- a/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CSCodeGeneratorTest.cs +++ b/OpenSim/Region/ScriptEngine/Shared/CodeTools/Tests/CSCodeGeneratorTest.cs @@ -133,6 +133,32 @@ state another_state } [Test] + public void TestLoneIdent() + { + // A lone ident should be removed completely as it's an error in C# + // (MONO at least). + string input = @"default +{ + touch_start(integer num_detected) + { + integer x; + x; + } +} +"; + string expected = + "\n public void default_event_touch_start(LSL_Types.LSLInteger num_detected)" + + "\n {" + + "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(0);" + + "\n ;" + + "\n }\n"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] public void TestAssignments() { string input = @"default @@ -1541,6 +1567,31 @@ default } [Test] + public void TestForLoopWithOnlyIdentInAssignment() + { + string input = @"default +{ + state_entry() + { + integer x = 4; + for (x; 1<0; x += 2); + } +}"; + + string expected = + "\n public void default_event_state_entry()" + + "\n {" + + "\n LSL_Types.LSLInteger x = new LSL_Types.LSLInteger(4);" + + "\n for (; new LSL_Types.LSLInteger(1) < new LSL_Types.LSLInteger(0); x += new LSL_Types.LSLInteger(2))" + + "\n ;" + + "\n }\n"; + + CSCodeGenerator cg = new CSCodeGenerator(); + string output = cg.Convert(input); + Assert.AreEqual(expected, output); + } + + [Test] public void TestAssignmentInIfWhileDoWhile() { string input = @"default -- cgit v1.1