Beispiel #1
0
        private List <AphidExpression> ParseRetExpression(string code)
        {
            var(formattedCode, allTokens) = FormatExpression(code);

            List <AphidExpression> ast = null;

            try
            {
                ast = AphidParser.Parse(allTokens, formattedCode);

                if (ast.Count != 1)
                {
                    throw new AphidParserException("Unexpected expression", ast[1]);
                }
            }
            catch (AphidParserException ex)
            {
                CodeViewer.AppendParserException(formattedCode, ex);

                return(null);
            }
            catch (Exception ex)
            {
                CodeViewer.AppendException(formattedCode, "Internal parser exception (please report)", ex, Interpreter);

                return(null);
            }

            var exp = ast[0];

            if (!(exp is UnaryOperatorExpression unary) ||
                unary.Operator != AphidTokenType.retKeyword)
            {
                ast.Clear();
                ast.Add(new UnaryOperatorExpression(AphidTokenType.retKeyword, exp)
                        .WithPositionFrom(exp));
            }

            return(ast);
        }
Beispiel #2
0
        private async void ExecuteStatements(string code)
        {
            ResetInterpreter();
            await CodeViewer.Document.BeginInvoke(CodeViewer.Document.Blocks.Clear);

            CodeViewer.AppendOutput("Running script...");

            if (string.IsNullOrEmpty(code))
            {
                return;
            }

            List <AphidExpression> ast = null;

            try
            {
                ast = AphidParser.Parse(code);
            }
            catch (AphidParserException ex)
            {
                CodeViewer.AppendParserException(code, ex);

                return;
            }
            catch (Exception ex)
            {
                CodeViewer.AppendException(code, "Internal parser exception (please report)", ex, Interpreter);

                return;
            }

            try
            {
                try
                {
                    Interpreter.ResetState();
                    Interpreter.TakeOwnership();

#if APHID_FRAME_ADD_DATA || APHID_FRAME_CATCH_POP
                    try
                    {
#endif
                    Interpreter.Interpret(ast);
#if APHID_FRAME_ADD_DATA || APHID_FRAME_CATCH_POP
                }
#endif
#if APHID_FRAME_ADD_DATA || APHID_FRAME_CATCH_POP
#if APHID_FRAME_ADD_DATA
                    catch (Exception e)
#else
                    catch
#endif
                    {
                        if (e.Source != AphidName.DebugInterpreter)
                        {
                            e.Source = AphidName.DebugInterpreter;

#if APHID_FRAME_CATCH_POP
                            Interpreter.PopQueuedFrames();
#endif

#if APHID_FRAME_ADD_DATA
                            e.Data.Add(AphidName.Interpreter, Interpreter);
                            e.Data.Add(AphidName.FramesKey, Interpreter.GetRawStackTrace());
#endif
                        }

                        throw;
                    }
#endif
                }
                catch (AphidRuntimeException ex)
                {
                    CodeViewer.AppendRuntimeException(code, ex, Interpreter);

                    return;
                }
                catch (AphidParserException ex)
                {
                    CodeViewer.AppendParserException(code, ex);

                    return;
                }
                catch (Exception ex)
                {
                    CodeViewer.AppendException(code, ".NET runtime error", ex, Interpreter);

                    return;
                }
            }
            finally
            {
                UpdateVariables();
            }

            await ExecuteWatchExpressionsAsync();
            await WaitDumpTasksAsync();

            CodeViewer.AppendOutput("Done");
        }
Beispiel #3
0
        private async void ExecuteExpression(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                return;
            }

            AddHistoricalCode();

            var ast = ParseRetExpression(code);

            if (ast == null)
            {
                return;
            }

            try
            {
                try
                {
                    Interpreter.ResetState();
                    Interpreter.TakeOwnership();

#if APHID_FRAME_ADD_DATA || APHID_FRAME_CATCH_POP
                    try
                    {
#endif
                    Interpreter.Interpret(ast);
#if APHID_FRAME_ADD_DATA || APHID_FRAME_CATCH_POP
                }
#endif
#if APHID_FRAME_ADD_DATA || APHID_FRAME_CATCH_POP
#if APHID_FRAME_ADD_DATA
                    catch (Exception e)
#else
                    catch
#endif
                    {
                        if (e.Source != AphidName.DebugInterpreter)
                        {
                            e.Source = AphidName.DebugInterpreter;

#if APHID_FRAME_CATCH_POP
                            Interpreter.PopQueuedFrames();
#endif

#if APHID_FRAME_ADD_DATA
                            e.Data.Add(AphidName.Interpreter, Interpreter);
                            e.Data.Add(AphidName.FramesKey, Interpreter.GetRawStackTrace());
#endif
                        }

                        throw;
                    }
#endif
                }
                catch (AphidRuntimeException ex)
                {
                    CodeViewer.AppendException(code, "Runtime error", ex, Interpreter);

                    return;
                }
                catch (AphidParserException ex)
                {
                    CodeViewer.AppendParserException(code, ex);

                    return;
                }
                catch (Exception ex)
                {
                    CodeViewer.AppendException(code, ".NET Runtime error", ex, Interpreter);

                    return;
                }

                var serialized = SerializingFormatter
                                 .CreateSerializer(Interpreter)
                                 .Serialize(Interpreter.GetReturnValue());

                await WaitDumpTasksAsync();

                await CodeViewer.Async(() =>
                                       CodeViewer.AppendColoredOutput(
                                           Highlight(code),
                                           Highlight(serialized)));
            }
            finally
            {
                UpdateVariables();
                await ExecuteWatchExpressionsAsync();
            }
        }
Beispiel #4
0
        private void ExecuteExpression()
        {
            if (string.IsNullOrEmpty(Code))
            {
                return;
            }

            AddHistoricalCode();

            var tokens = new AphidLexer(Code + " ").GetTokens();

            if (!tokens.Any(x => x.TokenType == AphidTokenType.EndOfStatement))
            {
                tokens.Add(new AphidToken(AphidTokenType.EndOfStatement, "", 0));
            }

            List <Components.Aphid.Parser.Expression> ast = null;

            try
            {
                ast = new AphidParser(tokens).Parse();
            }
            catch (AphidParserException ex)
            {
                InvokeDispatcher(() => _codeViewer.AppendParserException(Code, ex));

                return;
            }

            if (ast.Count != 1)
            {
                throw new InvalidOperationException();
            }

            var exp   = ast.First();
            var unary = exp as UnaryOperatorExpression;

            if (unary == null ||
                unary.Operator != AphidTokenType.retKeyword)
            {
                ast.Clear();
                ast.Add(new UnaryOperatorExpression(
                            AphidTokenType.retKeyword,
                            exp));
            }



            try
            {
                _interpreter.Interpret(ast);
            }
            catch (AphidRuntimeException ex)
            {
                InvokeDispatcher(() =>
                                 _codeViewer.AppendException(Code, "Runtime error", ex));

                return;
            }
            catch (AphidParserException ex)
            {
                InvokeDispatcher(() =>
                                 _codeViewer.AppendParserException(Code, ex));

                return;
            }

            var retVal = _interpreter.GetReturnValue();

            var serializer = new AphidSerializer()
            {
                IgnoreFunctions = false
            };

            InvokeDispatcher(() =>
                             _codeViewer.AppendOutput(Code, serializer.Serialize(retVal)));

            UpdateVariables();
            ExecuteWatchExpressions();
        }