예제 #1
0
        public override RuntimeValueWrapper Execute(DoExpression e, RuntimeContext context)
        {
            RuntimeValueWrapper result     = null;
            RuntimeContext      newContext = new RuntimeContext()
            {
                PreviousContext = context
            };

            foreach (Expression expression in e.Expressions)
            {
                VarExpression var = expression as VarExpression;
                if (var == null)
                {
                    expression.BuildContext(newContext);
                    result = expression.Execute(newContext);
                }
                else
                {
                    result = new RuntimeValueWrapper(new RuntimeUnevaluatedValue(var.Expression), context);
                    if (!var.Pattern.Match(context, result))
                    {
                        throw new Exception("模式匹配不成功。");
                    }
                }
            }
            if (result == null)
            {
                return(RuntimeValueWrapper.CreateValue(new object()));
            }
            else
            {
                return(result);
            }
        }
예제 #2
0
        public override RuntimeValueWrapper Execute(DoExpression e, RuntimeContext context)
        {
            Func <RuntimeValueWrapper[], RuntimeValueWrapper> monadFunction = arguments =>
            {
                StatePackage result = new StatePackage()
                {
                    result = null,
                    state  = arguments[0]
                };
                RuntimeContext newContext = new RuntimeContext()
                {
                    PreviousContext = context
                };
                newContext.Values.Add("return", RuntimeValueWrapper.CreateFunction(ReturnStateMonadValue, 2));
                foreach (Expression expression in e.Expressions)
                {
                    VarExpression var = expression as VarExpression;
                    if (var == null)
                    {
                        expression.BuildContext(newContext);
                        result = RunStateMonad(new RuntimeValueWrapper(new RuntimeUnevaluatedValue(expression), newContext), result.state);
                    }
                    else
                    {
                        result = RunStateMonad(new RuntimeValueWrapper(new RuntimeUnevaluatedValue(var.Expression), newContext), result.state);
                        if (!var.Pattern.Match(newContext, result.result))
                        {
                            throw new Exception("模式匹配不成功。");
                        }
                    }
                    if (!(bool)continueFunction.Invoke(result.state).RuntimeObject)
                    {
                        break;
                    }
                }
                if (result.result == null)
                {
                    result.result = RuntimeValueWrapper.CreateValue(new object());
                }
                return(RuntimeValueWrapper.CreateValue(result));
            };

            return(RuntimeValueWrapper.CreateFunction(monadFunction, 1));
        }