Exemple #1
0
        /// <summary>Native method representing the match method</summary>
        /// <param name="ctx">Current interpreter</param>
        /// <param name="req">Request that obtained this method</param>
        public GraceObject Match(EvaluationContext ctx, MethodRequest req)
        {
            MethodHelper.CheckArity(ctx, req, 1);
            var target = req[0].Arguments[0];

            if (Pattern == null)
            {
                var rrr = this.Apply(ctx,
                                     MethodRequest.Single("apply(_)", target));
                return(Matching.SuccessfulMatch(ctx, rrr));
            }
            var matchResult = Matching.Match(ctx, Pattern, target);

            if (!Matching.Succeeded(ctx, matchResult))
            {
                return(Matching.FailedMatch(ctx, target));
            }
            var result = matchResult.Request(ctx,
                                             MethodRequest.Nullary("result"));

            if (!explicitPattern)
            {
                var res = this.Apply(ctx,
                                     MethodRequest.Single("apply(_)", result));
                return(Matching.SuccessfulMatch(ctx, res));
            }
            var res2 = this.Apply(ctx,
                                  MethodRequest.Single("apply(_)", result));

            return(Matching.SuccessfulMatch(ctx, res2));
        }
Exemple #2
0
            /// <summary>Native method for Grace do</summary>
            /// <param name="ctx">Current interpreter</param>
            /// <param name="block">Block to apply for each element</param>
            public GraceObject Do(EvaluationContext ctx, GraceObject block)
            {
                var req = MethodRequest.Single("do", block);

                left.Request(ctx, req);
                right.Request(ctx, req);
                return(GraceObject.Done);
            }
Exemple #3
0
        /// <summary>Create a failed match</summary>
        /// <param name="ctx">Current interpreter</param>
        /// <param name="obj">Result value</param>
        public static GraceObject FailedMatch(EvaluationContext ctx,
                                              GraceObject obj)
        {
            var successfulMatchReq = MethodRequest.Single("_FailedMatch",
                                                          obj);
            GraceObject smRec = ctx.FindReceiver(successfulMatchReq);

            return(smRec.Request(ctx, successfulMatchReq));
        }
Exemple #4
0
        private GraceObject WithDo(EvaluationContext ctx, MethodRequest req)
        {
            var with      = req[0].Arguments[0];
            var block     = req[1].Arguments[0];
            var withBlock = new WithBlock(elements, block);
            var innerReq  = MethodRequest.Single("do", withBlock);

            with.Request(ctx, innerReq);
            return(GraceObject.Done);
        }
Exemple #5
0
        /// <summary>
        /// Execute a block of native code for each element
        /// of an iterable.
        /// </summary>
        /// <param name="ctx">Interpreter to use</param>
        /// <param name="iterable">Iterable to loop over</param>
        /// <param name="block">
        /// Block of code to execute.
        /// </param>
        public static void ForEach(
            EvaluationContext ctx,
            GraceObject iterable,
            GraceObject block
            )
        {
            var req = MethodRequest.Single("do", block);

            iterable.Request(ctx, req);
        }
Exemple #6
0
        /// <summary>Native method for Grace do</summary>
        /// <param name="ctx">Current interpreter</param>
        /// <param name="block">Block to apply for each element</param>
        public GraceObject Do(EvaluationContext ctx, GraceObject block)
        {
            var req = MethodRequest.Single("apply", null);

            foreach (var o in elements)
            {
                req[0].Arguments[0] = o;
                block.Request(ctx, req);
            }
            return(GraceObject.Done);
        }
        private static GraceObject mDo(
            EvaluationContext ctx,
            GraceString self,
            GraceObject blk
            )
        {
            var req = MethodRequest.Single("apply", null);

            for (var i = 0; i < self.graphemeIndices.Length; i++)
            {
                int    start = self.graphemeIndices[i];
                string c     = StringInfo.GetNextTextElement(self.Value, start);
                req[0].Arguments[0] = GraceString.Create(c);
                blk.Request(ctx, req);
            }
            return(GraceObject.Done);
        }
Exemple #8
0
        private GraceObject mDo(EvaluationContext ctx, GraceObject block)
        {
            var      apply = MethodRequest.Single("apply", null);
            Rational v     = _low;

            if (_step < 0)
            {
                while (v >= _high)
                {
                    apply[0].Arguments[0] = GraceNumber.Create(v);
                    block.Request(ctx, apply);
                    v += _step;
                }
                return(GraceObject.Done);
            }
            while (v <= _high)
            {
                apply[0].Arguments[0] = GraceNumber.Create(v);
                block.Request(ctx, apply);
                v += _step;
            }
            return(GraceObject.Done);
        }