Пример #1
0
        void Compile()
        {
            ClearLog();
            richTextBoxOutput.Clear();
            FinalExpr        = null;
            FinalBlock       = null;
            FinalExpressable = null;
            var enc = new UTF8Encoding(false);

            using (var sr = new StreamReader(new MemoryStream(enc.GetBytes(richTextBoxInput.Text))))
                using (var sb = new SyntaxBuilder(Cfe, sr, SelectedScope))
                {
                    while (sb.Step())
                    {
                        ;
                    }

                    foreach (var msg in sb.Warns)
                    {
                        Log(msg.Msg, Color.DarkOrange, msg.Location);
                    }
                    foreach (var msg in sb.Errors)
                    {
                        Log(msg.Msg, Color.LightPink, msg.Location);
                    }

                    if (!sb.Success)
                    {
                        Log("Compiling failed!", Color.Pink);
                        return;
                    }
                    else
                    {
                        Log("Compile Successful!", Color.LightSeaGreen);
                        if (SelectedScope == ScopeMode.Expression)
                        {
                            FinalExpr = sb.Syn.CreateFinalExpression();
                        }
                        else
                        if (SelectedScope == ScopeMode.Function)
                        {
                            FinalBlock = sb.Syn.CreateFinalFunction().RootStatement;
                        }
                        FinalExpressable       = sb.GetExpressable();
                        richTextBoxOutput.Text = FinalExpressable.Express(0);
                    }
                }
        }
Пример #2
0
        void Pack()
        {
            ClearLog();
            richTextBoxOutput.Clear();
            FinalExpr  = null;
            FinalBlock = null;
            var enc = new UTF8Encoding(false);

            using (var sr = new StreamReader(new MemoryStream(enc.GetBytes(richTextBoxInput.Text))))
                using (var sb = new SyntaxBuilder(Cfe, sr, SelectedScope))
                {
                    while (sb.Step())
                    {
                        if (sb.Stage == SyntaxBuilder.Stages.Syntaxing)
                        {
                            break;
                        }
                    }
                    foreach (var msg in sb.Warns)
                    {
                        Log(msg.Msg, Color.DarkOrange, msg.Location);
                    }
                    foreach (var msg in sb.Errors)
                    {
                        Log(msg.Msg, Color.LightPink, msg.Location);
                    }

                    if (sb.Stage != SyntaxBuilder.Stages.Syntaxing)
                    {
                        Log("Tokenizing failed!", Color.Pink);
                        return;
                    }
                    else
                    {
                        using (var ms = new MemoryStream())
                        {
                            using (var sw = new StreamWriter(ms))
                                Packer.PackToMinimumWhitespace(sb.Tokens.ToArray(), sw);
                            richTextBoxOutput.Text = enc.GetString(ms.ToArray());
                        }
                        Log("Packing Completed!", Color.LightSeaGreen);
                    }
                }
        }
