예제 #1
0
        static void Main(string[] args)
        {
            SetOptionsFromCmdLine(args);

            if (!Options.Compiling)
            {
                var shiro = new Interpreter();
                RegisterAutos(shiro);
                Interpreter.Output = s =>
                {
                    Console.Write(s);
                };

                if (!Options.Interpreting)
                {
                    //Welcome to the REPL
                    if (KeepREPLing)
                    {
                        Console.WriteLine("    shiro is running in interactive, REPL mode.  If you want to do other things try 'shiro -help'");
                        Console.WriteLine("    Shiro interpreter version: " + Interpreter.Version);
                        Console.WriteLine("        (double-tap Enter to run buffered code)");
                        Console.WriteLine("        Built-In Functions:  cls, input, exit");
                        Console.WriteLine();
                    }

                    var code = "";
                    while (KeepREPLing)
                    {
                        if (string.IsNullOrEmpty(code))
                        {
                            Console.Write(":>  ");
                        }
                        else
                        {
                            Console.Write("    ");
                        }

                        var line = Console.ReadLine();
                        if (line != "")
                        {
                            code += line + Environment.NewLine;
                        }
                        else
                        {
                            try
                            {
                                var ret = shiro.Eval(code);
                                if (Options.ShowResult)
                                {
                                    Console.WriteLine("[result] " + ret.ToString());
                                }
                                Console.WriteLine();
                            }
                            catch (ApplicationException aex)
                            {
                                Console.WriteLine("[error] " + aex.Message);
                                Console.WriteLine();
                            }
                            finally
                            {
                                code = "";
                            }
                        }
                    }
                }
                else
                {
                    //Interpret files
                    var modules = new Dictionary <string, string>();
                    Interpreter.LoadModule = (m, s) => {
                        if (modules.ContainsKey(s.ToLower()))
                        {
                            shiro.Eval(modules[s.ToLower()], false);
                            return(true);
                        }

                        return(Interpreter.DefaultModuleLoader(m, s));
                    };

                    foreach (var f in Options.Files)
                    {
                        var name = f.Split('.')[0].ToLower();
                        var code = File.ReadAllText(f);

                        modules.Add(name, code);
                    }

                    if (!modules.ContainsKey(Options.EntryModule.ToLower()))
                    {
                        Console.WriteLine("Entry point " + Options.EntryModule + " was not found.  Nothing to execute.");
                        return;
                    }
                    try
                    {
                        var res = shiro.Eval(modules[Options.EntryModule.ToLower()]);
                        if (Options.ShowResult)
                        {
                            Console.WriteLine("[result] " + res.ToString());
                        }
                    }
                    catch (ApplicationException aex)
                    {
                        Console.WriteLine("[error] " + aex.Message);
                        Console.WriteLine();
                    }
                }
            }
            else
            {
                //Compile time
                var  c       = new Compiler(Options.EntryModule);
                bool hadMain = false;
                foreach (var f in Options.Files)
                {
                    var name = f.Split('.')[0];
                    c.AddShiroModule(name, File.ReadAllText(f));

                    if (name == Options.EntryModule)
                    {
                        hadMain = true;
                    }
                }

                if (!hadMain)
                {
                    Console.WriteLine("Expected entry point for compiled application (" + Options.EntryModule + ") wasn't found.");
                }
                else
                {
                    if (Options.EXEName == null)
                    {
                        Options.EXEName = Options.EntryModule + ".exe";
                    }

                    var path = Directory.GetCurrentDirectory();

                    AssemblyName[] a = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
                    foreach (AssemblyName an in a)
                    {
                        if (an.FullName.ToLower().Contains("shiro"))
                        {
                            if (File.Exists(path + "\\Shiro.Lang.dll"))
                            {
                                File.Delete(path + "\\Shiro.Lang.dll");
                            }

                            var shiroPath = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName;
                            File.Copy(shiroPath + "\\Shiro.Lang.dll", path + "\\Shiro.Lang.dll");
                        }
                    }

                    System.CodeDom.Compiler.CompilerError ce;
                    c.Compile(Options.EXEName, path, out ce);

                    if (ce == null)
                    {
                        Console.WriteLine("Compile success");
                    }
                    else
                    {
                        Console.WriteLine("Compile failed: " + ce.ErrorText + "; press enter to continue");
                        Console.ReadLine();
                    }
                }
            }
        }
예제 #2
0
        private void compileMenu_Click(object sender, EventArgs e)
        {
            //Tear down and setup the bin directory
            if (!IsProjectOpen)
            {
                if (editorTabs.SelectedTab.Text.TrimStart('*').Trim().StartsWith("new"))
                {
                    MessageBox.Show("Please save this file before trying to compile it");
                    return;
                }
                else
                {
                    Directory.SetCurrentDirectory(Path.GetDirectoryName(DocumentManager.GetFileName(editorTabs.SelectedTab.Text)));
                }
            }

            var path = Directory.GetCurrentDirectory() + "\\bin";

            try
            {
                if (Directory.Exists(path))
                {
                    Directory.Delete(path, true);
                }
                Directory.CreateDirectory(path);
            }
            catch (Exception)
            {
                MessageBox.Show("Compilation failed -- it's probable that the bin directory or something in it is locked");
                return;
            }

            AssemblyName[] a = Assembly.GetExecutingAssembly().GetReferencedAssemblies();
            foreach (AssemblyName an in a)
            {
                if (an.FullName.ToLower().Contains("shiro"))
                {
                    if (File.Exists(path + "\\Shiro.Lang.dll"))
                    {
                        File.Delete(path + "\\Shiro.Lang.dll");
                    }

                    var shiroPath = new FileInfo(Assembly.GetExecutingAssembly().Location).DirectoryName;
                    File.Copy(shiroPath + "\\Shiro.Lang.dll", path + "\\Shiro.Lang.dll");
                }
            }

            foreach (var file in Directory.GetFiles(Directory.GetCurrentDirectory()))
            {
                if (file.EndsWith(".dll"))
                {
                    File.Copy(file, file.Replace(Directory.GetCurrentDirectory(), path));
                }
            }

            //Now do the compile
            System.CodeDom.Compiler.CompilerError ce = null;
            if (!IsProjectOpen)
            {
                //Single file compile, nice and easy
                var c = new Compiler(editorTabs.SelectedTab.Text.TrimStart('*').Trim());
                c.AddShiroModule(editorTabs.SelectedTab.Text.TrimStart('*').Trim(), editor.Text);
                c.Compile(editorTabs.SelectedTab.Text.Split('.')[0] + ".exe", path, out ce);
            }
            else
            {
                //Project compile.  Slightly trickier
                var pt = ProjectTree;
            }

            if (ce == null)
            {
                SafeWrite("Compile success");
            }
            else
            {
                SafeWrite("Compile failed: " + ce.ErrorText);
            }
        }