예제 #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 &lt;=</summary>
        /// <param name="self">Receiver of the method</param>
        /// <param name="other">Argument to the method</param>
        private static GraceObject mLessEqual(
            GraceNumber self,
            GraceObject other
            )
        {
            var oth = other.FindNativeParent <GraceNumber>();

            return(GraceBoolean.Create(self.Value <= oth.Value));
        }
예제 #4
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)));
        }
예제 #5
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])));
 }
예제 #6
0
 /// <summary>Native method for Grace match</summary>
 /// <param name="ctx">Current interpreter</param>
 /// <param name="self">Receiver of the method</param>
 /// <param name="target">Target of the match</param>
 private static GraceObject mMatch(
     EvaluationContext ctx,
     GraceNumber self,
     GraceObject target
     )
 {
     if (mEqualsEquals(self, target) == GraceBoolean.True)
     {
         return(Matching.SuccessfulMatch(ctx, target));
     }
     return(Matching.FailedMatch(ctx, target));
 }
예제 #7
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 mNotEquals(
            GraceNumber self,
            GraceObject other
            )
        {
            var oth = other.FindNativeParent <GraceNumber>();

            if (oth == null)
            {
                return(GraceBoolean.True);
            }
            return(GraceBoolean.Create(self.Value != oth.Value));
        }
예제 #8
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));
        }
예제 #9
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));
        }
예제 #10
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]));
        }
예제 #11
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));
        }
예제 #12
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 mDotDot(
            EvaluationContext ctx,
            GraceNumber self,
            GraceObject other
            )
        {
            var n = other.FindNativeParent <GraceNumber>();

            if (n == null)
            {
                ErrorReporting.RaiseError(ctx, "R2001",
                                          new Dictionary <string, string> {
                    { "method", ".." },
                    { "index", "1" },
                    { "part", ".." },
                    { "required", "Number" }
                },
                                          "ArgumentTypeError: .. requires a Number argument"
                                          );
            }
            return(new GraceRange(self.Value, n.Value, 1));
        }
예제 #13
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);
        }
예제 #14
0
 /// <summary>Native method for Grace numerator</summary>
 private static GraceObject mNumerator(GraceNumber self)
 {
     return(Create(self.Value.Numerator));
 }
예제 #15
0
 private static GraceObject mSize(GraceString self)
 {
     return(GraceNumber.Create(self.graphemeIndices.Length));
 }
예제 #16
0
 private static GraceObject mCombining(EvaluationContext ctx,
                                       MethodRequest req,
                                       CodepointObject self)
 {
     return(GraceNumber.Create(int.Parse(self.parts[2])));
 }
예제 #17
0
 private static GraceObject mCodepoint(EvaluationContext ctx,
                                       MethodRequest req,
                                       CodepointObject self)
 {
     return(GraceNumber.Create(self.codepoint));
 }
예제 #18
0
 private static GraceObject mSize(EvaluationContext ctx,
                                  MethodRequest req,
                                  StringCodepoints self)
 {
     return(GraceNumber.Create(self.utf32.Count));
 }
예제 #19
0
 private GraceObject mHash()
 {
     return(GraceNumber.Create(Boolean.GetHashCode()));
 }
예제 #20
0
 /// <summary>Native method for Grace asString</summary>
 private static GraceObject mAsString(GraceNumber self)
 {
     return(GraceString.Create("" + self.Value));
 }
예제 #21
0
 /// <summary>Native method for Grace unary negation</summary>
 private static GraceObject mNegate(GraceNumber self)
 {
     return(GraceNumber.Create(-self.Value));
 }
예제 #22
0
 /// <summary>Native method for Grace integral</summary>
 private static GraceObject mIntegral(GraceNumber self)
 {
     return(Create(self.Value.Integral));
 }
예제 #23
0
 /// <summary>Native method for Grace denominator</summary>
 private static GraceObject mDenominator(GraceNumber self)
 {
     return(Create(self.Value.Denominator));
 }
예제 #24
0
 private static GraceObject mHash(GraceString self)
 {
     return(GraceNumber.Create(self.nfc.GetHashCode()));
 }
예제 #25
0
 private static GraceObject mSize(EvaluationContext ctx,
                                  MethodRequest req,
                                  ByteString self)
 {
     return(GraceNumber.Create(self.data.Length));
 }
예제 #26
0
 private static GraceObject mHash(GraceNumber self)
 {
     return(GraceNumber.Create(self.Value.GetHashCode()));
 }