Exemplo n.º 1
0
 private static GraceObject getByteObject(byte b)
 {
     if (byteObjects[b] == null)
     {
         byteObjects[b] = GraceNumber.Create((int)b);
     }
     return(byteObjects[b]);
 }
Exemplo n.º 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));
        }
Exemplo n.º 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));
        }
Exemplo n.º 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)));
        }
Exemplo n.º 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])));
 }
Exemplo n.º 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));
 }
Exemplo n.º 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));
        }
Exemplo n.º 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));
        }
Exemplo n.º 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));
        }
Exemplo n.º 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]));
        }
Exemplo n.º 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));
        }
Exemplo n.º 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));
        }
Exemplo n.º 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);
        }
Exemplo n.º 14
0
 /// <summary>Native method for Grace numerator</summary>
 private static GraceObject mNumerator(GraceNumber self)
 {
     return(Create(self.Value.Numerator));
 }
Exemplo n.º 15
0
 private static GraceObject mSize(GraceString self)
 {
     return(GraceNumber.Create(self.graphemeIndices.Length));
 }
Exemplo n.º 16
0
 private static GraceObject mCombining(EvaluationContext ctx,
                                       MethodRequest req,
                                       CodepointObject self)
 {
     return(GraceNumber.Create(int.Parse(self.parts[2])));
 }
Exemplo n.º 17
0
 private static GraceObject mCodepoint(EvaluationContext ctx,
                                       MethodRequest req,
                                       CodepointObject self)
 {
     return(GraceNumber.Create(self.codepoint));
 }
Exemplo n.º 18
0
 private static GraceObject mSize(EvaluationContext ctx,
                                  MethodRequest req,
                                  StringCodepoints self)
 {
     return(GraceNumber.Create(self.utf32.Count));
 }
Exemplo n.º 19
0
 private GraceObject mHash()
 {
     return(GraceNumber.Create(Boolean.GetHashCode()));
 }
Exemplo n.º 20
0
 /// <summary>Native method for Grace asString</summary>
 private static GraceObject mAsString(GraceNumber self)
 {
     return(GraceString.Create("" + self.Value));
 }
Exemplo n.º 21
0
 /// <summary>Native method for Grace unary negation</summary>
 private static GraceObject mNegate(GraceNumber self)
 {
     return(GraceNumber.Create(-self.Value));
 }
Exemplo n.º 22
0
 /// <summary>Native method for Grace integral</summary>
 private static GraceObject mIntegral(GraceNumber self)
 {
     return(Create(self.Value.Integral));
 }
Exemplo n.º 23
0
 /// <summary>Native method for Grace denominator</summary>
 private static GraceObject mDenominator(GraceNumber self)
 {
     return(Create(self.Value.Denominator));
 }
Exemplo n.º 24
0
 private static GraceObject mHash(GraceString self)
 {
     return(GraceNumber.Create(self.nfc.GetHashCode()));
 }
Exemplo n.º 25
0
 private static GraceObject mSize(EvaluationContext ctx,
                                  MethodRequest req,
                                  ByteString self)
 {
     return(GraceNumber.Create(self.data.Length));
 }
Exemplo n.º 26
0
 private static GraceObject mHash(GraceNumber self)
 {
     return(GraceNumber.Create(self.Value.GetHashCode()));
 }