예제 #1
0
        /// <summary>
        /// Compiles an IParseItem tree indo an IModule object so that it can
        /// be executed.
        /// </summary>
        /// <param name="name">The name to given the module, can be null to
        /// auto-generate.</param>
        /// <param name="E">The current environment.</param>
        /// <param name="item">The item to compile.</param>
        /// <returns>A compiled version of the object.</returns>
        /// <exception cref="System.ArgumentNullException">If E or item is null.</exception>
        /// <exception cref="ModMaker.Lua.Parser.SyntaxException">If there is
        /// syntax errors in the item tree.</exception>
        public ILuaValue Compile(ILuaEnvironment E, IParseItem item, string name)
        {
            if (E == null)
            {
                throw new ArgumentNullException(nameof(E));
            }
            if (item == null)
            {
                throw new ArgumentNullException(nameof(item));
            }

            // get the name
            name = name ?? "<>_func_" + (_tid++);
            if (_types.Contains(name))
            {
                int i = 0;
                while (_types.Contains(name + i))
                {
                    i++;
                }
                name += i;
            }

            // resolve labels
            GetInfoVisitor lVisitor = new GetInfoVisitor();

            lVisitor.Resolve(item);

            // create the type
            TypeBuilder tb = _mb.DefineType(name, TypeAttributes.Public | TypeAttributes.BeforeFieldInit | TypeAttributes.Sealed,
                                            typeof(LuaValueBase), Type.EmptyTypes);
            ChunkBuilder cb = new ChunkBuilder(tb, lVisitor.GlobalCaptures, lVisitor.GlobalNested);

            // compile the code
            CompilerVisitor cVisitor = new CompilerVisitor(cb);

            item.Accept(cVisitor);
            var ret = cb.CreateChunk(E);

            return(ret);
        }
예제 #2
0
        /// <summary>
        /// Compiles an IParseItem tree indo an IModule object so that it can 
        /// be executed.
        /// </summary>
        /// <param name="name">The name to given the module, can be null to 
        /// auto-generate.</param>
        /// <param name="E">The current environment.</param>
        /// <param name="item">The item to compile.</param>
        /// <returns>A compiled version of the object.</returns>
        /// <exception cref="System.ArgumentNullException">If E or item is null.</exception>
        /// <exception cref="ModMaker.Lua.Parser.SyntaxException">If there is
        /// syntax errors in the item tree.</exception>
        public ILuaValue Compile(ILuaEnvironment E, IParseItem item, string name)
        {
            if (E == null)
                throw new ArgumentNullException("E");
            if (item == null)
                throw new ArgumentNullException("item");

            // get the name
            name = name ?? "<>_func_" + (_tid++);
            if (_types.Contains(name))
            {
                int i = 0;
                while (_types.Contains(name + i))
                    i++;
                name += i;
            }

            // resolve labels
            GetInfoVisitor lVisitor = new GetInfoVisitor();
            lVisitor.Resolve(item);

            // create the type
            TypeBuilder tb = _mb.DefineType(name, TypeAttributes.Public | TypeAttributes.BeforeFieldInit | TypeAttributes.Sealed,
                typeof(LuaValueBase), Type.EmptyTypes);
            ChunkBuilder cb = new ChunkBuilder(tb, lVisitor.GlobalCaptures, lVisitor.GlobalNested);

            // compile the code
            CompilerVisitor cVisitor = new CompilerVisitor(cb);
            item.Accept(cVisitor);
            var ret = cb.CreateChunk(E);
            return ret;
        }