Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        private static LuaTable GetMatches(LuaValue[] args)
        {
            LuaTable t = new LuaTable();

            string s      = (args[0] as LuaString).Text;
            string format = (args[1] as LuaString).Text;

            Regex re = new Regex(format);
            Match m  = re.Match(s);

            while (m.Success)
            {
                for (int i = 0; i < m.Groups.Count; i++)
                {
                    Group             g  = m.Groups[i];
                    CaptureCollection cc = g.Captures;
                    for (int j = 0; j < cc.Count; j++)
                    {
                        Capture c = cc[j];
                        t.AddValue(new LuaString(c.Value));
                    }
                }
                m = m.NextMatch();
            }
            return(t);
        }
Ejemplo n.º 3
0
        public static LuaValue find(LuaValue[] values) //[PixelCrushers] Added string.find()
        {
            LuaString strValue     = values[0] as LuaString;
            LuaString patternValue = values[1] as LuaString;
            LuaNumber initNumber   = values.Length > 2 ? values[2] as LuaNumber : null;
            //---Not implemented: LuaBoolean plainBoolean = values.Length > 3 ? values[3] as LuaBoolean : null;
            string s       = strValue.ToString();
            string pattern = patternValue.ToString();
            int    init    = (initNumber != null) ? (int)initNumber.Number : 1;
            //---Not implemented: bool plain = (plainBoolean != null) ? plainBoolean.BoolValue : false;
            var startIndex = s.IndexOf(pattern, init - 1) + 1;

            if (startIndex == 0)
            {
                return(LuaNil.Nil);
            }
            var endIndex = startIndex + pattern.Length - 1;
            var result   = new LuaTable();

            result.AddValue(new LuaNumber(startIndex));
            result.AddValue(new LuaNumber(endIndex));
            return(result);
        }
        public static LuaValue insert(LuaValue[] values)
        {
            LuaTable table = values[0] as LuaTable;

            if (values.Length == 2)
            {
                LuaValue item = values[1];
                table.AddValue(item);
            }
            else if (values.Length == 3)
            {
                LuaNumber number = values[1] as LuaNumber;
                LuaValue  item   = values[2];
                int       index  = (int)number.Number;
                table.InsertValue(index, item);
            }
            return(null);
        }
Ejemplo n.º 5
0
 private static LuaValue ToLuaValue(object value)
 {
     if (value is int || value is double)
     {
         return(new LuaNumber(Convert.ToDouble(value)));
     }
     else if (value is string)
     {
         return(new LuaString((string)value));
     }
     else if (value is bool)
     {
         return(LuaBoolean.From((bool)value));
     }
     else if (value == null)
     {
         return(LuaNil.Nil);
     }
     else if (value.GetType().IsEnum)
     {
         return(new LuaString(value.ToString()));
     }
     else if (value.GetType().IsArray)
     {
         LuaTable table = new LuaTable();
         foreach (var item in (value as Array))
         {
             table.AddValue(ToLuaValue(item));
         }
         return(table);
     }
     else if (value is LuaValue)
     {
         return((LuaValue)value);
     }
     else
     {
         LuaUserdata data = new LuaUserdata(value);
         data.MetaTable = GetControlMetaTable();
         return(data);
     }
 }
