Exemplo n.º 1
0
        private CompiledAssembly InternalBuild(string source, Document doc, IBuildLogger logger, params ExtendedOption[] options)
        {
            logger.WriteBuildInfo("Ela", ElaVersionInfo.Version);

            var bo   = new BuildOptionsManager(App);
            var lopt = bo.CreateLinkerOptions();
            var copt = bo.CreateCompilerOptions();

            if (options.Set(ForceRecompile.Code))
            {
                lopt.ForceRecompile = true;
            }

            if (!options.Set(NoDebug.Code))
            {
                copt.GenerateDebugInfo = true;
            }

            if (options.Set(NoWarnings.Code))
            {
                copt.NoWarnings = true;
            }

            logger.WriteBuildOptions("Compiler options: {0}", copt);
            logger.WriteBuildOptions("Linker options: {0}", lopt);
            logger.WriteBuildOptions("Module lookup directories:");
            lopt.CodeBase.Directories.ForEach(d => logger.WriteBuildOptions(d));

            var lnk = new ElaIncrementalLinker(lopt, copt, doc.FileInfo == null ? new ModuleFileInfo(doc.Title) : doc.FileInfo.ToModuleFileInfo());

            lnk.ModuleResolve += (o, e) =>
            {
                //TBD
            };

            var res      = lnk.Build(source);
            var messages = res.Messages.Take(100).ToList();

            logger.WriteMessages(messages.Select(m =>
                                                 new MessageItem(
                                                     m.Type == MessageType.Error ? MessageItemType.Error : (m.Type == MessageType.Warning ? MessageItemType.Warning : MessageItemType.Information),
                                                     String.Format("ELA{0}: {1}", m.Code, m.Message),
                                                     m.File == null || !new FileInfo(m.File.FullName).Exists ? doc : new VirtualDocument(new FileInfo(m.File.FullName)),
                                                     m.Line,
                                                     m.Column)
            {
                Tag = m.Code
            })
                                 , m => (Int32)m.Tag < 600); //Exclude linker messages
            return(res.Success ? new CompiledAssembly(doc, res.Assembly) : null);
        }
Exemplo n.º 2
0
        internal bool RunCode(string code, bool fastFail = false, bool onlyErrors = false)
        {
            if (link == null)
            {
                var bo = new BuildOptionsManager(App);
                link = new ElaIncrementalLinker(bo.CreateLinkerOptions(),
                                                bo.CreateCompilerOptions());
            }

            link.SetSource(code.Trim('\0'));
            var lr = link.Build();

            if (!lr.Success && fastFail)
            {
                return(false);
            }

            foreach (var m in lr.Messages)
            {
                if (m.Type == Ela.MessageType.Error || !onlyErrors)
                {
                    var tag = m.Type == Ela.MessageType.Error ? "``!!!" :
                              m.Type == Ela.MessageType.Warning ? "``|||" :
                              "``???";
                    Con.PrintLine(String.Format("{0}{1} ({2},{3}): {4} ELA{5}: {6}", tag, m.File.Name, m.Line, m.Column, m.Type, m.Code, m.Message));
                }
            }

            if (lr.Success)
            {
                var th = new Thread(() => ExecuteInput(lr.Assembly));
                Con.SetExecControl(th);
                App.GetService <IStatusBarService>().SetStatusString(StatusType.Information, "Executing code in Interactive...");
                th.Start();
                return(true);
            }
            else
            {
                Con.PrintLine();
                Con.Print(Con.Prompt + ">");
                Con.ScrollToCaret();
                return(false);
            }
        }
Exemplo n.º 3
0
        private static int InterpretFile(string fileName)
        {
            var res = default(LinkerResult);

            try
            {
                linker = new ElaIncrementalLinker(CreateLinkerOptions(), CreateCompilerOptions(),
                                                  new ModuleFileInfo(fileName));

                if (opt.Arguments.Count > 0)
                {
                    CompileArguments(linker);
                }

                res = linker.Build();
            }
            catch (ElaException ex)
            {
                helper.PrintInternalError(ex);
                return(R_ERR);
            }

            helper.PrintErrors(res.Messages);

            if (!res.Success)
            {
                return(R_ERR);
            }
            else
            {
                var ret = Execute(res.Assembly, false);

                if (ret == R_OK && opt.LunchInteractive)
                {
                    StartInteractiveMode();
                }

                return(ret);
            }
        }
