예제 #1
0
 private static GraceObject mAsString(EvaluationContext ctx,
                                      MethodRequest req,
                                      CodepointObject self)
 {
     return(GraceString.Create("U+" + self.codepoint.ToString("X4")
                               + " " + self.parts[0]));
 }
예제 #2
0
 private NativeBlock_1d(Action <GraceObject> act)
 {
     action = act;
     AddMethod("apply(_)", null);
     AddMethod("asString", null);
     stringification = GraceString.Create("{ _ -> native code }");
 }
예제 #3
0
        private static GraceObject mAt(
            EvaluationContext ctx,
            GraceString self,
            GraceObject other
            )
        {
            var oth = other.FindNativeParent <GraceNumber>();

            if (oth == null)
            {
                return(GraceString.Create("bad index"));
            }
            int idx = oth.GetInt() - 1;

            if (idx >= self.graphemeIndices.Length || idx < 0)
            {
                ErrorReporting.RaiseError(ctx, "R2013",
                                          new Dictionary <string, string> {
                    { "index", "" + (idx + 1) },
                    { "valid", self.graphemeIndices.Length > 0 ?
                      "1 .. " + self.graphemeIndices.Length
                                : "none (empty)" }
                }, "Index must be a number");
            }
            int start = self.graphemeIndices[idx];

            return(GraceString.Create(
                       StringInfo.GetNextTextElement(self.Value, start)));
        }
예제 #4
0
        /// <inheritdoc/>
        public override GraceObject Respond(
            EvaluationContext ctx,
            GraceObject self,
            MethodRequest req
            )
        {
            checkAccessibility(ctx, req);
            var arg = req[1].Arguments[0];

            if (Type != null)
            {
                if (Matching.TryMatch(ctx, Type, arg, out GraceObject result))
                {
                    arg = result;
                }
                else
                {
                    ErrorReporting.RaiseError(ctx, "R2025",
                                              new Dictionary <string, string> {
                        { "field", req[0].Name },
                        { "required", GraceString.AsNativeString(ctx, Type) }
                    },
                                              "TypeError: ${field} can only hold ${required}.");
                }
            }

            var tmp = cell.Value;

            cell.Value = arg;
            return(tmp);
        }
예제 #5
0
 private GraceBlock(Action <GraceObject> act)
 {
     AddMethod("apply(_)", null);
     AddMethod("spawn", null);
     AddMethod("asString", null);
     stringification = GraceString.Create("{ _ -> native code }");
 }
예제 #6
0
        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)));
        }
예제 #7
0
 /// <summary>Native method for Grace asString</summary>
 public GraceObject AsString()
 {
     if (Boolean)
     {
         return(GraceString.Create("true"));
     }
     return(GraceString.Create("false"));
 }
예제 #8
0
 private static GraceObject mCodepoints(GraceString self)
 {
     if (self.codepointsObject == null)
     {
         self.codepointsObject = new StringCodepoints(self.Value);
     }
     return(self.codepointsObject);
 }
예제 #9
0
 private static GraceObject mMatch(
     EvaluationContext ctx,
     GraceString self,
     GraceObject target)
 {
     return((mEqualsEquals(self, target) == GraceBoolean.True)
         ? Matching.SuccessfulMatch(ctx, target)
         : Matching.FailedMatch(ctx, target));
 }
예제 #10
0
        private static GraceObject mNotEquals(
            GraceString self,
            GraceObject other
            )
        {
            var oth = other.FindNativeParent <GraceString>();

            return((oth == null) ? GraceBoolean.True
                                 : GraceBoolean.Create(self.nfc != oth.nfc));
        }
예제 #11
0
        private static GraceObject mGreaterThanEqual(
            GraceString self,
            GraceObject other
            )
        {
            var oth = other.FindNativeParent <GraceString>();

            return((oth == null) ? GraceBoolean.False
                                 : GraceBoolean.Create(
                       compare(self, oth) >= 0
                       ));
        }
예제 #12
0
        /// <summary>
        /// Compare two GraceStrings, respecting grapheme cluster
        /// boundaries, using the fully-decomposed version of the
        /// string.
        /// </summary>
        private static int compare(GraceString a, GraceString b)
        {
            if (a.nfc == b.nfc)
            {
                return(0);
            }
            var ad = a.decomposedGraphemeClusters;
            var bd = b.decomposedGraphemeClusters;

            for (int i = 0, j = 0; i < ad.Length && j < bd.Length; i++, j++)
            {
                var ga  = ad[i];
                var gb  = bd[i];
                var len = ga.Length < gb.Length ? ga.Length : gb.Length;
                for (int k = 0; k < len; k++)
                {
                    if (ga[k] < gb[k])
                    {
                        return(-1);
                    }
                    if (ga[k] > gb[k])
                    {
                        return(1);
                    }
                }
                // If the clusters are the same as far as they go,
                // but one is longer, it comes afterwards.
                if (ga.Length > gb.Length)
                {
                    return(1);
                }
                if (gb.Length > ga.Length)
                {
                    return(-1);
                }
            }
            // If the strings are the same as far as they go, but
            // one is longer, it comes afterwards.
            if (ad.Length > bd.Length)
            {
                return(1);
            }
            if (bd.Length > ad.Length)
            {
                return(-1);
            }
            return(0);
        }
