コード例 #1
0
        public override LuaValue Execute(LuaTable enviroment, out bool isBreak)
        {
            //[PixelCrushers]LuaValue[] values = this.ExprList.ConvertAll(expr => expr.Evaluate(enviroment)).ToArray();
            LuaValue[] values = LuaInterpreterExtensions.EvaluateAll(this.ExprList, enviroment).ToArray();

            LuaValue[] neatValues = LuaMultiValue.UnWrapLuaValues(values);

            LuaFunction func    = neatValues[0] as LuaFunction;
            LuaValue    state   = neatValues[1];
            LuaValue    loopVar = neatValues[2];

            var table = new LuaTable(enviroment);

            this.Body.Enviroment = table;

            while (true)
            {
                LuaValue      result     = func.Invoke(new LuaValue[] { state, loopVar });
                LuaMultiValue multiValue = result as LuaMultiValue;

                if (multiValue != null)
                {
                    neatValues = LuaMultiValue.UnWrapLuaValues(multiValue.Values);
                    loopVar    = neatValues[0];

                    for (int i = 0; i < Math.Min(this.NameList.Count, neatValues.Length); i++)
                    {
                        table.SetNameValue(this.NameList[i], neatValues[i]);
                    }
                }
                else
                {
                    loopVar = result;
                    table.SetNameValue(this.NameList[0], result);
                }

                if (loopVar == LuaNil.Nil)
                {
                    break;
                }

                var returnValue = this.Body.Execute(out isBreak);
                if (returnValue != null || isBreak == true)
                {
                    isBreak = false;
                    return(returnValue);
                }
            }

            isBreak = false;
            return(null);
        }
コード例 #2
0
ファイル: ForInStmt.cs プロジェクト: pavlik-y/Scratch
        public override LuaValue Execute(LuaTable enviroment, out bool isBreak)
        {
            //            LuaValue[] values = this.ExprList.ConvertAll(expr => expr.Evaluate(enviroment)).ToArray();
            List<LuaValue> values = new List<LuaValue>();
            foreach (var expr in this.ExprList)
                values.Add(expr.Evaluate(enviroment));
            LuaValue[] neatValues = LuaMultiValue.UnWrapLuaValues(values.ToArray());

            LuaFunction func = neatValues[0] as LuaFunction;
            LuaValue state = neatValues[1];
            LuaValue loopVar = neatValues[2];

            var table = new LuaTable(enviroment);
            this.Body.Enviroment = table;

            while (true)
            {
                LuaValue result = func.Invoke(new LuaValue[] { state, loopVar });
                LuaMultiValue multiValue = result as LuaMultiValue;

                if (multiValue != null)
                {
                    neatValues = LuaMultiValue.UnWrapLuaValues(multiValue.Values);
                    loopVar = neatValues[0];

                    for (int i = 0; i < Math.Min(this.NameList.Count, neatValues.Length); i++)
                    {
                        table.SetNameValue(this.NameList[i], neatValues[i]);
                    }
                }
                else
                {
                    loopVar = result;
                    table.SetNameValue(this.NameList[0], result);
                }

                if (loopVar == LuaNil.Nil)
                {
                    break;
                }

                var returnValue = this.Body.Execute(out isBreak);
                if (returnValue != null || isBreak == true)
                {
                    isBreak = false;
                    return returnValue;
                }
            }

            isBreak = false;
            return null;
        }
コード例 #3
0
        public override LuaValue Evaluate(LuaTable enviroment)
        {
            LuaTable table = new LuaTable();

            foreach (Field field in this.FieldList)
            {
                NameValue nameValue = field as NameValue;
                if (nameValue != null)
                {
                    table.SetNameValue(nameValue.Name, nameValue.Value.Evaluate(enviroment));
                    continue;
                }

                KeyValue keyValue = field as KeyValue;
                if (keyValue != null)
                {
                    table.SetKeyValue(
                        keyValue.Key.Evaluate(enviroment),
                        keyValue.Value.Evaluate(enviroment));
                    continue;
                }

                ItemValue itemValue = field as ItemValue;
                if (itemValue != null)
                {
                    table.AddValue(itemValue.Value.Evaluate(enviroment));
                    continue;
                }
            }

            return(table);
        }
