Ejemplo n.º 1
0
 private static GraceObject mAsString(EvaluationContext ctx,
                                      MethodRequest req,
                                      CodepointObject self)
 {
     return(GraceString.Create("U+" + self.codepoint.ToString("X4")
                               + " " + self.parts[0]));
 }
Ejemplo n.º 2
0
 private NativeBlock_1d(Action <GraceObject> act)
 {
     action = act;
     AddMethod("apply(_)", null);
     AddMethod("asString", null);
     stringification = GraceString.Create("{ _ -> native code }");
 }
Ejemplo n.º 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)));
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 5
0
 private GraceBlock(Action <GraceObject> act)
 {
     AddMethod("apply(_)", null);
     AddMethod("spawn", null);
     AddMethod("asString", null);
     stringification = GraceString.Create("{ _ -> native code }");
 }
Ejemplo n.º 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)));
        }
Ejemplo n.º 7
0
 /// <summary>Native method for Grace asString</summary>
 public GraceObject AsString()
 {
     if (Boolean)
     {
         return(GraceString.Create("true"));
     }
     return(GraceString.Create("false"));
 }
Ejemplo n.º 8
0
 private static GraceObject mCodepoints(GraceString self)
 {
     if (self.codepointsObject == null)
     {
         self.codepointsObject = new StringCodepoints(self.Value);
     }
     return(self.codepointsObject);
 }
Ejemplo n.º 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));
 }
Ejemplo n.º 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));
        }
Ejemplo n.º 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
                       ));
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 13
0
 /// <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);
     }
 }
Ejemplo n.º 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);
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 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));
        }
Ejemplo n.º 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);
        }
Ejemplo n.º 18
0
 private GraceObject mAsString(EvaluationContext ctx)
 {
     return(GraceString.Create(GraceString.AsNativeString(ctx, lhs) +
                               " | " + GraceString.AsNativeString(ctx, rhs)));
 }
Ejemplo n.º 19
0
 private static GraceObject mString(EvaluationContext ctx,
                                    MethodRequest req,
                                    CodepointObject self)
 {
     return(GraceString.Create(Char.ConvertFromUtf32(self.codepoint)));
 }
Ejemplo n.º 20
0
 private static GraceObject mAsString(GraceString self)
 {
     return(self);
 }
Ejemplo n.º 21
0
 private static GraceObject mBidirectional(EvaluationContext ctx,
                                           MethodRequest req,
                                           CodepointObject self)
 {
     return(GraceString.Create(self.parts[3]));
 }
Ejemplo n.º 22
0
 /// <summary>Native method for Grace asString</summary>
 private static GraceObject mAsString(GraceNumber self)
 {
     return(GraceString.Create("" + self.Value));
 }
Ejemplo n.º 23
0
 private static GraceObject mString(EvaluationContext ctx,
                                    MethodRequest req,
                                    StringCodepoints self)
 {
     return(GraceString.Create(makeString(self)));
 }
Ejemplo n.º 24
0
 /// <summary>Native method for Grace message</summary>
 public GraceObject Message()
 {
     return(GraceString.Create(message));
 }
Ejemplo n.º 25
0
 /// <inheritdoc/>
 public GraceObject AsString(EvaluationContext ctx)
 {
     return(GraceString.Create(Name));
 }
Ejemplo n.º 26
0
 /// <summary>Native method for Grace asString</summary>
 new public GraceObject AsString(EvaluationContext ctx,
                                 GraceObject self)
 {
     return(GraceString.Create(KindName + ": " + message));
 }
Ejemplo n.º 27
0
 /// <summary>Native method supporting Grace AsString</summary>
 public virtual GraceObject AsString(EvaluationContext ctx,
                                     GraceObject self)
 {
     return(GraceString.Create(self.ToString()));
 }
Ejemplo n.º 28
0
 private GraceObject mAsString(EvaluationContext ctx)
 {
     return(GraceString.Create("Range[" + _low + " .. " + _high
                               + (_step != 1 ? " .. " + _step : "") + "]"));
 }
Ejemplo n.º 29
0
 private GraceObject mAsString()
 {
     return(GraceString.Create(desc));
 }
Ejemplo n.º 30
0
 private GraceObject mAsString(EvaluationContext ctx)
 {
     return(GraceString.Create((Success ? "Successful" : "Failed") + "Match["
                               + GraceString.AsNativeString(ctx, Result) + "]"));
 }