private static GraceObject mAsString(EvaluationContext ctx, MethodRequest req, CodepointObject self) { return(GraceString.Create("U+" + self.codepoint.ToString("X4") + " " + self.parts[0])); }
private NativeBlock_1d(Action <GraceObject> act) { action = act; AddMethod("apply(_)", null); AddMethod("asString", null); stringification = GraceString.Create("{ _ -> native code }"); }
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))); }
/// <inheritdoc/> public override GraceObject Respond( EvaluationContext ctx, GraceObject self, MethodRequest req ) { checkAccessibility(ctx, req); var arg = req[1].Arguments[0]; if (Type != null) { if (Matching.TryMatch(ctx, Type, arg, out GraceObject result)) { arg = result; } else { ErrorReporting.RaiseError(ctx, "R2025", new Dictionary <string, string> { { "field", req[0].Name }, { "required", GraceString.AsNativeString(ctx, Type) } }, "TypeError: ${field} can only hold ${required}."); } } var tmp = cell.Value; cell.Value = arg; return(tmp); }
private GraceBlock(Action <GraceObject> act) { AddMethod("apply(_)", null); AddMethod("spawn", null); AddMethod("asString", null); stringification = GraceString.Create("{ _ -> native code }"); }
private static GraceObject substringFromTo( EvaluationContext ctx, MethodRequest req, GraceString self ) { MethodHelper.CheckArity(ctx, req, 1, 1); // Index of first grapheme to include. var start = req[0].Arguments[0]; // Index of last grapheme to include. var end = req[1].Arguments[0]; var st = start.FindNativeParent <GraceNumber>(); if (st == null) { ErrorReporting.RaiseError(ctx, "R2001", new Dictionary <string, string> { { "method", req.Name }, { "index", "1" }, { "part", "substringFrom" } }, "Start must be a number"); } var en = end.FindNativeParent <GraceNumber>(); if (en == null) { ErrorReporting.RaiseError(ctx, "R2001", new Dictionary <string, string> { { "method", req.Name }, { "index", "1" }, { "part", "to" } }, "End must be a number"); } // Because, e.g., substringFrom(1) to(1) should return the // first grapheme, the start value must be adjusted for // base-one indexing, but the end value must not be. int stInd = st.GetInt() - 1; int enInd = en.GetInt(); if (stInd < 0) { stInd = 0; } if (enInd < 0) { enInd = 0; } if (enInd >= self.graphemeIndices.Length) { enInd = self.graphemeIndices.Length; } int endIndex = enInd < self.graphemeIndices.Length ? self.graphemeIndices[enInd] : self.Value.Length; stInd = self.graphemeIndices[stInd]; return(GraceString.Create(self.Value.Substring(stInd, endIndex - stInd))); }
/// <summary>Native method for Grace asString</summary> public GraceObject AsString() { if (Boolean) { return(GraceString.Create("true")); } return(GraceString.Create("false")); }
private static GraceObject mCodepoints(GraceString self) { if (self.codepointsObject == null) { self.codepointsObject = new StringCodepoints(self.Value); } return(self.codepointsObject); }
private static GraceObject mMatch( EvaluationContext ctx, GraceString self, GraceObject target) { return((mEqualsEquals(self, target) == GraceBoolean.True) ? Matching.SuccessfulMatch(ctx, target) : Matching.FailedMatch(ctx, target)); }
private static GraceObject mNotEquals( GraceString self, GraceObject other ) { var oth = other.FindNativeParent <GraceString>(); return((oth == null) ? GraceBoolean.True : GraceBoolean.Create(self.nfc != oth.nfc)); }
private static GraceObject mGreaterThanEqual( GraceString self, GraceObject other ) { var oth = other.FindNativeParent <GraceString>(); return((oth == null) ? GraceBoolean.False : GraceBoolean.Create( compare(self, oth) >= 0 )); }
/// <summary> /// Compare two GraceStrings, respecting grapheme cluster /// boundaries, using the fully-decomposed version of the /// string. /// </summary> private static int compare(GraceString a, GraceString b) { if (a.nfc == b.nfc) { return(0); } var ad = a.decomposedGraphemeClusters; var bd = b.decomposedGraphemeClusters; for (int i = 0, j = 0; i < ad.Length && j < bd.Length; i++, j++) { var ga = ad[i]; var gb = bd[i]; var len = ga.Length < gb.Length ? ga.Length : gb.Length; for (int k = 0; k < len; k++) { if (ga[k] < gb[k]) { return(-1); } if (ga[k] > gb[k]) { return(1); } } // If the clusters are the same as far as they go, // but one is longer, it comes afterwards. if (ga.Length > gb.Length) { return(1); } if (gb.Length > ga.Length) { return(-1); } } // If the strings are the same as far as they go, but // one is longer, it comes afterwards. if (ad.Length > bd.Length) { return(1); } if (bd.Length > ad.Length) { return(-1); } return(0); }
/// <summary> /// Attempt to match a pattern, raising R2025 TypeError on failure. /// </summary> /// <param name="ctx">Current interpreter</param> /// <param name="pattern">Pattern to match against</param> /// <param name="target">Object to examine</param> /// <param name="name">Name to report in error (e.g. field or parameter name)</param> /// <returns></returns> public static GraceObject TypeMatch(EvaluationContext ctx, GraceObject pattern, GraceObject target, string name) { if (Matching.TryMatch(ctx, pattern, target, out var result)) { return(result); } else { ErrorReporting.RaiseError(ctx, "R2025", new Dictionary <string, string> { { "field", name }, { "required", GraceString.AsNativeString(ctx, pattern) } }, "TypeError: argument type mismatch"); return(null); } }
private static GraceObject mDo( EvaluationContext ctx, GraceString self, GraceObject blk ) { var req = MethodRequest.Single("apply", null); for (var i = 0; i < self.graphemeIndices.Length; i++) { int start = self.graphemeIndices[i]; string c = StringInfo.GetNextTextElement(self.Value, start); req[0].Arguments[0] = GraceString.Create(c); blk.Request(ctx, req); } return(GraceObject.Done); }
/// <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 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)); }
private GraceObject mAsString(EvaluationContext ctx) { if (stringification != null) { return(stringification); } var p = ParseNodeMeta.PrettyPrint(ctx, node.Origin); if (p.LastIndexOf(Environment.NewLine) != -1 && node.Body.Count != 1) { var last = "..."; if (node.Body.Count > 0) { last = "... " + ParseNodeMeta.PrettyPrint(ctx, node.Body.Last().Origin); } var bpn = node.Origin as BlockParseNode; if (bpn == null) { p = String.Join(", ", from x in parameters select ParseNodeMeta.PrettyPrint(ctx, x.Origin)) + " -> " + last; } else { p = String.Join(", ", from x in bpn.Parameters select ParseNodeMeta.PrettyPrint(ctx, x)) + " -> " + last; } } stringification = GraceString.Create("Block[" + p + "]"); return(stringification); }
private GraceObject mAsString(EvaluationContext ctx) { return(GraceString.Create(GraceString.AsNativeString(ctx, lhs) + " | " + GraceString.AsNativeString(ctx, rhs))); }
private static GraceObject mString(EvaluationContext ctx, MethodRequest req, CodepointObject self) { return(GraceString.Create(Char.ConvertFromUtf32(self.codepoint))); }
private static GraceObject mAsString(GraceString self) { return(self); }
private static GraceObject mBidirectional(EvaluationContext ctx, MethodRequest req, CodepointObject self) { return(GraceString.Create(self.parts[3])); }
/// <summary>Native method for Grace asString</summary> private static GraceObject mAsString(GraceNumber self) { return(GraceString.Create("" + self.Value)); }
private static GraceObject mString(EvaluationContext ctx, MethodRequest req, StringCodepoints self) { return(GraceString.Create(makeString(self))); }
/// <summary>Native method for Grace message</summary> public GraceObject Message() { return(GraceString.Create(message)); }
/// <inheritdoc/> public GraceObject AsString(EvaluationContext ctx) { return(GraceString.Create(Name)); }
/// <summary>Native method for Grace asString</summary> new public GraceObject AsString(EvaluationContext ctx, GraceObject self) { return(GraceString.Create(KindName + ": " + message)); }
/// <summary>Native method supporting Grace AsString</summary> public virtual GraceObject AsString(EvaluationContext ctx, GraceObject self) { return(GraceString.Create(self.ToString())); }
private GraceObject mAsString(EvaluationContext ctx) { return(GraceString.Create("Range[" + _low + " .. " + _high + (_step != 1 ? " .. " + _step : "") + "]")); }
private GraceObject mAsString() { return(GraceString.Create(desc)); }
private GraceObject mAsString(EvaluationContext ctx) { return(GraceString.Create((Success ? "Successful" : "Failed") + "Match[" + GraceString.AsNativeString(ctx, Result) + "]")); }