コード例 #1
0
 private static GraceObject getByteObject(byte b)
 {
     if (byteObjects[b] == null)
     {
         byteObjects[b] = GraceNumber.Create((int)b);
     }
     return(byteObjects[b]);
 }
コード例 #2
0
        /// <summary>Native method for Grace -</summary>
        /// <param name="self">Receiver of the method</param>
        /// <param name="other">Argument to the method</param>
        private static GraceObject mSubtract(
            GraceNumber self,
            GraceObject other
            )
        {
            var oth = other.FindNativeParent <GraceNumber>();

            return(GraceNumber.Create(self.Value - oth.Value));
        }
コード例 #3
0
        /// <summary>Native method for Grace ^</summary>
        /// <param name="self">Receiver of the method</param>
        /// <param name="other">Argument to the method</param>
        private static GraceObject mExponentiate(
            GraceNumber self,
            GraceObject other
            )
        {
            var oth = other.FindNativeParent <GraceNumber>();

            return(GraceNumber.Create(self.Value.Exponentiate(oth.Value)));
        }
コード例 #4
0
 private static GraceObject mDigit(EvaluationContext ctx,
                                   MethodRequest req,
                                   CodepointObject self)
 {
     // By convention, return -1 for non-numeric codepoints.
     if (self.parts[6] == "")
     {
         return(GraceNumber.Create(-1));
     }
     return(GraceNumber.Create(int.Parse(self.parts[6])));
 }
コード例 #5
0
        /// <summary>Native method for Grace /</summary>
        /// <param name="ctx">Current interpreter</param>
        /// <param name="self">Receiver of the method</param>
        /// <param name="other">Argument to the method</param>
        private static GraceObject mDivide(
            EvaluationContext ctx,
            GraceNumber self,
            GraceObject other
            )
        {
            var oth = other.FindNativeParent <GraceNumber>();

            if (oth.Value == Rational.Zero)
            {
                ErrorReporting.RaiseError(ctx, "R2012",
                                          new Dictionary <string, string> {
                    { "dividend", self.Value.ToString() },
                },
                                          "ZeroDivisionError: Division by zero.");
            }
            return(GraceNumber.Create(self.Value / oth.Value));
        }
コード例 #6
0
ファイル: GraceObjectProxy.cs プロジェクト: smarr/kernan
        /// <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));
        }
コード例 #7
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]));
        }
コード例 #8
0
        private static GraceObject mNumeric(EvaluationContext ctx,
                                            MethodRequest req,
                                            CodepointObject self)
        {
            // By convention, return -1 for non-numeric codepoints.
            if (self.parts[7] == "")
            {
                return(GraceNumber.Create(-1));
            }
            int val;

            if (int.TryParse(self.parts[7], out val))
            {
                return(GraceNumber.Create(val));
            }
            // At this point, it must be a fraction n/m.
            var bits = self.parts[7].Split('/');
            var rat  = Rational.Create(int.Parse(bits[0]), int.Parse(bits[1]));

            return(GraceNumber.Create(rat));
        }
コード例 #9
0
        private GraceObject mDo(EvaluationContext ctx, GraceObject block)
        {
            var      apply = MethodRequest.Single("apply", null);
            Rational v     = _low;

            if (_step < 0)
            {
                while (v >= _high)
                {
                    apply[0].Arguments[0] = GraceNumber.Create(v);
                    block.Request(ctx, apply);
                    v += _step;
                }
                return(GraceObject.Done);
            }
            while (v <= _high)
            {
                apply[0].Arguments[0] = GraceNumber.Create(v);
                block.Request(ctx, apply);
                v += _step;
            }
            return(GraceObject.Done);
        }
コード例 #10
0
 /// <summary>Native method for Grace unary negation</summary>
 private static GraceObject mNegate(GraceNumber self)
 {
     return(GraceNumber.Create(-self.Value));
 }
コード例 #11
0
ファイル: GraceString.cs プロジェクト: tuan-nguyen-minh/Grace
 private static GraceObject mSize(GraceString self)
 {
     return(GraceNumber.Create(self.graphemeIndices.Length));
 }
コード例 #12
0
 private static GraceObject mCombining(EvaluationContext ctx,
                                       MethodRequest req,
                                       CodepointObject self)
 {
     return(GraceNumber.Create(int.Parse(self.parts[2])));
 }
コード例 #13
0
 private static GraceObject mCodepoint(EvaluationContext ctx,
                                       MethodRequest req,
                                       CodepointObject self)
 {
     return(GraceNumber.Create(self.codepoint));
 }
コード例 #14
0
 private static GraceObject mSize(EvaluationContext ctx,
                                  MethodRequest req,
                                  StringCodepoints self)
 {
     return(GraceNumber.Create(self.utf32.Count));
 }
コード例 #15
0
 private GraceObject mHash()
 {
     return(GraceNumber.Create(Boolean.GetHashCode()));
 }
コード例 #16
0
ファイル: GraceString.cs プロジェクト: tuan-nguyen-minh/Grace
 private static GraceObject mHash(GraceString self)
 {
     return(GraceNumber.Create(self.nfc.GetHashCode()));
 }
コード例 #17
0
 private static GraceObject mHash(GraceNumber self)
 {
     return(GraceNumber.Create(self.Value.GetHashCode()));
 }
コード例 #18
0
 private static GraceObject mSize(EvaluationContext ctx,
                                  MethodRequest req,
                                  ByteString self)
 {
     return(GraceNumber.Create(self.data.Length));
 }