示例#1
0
文件: Commands.cs 项目: quwahara/Nana
        public static string Compile(LineEditMode lem)
        {
            Ctrl c = new Ctrl();
            StringBuilder b = new StringBuilder();
            Action<string> trace = delegate(string s) { b.Append(s); };
            c.StdOut = trace;
            c.StdErr = trace;

            c.StartCompile(new string[] { lem.DefaultSrcPath
                , "/xxxil"
                , "/xxxtrace"
            });

            return b.ToString();
        }
示例#2
0
文件: Ctrl.cs 项目: quwahara/Nana
        public int StartCompile(string[] args)
        {
            UTF8Encoding utf8 = new UTF8Encoding(false /* no byte order mark */);
            Action<string> prt = delegate(string s) { StdOut(s + Environment.NewLine); };
            string nl = Environment.NewLine;
            string ilpath;
            string code;
            Token root = null;
            try
            {
                root = CmdLnArgs.GetCmdLnArgs(args);
                root.Group = "Root";

                if (args.Length == 0 || root.Contains("CompileOptions/help"))
                {
                    foreach (string opt in CmdLnArgs.Options)
                    { StdErr(opt + nl); }
                    return 0;
                }

                if (root.Contains("CompileOptions/verbose"))
                {
                    prt("Specified options:");
                    foreach (Token t in root.Find("CompileOptions").Follows)
                    { prt(t.Group + (string.IsNullOrEmpty(t.Value) ? "" : ":" + t.Value)); }
                }

                Ctrl.Check(root);
                Ctrl c = new Ctrl();

                if (root.Contains("CompileOptions/xxxsyntax"))
                {
                    c.AfterSyntaxAnalyze = delegate(Token root_)
                    {
                        foreach (Token t in root_.Select("Syntax/@0Source"))
                        {
                            StdOut(TokenEx.ToTree(t));
                        }
                    };
                }

                c.Compile(root);

                if (root.Contains("CompileOptions/xxxil"))
                {
                    StdOut(root.Find("Code").Value);
                }

                ilpath = root.Find("CompileOptions/out").Value;
                ilpath = Path.ChangeExtension(ilpath, ".il");
                code = root.Find("Code").Value;
                File.WriteAllText(ilpath, code, utf8);

                ILASMRunner r = new ILASMRunner();
                r.DetectILASM();
                r.Run(ilpath);
                return 0;
            }
            catch (Exception e)
            {
                StdErr("Error:" + nl);
                StringBuilder b = new StringBuilder();
                if (e is SemanticError)
                {
                    SemanticError se = e as SemanticError;
                    b.Append(se.Path)
                        .Append(":").Append(se.Row).Append(",").Append(se.Col)
                        .Append(":");
                }
                b.Append(e.Message);

                if (e is FileNotFoundException)
                {
                    FileNotFoundException ffx = e as FileNotFoundException;
                    b.Append(", Filename: ").Append(ffx.FileName);
                }

                b.AppendLine();
                StdErr(b.ToString());
                if (null != root && root.Contains("CompileOptions/xxxtrace"))
                { StdErr(e.StackTrace); }
                return -1;
            }
        }