Exemplo n.º 4
0
        private static int InterpretString(string source)
        {
            if (linker == null)
            {
                linker = new ElaIncrementalLinker(CreateLinkerOptions(), CreateCompilerOptions());

                if (opt.Arguments.Count > 0)
                {
                    CompileArguments(linker);
                }
            }

            linker.SetSource(source);
            var res = linker.Build();

            helper.PrintErrors(res.Messages);

            if (!res.Success)
            {
                if (res.Assembly != null)
                {
                    var r = res.Assembly.GetRootModule();

                    if (r != null)
                    {
                        lastOffset = r.Ops.Count;
                    }
                }

                return(R_ERR);
            }
            else
            {
                return(Execute(res.Assembly, true));
            }
        }
Exemplo n.º 5
0
        private void WalkLines(IEnumerable <DocLine> lines, StringBuilder toc, StringBuilder sb)
        {
            int tocId = 0;

            WalkLines(lines,
                      (str, type) =>
            {
                if (type == LineType.CodeItem)
                {
                    GenerateCodeItem(str, sb);
                }
                else if (type == LineType.Table)
                {
                    str      = HttpUtility.HtmlEncode(str);
                    var rows = str.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

                    sb.Append("<table class='textTable'>");

                    foreach (var r in rows)
                    {
                        var cells  = new List <String>();
                        var buffer = r.ToCharArray();
                        var pos    = 0;
                        var l      = '\0';

                        for (var i = 0; i < buffer.Length; i++)
                        {
                            var c    = buffer[i];
                            var last = i == buffer.Length - 1;

                            if ((l == ' ' && c == '|') || last)
                            {
                                cells.Add(new String(buffer, pos, i - pos + (last ? 1 : 0)));
                                pos = i + 1;
                            }

                            l = c;
                        }

                        sb.Append("<tr>");

                        foreach (var c in cells)
                        {
                            sb.AppendFormat("<td class='textTd'>{0}</td>",
                                            MarkupParser.Parse(c));
                        }

                        sb.Append("</tr>");
                    }

                    sb.Append("</table>");
                }
                else if (type == LineType.ElaCode || type == LineType.EvalCode)
                {
                    if (type == LineType.EvalCode)
                    {
                        lnk.SetSource(str);
                        var res = lnk.Build();

                        if (res.Success)
                        {
                            if (vm == null)
                            {
                                vm = new ElaMachine(res.Assembly);
                            }
                            else
                            {
                                vm.RefreshState();
                            }

                            try
                            {
                                var rv = vm.Resume();

                                if (!rv.ReturnValue.Is <ElaUnit>())
                                {
                                    var val = vm.PrintValue(rv.ReturnValue);

                                    if (val.Contains('\r') || val.Length > 30)
                                    {
                                        str += "\r\n/*\r\nThe result is:\r\n" + val + "\r\n*/";
                                    }
                                    else
                                    {
                                        str += " //The result is: " + val;
                                    }
                                }
                            }
                            catch (ElaCodeException ex)
                            {
                                Error(ex.Message);
                            }
                        }
                        else
                        {
                            res.Messages.ToList().ForEach(m => Console.Write("\r\n" + m.ToString()));
                        }
                    }

                    sb.AppendFormat(ELACODE, Lex(str));
                }
                else if (type == LineType.Code)
                {
                    str = HttpUtility.HtmlEncode(str);
                    sb.AppendFormat(CODE, str);
                }
                else if (type == LineType.Header1)
                {
                    str    = HttpUtility.HtmlEncode(str);
                    var id = (tocId++).ToString();
                    sb.AppendFormat("<a name=\"{0}\"></a>", id);
                    sb.AppendFormat(H1, str);
                    toc.Append("<b>");
                    toc.AppendFormat(A, id, str);
                    toc.Append("</b><br/>");
                }
                else if (type == LineType.Header2)
                {
                    str    = HttpUtility.HtmlEncode(str);
                    var id = (tocId++).ToString();
                    sb.AppendFormat("<a name=\"{0}\"></a>", id);
                    sb.AppendFormat(H2, str);
                    toc.Append("&nbsp;&nbsp;&nbsp;&nbsp;");
                    toc.AppendFormat(A, id, str);
                    toc.Append("<br/>");
                }
                else if (type == LineType.Header3)
                {
                    str = HttpUtility.HtmlEncode(str);
                    sb.AppendFormat(H3, str);
                }
                else if (type == LineType.List)
                {
                    str = HttpUtility.HtmlEncode(str);
                    sb.Append("<ul>");
                    var split = str.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                    split.ToList().ForEach(s => sb.AppendFormat(LI, MarkupParser.Parse(s)));
                    sb.Append("</ul>");
                }
                else
                {
                    str = HttpUtility.HtmlEncode(str);
                    sb.AppendFormat(TEXT, MarkupParser.Parse(str));
                }
            });
        }