Пример #3
0
        static Boolean ProcessFile(CompilerFrontEnd cfe, FileInfo file, int total, int current, Boolean basic = true)
        {
            SyntaxBuilder sb        = null;
            Boolean       OscarKilo = false;
            ConsoleKey    ck;

            while (!OscarKilo)
            {
                sb = new SyntaxBuilder(cfe, file.FullName);
                WriteLine(file.Name);
                SyntaxBuilder.Stages last = (SyntaxBuilder.Stages)(-1);
                while (sb.Step())
                {
                    if (last != sb.Stage)
                    {
                        Write(ConCol.Info, "\r({0}/{1}): {2}", current + 1, total, (last = sb.Stage).ToString());
                    }
                }
                WriteLine();


                if (!sb.OscarKilo)
                {
                    if (!sb.Success)
                    {
                        WriteLine(ConCol.Error, "Unable to process [{0}]:", file.Name);
                    }
                    if (sb.Warns.Count > 0)
                    {
                        WriteLine(ConCol.Warning, "Warnings:");
                        foreach (var m in sb.Warns.AsIndexable())
                        {
                            WriteLine(ConCol.Warning, "{0}) {1}", m.Index + 1, m.Value);
                        }
                        WriteLine();
                    }
                    if (sb.Errors.Count > 0)
                    {
                        WriteLine(ConCol.Error, "Errors:");
                        foreach (var m in sb.Errors.AsIndexable())
                        {
                            WriteLine(ConCol.Error, "{0}) {1}", m.Index + 1, m.Value);
                        }
                        if (sb.Errors.Count > 0)
                        {
                            ExecuteOnError(file.FullName, sb.Errors[0].Location.Value);
                        }
                    }
                    WriteLine("Press anything...");
                    Console.ReadKey(true);

                    ck = CharQuestion("Do you want to (c)ontinue or (t)ry again?", ConsoleKey.C, ConsoleKey.T, ConsoleKey.F);
                    if (ck == ConsoleKey.C)
                    {
                        break;
                    }
                }
                else
                {
                    OscarKilo = true;
                }
            }

            if (sb.Success)
            {
                cfe.UserDefinedDOM = sb.GetFinalDOM(ValidationBuilder.GetExtlessName(file.Name));
            }

            if (sb.Success && !basic)
            {
                String targetName   = paramCopyName == null ? file.Name : paramCopyName;
                String finalTarget  = Path.Combine(paramOutputFolder, targetName);
                String targetPacked = "{0}.packed{1}".Fmt(ValidationBuilder.GetExtlessName(targetName),
                                                          (new FileInfo(targetName)).Extension), finalTargetPacked = null;


                if (paramCopyName != null || paramCrossCheck)
                {
                    CheckOutputFolder();
                    WriteLine(ConCol.Info, "Outputing generated source.");
                    using (var sw = new StreamWriter(finalTarget))
                        sw.Write(cfe.UserDefinedDOM.Express(0));
                    WriteLine(ConCol.Info, "Emitted generated file [{0}].".Fmt(targetName));
                }

                if (paramEmitPacked)
                {
                    finalTargetPacked = Path.Combine(paramOutputFolder, targetPacked);
                    try
                    {
                        using (StreamWriter sw = new StreamWriter(finalTargetPacked))
                            sb.SavePacked(sw);
                        WriteLine(ConCol.Success, "Emitted packed source [{0}].", finalTargetPacked);
                    }
                    catch (Exception e)
                    {
                        WriteLine(ConCol.Error, "Unable to emit packed source of [{0}]: {1}", targetPacked, e.Message);
                        e.BreakException();
                        return(false);
                    }
                }

                if (paramCrossCheck)
                {
                    try
                    {
                        CodeDOMModule A, B;
                        CheckOutputFolder();
                        A = cfe.UserDefinedDOM;

                        if (!ProcessFile(cfe, new FileInfo(finalTarget), total, current))
                        {
                            WriteLine(ConCol.Error, "Cross check failed! Please contact Chaz pronto!");
                            throw new Exception("Cross check failed. Compiler isn't up to standard.");
                        }

                        B = cfe.UserDefinedDOM;

                        var d = CodeDOMComparer.CompareDOM(A, B);

                        if (d.Count == 0)
                        {
                            WriteLine(ConCol.Success, "Crosscheck Original To Copy Success!");
                        }
                        else
                        {
                            WriteLine(ConCol.Warning, "Crosscheck returned differences:");
                            foreach (var item in d.AsIndexable())
                            {
                                WriteLine(ConCol.Warning, "{0}> {1}", item.Index + 1, item.Value.ToString());
                            }
                            throw new Exception("Cross check failed. Compiler isn't up to standard.");
                        }

                        if (paramEmitPacked)
                        {
                            WriteLine(ConCol.Info, "Now cross checking with packed [{0}].", finalTargetPacked);
                            if (!ProcessFile(cfe, new FileInfo(finalTargetPacked), total, current))
                            {
                                WriteLine(ConCol.Error, "Packed cross check failed! Please contact Chaz pronto!");
                                throw new Exception("Packed cross check failed. Compiler isn't up to standard.");
                            }

                            B = cfe.UserDefinedDOM;

                            d = CodeDOMComparer.CompareDOM(A, B);

                            if (d.Count == 0)
                            {
                                WriteLine(ConCol.Success, "Crosscheck Original To Packed Success!");
                            }
                            else
                            {
                                WriteLine(ConCol.Warning, "Packed crosscheck returned differences:");
                                foreach (var item in d.AsIndexable())
                                {
                                    WriteLine(ConCol.Warning, "{0}> {1}", item.Index + 1, item.Value.ToString());
                                }
                                throw new Exception("Packed Cross check failed. Compiler isn't up to standard.");
                            }
                        }

                        cfe.UserDefinedDOM = A;
                    }
                    catch (Exception e)
                    {
                        WriteLine(ConCol.Error, "Crosscheck of file [{0}] failed: {1}".Fmt(targetName, e.Message));
                        e.BreakException();
                        return(false);
                    }
                }
            }
            return(sb.Success);
        }