コード例 #4
0
ファイル: ForStmt.cs プロジェクト: farreltr/OneLastSunset
        public override LuaValue Execute(LuaTable enviroment, out bool isBreak)
        {
            LuaNumber start = this.Start.Evaluate(enviroment) as LuaNumber;
            LuaNumber end = this.End.Evaluate(enviroment) as LuaNumber;

            double step = 1;
            if (this.Step != null)
            {
                step = (this.Step.Evaluate(enviroment) as LuaNumber).Number;
            }

            var table = new LuaTable(enviroment);
            table.SetNameValue(this.VarName, start);
            this.Body.Enviroment = table;

            while (step > 0 && start.Number <= end.Number ||
                   step <= 0 && start.Number >= end.Number)
            {
                var returnValue = this.Body.Execute(out isBreak);
                if (returnValue != null || isBreak == true)
                {
                    isBreak = false;
                    return returnValue;
                }
                start.Number += step;
            }

            isBreak = false;
            return null;
        }
コード例 #5
0
        public override LuaValue Execute(LuaTable enviroment, out bool isBreak)
        {
            LuaNumber start = Start.Evaluate(enviroment) as LuaNumber;
            LuaNumber end   = End.Evaluate(enviroment) as LuaNumber;

            double step = 1;

            if (Step != null)
            {
                step = (Step.Evaluate(enviroment) as LuaNumber).Number;
            }

            var table = new LuaTable(enviroment);

            table.SetNameValue(VarName, start);
            Body.Enviroment = table;

            while (step > 0 && start.Number <= end.Number ||
                   step <= 0 && start.Number >= end.Number)
            {
                var returnValue = Body.Execute(out isBreak);
                if (returnValue != null || isBreak == true)
                {
                    isBreak = false;
                    return(returnValue);
                }
                start.Number += step;
            }

            isBreak = false;
            return(null);
        }
コード例 #6
0
        public override LuaValue Evaluate(LuaTable enviroment)
        {
            LuaTable table = new LuaTable();

            foreach (Field field in this.FieldList)
            {
                NameValue nameValue = field as NameValue;
                if (nameValue != null)
                {
                    table.SetNameValue(nameValue.Name, nameValue.Value.Evaluate(enviroment));
                    continue;
                }

                KeyValue keyValue = field as KeyValue;
                if (keyValue != null)
                {
                    table.SetKeyValue(
                        keyValue.Key.Evaluate(enviroment),
                        keyValue.Value.Evaluate(enviroment));
                    continue;
                }

                ItemValue itemValue = field as ItemValue;
                if (itemValue != null)
                {
                    table.AddValue(itemValue.Value.Evaluate(enviroment));
                    continue;
                }
            }

            return table;
        }
コード例 #7
0
        public override LuaValue Execute(LuaTable enviroment, out bool isBreak)
        {
            LuaTable table = enviroment;

            if (Name.MethodName == null)
            {
                for (int i = 0; i < Name.FullName.Count - 1; i++)
                {
                    LuaValue obj = enviroment.GetValue(Name.FullName[i]);
                    table = obj as LuaTable;

                    if (table == null)
                    {
                        throw new Exception("Not a table: " + Name.FullName[i]);
                    }
                }

                table.SetNameValue(
                    Name.FullName[Name.FullName.Count - 1],
                    Body.Evaluate(enviroment));
            }
            else
            {
                for (int i = 0; i < Name.FullName.Count; i++)
                {
                    LuaValue obj = enviroment.GetValue(Name.FullName[i]);
                    table = obj as LuaTable;

                    if (table == null)
                    {
                        throw new Exception("Not a table " + Name.FullName[i]);
                    }
                }

                Body.ParamList.NameList.Insert(0, "self");

                table.SetNameValue(
                    Name.MethodName,
                    Body.Evaluate(enviroment));
            }

            isBreak = false;
            return(null);
        }
