private static GraceObject getByteObject(byte b) { if (byteObjects[b] == null) { byteObjects[b] = GraceNumber.Create((int)b); } return(byteObjects[b]); }
/// <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)); }
/// <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))); }
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]))); }
/// <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)); }
/// <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)); }
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])); }
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)); }
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); }
/// <summary>Native method for Grace unary negation</summary> private static GraceObject mNegate(GraceNumber self) { return(GraceNumber.Create(-self.Value)); }
private static GraceObject mSize(GraceString self) { return(GraceNumber.Create(self.graphemeIndices.Length)); }
private static GraceObject mCombining(EvaluationContext ctx, MethodRequest req, CodepointObject self) { return(GraceNumber.Create(int.Parse(self.parts[2]))); }
private static GraceObject mCodepoint(EvaluationContext ctx, MethodRequest req, CodepointObject self) { return(GraceNumber.Create(self.codepoint)); }
private static GraceObject mSize(EvaluationContext ctx, MethodRequest req, StringCodepoints self) { return(GraceNumber.Create(self.utf32.Count)); }
private GraceObject mHash() { return(GraceNumber.Create(Boolean.GetHashCode())); }
private static GraceObject mHash(GraceString self) { return(GraceNumber.Create(self.nfc.GetHashCode())); }
private static GraceObject mHash(GraceNumber self) { return(GraceNumber.Create(self.Value.GetHashCode())); }
private static GraceObject mSize(EvaluationContext ctx, MethodRequest req, ByteString self) { return(GraceNumber.Create(self.data.Length)); }