コード例 #1
0
        public override void ExitLogStatement([NotNull] SBP.LogStatementContext context)
        {
            var stack = m_expressionData.PopStackLevel();

            if (stack.Count == 0)
            {
                m_errors.InternalError(context.Start.Line, context.Start.Column, "");
                return;
            }
            var exp = stack.Pop();

            exp = this.ResolveIfIdentifier(exp, m_inFunctionScope);
            var code = exp.IsError() ? Expression.Constant("<EXPRESSION ERROR>") : exp.ExpressionCode;

            if (code != null)
            {
                var propertyBlock = m_lastElementPropertyBlock;

                var resultVar = Expression.Variable(code.Type, "logValue");
                var assignVar = Expression.Assign(resultVar, code);

                var loggingEnabled = Expression.Property(
                    Expression.Convert(m_currentProcedure.ContextReferenceInternal, typeof(ICallContext)),
                    typeof(ICallContext).GetProperty("LoggingEnabled"));

                var logValue = code;
                if (code.Type != typeof(string))
                {
                    if (code.Type.IsPrimitive || code.Type.IsEnum)
                    {
                        logValue = Expression.Call(resultVar, typeof(object).GetMethod("ToString", new Type[] { }));
                    }
                    else
                    {
                        if (typeof(IEnumerable <string>).IsAssignableFrom(code.Type))
                        {
                            #region Special handling of IEnumerable<string>

                            var helper = typeof(ExecutionHelperMethods).GetMethod(
                                nameof(ExecutionHelperMethods.LogList));

                            var helperCall = Expression.Call(helper,
                                                             m_currentProcedure?.ContextReferenceInternal,
                                                             logValue);

                            var logListStatementBlock = Expression.IfThen(
                                loggingEnabled,
                                Expression.Block(
                                    new ParameterExpression[] { resultVar },
                                    Expression.TryCatch(
                                        Expression.Block(
                                            assignVar,
                                            helperCall),
                                        Expression.Catch(
                                            typeof(Exception),
                                            Expression.Empty()))));

                            m_scopeStack.Peek().AddStatementCode(logListStatementBlock);

                            return;

                            #endregion
                        }
                        else
                        {
                            logValue = Expression.Condition(
                                Expression.Equal(resultVar, Expression.Constant(null)),
                                Expression.Constant("<null>"),
                                Expression.Call(typeof(string).GetMethod("Concat", new Type[] { typeof(string), typeof(string), typeof(string) }),
                                                Expression.Property(
                                                    Expression.Call(resultVar, typeof(object).GetMethod("GetType", new Type[] { })),
                                                    "FullName"),
                                                Expression.Constant(" - "),
                                                Expression.Call(resultVar, typeof(object).GetMethod("ToString", new Type[] { }))));
                        }
                    }
                }
                logValue = Expression.Call(
                    typeof(string).GetMethod("Concat", new Type[] { typeof(string), typeof(string) }),
                    Expression.Constant("log: "),
                    logValue);

                var loggingCall = Expression.Call(
                    m_currentProcedure.ContextReferenceInternal,
                    typeof(IScriptCallContext).GetMethod("Log", new Type[] { typeof(string) }),
                    logValue);

                var statementBlock = Expression.IfThen(
                    loggingEnabled,
                    Expression.Block(
                        new ParameterExpression[] { resultVar },
                        Expression.TryCatch(
                            Expression.Block(
                                assignVar,
                                loggingCall),
                            Expression.Catch(
                                typeof(Exception),
                                Expression.Empty()))));

                m_scopeStack.Peek().AddStatementCode(statementBlock);
            }
        }
コード例 #2
0
 public override void EnterLogStatement([NotNull] SBP.LogStatementContext context)
 {
     this.AddEnterStatement(context);
     m_expressionData.PushStackLevel("LogStatement");
     m_logStatementModifier = null;
 }