Ejemplo n.º 1
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.º 2
0
 private static GraceObject mAsString(EvaluationContext ctx,
                                      MethodRequest req,
                                      CodepointObject self)
 {
     return(GraceString.Create("U+" + self.codepoint.ToString("X4")
                               + " " + self.parts[0]));
 }
Ejemplo n.º 3
0
 private NativeBlock_1d(Action <GraceObject> act)
 {
     action = act;
     AddMethod("apply(_)", null);
     AddMethod("asString", null);
     stringification = GraceString.Create("{ _ -> native code }");
 }
Ejemplo n.º 4
0
 private GraceBlock(Action <GraceObject> act)
 {
     AddMethod("apply(_)", null);
     AddMethod("spawn", null);
     AddMethod("asString", null);
     stringification = GraceString.Create("{ _ -> native code }");
 }
Ejemplo n.º 5
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.º 6
0
 /// <summary>Native method for Grace asString</summary>
 public GraceObject AsString()
 {
     if (Boolean)
     {
         return(GraceString.Create("true"));
     }
     return(GraceString.Create("false"));
 }
Ejemplo n.º 7
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.º 8
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.º 9
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.º 10
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.º 11
0
 /// <summary>Native method supporting Grace AsString</summary>
 public virtual GraceObject AsString(EvaluationContext ctx,
                                     GraceObject self)
 {
     return(GraceString.Create(self.ToString()));
 }
Ejemplo n.º 12
0
 private static GraceObject mString(EvaluationContext ctx,
                                    MethodRequest req,
                                    CodepointObject self)
 {
     return(GraceString.Create(Char.ConvertFromUtf32(self.codepoint)));
 }
Ejemplo n.º 13
0
 /// <summary>Native method for Grace asString</summary>
 private static GraceObject mAsString(GraceNumber self)
 {
     return(GraceString.Create("" + self.Value));
 }
Ejemplo n.º 14
0
 private static GraceObject mBidirectional(EvaluationContext ctx,
                                           MethodRequest req,
                                           CodepointObject self)
 {
     return(GraceString.Create(self.parts[3]));
 }
Ejemplo n.º 15
0
 private GraceObject mAsString(EvaluationContext ctx)
 {
     return(GraceString.Create("Range[" + _low + " .. " + _high
                               + (_step != 1 ? " .. " + _step : "") + "]"));
 }
Ejemplo n.º 16
0
 private static GraceObject mString(EvaluationContext ctx,
                                    MethodRequest req,
                                    StringCodepoints self)
 {
     return(GraceString.Create(makeString(self)));
 }
Ejemplo n.º 17
0
 /// <summary>Native method for Grace message</summary>
 public GraceObject Message()
 {
     return(GraceString.Create(message));
 }
Ejemplo n.º 18
0
 /// <inheritdoc/>
 public GraceObject AsString(EvaluationContext ctx)
 {
     return(GraceString.Create(Name));
 }
Ejemplo n.º 19
0
 /// <summary>Native method for Grace asString</summary>
 new public GraceObject AsString(EvaluationContext ctx,
                                 GraceObject self)
 {
     return(GraceString.Create(KindName + ": " + message));
 }
Ejemplo n.º 20
0
        /// <inheritsdoc/>
        /// <remarks>Uses reflection to access the method, or the
        /// dynamic type to access operators.</remarks>
        public override GraceObject Request(EvaluationContext ctx, MethodRequest req)
        {
            string name = req.Name;

            switch (name)
            {
            case "isNull":
                if (obj == null)
                {
                    return(GraceBoolean.True);
                }
                return(GraceBoolean.False);

            case "+(_)":
                return(GraceObjectProxy.Create((dynamic)obj + (dynamic)viewAsNative(req[0].Arguments[0])));

            case "-(_)":
                return(GraceObjectProxy.Create((dynamic)obj - (dynamic)viewAsNative(req[0].Arguments[0])));

            case "*(_)":
                return(GraceObjectProxy.Create((dynamic)obj * (dynamic)viewAsNative(req[0].Arguments[0])));

            case "/(_)":
                return(GraceObjectProxy.Create((dynamic)obj / (dynamic)viewAsNative(req[0].Arguments[0])));

            case "<(_)":
                return(GraceObjectProxy.Create((dynamic)obj < (dynamic)viewAsNative(req[0].Arguments[0])));

            case "<=(_)":
                return(GraceObjectProxy.Create((dynamic)obj <= (dynamic)viewAsNative(req[0].Arguments[0])));

            case ">(_)":
                return(GraceObjectProxy.Create((dynamic)obj > (dynamic)viewAsNative(req[0].Arguments[0])));

            case ">=(_)":
                return(GraceObjectProxy.Create((dynamic)obj >= (dynamic)viewAsNative(req[0].Arguments[0])));

            case "==(_)":
                return(GraceObjectProxy.Create((dynamic)obj == (dynamic)viewAsNative(req[0].Arguments[0])));

            case "!=(_)":
                return(GraceObjectProxy.Create((dynamic)obj != (dynamic)viewAsNative(req[0].Arguments[0])));

            case "%(_)":
                return(GraceObjectProxy.Create((dynamic)obj % (dynamic)viewAsNative(req[0].Arguments[0])));

            case "^(_)":
                return(GraceObjectProxy.Create(Math.Pow((dynamic)obj, (dynamic)viewAsNative(req[0].Arguments[0]))));

            case "asString":
                if (obj == null)
                {
                    return(GraceString.Create("(null)"));
                }
                return(GraceString.Create(obj.ToString()));

            case "prefix!":
                return(GraceObjectProxy.Create(!(dynamic)obj));

            case "at(_)":
                if (Interpreter.JSIL)
                {
                    // Calling get_Item directly is iffy, but
                    // works on JSIL where accessing Item fails,
                    // and [] uses native (the wrong) [].
                    return(GraceObjectProxy.Create(
                               ((dynamic)obj)
                               .get_Item(
                                   (dynamic)viewAsNative(req[0].Arguments[0]))
                               ));
                }
                return(GraceObjectProxy.Create(
                           ((dynamic)obj)[
                               (dynamic)viewAsNative(req[0].Arguments[0])]));
            }
            object[] args = new object[req[0].Arguments.Count];
            for (int i = 0; i < req[0].Arguments.Count; i++)
            {
                args[i] = viewAsNative(req[0].Arguments[i]);
            }
            Type[] types = new Type[args.Length];
            for (int i = 0; i < types.Length; i++)
            {
                types[i] = args[i].GetType();
            }
            MethodInfo meth = type.GetMethod(name, types);

            if (meth == null)
            {
                ErrorReporting.RaiseError(ctx, "R2000",
                                          new Dictionary <string, string>()
                {
                    { "method", req.Name },
                    { "receiver", "Native Proxy" }
                },
                                          "LookupError: Native proxy failed to find method «${method}»"
                                          );
            }
            return(GraceObjectProxy.Create(meth.Invoke(obj, args)));
        }
Ejemplo n.º 21
0
 private GraceObject mAsString()
 {
     return(GraceString.Create(desc));
 }
Ejemplo n.º 22
0
 private GraceObject mAsString(EvaluationContext ctx)
 {
     return(GraceString.Create(GraceString.AsNativeString(ctx, lhs) +
                               " | " + GraceString.AsNativeString(ctx, rhs)));
 }
Ejemplo n.º 23
0
 private GraceObject mAsString(EvaluationContext ctx)
 {
     return(GraceString.Create((Success ? "Successful" : "Failed") + "Match["
                               + GraceString.AsNativeString(ctx, Result) + "]"));
 }