コード例 #8
0
        public LuaValue Evaluate(LuaTable enviroment)
        {
            return(new LuaFunction(
                       new LuaFunc(delegate(LuaValue[] args)
            {
                var table = new LuaTable(enviroment);

                List <string> names = this.ParamList.NameList;

                if (names.Count > 0)
                {
                    int argCount = Math.Min(names.Count, args.Length);

                    for (int i = 0; i < argCount; i++)
                    {
                        table.SetNameValue(names[i], args[i]);
                    }

                    if (this.ParamList.HasVarArg)
                    {
                        if (argCount < args.Length)
                        {
                            LuaValue[] remainedArgs = new LuaValue[args.Length - argCount];
                            for (int i = 0; i < remainedArgs.Length; i++)
                            {
                                remainedArgs[i] = args[argCount + i];
                            }
                            table.SetNameValue("...", new LuaMultiValue(remainedArgs));
                        }
                    }
                }
                else if (this.ParamList.IsVarArg != null)
                {
                    table.SetNameValue("...", new LuaMultiValue(args));
                }

                this.Chunk.Enviroment = table;

                return this.Chunk.Execute();
            })
                       ));
        }
コード例 #9
0
ファイル: Expr.cs プロジェクト: rhinohq/A2-Computing-Project
        public LuaValue Evaluate(LuaTable environment)
        {
            return new LuaFunction(
                new LuaFunc(delegate (LuaValue[] args)
                {
                    var table = new LuaTable(environment);

                    List<string> names = ParamList.NameList;

                    if (names.Count > 0)
                    {
                        int argCount = Math.Min(names.Count, args.Length);

                        for (int i = 0; i < argCount; i++)
                        {
                            table.SetNameValue(names[i], args[i]);
                        }

                        if (ParamList.HasVarArg)
                        {
                            if (argCount < args.Length)
                            {
                                LuaValue[] remainedArgs = new LuaValue[args.Length - argCount];
                                for (int i = 0; i < remainedArgs.Length; i++)
                                {
                                    remainedArgs[i] = args[argCount + i];
                                }
                                table.SetNameValue("...", new LuaMultiValue(remainedArgs));
                            }
                        }
                    }
                    else if (ParamList.IsVarArg != null)
                    {
                        table.SetNameValue("...", new LuaMultiValue(args));
                    }

                    Chunk.Environment = table;

                    return Chunk.Execute();
                })
            );
        }
コード例 #10
0
        // Creates global environment for the program and registers libraries
        public static LuaTable CreateGlobalEnviroment()
        {
            LuaTable global = new LuaTable();

            BaseLib.RegisterFunctions(global);
            StringLib.RegisterModule(global);
            MathLib.RegisterModule(global);

            global.SetNameValue("_G", global);

            return global;
        }
コード例 #11
0
        public static LuaTable CreateGlobalEnviroment()
        {
            LuaTable global = new LuaTable();

            BaseLib.RegisterFunctions(global);
            StringLib.RegisterModule(global);
            TableLib.RegisterModule(global);
            IOLib.RegisterModule(global);
            MathLib.RegisterModule(global);

            global.SetNameValue("_G", global);

            return(global);
        }
