Ejemplo n.º 1
0
            public static void InterpretingInConsole(this SSScope scope)
            {
                var w      = new Stopwatch();
                var file   = new CodeFile();
                var syntax = new SyntaxAnalyzer();

                while (true)
                {
                    file.ReSet();
                    syntax.ReSet();
                    try
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.Write(">>> ");
                        String code;
                        if ((code = Console.ReadLine()).NotEmpty())
                        {
                            file.SourceCode = code;
                            file.Parse();
                            if (file.ErrorList.NotEmpty())
                            {
                                file.PrintErrors(Console.Out);
                            }
                            syntax.Take(file.TokenList);
                            if (syntax.ErrorList.NotEmpty())
                            {
                                syntax.PrintErrors(Console.Out);
                            }

                            Console.ForegroundColor = ConsoleColor.Green;

                            w.Reset();
                            w.Start();
                            Console.WriteLine(">>> " + syntax.Expressions.First().Evaluate(scope));
                            w.Stop();
                            Console.WriteLine(w.ElapsedMilliseconds + "ms");
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine(ex.Message);
                        Console.WriteLine(ex.StackTrace);
                        // ex.StackTrace.Split("at".ToCharArray()).Take(3).ForEach(a => scope.Output.WriteLine(a));
                    }
                }
            }
Ejemplo n.º 2
0
            public static void BulkEval(this SSScope scope, string src)
            {
                if (!src.NotEmpty())
                {
                    return;
                }

                var file   = new CodeFile();
                var syntax = new SyntaxAnalyzer();

                file.SourceCode = src;
                file.Parse();
                if (file.ErrorList.Count > 0)
                {
                    scope.Output.WriteLine("Errors:");
                    syntax.ErrorList.ForEach(a => scope.Output.WriteLine(a.ToString()));
                    return;
                }
                syntax.Take(file.TokenList);
                if (syntax.ErrorList.NotEmpty())
                {
                    scope.Output.WriteLine("Errors:");
                    syntax.ErrorList.ForEach(a => scope.Output.WriteLine(a.ToString()));
                    return;
                }
                try
                {
                    // prevent lazy eval
                    foreach (var exp in syntax.Expressions)
                    {
                        exp.Evaluate(scope); //);
                    }
                }//
                catch (Exception ex)
                {
                    scope.Output.WriteLine("Failed to evaluate");
                    scope.Output.WriteLine(ex);
                    ex.StackTrace.Split('\r').Take(3).ForEach(a => scope.Output.WriteLine(a));
                }
                //    scope.VariableTable.ForEach(a => scope.Output.WriteLine(">>> Added {0} : {1} "
                //       .Fmt(a.Key, a.Value.GetType())));
            }