Esempio n. 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));
        }
Esempio n. 2
0
 private GraceObject mIfTrueIfFalse(EvaluationContext ctx, MethodRequest req)
 {
     if (Success)
     {
         return(req[0].Arguments[0].Request(ctx, MethodRequest.Nullary("apply")));
     }
     return(req[1].Arguments[0].Request(ctx, MethodRequest.Nullary("apply")));
 }
Esempio n. 3
0
 private GraceObject mIfFalse(EvaluationContext ctx, GraceObject b)
 {
     if (!Success)
     {
         return(b.Request(ctx, MethodRequest.Nullary("apply")));
     }
     return(GraceObject.Done);
 }
Esempio n. 4
0
        /// <summary>Native method for Grace refine</summary>
        /// <param name="ctx">Current interpreter</param>
        /// <param name="name">Name of the sub-exception</param>
        public GraceObject Refine(EvaluationContext ctx, GraceObject name)
        {
            var asGraceString = name.Request(ctx,
                                             MethodRequest.Nullary("asString"))
                                .FindNativeParent <GraceString>();
            var nameStr = asGraceString.Value;

            AddDescendant(nameStr);
            return(new GraceExceptionKind(this, nameStr));
        }
Esempio n. 5
0
        private void start()
        {
            var apply = MethodRequest.Nullary("apply");

            try
            {
                block.Request(interpreter, apply);
            }
            catch (GraceExceptionPacketException gepe)
            {
                exception = gepe.ExceptionPacket;
                ErrorReporting.WriteException(exception);
            }
        }
Esempio n. 6
0
        /// <summary>Native method for Grace raise</summary>
        /// <param name="ctx">Current interpreter</param>
        /// <param name="message">Message string</param>
        public GraceObject Raise(EvaluationContext ctx, GraceObject message)
        {
            var msg           = "<<No message>>";
            var asGraceString = message.Request(ctx,
                                                MethodRequest.Nullary("asString"))
                                .FindNativeParent <GraceString>();

            if (asGraceString != null)
            {
                msg = asGraceString.Value;
            }
            GraceExceptionPacket.Throw(this, msg, ctx.GetStackTrace());
            return(GraceObject.Done);
        }
Esempio n. 7
0
        /// <summary>
        /// Convert any GraceObject into a CLI string.
        /// </summary>
        /// <param name="ctx">Interpreter to call asString under</param>
        /// <param name="o">Object to convert</param>
        public static string AsNativeString(EvaluationContext ctx,
                                            GraceObject o)
        {
            var s = o as GraceString;

            if (s != null)
            {
                return(s.Value);
            }
            s = o.Request(ctx, MethodRequest.Nullary("asString"))
                as GraceString;
            if (s != null)
            {
                return(s.Value);
            }
            return("not a string, nor stringifiable");
        }
Esempio n. 8
0
        private static GraceObject mConcatenate(EvaluationContext ctx,
                                                GraceString self,
                                                GraceObject other)
        {
            var oth = other.FindNativeParent <GraceString>();

            if (oth != null)
            {
                return(GraceString.Create(self.Value + oth.Value));
            }
            var op = other as GraceObjectProxy;

            if (op != null)
            {
                return(GraceString.Create(self.Value + op.Object));
            }
            other = other.Request(ctx, MethodRequest.Nullary("asString"));
            oth   = other.FindNativeParent <GraceString>();
            return(GraceString.Create(self.Value + oth.Value));
        }
Esempio n. 9
0
        /// <summary>Native method for Grace ifTrue ifFalse</summary>
        /// <param name="ctx">Current interpreter</param>
        /// <param name="req">Method request that gave rise to this method
        /// execution</param>
        public GraceObject IfTrueIfFalse(EvaluationContext ctx,
                                         MethodRequest req)
        {
            MethodHelper.CheckArity(ctx, req, 1, 1);
            var trueBlock  = req[0].Arguments[0];
            var falseBlock = req[1].Arguments[0];

            if (!(trueBlock is GraceBlock))
            {
                ErrorReporting.RaiseError(ctx, "R2001",
                                          new Dictionary <string, string>()
                {
                    { "method", req.Name },
                    { "index", "1" },
                    { "part", "ifTrue" },
                    { "required", "Block" }
                },
                                          "ArgumentTypeError: ifTrue ifFalse requires two block arguments"
                                          );
            }
            if (!(falseBlock is GraceBlock))
            {
                ErrorReporting.RaiseError(ctx, "R2001",
                                          new Dictionary <string, string>()
                {
                    { "method", req.Name },
                    { "index", "1" },
                    { "part", "ifFalse" },
                    { "required", "Block" }
                },
                                          "ArgumentTypeError: ifTrue ifFalse requires two block arguments"
                                          );
            }
            var apply = MethodRequest.Nullary("apply");

            if (Boolean)
            {
                return(trueBlock.Request(ctx, apply));
            }
            return(falseBlock.Request(ctx, apply));
        }
Esempio n. 10
0
        /// <summary>Native method for Grace orElse</summary>
        /// <param name="ctx">Current interpreter</param>
        /// <param name="other">Block to apply if false</param>
        public GraceObject OrElse(EvaluationContext ctx, GraceObject other)
        {
            var oth = other as GraceBlock;

            if (oth == null)
            {
                ErrorReporting.RaiseError(ctx, "R2001",
                                          new Dictionary <string, string>()
                {
                    { "method", "orElse" },
                    { "index", "1" },
                    { "part", "ifFalse" },
                    { "required", "Block" }
                },
                                          "ArgumentTypeError: orElse requires a block argument"
                                          );
            }
            if (!Boolean)
            {
                var req = MethodRequest.Nullary("apply");
                return(other.Request(ctx, req));
            }
            return(True);
        }
Esempio n. 11
0
 /// <summary>Determine whether MatchResult failed</summary>
 /// <param name="ctx">Current interpreter</param>
 /// <param name="matchResult">MatchResult to examine</param>
 public static bool Failed(EvaluationContext ctx,
                           GraceObject matchResult)
 {
     return(GraceBoolean.False == matchResult.Request(ctx, MethodRequest.Nullary("succeeded")));
 }
Esempio n. 12
0
 /// <summary>Get the result of a MatchResult</summary>
 /// <param name="ctx">Current interpreter</param>
 /// <param name="matchResult">MatchResult to examine</param>
 public static GraceObject GetResult(EvaluationContext ctx,
                                     GraceObject matchResult)
 {
     return(matchResult.Request(ctx, MethodRequest.Nullary("result")));
 }