Example #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)));
        }
Example #2
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));
        }
Example #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)));
        }
Example #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 mSubtract(
            GraceNumber self,
            GraceObject other
            )
        {
            var oth = other.FindNativeParent <GraceNumber>();

            return(GraceNumber.Create(self.Value - oth.Value));
        }
Example #5
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));
        }
Example #6
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));
        }
Example #7
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
                       ));
        }
Example #8
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));
        }
Example #9
0
        private GraceObject mDotDot(EvaluationContext ctx, GraceObject step)
        {
            var n = step.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(_low, _high, _step * n.Value));
        }
Example #10
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));
        }
Example #11
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));
        }