コード例 #1
0
        private static GraceObject mAt(EvaluationContext ctx,
                                       MethodRequest req,
                                       StringCodepoints self)
        {
            MethodHelper.CheckArity(ctx, req, 1);
            var oth = req[0].Arguments[0].FindNativeParent <GraceNumber>();

            if (oth == null)
            {
                ErrorReporting.RaiseError(ctx, "R2001",
                                          new Dictionary <string, string> {
                    { "method", req.Name },
                    { "index", "1" },
                    { "part", req.Name },
                    { "required", "Number" },
                }, "ArgumentTypeError: Index must be a number");
            }
            int idx = oth.GetInt() - 1;

            if (idx >= self.codepoints.Length || idx < 0)
            {
                ErrorReporting.RaiseError(ctx, "R2013",
                                          new Dictionary <string, string> {
                    { "index", "" + (idx + 1) },
                    { "valid", self.codepoints.Length > 0 ?
                      "1 .. " + self.codepoints.Length
                                : "none (empty)" }
                }, "IndexError: Index out of range");
            }
            if (self.codepoints[idx] == null)
            {
                self.codepoints[idx] = CodepointObject.Create(self.utf32[idx]);
            }
            return(self.codepoints[idx]);
        }
コード例 #2
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));
        }
コード例 #3
0
ファイル: Methods.cs プロジェクト: smarr/kernan
 /// <inheritdoc/>
 public override GraceObject Respond(EvaluationContext ctx,
                                     GraceObject self,
                                     MethodRequest req)
 {
     MethodHelper.CheckArity(ctx, req, 1);
     return(method(ctx, (T)self, req[0].Arguments[0]));
 }
コード例 #4
0
ファイル: Methods.cs プロジェクト: smarr/kernan
 /// <inheritdoc/>
 public override GraceObject Respond(EvaluationContext ctx,
                                     GraceObject self,
                                     MethodRequest req)
 {
     MethodHelper.CheckArity(ctx, req, 0);
     return(method((T)self));
 }
コード例 #5
0
ファイル: GraceString.cs プロジェクト: tuan-nguyen-minh/Grace
        private static GraceObject substringFromTo(
            EvaluationContext ctx,
            MethodRequest req,
            GraceString self
            )
        {
            MethodHelper.CheckArity(ctx, req, 1, 1);
            // Index of first grapheme to include.
            var start = req[0].Arguments[0];
            // Index of last grapheme to include.
            var end = req[1].Arguments[0];
            var st  = start.FindNativeParent <GraceNumber>();

            if (st == null)
            {
                ErrorReporting.RaiseError(ctx, "R2001",
                                          new Dictionary <string, string> {
                    { "method", req.Name },
                    { "index", "1" },
                    { "part", "substringFrom" }
                }, "Start must be a number");
            }
            var en = end.FindNativeParent <GraceNumber>();

            if (en == null)
            {
                ErrorReporting.RaiseError(ctx, "R2001",
                                          new Dictionary <string, string> {
                    { "method", req.Name },
                    { "index", "1" },
                    { "part", "to" }
                }, "End must be a number");
            }
            // Because, e.g., substringFrom(1) to(1) should return the
            // first grapheme, the start value must be adjusted for
            // base-one indexing, but the end value must not be.
            int stInd = st.GetInt() - 1;
            int enInd = en.GetInt();

            if (stInd < 0)
            {
                stInd = 0;
            }
            if (enInd < 0)
            {
                enInd = 0;
            }
            if (enInd >= self.graphemeIndices.Length)
            {
                enInd = self.graphemeIndices.Length;
            }
            int endIndex = enInd < self.graphemeIndices.Length
                ? self.graphemeIndices[enInd]
                : self.Value.Length;

            stInd = self.graphemeIndices[stInd];
            return(GraceString.Create(self.Value.Substring(stInd,
                                                           endIndex - stInd)));
        }
コード例 #6
0
        private static GraceObject mGTE(EvaluationContext ctx,
                                        MethodRequest req,
                                        CodepointObject self)
        {
            MethodHelper.CheckArity(ctx, req, 1);
            var other = req[0].Arguments[0] as CodepointObject;

            if (other == null)
            {
                return(GraceBoolean.False);
            }
            return(GraceBoolean.Create(self.codepoint >= other.codepoint));
        }