Ejemplo n.º 6
0
        public static void RegisterFunctions(LuaTable module)
        {
            // cpath -> cspath?
            module.SetNameValue("cpath", new LuaString(".\\?;.\\?.dll;.\\?.exe"));
            module.SetNameValue("path", new LuaString(".\\?;.\\?.lua;.\\?.slua;.\\?.wlua;" + System.Environment.GetEnvironmentVariable("LUA_PATH")));
            module.SetNameValue("bpath", new LuaString(".\\?;.\\?.luac;.\\?.out;.\\?.sluac"));
            module.SetNameValue("loaded", new LuaTable());
            module.SetNameValue("preload", new LuaTable());
            module.Register("seeall", SeeAll);

            // Load package loader functions
            LuaTable loaderfunctions = new LuaTable();

            loaderfunctions.Register("PreloadSearcher", new LuaFunc(delegate(LuaValue[] args) {
                LuaTable t = (LuaRuntime.GlobalEnvironment.GetValue("package") as LuaTable).GetValue("preload") as LuaTable;
                string mod = (args[0] as LuaString).Text;
                LuaValue v = t.GetValue(mod);
                if (v != null && v != LuaNil.Nil)
                {
                    return(new LuaMultiValue(new LuaValue[] { LuaBoolean.True, v as LuaTable }));
                }
                else
                {
                    return(new LuaMultiValue(new LuaValue[] { LuaBoolean.False }));
                }
            }));

            loaderfunctions.Register("PathSearcher", new LuaFunc(delegate(LuaValue[] args)
            {
                // get package.path variable
                string path = (LuaRuntime.GlobalEnvironment.GetValue("package") as LuaTable).GetValue("path").Value.ToString();
                // split into paths
                string[] paths = path.Split(';');
                // check file names
                foreach (string p in paths)
                {
                    foreach (LuaValue arg in args)
                    {
                        string sfn = arg.Value.ToString();
                        string fn  = p.Replace("?", sfn);
                        if (File.Exists(fn))
                        {
                            LuaTable m = new LuaTable();
                            m.AddValue(LuaRuntime.RunFile(fn, LuaRuntime.GlobalEnvironment));
                            return(new LuaMultiValue(new LuaValue[] { LuaBoolean.True, LuaRuntime.GlobalEnvironment.GetValue(Path.GetFileNameWithoutExtension(fn)) }));
                        }
                    }
                }
                return(new LuaMultiValue(new LuaValue[] { LuaBoolean.False }));
            }));

            loaderfunctions.Register("CSPathSearcher", new LuaFunc(delegate(LuaValue[] args)
            {
                // get package.path variable
                string path = (LuaRuntime.GlobalEnvironment.GetValue("package") as LuaTable).GetValue("cpath").Value.ToString();
                // split into paths
                string[] paths = path.Split(';');
                // check file names
                foreach (string p in paths)
                {
                    foreach (LuaValue arg in args)
                    {
                        string sfn = arg.Value.ToString();
                        string fn  = p.Replace("?", sfn);
                        if (File.Exists(fn))
                        {
                            Console.WriteLine("Loading file '" + fn + "'...");
                            string[] modules = ExternalLibraryLoader.Load(fn);
                            LuaTable t       = LuaRuntime.GlobalEnvironment.GetValue(modules[0]) as LuaTable;

                            return(new LuaMultiValue(new LuaValue[] { LuaBoolean.True, t }));
                        }
                    }
                }
                return(new LuaMultiValue(new LuaValue[] { LuaBoolean.False }));
            }));
            loaderfunctions.Register("BinarySearcher", new LuaFunc(delegate(LuaValue[] args)
            {
                // get package.path variable
                string path = (LuaRuntime.GlobalEnvironment.GetValue("package") as LuaTable).GetValue("bpath").Value.ToString();
                // split into paths
                string[] paths = path.Split(';');
                // check file names
                foreach (string p in paths)
                {
                    foreach (LuaValue arg in args)
                    {
                        string sfn = arg.Value.ToString();
                        string fn  = p.Replace("?", sfn);
                        if (File.Exists(fn))
                        {
                            LuaTable m = new LuaTable();
                            bool isBreak;
                            m.AddValue((Serializer.Deserialize(fn) as AST.Chunk).Execute(LuaRuntime.GlobalEnvironment, out isBreak));
                            return(new LuaMultiValue(new LuaValue[] { LuaBoolean.True, LuaRuntime.GlobalEnvironment.GetValue(Path.GetFileNameWithoutExtension(fn)) }));
                        }
                    }
                }
                return(new LuaMultiValue(new LuaValue[] { LuaBoolean.False }));
            }));
            module.SetNameValue("loaders", loaderfunctions);
            module.Register("loadlib", new LuaFunc((LuaValue[] args) =>
            {
                return(args[0]);
            }));
        }