示例#1
0
文件: Parser.cs 项目: strogo/neolua
        /// <summary>Parses the chunk to an function.</summary>
        /// <param name="runtime">Binder</param>
        /// <param name="options">Compile options for the script.</param>
        /// <param name="lHasEnvironment">Creates the _G parameter.</param>
        /// <param name="code">Lexer for the code.</param>
        /// <param name="typeDelegate">Type for the delegate. <c>null</c>, for an automatic type</param>
        /// <param name="returnType">Defines the return type of the chunk.</param>
        /// <param name="args">Arguments of the function.</param>
        /// <returns>Expression-Tree for the code.</returns>
        public static LambdaExpression ParseChunk(Lua runtime, LuaCompileOptions options, bool lHasEnvironment, LuaLexer code, Type typeDelegate, Type returnType, IEnumerable<KeyValuePair<string, Type>> args)
        {
            List<ParameterExpression> parameters = new List<ParameterExpression>();
            if (returnType == null)
                returnType = typeof(LuaResult);
            var globalScope = new GlobalScope(runtime, options, returnType, returnType == typeof(LuaResult) ? Expression.Property(null, Lua.ResultEmptyPropertyInfo) : null);

            // Registers the global LuaTable
            if (lHasEnvironment)
                parameters.Add(globalScope.RegisterParameter(typeof(LuaTable), csEnv));

            if (args != null)
            {
                foreach (var c in args)
                    parameters.Add(globalScope.RegisterParameter(c.Value, c.Key)); // Add alle arguments
            }

            // Get the first token
            if (code.Current == null)
                code.Next();

            // Get the name for the chunk and clean it from all unwanted chars
            string sChunkName = CreateNameFromFile(code.Current.Start.FileName);
              if ((globalScope.EmitDebug & LuaDebugLevel.RegisterMethods) == LuaDebugLevel.RegisterMethods)
                sChunkName = Lua.RegisterUniqueName(sChunkName);

            // Create the block
            ParseBlock(globalScope, code);

            if (code.Current.Typ != LuaToken.Eof)
                throw ParseError(code.Current, Properties.Resources.rsParseEof);

            // Create the function
            return typeDelegate == null ?
                Expression.Lambda(globalScope.ExpressionBlock, sChunkName, parameters) :
                Expression.Lambda(typeDelegate, globalScope.ExpressionBlock, sChunkName, parameters);
        }