コード例 #7
0
        private static GraceObject mNE(EvaluationContext ctx,
                                       MethodRequest req,
                                       StringCodepoints self)
        {
            MethodHelper.CheckArity(ctx, req, 1);
            var other = req[0].Arguments[0] as StringCodepoints;

            if (other == null)
            {
                return(GraceBoolean.True);
            }
            return(GraceBoolean.Create(cmp(self, other) != 0));
        }
コード例 #8
0
ファイル: Exceptions.cs プロジェクト: smarr/kernan
        /// <summary>Native method for Grace match</summary>
        /// <param name="ctx">Current interpreter</param>
        /// <param name="req">Request that resolved to this method</param>
        public GraceObject Match(EvaluationContext ctx, MethodRequest req)
        {
            MethodHelper.CheckArity(ctx, req, 1);
            var target = req[0].Arguments[0];
            var gep    = target as GraceExceptionPacket;

            if (gep == null)
            {
                return(Matching.FailedMatch(ctx, target));
            }
            if (Parent == null || Children.Contains(gep.KindName))
            {
                return(Matching.SuccessfulMatch(ctx, target));
            }
            return(Matching.FailedMatch(ctx, target));
        }
コード例 #9
0
        private static GraceObject mConcat(EvaluationContext ctx,
                                           MethodRequest req,
                                           StringCodepoints self)
        {
            MethodHelper.CheckArity(ctx, req, 1);
            var oth = req[0].Arguments[0].FindNativeParent <StringCodepoints>();

            if (oth == null)
            {
                ErrorReporting.RaiseError(ctx, "R2001",
                                          new Dictionary <string, string> {
                    { "method", req.Name },
                    { "index", "1" },
                    { "part", req.Name },
                    { "required", "codepoints" },
                }, "ArgumentTypeError: Needed codepoints object");
            }
            return(new StringCodepoints(self.utf32.Concat(oth.utf32)));
        }
コード例 #10
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));
        }
コード例 #11
0
        private static GraceObject mAt(EvaluationContext ctx,
                                       MethodRequest req,
                                       UTF32CodepointsView self)
        {
            MethodHelper.CheckArity(ctx, req, 1);
            var arg   = req[0].Arguments[0];
            var index = arg.FindNativeParent <GraceNumber>();
            var idx   = index.GetInt() - 1;

            if (idx < 0 || idx >= self.utf32.Count)
            {
                ErrorReporting.RaiseError(ctx, "R2013",
                                          new Dictionary <string, string> {
                    { "index", "" + (idx + 1) },
                    { "valid", self.utf32.Count > 0 ?
                      "1 .. " + self.utf32.Count
                                : "none (empty)" }
                }, "IndexError: Index out of range");
            }
            return(GraceNumber.Create(self.utf32[idx]));
        }
コード例 #12
0
        private static GraceObject mAt(EvaluationContext ctx,
                                       MethodRequest req,
                                       ByteString self)
        {
            MethodHelper.CheckArity(ctx, req, 1);
            var arg   = req[0].Arguments[0];
            var index = arg.FindNativeParent <GraceNumber>();
            var idx   = index.GetInt() - 1;

            if (idx < 0 || idx >= self.data.Length)
            {
                ErrorReporting.RaiseError(ctx, "R2013",
                                          new Dictionary <string, string> {
                    { "index", "" + (idx + 1) },
                    { "valid", self.data.Length > 0 ?
                      "1 .. " + self.data.Length
                                : "none (empty)" }
                }, "IndexError: Index out of range");
            }
            return(getByteObject(self.data[idx]));
        }
コード例 #13
0
        private static GraceObject mConcat(EvaluationContext ctx,
                                           MethodRequest req,
                                           ByteString self)
        {
            MethodHelper.CheckArity(ctx, req, 1);
            var oth = req[0].Arguments[0].FindNativeParent <ByteString>();

            if (oth == null)
            {
                ErrorReporting.RaiseError(ctx, "R2001",
                                          new Dictionary <string, string> {
                    { "method", req.Name },
                    { "index", "1" },
                    { "part", req.Name },
                    { "required", "byte string" },
                }, "ArgumentTypeError: Needed byte string");
            }
            var d2 = new byte[self.data.Length + oth.data.Length];

            self.data.CopyTo(d2, 0);
            oth.data.CopyTo(d2, self.data.Length);
            return(new ByteString(d2));
        }