예제 #1
0
        /// <summary>
        /// Determine whether a Grace object is truthy or not.
        /// </summary>
        /// <param name="ctx">Current interpreter</param>
        /// <param name="val">Grace object to test for truthiness</param>
        /// <remarks>
        /// If the value is the true or false boolean literal, this
        /// method returns the appropriate value immediately. If it is
        /// an object conforming to the boolean type, this method requests
        /// ifTrue ifFalse on the object with suitable blocks and returns
        /// the result. If it is neither, an error will be reported.
        /// </remarks>
        public static bool IsTrue(EvaluationContext ctx, GraceObject val)
        {
            if (val == True)
            {
                return(true);
            }
            if (val == False)
            {
                return(false);
            }
            var req = new MethodRequest();

            req.AddPart(new RequestPart("ifTrue",
                                        new List <GraceObject> {
                _trueBlock
            },
                                        RequestPart.EmptyList
                                        )
                        );
            req.AddPart(new RequestPart("ifFalse",
                                        new List <GraceObject> {
                _falseBlock
            },
                                        RequestPart.EmptyList
                                        )
                        );
            return(val.Request(ctx, req) == True);
        }
예제 #2
0
        /// <summary>Create a method request with a single part and
        /// a lone ordinary argument</summary>
        /// <param name="name">Name of method</param>
        /// <param name="arg">Value of lone argument</param>
        public static MethodRequest Single(string name, GraceObject arg)
        {
            var ret = new MethodRequest();

            ret.AddPart(RequestPart.Single(name, arg));
            return(ret);
        }
예제 #3
0
        /// <summary>Create a method request with a single part and
        /// no arguments of any kind</summary>
        /// <param name="name">Name of method</param>
        public static MethodRequest Nullary(string name)
        {
            var ret = new MethodRequest();

            ret.AddPart(RequestPart.Nullary(name));
            return(ret);
        }
예제 #4
0
 /// <summary>Native method implementing the .match Grace method
 /// for types</summary>
 /// <remarks>At present, this matching uses only the method
 /// names in both the object and the type.</remarks>
 public GraceObject Match(EvaluationContext ctx, GraceObject target)
 {
     if (requests == null)
     {
         var l = new List <MethodRequest>();
         foreach (var m in methods)
         {
             var req = new MethodRequest();
             foreach (var p in m)
             {
                 var rp = RequestPart.Nullary(p.Name);
                 req.AddPart(rp);
             }
             l.Add(req);
         }
         requests = l;
     }
     foreach (var req in requests)
     {
         if (!target.RespondsTo(req))
         {
             return(Matching.FailedMatch(ctx, target));
         }
     }
     return(Matching.SuccessfulMatch(ctx, target));
 }
예제 #5
0
        /// <summary>Create a method request with a single part and
        /// a list of arguments</summary>
        /// <param name="name">Name of method</param>
        /// <param name="args">List of arguments</param>
        public static MethodRequest WithArgs(string name,
                                             IList <GraceObject> args)
        {
            var ret = new MethodRequest();
            var rp  = new RequestPart(name, RequestPart.EmptyList,
                                      args);

            ret.AddPart(rp);
            return(ret);
        }
예제 #6
0
        /// <summary>Native method for the | combinator</summary>
        /// <param name="ctx">Current interpreter</param>
        /// <param name="self">Left-hand side</param>
        /// <param name="other">Right-hand side</param>
        public static GraceObject OrCombinator(EvaluationContext ctx,
                                               GraceObject self, GraceObject other)
        {
            var patReq = new MethodRequest();

            patReq.AddPart(new RequestPart("_OrPattern",
                                           RequestPart.EmptyList,
                                           new List <GraceObject> {
                self, other
            }));
            GraceObject patRec = ctx.FindReceiver(patReq);

            return(patRec.Request(ctx, patReq));
        }
예제 #7
0
        /// <summary>Match a pattern against a target</summary>
        /// <param name="ctx">Current interpreter</param>
        /// <param name="pattern">Pattern to match</param>
        /// <param name="target">Target of the match</param>
        public static GraceObject Match(EvaluationContext ctx,
                                        GraceObject pattern,
                                        GraceObject target)
        {
            var matchReq = new MethodRequest();
            var rp       = new RequestPart("match", new List <GraceObject>(),
                                           new List <GraceObject>()
            {
                target
            });

            matchReq.AddPart(rp);
            return(pattern.Request(ctx, matchReq));
        }
예제 #8
0
            private GraceObject apply(EvaluationContext ctx,
                                      GraceObject arg)
            {
                if (index >= _elements.Count)
                {
                    return(GraceObject.Done);
                }
                var el  = _elements[index++];
                var req = new MethodRequest();
                var rpn = new RequestPart("apply",
                                          new List <GraceObject>(),
                                          new List <GraceObject>()
                {
                    el, arg
                }
                                          );

                req.AddPart(rpn);
                _block.Request(ctx, req);
                return(GraceObject.Done);
            }