コード例 #12
0
        public LuaTable CreateModuleFromTypes(IEnumerable<Type> types, string moduleName = "")
        {
            LuaTable module = new LuaTable();
            StringBuilder moduleDocs = new StringBuilder();

            foreach(Type t in types) {
                if (t.GetCustomAttributes(true).OfType<LuaExcludeAttribute>().Any())
                    continue;

                string typeName = t.Name;
                moduleDocs.AppendLine(typeName);
                StringBuilder typeDoc = new StringBuilder();
                LuaTable subModule = new LuaTable();
                LuaTable metatable = new LuaTable();

                metatable.SetNameValue("__index", metatable);
                subModule.MetaTable = metatable;

                // Create a constructor where appropriate
                ConstructorInfo constructorInfo = null;
                if (t.IsValueType) {
                    subModule.Register("new", (args) => {
                        return ObjectToLua(Activator.CreateInstance(t));
                    });
                    typeDoc.AppendFormat("new() - creates a new instance of the {0} type.\n", typeName);
                } else {
                    if ((constructorInfo = t.GetConstructor(new Type[] { })) != null) {
                        subModule.Register("new", (args) => {
                            try {
                                return ObjectToLua(constructorInfo.Invoke(new object[] { }));
                            } catch (Exception e) {
                                Console.Error.WriteLine("Lua constructor for {0} threw an exception of type {1}: {2}", t.Name, e.GetType().Name, e.Message);
                                if (e.InnerException != null)
                                    Console.Error.WriteLine("Inner exception of type {0}: {1}", e.InnerException.GetType().Name, e.InnerException.Message);
                            }
                            return LuaNil.Nil;
                        });
                    } else if ((constructorInfo = t.GetConstructor(new Type[] { typeof(LuaValue[]) })) != null) {
                        subModule.Register("new", (args) => {
                            try {
                                return ObjectToLua(constructorInfo.Invoke(new object[] { args }));
                            } catch (Exception e) {
                                Console.Error.WriteLine("Lua constructor for {0} threw an exception of type {1}: {2}", t.Name, e.GetType().Name, e.Message);
                                if (e.InnerException != null)
                                    Console.Error.WriteLine("Inner exception of type {0}: {1}", e.InnerException.GetType().Name, e.InnerException.Message);
                            }
                            return LuaNil.Nil;
                        });
                    }
                    if (constructorInfo != null) {
                        if (constructorInfo.GetCustomAttributes(true).OfType<LuaCommandUsageAttribute>().Any()) {
                            var attribute = constructorInfo.GetCustomAttributes(true).OfType<LuaCommandUsageAttribute>().Single();
                            typeDoc.Append(attribute.Usage);
                        } else {
                            typeDoc.AppendFormat("new() - creates a new instance of the {0} type.\n", typeName);
                        }
                    }
                }

                // Create member accessors
                var members = from m in t.GetMembers(BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public)
                              where (m.MemberType == MemberTypes.Field ||
                                m.MemberType == MemberTypes.Property ||
                                m.MemberType == MemberTypes.Method)
                              select m;

                foreach(MemberInfo member in members) {
                    bool excludeGetter = false, excludeSetter = false;
                    if (member.GetCustomAttributes(true).OfType<LuaExcludeAttribute>().Any()) {
                        LuaExcludeAttribute exclude = member.GetCustomAttributes(true).OfType<LuaExcludeAttribute>().Single();
                        excludeSetter = true;
                        if (!exclude.ReadOnly)
                            excludeGetter = true;
                    }

                    var field = member as FieldInfo;
                    var prop = member as PropertyInfo;
                    var method = member as MethodInfo;

                    string memberType = "";
                    if (field != null) {
                        memberType = field.FieldType.Name;
                    } else if (prop != null) {
                        memberType = prop.PropertyType.Name;
                    } else if (method != null) {
                        memberType = method.DeclaringType.Name;
                    }

                    LuaFunction func = excludeSetter ? null : GetLuaFunc(member as MethodInfo);
                    if(func != null) {
                        // It's a function, and we'll register it.  No way to create
                        // documentation for it, so ideally it also makes use of the
                        // LuaCommandUsage attribute for documentation.
                        metatable.SetNameValue(member.Name, func);
                        if (member.GetCustomAttributes(true).OfType<LuaCommandUsageAttribute>().Any()) {
                            LuaCommandUsageAttribute usage = member.GetCustomAttributes(true).OfType<LuaCommandUsageAttribute>().Single();
                            typeDoc.AppendLine(String.Format("{0}:{1} - {2}", t.Name, member.Name, usage.Usage));
                        } else {
                            typeDoc.AppendLine(member.Name);
                        }
                    }

                    LuaFunction getter = excludeGetter ? null : GetAccessorLuaFunction(member);
                    if(getter != null) {
                        typeDoc.AppendFormat("{0} - Gets a {1} value from the {2} struct\n",
                                             "Get" + member.Name, memberType, typeName);
                        metatable.SetNameValue("Get" + member.Name, getter);
                    }

                    LuaFunction setter = excludeSetter ? null : GetSetterLuaFunction(member);
                    if(setter != null) {
                        typeDoc.AppendFormat("{0} - Sets a {1} value to the {2} struct\n",
                                             "Set" + member.Name, memberType, typeName);
                        metatable.SetNameValue("Set" + member.Name, setter);
                    }
                }

                // Register the type metatable and help docs
                _environment.SetMetatableForType(t, metatable);
                module.SetNameValue(typeName, subModule);
                LuaEnvironment.RegisterNewUsage(moduleName + "." + typeName, typeDoc.ToString());
            }
            // Register the module's documentation
            LuaEnvironment.RegisterNewUsage(moduleName, moduleDocs.ToString());

            return module;
        }
