Beispiel #1
0
        private CompilerResults CompileScript(
            string input, out Dictionary <KeyValuePair <int, int>, KeyValuePair <int, int> > positionMap)
        {
            m_compilerParameters.OutputAssembly = Path.Combine(m_testDir, Path.GetRandomFileName() + ".dll");

            CSCodeGenerator cg     = new CSCodeGenerator();
            string          output = cg.Convert(input);

            output = Compiler.CreateCSCompilerScript(output, "script1", typeof(ScriptBaseClass).FullName, null);
            //            System.Console.WriteLine(output);

            positionMap = cg.PositionMap;

            CompilerResults compilerResults = m_CSCodeProvider.CompileAssemblyFromSource(m_compilerParameters, output);

            //            foreach (KeyValuePair<int, int> key in positionMap.Keys)
            //            {
            //                KeyValuePair<int, int> val = positionMap[key];
            //
            //                System.Console.WriteLine("{0},{1} => {2},{3}", key.Key, key.Value, val.Key, val.Value);
            //            }
            //
            //            foreach (CompilerError compErr in m_compilerResults.Errors)
            //            {
            //                System.Console.WriteLine("Error: {0},{1} => {2}", compErr.Line, compErr.Column, compErr);
            //            }

            return(compilerResults);
        }
        /// <summary>
        /// Test the C# compiler error message can be mapped to the correct
        /// line/column in the LSL source when an undeclared variable is used.
        /// </summary>
        //[Test]
        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 <int, int>, KeyValuePair <int, int> > positionMap = cg.PositionMap;

            m_compilerResults = m_CSCodeProvider.CompileAssemblyFromSource(m_compilerParameters, output);

            Assert.AreEqual(new KeyValuePair <int, int>(5, 21),
                            positionMap[new KeyValuePair <int, int>(m_compilerResults.Errors[0].Line, m_compilerResults.Errors[0].Column)]);
        }
        /// <summary>
        /// Test that a string can be cast to string and another string
        /// concatenated.
        /// </summary>
        //[Test]
        public void TestCastAndConcatString()
        {
            m_compilerParameters.OutputAssembly = Path.Combine(m_testDir, Path.GetRandomFileName() + ".dll");

            string input = @"string s = "" a string"";

default
{
    state_entry()
    {
        key gAvatarKey = llDetectedKey(0);
        string tmp = (string) gAvatarKey + s;
        llSay(0, tmp);
    }
}";

            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";

            m_compilerResults = m_CSCodeProvider.CompileAssemblyFromSource(m_compilerParameters, output);

            Assert.AreEqual(0, m_compilerResults.Errors.Count);
        }
 public void Convert(string Script, out string CompiledScript,
                     out object PositionMap)
 {
     // Its LSL, convert it to C#
     LSL_Converter  = new CSCodeGenerator(m_compiler);
     CompiledScript = LSL_Converter.Convert(Script);
     PositionMap    = LSL_Converter.PositionMap;
 }
Beispiel #5
0
        public void FindErrorLine(CompilerError CompErr, object PositionMap, string script, out int LineN, out int CharN)
        {
            Dictionary <int, int> PositionMapp = (Dictionary <int, int>)PositionMap;

            LineN = CompErr.Line - CSCodeGenerator.GetHeaderCount(m_compiler);
            CharN = 1;
            LineN = PositionMapp[LineN - 1];//LSL is zero based, so subtract one
        }
Beispiel #6
0
        public void Convert(string Script, out string CompiledScript, out Dictionary <KeyValuePair <int, int>, KeyValuePair <int, int> > PositionMap)
        {
            // Its LSL, convert it to C#
            LSL_Converter  = new CSCodeGenerator(m_compiler);
            CompiledScript = LSL_Converter.Convert(Script);
            PositionMap    = LSL_Converter.PositionMap;

            LSL_Converter.Dispose(); //Resets it for next time
        }