예제 #13
0
파일: Matching.cs 프로젝트: mwh/kernan
 /// <summary>
 /// Attempt to match a pattern, raising R2025 TypeError on failure.
 /// </summary>
 /// <param name="ctx">Current interpreter</param>
 /// <param name="pattern">Pattern to match against</param>
 /// <param name="target">Object to examine</param>
 /// <param name="name">Name to report in error (e.g. field or parameter name)</param>
 /// <returns></returns>
 public static GraceObject TypeMatch(EvaluationContext ctx, GraceObject pattern, GraceObject target, string name)
 {
     if (Matching.TryMatch(ctx, pattern, target, out var result))
     {
         return(result);
     }
     else
     {
         ErrorReporting.RaiseError(ctx, "R2025",
                                   new Dictionary <string, string> {
             { "field", name },
             { "required", GraceString.AsNativeString(ctx, pattern) }
         },
                                   "TypeError: argument type mismatch");
         return(null);
     }
 }
예제 #14
0
        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);
        }
예제 #15
0
        /// <summary>Make a proxy for an object</summary>
        /// <param name="o">Object to proxy</param>
        public static GraceObject Create(Object o)
        {
            if (o is bool)
            {
                return(GraceBoolean.Create((dynamic)o));
            }
            if (o is int)
            {
                return(GraceNumber.Create((dynamic)o));
            }
            var s = o as string;

            if (s != null)
            {
                return(GraceString.Create(s));
            }
            return(new GraceObjectProxy(o));
        }
예제 #16
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));
        }
예제 #17
0
        private GraceObject mAsString(EvaluationContext ctx)
        {
            if (stringification != null)
            {
                return(stringification);
            }
            var p = ParseNodeMeta.PrettyPrint(ctx, node.Origin);

            if (p.LastIndexOf(Environment.NewLine) != -1 &&
                node.Body.Count != 1)
            {
                var last = "...";
                if (node.Body.Count > 0)
                {
                    last = "... " + ParseNodeMeta.PrettyPrint(ctx,
                                                              node.Body.Last().Origin);
                }
                var bpn = node.Origin as BlockParseNode;
                if (bpn == null)
                {
                    p = String.Join(", ",
                                    from x in parameters
                                    select ParseNodeMeta.PrettyPrint(ctx, x.Origin))
                        + " -> " + last;
                }
                else
                {
                    p = String.Join(", ",
                                    from x in bpn.Parameters
                                    select ParseNodeMeta.PrettyPrint(ctx, x))
                        + " -> " + last;
                }
            }
            stringification = GraceString.Create("Block["
                                                 + p
                                                 + "]");
            return(stringification);
        }
예제 #18
0
파일: Matching.cs 프로젝트: mwh/kernan
 private GraceObject mAsString(EvaluationContext ctx)
 {
     return(GraceString.Create(GraceString.AsNativeString(ctx, lhs) +
                               " | " + GraceString.AsNativeString(ctx, rhs)));
 }
예제 #19
0
 private static GraceObject mString(EvaluationContext ctx,
                                    MethodRequest req,
                                    CodepointObject self)
 {
     return(GraceString.Create(Char.ConvertFromUtf32(self.codepoint)));
 }
예제 #20
0
 private static GraceObject mAsString(GraceString self)
 {
     return(self);
 }
예제 #21
0
 private static GraceObject mBidirectional(EvaluationContext ctx,
                                           MethodRequest req,
                                           CodepointObject self)
 {
     return(GraceString.Create(self.parts[3]));
 }
예제 #22
0
 /// <summary>Native method for Grace asString</summary>
 private static GraceObject mAsString(GraceNumber self)
 {
     return(GraceString.Create("" + self.Value));
 }
예제 #23
0
 private static GraceObject mString(EvaluationContext ctx,
                                    MethodRequest req,
                                    StringCodepoints self)
 {
     return(GraceString.Create(makeString(self)));
 }
예제 #24
0
파일: Exceptions.cs 프로젝트: smarr/kernan
 /// <summary>Native method for Grace message</summary>
 public GraceObject Message()
 {
     return(GraceString.Create(message));
 }
예제 #25
0
파일: Exceptions.cs 프로젝트: smarr/kernan
 /// <inheritdoc/>
 public GraceObject AsString(EvaluationContext ctx)
 {
     return(GraceString.Create(Name));
 }
예제 #26
0
파일: Exceptions.cs 프로젝트: smarr/kernan
 /// <summary>Native method for Grace asString</summary>
 new public GraceObject AsString(EvaluationContext ctx,
                                 GraceObject self)
 {
     return(GraceString.Create(KindName + ": " + message));
 }
예제 #27
0
파일: GraceObject.cs 프로젝트: smarr/kernan
 /// <summary>Native method supporting Grace AsString</summary>
 public virtual GraceObject AsString(EvaluationContext ctx,
                                     GraceObject self)
 {
     return(GraceString.Create(self.ToString()));
 }
예제 #28
0
 private GraceObject mAsString(EvaluationContext ctx)
 {
     return(GraceString.Create("Range[" + _low + " .. " + _high
                               + (_step != 1 ? " .. " + _step : "") + "]"));
 }
예제 #29
0
파일: Matching.cs 프로젝트: mwh/kernan
 private GraceObject mAsString()
 {
     return(GraceString.Create(desc));
 }
예제 #30
0
파일: Matching.cs 프로젝트: mwh/kernan
 private GraceObject mAsString(EvaluationContext ctx)
 {
     return(GraceString.Create((Success ? "Successful" : "Failed") + "Match["
                               + GraceString.AsNativeString(ctx, Result) + "]"));
 }