Example #1
0
        /// <summary>
        /// Creates a global environment with all the base modules registered and
        /// some default values set.
        /// </summary>
        /// <returns></returns>
        public static LuaTable CreateGlobalEnviroment(bool createBaseLib       = true,
                                                      bool createStringLib     = true,
                                                      bool createTableLib      = true,
                                                      bool createOSLib         = true,
                                                      bool createIOLib         = true,
                                                      bool createFileLib       = true,
                                                      bool createMathLib       = true,
                                                      bool createScriptLib     = true,
                                                      bool createWinFormsLib   = true,
                                                      bool createConsoleLib    = true,
                                                      bool createCoroutineLib  = true,
                                                      bool createPackageLib    = true,
                                                      bool createClassLib      = true,
                                                      bool createFileSystemLib = true)
        {
            LuaTable global = new LuaTable();

            // Register Lua Modules

            if (createBaseLib)
            {
                BaseLib.RegisterFunctions(global);
            }
            if (createStringLib)
            {
                StringLib.RegisterModule(global);
            }
            if (createTableLib)
            {
                TableLib.RegisterModule(global);
            }
            if (createIOLib)
            {
                IOLib.RegisterModule(global);
            }
            if (createFileLib)
            {
                FileLib.RegisterModule(global);
            }
            if (createMathLib)
            {
                MathLib.RegisterModule(global);
            }
            if (createOSLib)
            {
                OSLib.RegisterModule(global);
            }
            if (createScriptLib)
            {
                ScriptLib.RegisterModule(global);
            }
            //if (createWinFormsLib)
            //    WinFormLib.RegisterModule(global);
            if (createConsoleLib)
            {
                ConsoleLib.RegisterModule(global);
            }
            if (createCoroutineLib)
            {
                CoroutineLib.RegisterModule(global);
            }
            if (createPackageLib)
            {
                PackageLib.RegisterModule(global);
            }
            if (createClassLib)
            {
                ClassLib.RegisterModule(global);
            }
            if (createFileSystemLib)
            {
                FileSystemLib.RegisterModule(global);
            }

            //global.SetNameValue("_WORKDIR", new LuaString(Application.StartupPath + "\\"));
            global.SetNameValue("_VERSION", new LuaString("Sharp Lua 1.1"));
            global.SetNameValue("_G", global);

            if (createPackageLib)
            {
                // set package.preload table
                LuaTable preload = (LuaTable)(global.GetValue("package") as LuaTable).GetValue("preload");
                if (createStringLib)
                {
                    preload.SetNameValue("string", (LuaTable)global.GetValue("string"));
                }
                if (createTableLib)
                {
                    preload.SetNameValue("table", (LuaTable)global.GetValue("table"));
                }
                if (createIOLib)
                {
                    preload.SetNameValue("io", (LuaTable)global.GetValue("io"));
                }
                if (createFileLib)
                {
                    preload.SetNameValue("file", (LuaTable)global.GetValue("file"));
                }
                if (createMathLib)
                {
                    preload.SetNameValue("math", (LuaTable)global.GetValue("math"));
                }
                if (createOSLib)
                {
                    preload.SetNameValue("os", (LuaTable)global.GetValue("os"));
                }
                if (createScriptLib)
                {
                    preload.SetNameValue("script", (LuaTable)global.GetValue("script"));
                }
                //if (createWinFormsLib)
                //    preload.SetNameValue("WinForms", (LuaTable) global.GetValue("WinForms"));
                if (createConsoleLib)
                {
                    preload.SetNameValue("console", (LuaTable)global.GetValue("console"));
                }
                if (createCoroutineLib)
                {
                    preload.SetNameValue("coroutine", (LuaTable)global.GetValue("coroutine"));
                }
                if (createPackageLib) // wait a second...
                {
                    preload.SetNameValue("package", (LuaTable)global.GetValue("package"));
                }
                if (createClassLib)
                {
                    preload.SetNameValue("class", (LuaTable)global.GetValue("class"));
                }
                if (createFileSystemLib)
                {
                    preload.SetNameValue("filesystem", (LuaTable)global.GetValue("filesystem"));
                }
            }
            if (createFileSystemLib)
            {
                FileSystemLib.currentDir = global.GetValue("_WORKDIR").ToString();
            }

            GlobalEnvironment = global;
            return(global);
        }