Beispiel #7
0
        public void Convert(string Script, out string CompiledScript,
                            out object PositionMap)
        {
            // Its LSL, convert it to C#
            LSL_Converter  = new CSCodeGenerator(m_compiler);
            CompiledScript = LSL_Converter.Convert(Script);
            PositionMap    = LSL_Converter.PositionMap;

            //Unless we are using the same LSL_Converter more than once, we don't need to do this
            //LSL_Converter.Dispose(); //Resets it for next time
        }
    public static void Main(string[] argv)
    {
        StreamReader s      = new StreamReader(argv[0]);
        string       source = s.ReadToEnd();

        CSCodeGenerator cscg   = new CSCodeGenerator();
        string          output = cscg.Convert(source);

        if (1 < argv.Length && "-t" == argv[1])
        {
            Parser p = new LSLSyntax();
            LSL2CSCodeTransformer codeTransformer = new LSL2CSCodeTransformer(p.Parse(source));
            SYMBOL ast = codeTransformer.Transform();
            printThisTree(ast);
        }
        else
        {
            Console.Write(output);
        }
    }
Beispiel #9
0
        public void Convert(string Script, out string CompiledScript,
                            out object PositionMap)
        {
            // Its LSL, convert it to C#
            Dictionary <int, int> map     = new Dictionary <int, int>();
            List <string>         csClass = new List <string>();

            Script = Script.Replace("\n", "\r\n");

            List <string> GlobalFunctions;

            string[] lineSplit = ConvertLSLTypes(Script, out GlobalFunctions);
            Dictionary <string, string[]> EnumerableFunctionInfos = new Dictionary <string, string[]>();

            foreach (string function in GlobalFunctions)
            {
                string[] func = GetInfo(function);
                EnumerableFunctionInfos[func[1]] = func;
            }
            List <string> splits      = new List <string>();
            List <string> breaksplits = new List <string>();

            foreach (string s in lineSplit)
            {
                splits.AddRange(s.Trim().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
                breaksplits.AddRange(s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries));
            }
            breaksplits.RemoveAll(RemoveR);
            string[]   split                      = splits.ToArray();
            string[]   breaksplit                 = breaksplits.ToArray();
            int        i                          = 0;
            string     currentState               = "";
            bool       InState                    = false;
            bool       inMethod                   = false;
            int        skipUntil                  = 0;
            int        bracketInsideMethod        = 0;
            List <int> ProtectedBracketLoops      = new List <int>();
            bool       AddBracketAfterNextCommand = false;

            foreach (string wword in split)
            {
                if (i < skipUntil)
                {
                    i++;
                    continue;
                }
                string word = wword.Replace("\r", "");
                if (word.StartsWith("//"))
                {
                    GetUntilBreak(split, breaksplit, i, out skipUntil);
                }
                else if (word.StartsWith("/*"))
                {
                    GetUntil(split, "*/", i, out skipUntil);
                }
                else if (word == "default")
                {
                    currentState = "default";
                    InState      = true;
                }
                else if (word == "state")
                {
                    if (inMethod)
                    {
                        currentState = GetUntilBreak(split, breaksplit, i, out skipUntil).Replace(";", "").Trim();
                        AddToClass(csClass, string.Format(
                                       "((ILSL_Api)m_apis[\"ll\"]).state(\"{0}\");",
                                       currentState), split, breaksplit, lineSplit, i, ref map);
                    }
                    else
                    {
                        currentState = GetNextWord(split, i);
                        skipUntil    = i + 2;
                        InState      = true;
                    }
                }
                else if (word.IndexOf("(") != -1 &&
                         Events.Contains(word.Substring(0, word.IndexOf("("))))
                {
                    if (!InState)
                    {
                        throw new ParseException("Event is not in a state");
                    }
                    else
                    {
                        AddToClass(csClass, GenerateEvent(currentState, split, breaksplit, i, out skipUntil),
                                   split, breaksplit, lineSplit, i, ref map);
                        inMethod = true;
                    }
                }
                else if (word == "{" || word == "}")
                {
                    bool addToClass = !(word == "{" && bracketInsideMethod == 0 && InState) &&
                                      !(word == "}" && bracketInsideMethod == 1 && InState);
                    if (word == "{")
                    {
                        bracketInsideMethod++;
                    }
                    else
                    {
                        bracketInsideMethod--;
                        if (ProtectedBracketLoops.Contains(bracketInsideMethod))
                        {
                            ProtectedBracketLoops.Remove(bracketInsideMethod);
                            AddToClass(csClass, GenerateTimeCheck("", true), split, breaksplit, lineSplit, i, ref map);
                        }
                        if (bracketInsideMethod == (InState ? 1 : 0))
                        {
                            //We're inside an enumerable function, add the yield return/break
                            AddToClass(csClass, GenerateReturn(""), split, breaksplit, lineSplit, i, ref map);
                            inMethod = false;
                        }
                        if (bracketInsideMethod == 0)
                        {
                            InState = false;
                        }
                    }
                    if (addToClass)
                    {
                        AddToClass(csClass, word,
                                   split, breaksplit, lineSplit, i, ref map);
                    }
                }
                else if (inMethod)
                {
                    bool   wasSemicolan = false;
                    string csLine       = split[i].StartsWith("for") ?
                                          GetUntilBreak(split, breaksplit, i, out skipUntil)
                        :
                                          GetUntilSemicolanOrBreak(split, breaksplit, i, out skipUntil, out wasSemicolan);
                    if (wasSemicolan && csLine.StartsWith("return"))
                    {
                        csLine = GenerateReturn(csLine);
                    }
                    AddDefaultInitializers(ref csLine);
                    if (AddBracketAfterNextCommand)
                    {
                        AddBracketAfterNextCommand = false;
                        csLine += "\n }";
                    }

                    foreach (string call in ProtectedCalls)
                    {
                        if (csLine.StartsWith(call))
                        {
                            if (!GetNextWord(split, skipUntil - 1).StartsWith("{"))//Someone is trying to do while(TRUE) X();
                            {
                                csLine += " { ";
                                csLine  = GenerateTimeCheck(csLine, false);
                                AddBracketAfterNextCommand = true;
                            }
                            else
                            {
                                ProtectedBracketLoops.Add(bracketInsideMethod);//Make sure no loops are created as well
                            }
                        }
                    }
                    foreach (string call in CalledBeforeProtectedCalls)
                    {
                        if (csLine.StartsWith(call))
                        {
                            csLine = GenerateTimeCheck(csLine, true);//Make sure no other loops are created as well
                        }
                    }
                    string noSpacedLine = csLine.Replace(" ", "");
                    Match match;
                    foreach (string[] globFunc in EnumerableFunctionInfos.Values)
                    {
                        if (RegexContains(csLine, GenerateRegex(globFunc[1], int.Parse(globFunc[3])), out match))
                        {
                            csLine = GenerateNewFunction(csLine, globFunc, match);
                        }
                    }


                    AddToClass(csClass, csLine,
                               split, breaksplit, lineSplit, i, ref map);
                }
                else if (!InState)
                {
                    bool   wasSemicolan;
                    string line = GetUntil(split, ";", ")", i, out skipUntil, out wasSemicolan);
                    if (!wasSemicolan)
                    {
                        GenerateGlobalFunction(ref inMethod, ref line);
                    }
                    else
                    {
                        AddDefaultInitializers(ref line);
                    }

                    AddToClass(csClass, line,
                               split, breaksplit, lineSplit, i, ref map);
                }
                else
                {
                    AddToClass(csClass, "fake", split, breaksplit, lineSplit, i, ref map);
                    m_compiler.AddError(String.Format("({0},{1}): {3}: {2}\n",
                                                      map[csClass.Count - 1], 1, "Invalid expression term '" + word + "'", "Error"));
                    CompiledScript = "";
                    PositionMap    = null;
                    return;
                }
                i++;
            }

            PositionMap    = map;
            CompiledScript = CSCodeGenerator.CreateCompilerScript(m_compiler, new List <string>(), string.Join("\n", csClass.ToArray()));
        }