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)); }
/// <summary> /// Request a method of this object in a given /// context with a particular receiver identity /// </summary> /// <param name="ctx">Current interpreter</param> /// <param name="req">Request</param> /// <param name="receiver">Receiver identity</param> /// <returns>Return value of the resolved method</returns> public virtual GraceObject Request(EvaluationContext ctx, MethodRequest req, GraceObject receiver) { var m = FindMethod(req.Name); if (m == null) { ErrorReporting.RaiseError(ctx, "R2000", new Dictionary <string, string> { { "method", req.Name }, { "receiver", ToString() } }, "LookupError: Method «" + req.Name + "» not found."); } var ret = m.Respond(ctx, receiver, req); return(ret); }
/// <summary>Native method for Grace ifTrue ifFalse</summary> /// <param name="ctx">Current interpreter</param> /// <param name="req">Method request that gave rise to this method /// execution</param> public GraceObject IfTrueIfFalse(EvaluationContext ctx, MethodRequest req) { MethodHelper.CheckArity(ctx, req, 1, 1); var trueBlock = req[0].Arguments[0]; var falseBlock = req[1].Arguments[0]; if (!(trueBlock is GraceBlock)) { ErrorReporting.RaiseError(ctx, "R2001", new Dictionary <string, string>() { { "method", req.Name }, { "index", "1" }, { "part", "ifTrue" }, { "required", "Block" } }, "ArgumentTypeError: ifTrue ifFalse requires two block arguments" ); } if (!(falseBlock is GraceBlock)) { ErrorReporting.RaiseError(ctx, "R2001", new Dictionary <string, string>() { { "method", req.Name }, { "index", "1" }, { "part", "ifFalse" }, { "required", "Block" } }, "ArgumentTypeError: ifTrue ifFalse requires two block arguments" ); } var apply = MethodRequest.Nullary("apply"); if (Boolean) { return(trueBlock.Request(ctx, apply)); } return(falseBlock.Request(ctx, apply)); }
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 static GraceObject mAt(EvaluationContext ctx, MethodRequest req, ByteString 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.data.Length) { ErrorReporting.RaiseError(ctx, "R2013", new Dictionary <string, string> { { "index", "" + (idx + 1) }, { "valid", self.data.Length > 0 ? "1 .. " + self.data.Length : "none (empty)" } }, "IndexError: Index out of range"); } return(getByteObject(self.data[idx])); }
private GraceObject apply(EvaluationContext ctx, GraceObject arg) { if (index >= _elements.Count) { return(GraceObject.Done); } var el = _elements[index++]; var req = new MethodRequest(); var rpn = new RequestPart("apply", new List <GraceObject>(), new List <GraceObject>() { el, arg } ); req.AddPart(rpn); _block.Request(ctx, req); return(GraceObject.Done); }
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])); }
/// <inheritdoc/> public override GraceObject Respond( EvaluationContext ctx, GraceObject self, MethodRequest req ) { checkAccessibility(ctx, req); MethodHelper.CheckNoInherits(ctx, req); MethodNode.CheckArgCount(ctx, req.Name, req.Name, 0, false, req[0].Arguments.Count); if (cell.Value == GraceObject.Uninitialised) { ErrorReporting.RaiseError(ctx, "R2008", new Dictionary <string, string> { { "name", req.Name }, { "receiver", self.ToString() } }, "UninitialisedReadError: Cannot read from " + req.Name ); } return(cell.Value); }
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); }
private static GraceObject mConcat(EvaluationContext ctx, MethodRequest req, ByteString self) { MethodHelper.CheckArity(ctx, req, 1); var oth = req[0].Arguments[0].FindNativeParent <ByteString>(); if (oth == null) { ErrorReporting.RaiseError(ctx, "R2001", new Dictionary <string, string> { { "method", req.Name }, { "index", "1" }, { "part", req.Name }, { "required", "byte string" }, }, "ArgumentTypeError: Needed byte string"); } var d2 = new byte[self.data.Length + oth.data.Length]; self.data.CopyTo(d2, 0); oth.data.CopyTo(d2, self.data.Length); return(new ByteString(d2)); }
/// <inheritdoc/> /// <remarks>This method uses the indexer on the LocalScope /// object the method was requested on.</remarks> public override GraceObject Respond(EvaluationContext ctx, GraceObject self, MethodRequest req) { checkAccessibility(ctx, req); MethodHelper.CheckNoInherits(ctx, req); MethodNode.CheckArgCount(ctx, req.Name, req.Name, 0, false, req[0].Arguments.Count); LocalScope s = self as LocalScope; string name = req.Name; if (s[name] == GraceObject.Uninitialised || s[name] == null) { ErrorReporting.RaiseError(ctx, "R2008", new Dictionary <string, string> { { "name", name }, { "receiver", ToString() } }, "UninitialisedReadError: Cannot read from «" + name + "»" ); } return(s[name]); }
/// <summary>Native method for Grace orElse</summary> /// <param name="ctx">Current interpreter</param> /// <param name="other">Block to apply if false</param> public GraceObject OrElse(EvaluationContext ctx, GraceObject other) { var oth = other as GraceBlock; if (oth == null) { ErrorReporting.RaiseError(ctx, "R2001", new Dictionary <string, string>() { { "method", "orElse" }, { "index", "1" }, { "part", "ifFalse" }, { "required", "Block" } }, "ArgumentTypeError: orElse requires a block argument" ); } if (!Boolean) { var req = MethodRequest.Nullary("apply"); return(other.Request(ctx, req)); } return(True); }
private static GraceObject mString(EvaluationContext ctx, MethodRequest req, CodepointObject self) { return(GraceString.Create(Char.ConvertFromUtf32(self.codepoint))); }
private static GraceObject mMirrored(EvaluationContext ctx, MethodRequest req, CodepointObject self) { return(GraceBoolean.Create(self.parts[8] == "Y")); }
private static GraceObject mTitlecase(EvaluationContext ctx, MethodRequest req, CodepointObject self) { return(mapCodepointSequenceString(self.parts[13])); }
private static GraceObject mDecomposition(EvaluationContext ctx, MethodRequest req, CodepointObject self) { return(mapCodepointSequenceString(self.parts[4])); }
/// <summary>Request a method of this object in a given /// context</summary> /// <param name="ctx">Current interpreter</param> /// <param name="req">Request</param> /// <returns>Return value of the resolved method</returns> public virtual GraceObject Request(EvaluationContext ctx, MethodRequest req) { return(Request(ctx, req, this.Identity)); }
private static GraceObject mCombining(EvaluationContext ctx, MethodRequest req, CodepointObject self) { return(GraceNumber.Create(int.Parse(self.parts[2]))); }
private static GraceObject mBidirectional(EvaluationContext ctx, MethodRequest req, CodepointObject self) { return(GraceString.Create(self.parts[3])); }
/// <inheritdoc/> public override GraceObject Request(EvaluationContext ctx, MethodRequest req, GraceObject receiver) { return(target.Request(ctx, req, target)); }
/// <summary>Make a request block</summary> /// <param name="ctx">Current interpreter</param> /// <param name="receiver">Receiver to dispatch the method on</param> /// <param name="req">Request to send</param> public static GraceRequestBlock Create(EvaluationContext ctx, GraceObject receiver, MethodRequest req) { return(new GraceRequestBlock(ctx, receiver, req)); }
/// <summary>Determine whether MatchResult failed</summary> /// <param name="ctx">Current interpreter</param> /// <param name="matchResult">MatchResult to examine</param> public static bool Failed(EvaluationContext ctx, GraceObject matchResult) { return(GraceBoolean.False == matchResult.Request(ctx, MethodRequest.Nullary("succeeded"))); }
private static GraceObject mLEndian(EvaluationContext ctx, MethodRequest req, UTF32CodepointsView self) { return(new ByteString(Encoding.UTF32.GetBytes(self.stringData))); }
/// <inheritsdoc/> public override bool RespondsTo(MethodRequest req) { return(redirectedMethods.Contains(req.Name)); }
/// <summary>Get the result of a MatchResult</summary> /// <param name="ctx">Current interpreter</param> /// <param name="matchResult">MatchResult to examine</param> public static GraceObject GetResult(EvaluationContext ctx, GraceObject matchResult) { return(matchResult.Request(ctx, MethodRequest.Nullary("result"))); }
private static GraceObject mSize(EvaluationContext ctx, MethodRequest req, StringCodepoints self) { return(GraceNumber.Create(self.utf32.Count)); }
/// <summary>Native method representing the apply method</summary> /// <param name="ctx">Current interpreter</param> /// <param name="req">Request that obtained this method</param> public GraceObject Apply(EvaluationContext ctx, MethodRequest req) { GraceObject ret = GraceObject.Done; MethodNode.CheckArgCount(ctx, "apply", "apply", parameters.Count, Variadic, req[0].Arguments.Count); ctx.Remember(lexicalScope); var myScope = new LocalScope(req.Name); // Bind any local methods (types) on the scope foreach (var localMeth in body.OfType <MethodNode>()) { myScope.AddMethod(localMeth.Name, new Method(localMeth, lexicalScope)); } // Bind parameters and arguments foreach (var arg in parameters.Zip(req[0].Arguments, (a, b) => new { name = a, val = b })) { var id = arg.name as ParameterNode; if (id != null && id.Variadic) { // Populate variadic parameter with all remaining // arguments. var gvl = new GraceVariadicList(); for (var i = parameters.Count - 1; i < req[0].Arguments.Count; i++) { gvl.Add(req[0].Arguments[i]); } myScope.AddLocalDef(id.Name, gvl); } else { string name = ((IdentifierNode)arg.name).Name; var t = id?.Type?.Evaluate(ctx); var result = Matching.TypeMatch(ctx, t, arg.val, name); var p = myScope.AddLocalVar(name, result); p.Write.Type = t; } } if (Variadic && parameters.Count > req[0].Arguments.Count) { // Empty variadic parameter. var param = parameters.Last(); var idNode = param as ParameterNode; if (idNode != null && idNode.Variadic) { var gvl = new GraceVariadicList(); myScope.AddLocalDef(idNode.Name, gvl); } } ctx.Extend(myScope); foreach (Node n in body) { ret = n.Evaluate(ctx); } ctx.Unextend(myScope); ctx.Forget(lexicalScope); return(ret); }
private static GraceObject mString(EvaluationContext ctx, MethodRequest req, StringCodepoints self) { return(GraceString.Create(makeString(self))); }
/// <summary>Native method for the Grace apply method</summary> /// <param name="ctx">Current interpreter</param> /// <param name="req">Request that reached this method</param> public GraceObject Apply(EvaluationContext ctx, MethodRequest req) { return(receiver.Request(ctx, request)); }
private static GraceObject mCodepoint(EvaluationContext ctx, MethodRequest req, CodepointObject self) { return(GraceNumber.Create(self.codepoint)); }