Exemplo n.º 6
0
        static int Run(string file, string outputFile)
        {
            Console.Write("\r\nGenerating documentation file: {0}...", outputFile);

            var fi  = new FileInfo(file);
            var doc = default(Doc);

            using (var sr = new StreamReader(file))
            {
                var lst  = new List <String>();
                var line = String.Empty;

                while ((line = sr.ReadLine()) != null)
                {
                    lst.Add(line);
                }

                var p = new DocParser();
                doc = p.Parse(lst.ToArray());
            }

            var gen  = default(HtmGenerator);
            var lopt = new LinkerOptions();
            var copt = new CompilerOptions {
                Prelude = "prelude"
            };

            ConfigurationManager.AppSettings["refs"].Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
            .ToList().ForEach(s => lopt.CodeBase.Directories.Add(s));

            if (!String.IsNullOrEmpty(doc.File) && doc.File.Trim(' ').Length != 0)
            {
                var elaFile = new FileInfo(Path.Combine(fi.DirectoryName, doc.File));

                if (!elaFile.Exists)
                {
                    foreach (var d in lopt.CodeBase.Directories)
                    {
                        elaFile = new FileInfo(Path.Combine(d, doc.File));

                        if (elaFile.Exists)
                        {
                            break;
                        }
                    }
                }

                var elap = new ElaParser();
                var res  = elap.Parse(elaFile);

                if (!res.Success)
                {
                    res.Messages.ToList().ForEach(m => Console.WriteLine("\r\n" + m));
                    return(-1);
                }

                lopt.CodeBase.Directories.Add(elaFile.Directory.FullName);
                var lnk  = new ElaIncrementalLinker(lopt, copt, new ModuleFileInfo(elaFile.FullName));
                var lres = lnk.Build();

                if (!lres.Success)
                {
                    lres.Messages.ToList().ForEach(m => Console.Write("\r\n" + m));
                    return(-1);
                }

                var vm = new ElaMachine(lres.Assembly);
                vm.Run();
                gen = new HtmGenerator(res.Program, doc, lnk, vm);
            }
            else
            {
                var lnk = new ElaIncrementalLinker(lopt, copt);
                gen = new HtmGenerator(null, doc, lnk, null);
            }

            var src = gen.Generate();

            var outFi = new FileInfo(outputFile);

            if (!outFi.Directory.Exists)
            {
                outFi.Directory.Create();
            }

            using (var fs = new StreamWriter(File.Create(outputFile)))
                fs.Write(src);

            Console.Write(" Done");
            return(0);
        }
Exemplo n.º 7
0
        private static int InterpretFile(string fileName)
        {
            var res = default(LinkerResult);

            try
            {
                linker = new ElaIncrementalLinker(CreateLinkerOptions(), CreateCompilerOptions(),
                    new FileInfo(fileName));

                if (opt.Arguments.Count > 0)
                    CompileArguments(linker);

                res = linker.Build();
            }
            catch (ElaException ex)
            {
                helper.PrintInternalError(ex);
                return R_ERR;
            }

            helper.PrintErrors(res.Messages);

            if (!res.Success)
                return R_ERR;
            else
            {
                var ret = Execute(res.Assembly, false);

                if (ret == R_OK && opt.LunchInteractive)
                    StartInteractiveMode();

                return ret;
            }
        }