示例#1
0
        public object Evaluate(int line, Context context, object[] args)
        {
            object result = null;

            if (args.Length != m_argNames.Length)
                throw new EvaluateException(line, "{0} method takes {1}, not {2}.", Name, DoGetArgsStr(m_argNames.Length), DoGetArgsStr(args.Length));

            int depth = context.PushLocals();
            if (depth > 256)
                throw new EvaluateException(line, "Method calls have recursed more than 256 times");

            for (int i = 0; i < args.Length; ++i)
                context.AddLocal(m_argNames[i], args[i]);

            foreach (Statement statement in m_statements)
            {
                try
                {
                    result = statement.Evaluate(context);
                }
                catch (ReturnException e)
                {
                    result = e.Result;
                    break;
                }
            }

            context.PopLocals();

            return result;
        }
示例#2
0
        public RefactorCommand[] Evaluate(Context context)
        {
            ScriptType.Instance.RegisterCustomMethods(context, Methods);

            Context = context;
            try
            {
                context.TracingEnabled = false;
                context.MethodDepth = 0;
                m_saved.Clear();
                Method method = Methods.FirstOrDefault(m => m.Name == "get_EnableTracing");
                if (method != null)
                {
                    object value = method.Evaluate(0, context, new object[0]);
                    context.TracingEnabled = Equals(value, true);
                }

                method = Methods.First(m => m.Name == "Run");	// parser will ensure this exists
                object result = method.Evaluate(0, context, new object[0]);

                if (result != null && !typeof(RefactorCommand).IsAssignableFrom(result.GetType()))
                    throw new EvaluateException(1, "Run should return null or an Edit, not {0}.", RefactorType.GetName(result.GetType()));
            }
            finally
            {
                Context = null;
            }

            return context.Commands;
        }
示例#3
0
        public static object Call(Context context, int line, object instance, string method, object[] args)
        {
            RefactorType type;
            if (instance == null)
                type = VoidType.Instance;

            else if (instance.GetType().IsArray)
                type = SequenceType.Instance;

            else if (!ms_types.TryGetValue(instance.GetType(), out type))
                throw new EvaluateException(line, "{0} is not a valid refactor type.", instance.GetType());

            if (context.TracingEnabled)
                Console.WriteLine("{0} {1}.{2}({3})", new string(' ', 4*context.MethodDepth), type.Name, method, DoStringifyArgs(args));
            context.MethodDepth += 1;
            if (context.MethodDepth > 256)
                throw new EvaluateException(line, "Method calls have recursed more than 256 times");

            object result = type.Execute(line, instance, method, args);

            context.MethodDepth -= 1;
            if (context.TracingEnabled)
                Console.WriteLine("{0} => {1}", new string(' ', 4*context.MethodDepth), result.QuotedStringify());

            return result;
        }
示例#4
0
 public abstract object Evaluate(Context context);
示例#5
0
        public override object Evaluate(Context context)
        {
            Log.WriteLine("Refactor Evaluate", "{0}: Return", Line);

            throw new ReturnException(m_expr.Evaluate(context));
        }
示例#6
0
        public void RegisterCustomMethods(Context context, Method[] methods)
        {
            RegisterAllMethods();

            foreach (Method method in methods)
            {
                Register(context, method);
            }
        }
示例#7
0
        public override object Evaluate(Context context)
        {
            Log.WriteLine("Refactor Evaluate", "{0}: MethodCall", Line);

            object result = m_invoke.Evaluate(context);

            RefactorCommand command = result as RefactorCommand;
            if (command != null)
                context.AddCommand(command);

            return result;
        }
示例#8
0
        public override object Evaluate(Context context)
        {
            Log.WriteLine("Refactor Evaluate", "{0}: Let", Line);

            for (int i = 0; i < m_locals.Length; ++i)
            {
                object value = m_values[i].Evaluate(context);
                context.AddLocal(m_locals[i], value);
            }

            object result = null;
            foreach (Statement statement in m_block)
            {
                result = statement.Evaluate(context);
            }

            for (int i = 0; i < m_locals.Length; ++i)
            {
                context.RemoveLocal(m_locals[i]);
            }

            return result;
        }
示例#9
0
        private bool DoIsValidElement(Context context)
        {
            bool valid = true;

            if (m_filter != null)
            {
                object predicate = m_filter.Evaluate(context);

                if (Equals(predicate, false))
                {
                    valid = false;
                }
                else if (!Equals(predicate, true))
                {
                    throw new EvaluateException(m_filter.Line, "Where clause should be a Boolean, but is {0}.", predicate != null ? RefactorType.GetName(predicate.GetType()) : "null");
                }
            }

            return valid;
        }
示例#10
0
        public override object Evaluate(Context context)
        {
            Log.WriteLine("Refactor Evaluate", "{0}: For", Line);

            object result = m_elements.Evaluate(context);

            object[] elements = (object[]) result;
            if (elements == null && result != null)
                throw new EvaluateException(m_elements.Line, "For statement should return a Sequence, but was a {0}.", RefactorType.GetName(result.GetType()));

            result = null;
            if (elements != null)
            {
                foreach (object element in elements)
                {
                    context.AddLocal(m_local, element);

                    if (DoIsValidElement(context))
                    {
                        foreach (Statement statement in m_block)
                        {
                            result = statement.Evaluate(context);
                        }
                    }

                    context.RemoveLocal(m_local);
                }
            }

            return result;
        }
示例#11
0
        public override object Evaluate(Context context)
        {
            Log.WriteLine("Refactor Evaluate", "{0}: Conditional", Line);

            object result = null;
            for (int i = 0; i < m_predicates.Length; ++i)
            {
                object predicate = m_predicates[i].Evaluate(context);
                if (Equals(predicate, true))
                {
                    foreach (Statement statement in m_blocks[i])
                    {
                        result = statement.Evaluate(context);
                    }
                    break;
                }
                else if (!Equals(predicate, false))
                {
                    throw new EvaluateException(m_predicates[i].Line, "Predicate should be a Boolean, but is {0}.", predicate != null ? RefactorType.GetName(predicate.GetType()) : "null");
                }
            }

            return result;
        }