コード例 #13
0
 public override LuaValue Execute(LuaTable enviroment, out bool isBreak)
 {
     enviroment.SetNameValue(Name, Body.Evaluate(enviroment));
     isBreak = false;
     return(null);
 }
コード例 #14
0
 public LuaTable(LuaTable parent)
 {
     MetaTable = new LuaTable();
     MetaTable.SetNameValue("__index", parent);
     MetaTable.SetNameValue("__newindex", parent);
 }
コード例 #15
0
ファイル: Chunk.cs プロジェクト: rhinohq/A2-Computing-Project
 public override LuaValue Execute(LuaTable environment, out bool isBreak)
 {
     environment.SetNameValue(Name, Body.Evaluate(environment));
     isBreak = false;
     return null;
 }
コード例 #16
0
 public LuaTable(LuaTable parent)
 {
     MetaTable = new LuaTable();
     MetaTable.SetNameValue("__index", parent);
     MetaTable.SetNameValue("__newindex", parent);
 }
コード例 #17
0
ファイル: Game1.cs プロジェクト: pavlik-y/Scratch
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // TODO: use this.Content to load your game content here
            m_env = new Env();
            m_sunImg = Content.Load<Texture2D>("sun");
            string sunSpriteSer = @"
            {Sprites:[
            {Offset:{X:0,Y:0},Rectangle:{Height:122,Width:158,X:0,Y:0}},
            {Offset:{X:4,Y:0},Rectangle:{Height:122,Width:135,X:158,Y:0}}
            ]}
            ";
            MemoryStream ms = new MemoryStream();
            StreamWriter sw = new StreamWriter(ms);
            sw.Write(sunSpriteSer);
            sw.Flush();
            ms.Flush();
            ms.Seek(0, SeekOrigin.Begin);
            System.Runtime.Serialization.Json.DataContractJsonSerializer serializer = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(SpriteSheet));
            m_sunSS = (SpriteSheet)serializer.ReadObject(ms);
            m_sun = new Item(m_sunImg, m_sunSS);
            string script = "a != b";
            /*
            string script = @"
            item.SetSprite((env.GetTimeMs() / 200) % 2);
            item.SetSprite = 0;
            ";
            */
            Parser parser = new Parser();
            TextInput ti = new TextInput(script);
            bool succ;
            m_script = parser.ParseChunk(ti, out succ);
            if (!succ)
                Debug.WriteLine(parser.GetEorrorMessages());
            m_globals = new LuaTable();
            LuaTable methods = new LuaTable();
            methods.SetNameValue("__index", methods);
            methods.SetNameValue("__newindex", methods);
            methods.Register("SetSprite", m_sun.SetSprite_FromScript);
            m_globals.SetNameValue("item", new Language.Lua.LuaUserdata(m_sun, methods));

            methods = new LuaTable();
            methods.SetNameValue("__index", methods);
            methods.Register("GetTimeMs", m_env.GetTimeMs);
            m_globals.SetNameValue("env", new LuaUserdata(m_env, methods));

            m_env.TimeMs = 0;
        }