Example #2
0
        public static void Main(string[] arg)
        {
            // *** Parse arguments.

            if (arg.Length == 0)
            {
                PrintUsage();
                return;
            }

            bool optRunTests        = false;
            bool optInteractiveMode = false;
            bool optVerbose         = false;
            bool optPause           = false;
            bool optDump            = false;

            List <string> optFiles        = new List <string>();
            List <string> passThroughArgs = null;

            for (int iArg = 0; iArg < arg.Length; ++iArg)
            {
                string ar = arg[iArg];
                if (ar[0] == '-' || ar[0] == '/')
                {
                    if (ar[1] == 't')
                    {
                        optRunTests = true;
                    }
                    else if (ar[1] == 'i')
                    {
                        optInteractiveMode = true;
                    }
                    else if (ar[1] == 'v')
                    {
                        Console.WriteLine("verbose enabled.");
                        optVerbose = true;
                    }
                    else if (ar[1] == 'p')
                    {
                        optPause = true;
                    }
                    else if (ar[1] == 'd')
                    {
                        optDump = true;
                    }
                    else if (ar[1] == 'a')
                    {
                        if (iArg == arg.Length - 1)
                        {
                            Console.WriteLine("Argument expected after -a option.");
                            PrintUsage();
                            return;
                        }
                        else
                        {
                            passThroughArgs = passThroughArgs ?? new List <string>();
                            passThroughArgs.Add(arg[++iArg]);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Unrecognized option '" + ar[1] + "'.");
                        PrintUsage();
                        return;
                    }
                }
                else
                {
                    optFiles.Add(ar);
                }
            }


            // *** Initialize engine.

            // Create engine.
            Engine engine = new Engine();

            engine.LogError = (msg) => {
                ConsoleColor sav = Console.ForegroundColor;
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(msg);
                Console.ForegroundColor = sav;
            };

            // Register optional libraries.
            DebugLib.Register(engine);
            FileLib.Register(engine);
            DateTimeLib.Register(engine);
            ConsoleLib.Register(engine);

            List <ParseErrorInst> errors = new List <ParseErrorInst>();

            // Create pass-through args.
            if (null != passThroughArgs)
            {
                string initScript = "global List<string> CLIargs = new { ";
                foreach (string pta in passThroughArgs)
                {
                    initScript += "Add(\"" + pta + "\"); ";
                }
                initScript += "};";

                ScriptResult result = engine.RunScript(initScript, false);
                if (!result.success)
                {
                    Console.WriteLine("INTERNAL ERROR creating passthrough arguments array.");
                    return;
                }
                errors.Clear();
            }

            // *** Do tasks.

            // Tests...
            if (optRunTests)
            {
                UnitTests.RunTests(engine, optVerbose);
            }

            // Files...
            foreach (string filename in optFiles)
            {
                string fileContents;
                try {
                    fileContents = File.ReadAllText(filename);
                } catch (Exception e) {
                    Console.WriteLine("Error attempting to open '" + filename + "': " + e.Message);
                    break;
                }
                errors.Clear();

                ScriptResult result = engine.RunScript(fileContents, optVerbose, filename);
                if (result.success)
                {
                    Console.WriteLine("Returned: " + CoreLib.ValueToString(engine.defaultContext, result.value, true));
                }
                else
                {
                    // At time of writing, errors get printed (in red) when they are created, so don't need to do it again here.
                    //Console.WriteLine(result);
                    Console.WriteLine("  " + filename + " failed to compile or execute.");
                }
            }

            // Interactive mode...
            if (optInteractiveMode)
            {
                Console.WriteLine("Pebble Interpreter (C) 2021 Patrick Cyr");
                Console.WriteLine("Interactive mode. Enter 'exit' to exit, 'help' for help:");

                // Lines in interactive mode don't use their own scope. Instead, they
                // use one shared scope that is created here.
                engine.defaultContext.stack.PushTerminalScope("<interactive mode>", null);

                while (true)
                {
                    Console.Write("> ");
                    string line = Console.ReadLine();

                    line = line.Trim();
                    if (line.Equals("exit", StringComparison.OrdinalIgnoreCase))
                    {
                        break;
                    }
                    if (line.Equals("help", StringComparison.OrdinalIgnoreCase))
                    {
                        line = "Debug::DumpClass(\"Debug\");";
                        Console.WriteLine(line);
                    }

                    ScriptResult result = engine.RunInteractiveScript(line, optVerbose);
                    if (result.success)
                    {
                        if (null == result.value)
                        {
                            Console.WriteLine("<null>");
                        }
                        else
                        {
                            Console.WriteLine(CoreLib.ValueToString(engine.defaultContext, result.value, true));
                        }
                    }
                }
            }

            // Dump memory...
            if (optDump)
            {
                Console.WriteLine();
                Console.WriteLine(engine.defaultContext);
            }

            // Pause before exiting...
            if (optPause)
            {
                Console.WriteLine("Press any key to exit.");
                Console.ReadKey();
            }
        }