本文整理汇总了C#中Ioke.Lang.IokeObject.Mimics方法的典型用法代码示例。如果您正苦于以下问题:C# IokeObject.Mimics方法的具体用法?C# IokeObject.Mimics怎么用?C# IokeObject.Mimics使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Ioke.Lang.IokeObject
的用法示例。
在下文中一共展示了IokeObject.Mimics方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Init
public override void Init(IokeObject obj)
{
Runtime runtime = obj.runtime;
obj.Kind = "Pair";
obj.Mimics(IokeObject.As(runtime.Mixins.GetCell(null, null, "Enumerable"), null), runtime.nul, runtime.nul);
obj.Mimics(IokeObject.As(runtime.Mixins.GetCell(null, null, "Comparing"), null), runtime.nul, runtime.nul);
obj.RegisterMethod(runtime.NewNativeMethod("returns true if the left hand side pair is equal to the right hand side pair.",
new TypeCheckingNativeMethod("==", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(runtime.Pair)
.WithRequiredPositional("other")
.Arguments,
(method, on, args, keywords, context, message) => {
Pair d = (Pair)IokeObject.dataOf(on);
object other = args[0];
return ((other is IokeObject) &&
(IokeObject.dataOf(other) is Pair)
&& d.first.Equals(((Pair)IokeObject.dataOf(other)).first)
&& d.second.Equals(((Pair)IokeObject.dataOf(other)).second)) ? context.runtime.True : context.runtime.False;
})));
obj.RegisterMethod(runtime.NewNativeMethod("Returns the first value",
new TypeCheckingNativeMethod.WithNoArguments("first", obj,
(method, on, args, keywords, context, message) => {
return ((Pair)IokeObject.dataOf(on)).first;
})));
obj.RegisterMethod(runtime.NewNativeMethod("Returns the first value",
new TypeCheckingNativeMethod.WithNoArguments("key", obj,
(method, on, args, keywords, context, message) => {
return ((Pair)IokeObject.dataOf(on)).first;
})));
obj.RegisterMethod(runtime.NewNativeMethod("Returns the second value",
new TypeCheckingNativeMethod.WithNoArguments("second", obj,
(method, on, args, keywords, context, message) => {
return ((Pair)IokeObject.dataOf(on)).second;
})));
obj.RegisterMethod(runtime.NewNativeMethod("Returns the second value",
new TypeCheckingNativeMethod.WithNoArguments("value", obj,
(method, on, args, keywords, context, message) => {
return ((Pair)IokeObject.dataOf(on)).second;
})));
obj.RegisterMethod(runtime.NewNativeMethod("Returns a text inspection of the object",
new TypeCheckingNativeMethod.WithNoArguments("inspect", obj,
(method, on, args, keywords, context, message) => {
return method.runtime.NewText(Pair.GetInspect(on));
})));
obj.RegisterMethod(runtime.NewNativeMethod("Returns a brief text inspection of the object",
new TypeCheckingNativeMethod.WithNoArguments("notice", obj,
(method, on, args, keywords, context, message) => {
return method.runtime.NewText(Pair.GetNotice(on));
})));
}
示例2: Init
public override void Init(IokeObject obj)
{
Runtime runtime = obj.runtime;
obj.Kind = "Pair";
obj.Mimics(IokeObject.As(runtime.Mixins.GetCell(null, null, "Enumerable"), null), runtime.nul, runtime.nul);
obj.Mimics(IokeObject.As(runtime.Mixins.GetCell(null, null, "Comparing"), null), runtime.nul, runtime.nul);
obj.RegisterMethod(runtime.NewNativeMethod("Returns the first value",
new TypeCheckingNativeMethod.WithNoArguments("first", obj,
(method, on, args, keywords, context, message) => {
return ((Pair)IokeObject.dataOf(on)).first;
})));
obj.RegisterMethod(runtime.NewNativeMethod("Returns the first value",
new TypeCheckingNativeMethod.WithNoArguments("key", obj,
(method, on, args, keywords, context, message) => {
return ((Pair)IokeObject.dataOf(on)).first;
})));
obj.RegisterMethod(runtime.NewNativeMethod("Returns the second value",
new TypeCheckingNativeMethod.WithNoArguments("second", obj,
(method, on, args, keywords, context, message) => {
return ((Pair)IokeObject.dataOf(on)).second;
})));
obj.RegisterMethod(runtime.NewNativeMethod("Returns the second value",
new TypeCheckingNativeMethod.WithNoArguments("value", obj,
(method, on, args, keywords, context, message) => {
return ((Pair)IokeObject.dataOf(on)).second;
})));
obj.RegisterMethod(runtime.NewNativeMethod("Returns a text inspection of the object",
new TypeCheckingNativeMethod.WithNoArguments("inspect", obj,
(method, on, args, keywords, context, message) => {
return method.runtime.NewText(Pair.GetInspect(on));
})));
obj.RegisterMethod(runtime.NewNativeMethod("Returns a brief text inspection of the object",
new TypeCheckingNativeMethod.WithNoArguments("notice", obj,
(method, on, args, keywords, context, message) => {
return method.runtime.NewText(Pair.GetNotice(on));
})));
}
示例3: Init
public override void Init(IokeObject obj)
{
Runtime runtime = obj.runtime;
obj.Kind = "List";
obj.Mimics(IokeObject.As(IokeObject.FindCell(runtime.Mixins, "Sequenced"), null), runtime.nul, runtime.nul);
obj.RegisterMethod(obj.runtime.NewNativeMethod("returns a hash for the list",
new NativeMethod.WithNoArguments("hash", (method, context, message, on, outer) => {
outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);
return context.runtime.NewNumber(((IokeList)IokeObject.dataOf(on)).list.GetHashCode());
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns true if the left hand side list is equal to the right hand side list.",
new TypeCheckingNativeMethod("==", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(runtime.List)
.WithRequiredPositional("other")
.Arguments,
(method, on, args, keywords, context, message) => {
IokeList d = (IokeList)IokeObject.dataOf(on);
object other = args[0];
return ((other is IokeObject) &&
(IokeObject.dataOf(other) is IokeList) &&
d.list.Equals(((IokeList)IokeObject.dataOf(other)).list)) ? context.runtime.True : context.runtime.False;
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns a new sequence to iterate over this list",
new TypeCheckingNativeMethod.WithNoArguments("seq", runtime.List,
(method, on, args, keywords, context, message) => {
IokeObject ob = method.runtime.IteratorSequence.AllocateCopy(null, null);
ob.MimicsWithoutCheck(method.runtime.IteratorSequence);
ob.Data = new Sequence.IteratorSequence(IokeList.GetList(on).GetEnumerator());
return ob;
})));
obj.RegisterMethod(runtime.NewNativeMethod("takes either one or two or three arguments. if one argument is given, it should be a message chain that will be sent to each object in the list. the result will be thrown away. if two arguments are given, the first is an unevaluated name that will be set to each of the values in the list in succession, and then the second argument will be evaluated in a scope with that argument in it. if three arguments is given, the first one is an unevaluated name that will be set to the index of each element, and the other two arguments are the name of the argument for the value, and the actual code. the code will evaluate in a lexical context, and if the argument name is available outside the context, it will be shadowed. the method will return the list.",
new NativeMethod("each", DefaultArgumentsDefinition.builder()
.WithOptionalPositionalUnevaluated("indexOrArgOrCode")
.WithOptionalPositionalUnevaluated("argOrCode")
.WithOptionalPositionalUnevaluated("code")
.Arguments,
(method, context, message, on, outer) => {
outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);
object onAsList = context.runtime.List.ConvertToThis(on, message, context);
var ls = ((IokeList)IokeObject.dataOf(onAsList)).list;
switch(message.Arguments.Count) {
case 0: {
return Interpreter.Send(runtime.seqMessage, context, on);
}
case 1: {
IokeObject code = IokeObject.As(message.Arguments[0], context);
foreach(object o in ls) {
context.runtime.interpreter.Evaluate(code, context, context.RealContext, o);
}
break;
}
case 2: {
IokeObject c = context.runtime.NewLexicalContext(context, "Lexical activation context for List#each", context);
string name = IokeObject.As(message.Arguments[0], context).Name;
IokeObject code = IokeObject.As(message.Arguments[1], context);
foreach(object o in ls) {
c.SetCell(name, o);
context.runtime.interpreter.Evaluate(code, c, c.RealContext, c);
}
break;
}
case 3: {
IokeObject c = context.runtime.NewLexicalContext(context, "Lexical activation context for List#each", context);
string iname = IokeObject.As(message.Arguments[0], context).Name;
string name = IokeObject.As(message.Arguments[1], context).Name;
IokeObject code = IokeObject.As(message.Arguments[2], context);
int index = 0;
foreach(object o in ls) {
c.SetCell(name, o);
c.SetCell(iname, runtime.NewNumber(index++));
context.runtime.interpreter.Evaluate(code, c, c.RealContext, c);
}
break;
}
}
return onAsList;
})));
obj.RegisterMethod(runtime.NewNativeMethod("takes one argument, the index of the element to be returned. can be negative, and will in that case return indexed from the back of the list. if the index is outside the bounds of the list, will return nil. the argument can also be a range, and will in that case interpret the first index as where to start, and the second the end. the end can be negative and will in that case be from the end. if the first argument is negative, or after the second, an empty list will be returned. if the end point is larger than the list, the size of the list will be used as the end point.",
new TypeCheckingNativeMethod("at", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(runtime.List)
.WithRequiredPositional("index")
.Arguments,
(method, on, args, keywords, context, message) => {
object arg = args[0];
if(IokeObject.dataOf(arg) is Range) {
int first = Number.ExtractInt(Range.GetFrom(arg), message, context);
if(first < 0) {
return context.runtime.NewList(new SaneArrayList());
//.........这里部分代码省略.........
示例4: Init
public override void Init(IokeObject obj)
{
Runtime runtime = obj.runtime;
obj.Kind = "Range";
obj.Mimics(IokeObject.As(IokeObject.FindCell(runtime.Mixins, "Sequenced"), null), runtime.nul, runtime.nul);
obj.RegisterMethod(runtime.NewNativeMethod("returns true if the left hand side range is equal to the right hand side range.",
new TypeCheckingNativeMethod("==", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(runtime.Range)
.WithRequiredPositional("other")
.Arguments,
(method, on, args, keywords, context, message) => {
Range d = (Range)IokeObject.dataOf(on);
object other = args[0];
return ((other is IokeObject) &&
(IokeObject.dataOf(other) is Range)
&& d.inclusive == ((Range)IokeObject.dataOf(other)).inclusive
&& d.from.Equals(((Range)IokeObject.dataOf(other)).from)
&& d.to.Equals(((Range)IokeObject.dataOf(other)).to)) ? context.runtime.True : context.runtime.False;
})));
obj.RegisterMethod(runtime.NewNativeMethod("will return a new inclusive Range based on the two arguments",
new NativeMethod("inclusive", DefaultArgumentsDefinition.builder()
.WithRequiredPositional("from")
.WithRequiredPositional("to")
.Arguments,
(method, context, message, on, outer) => {
var args = new SaneArrayList();
outer.ArgumentsDefinition.GetEvaluatedArguments(context, message, on, args, new SaneDictionary<string, object>());
object from = args[0];
object to = args[1];
bool comparing = IokeObject.IsMimic(from, IokeObject.As(context.runtime.Mixins.body.Get("Comparing"), context), context);
bool inverted = false;
if(comparing) {
object result = Interpreter.Send(context.runtime.spaceShipMessage, context, from, to);
if(result != context.runtime.nil && Number.ExtractInt(result, message, context) == 1) {
inverted = true;
}
}
return runtime.NewRange(IokeObject.As(from, context), IokeObject.As(to, context), true, inverted);
})));
obj.RegisterMethod(runtime.NewNativeMethod("will return a new exclusive Range based on the two arguments",
new NativeMethod("exclusive", DefaultArgumentsDefinition.builder()
.WithRequiredPositional("from")
.WithRequiredPositional("to")
.Arguments,
(method, context, message, on, outer) => {
var args = new SaneArrayList();
outer.ArgumentsDefinition.GetEvaluatedArguments(context, message, on, args, new SaneDictionary<string, object>());
object from = args[0];
object to = args[1];
bool comparing = IokeObject.IsMimic(from, IokeObject.As(context.runtime.Mixins.body.Get("Comparing"), context), context);
bool inverted = false;
if(comparing) {
object result = Interpreter.Send(context.runtime.spaceShipMessage, context, from, to);
if(result != context.runtime.nil && Number.ExtractInt(result, message, context) == 1) {
inverted = true;
}
}
return runtime.NewRange(IokeObject.As(from, context), IokeObject.As(to, context), false, inverted);
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns true if the receiver is an exclusive range, false otherwise",
new NativeMethod.WithNoArguments("exclusive?",
(method, context, message, on, outer) => {
outer.ArgumentsDefinition.GetEvaluatedArguments(context, message, on, new SaneArrayList(), new SaneDictionary<string, object>());
return ((Range)IokeObject.dataOf(on)).inclusive ? context.runtime.False : context.runtime.True;
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns true if the receiver is an inclusive range, false otherwise",
new NativeMethod.WithNoArguments("inclusive?",
(method, context, message, on, outer) => {
outer.ArgumentsDefinition.GetEvaluatedArguments(context, message, on, new SaneArrayList(), new SaneDictionary<string, object>());
return ((Range)IokeObject.dataOf(on)).inclusive ? context.runtime.True : context.runtime.False;
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns the 'from' part of the range",
new TypeCheckingNativeMethod.WithNoArguments("from", obj,
(method, on, args, keywords, context, message) => {
return ((Range)IokeObject.dataOf(on)).from;
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns the 'to' part of the range",
new NativeMethod.WithNoArguments("to",
(method, context, message, on, outer) => {
outer.ArgumentsDefinition.GetEvaluatedArguments(context, message, on, new SaneArrayList(), new SaneDictionary<string, object>());
return ((Range)IokeObject.dataOf(on)).to;
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns a new sequence to iterate over this range",
//.........这里部分代码省略.........
示例5: Init
public override void Init(IokeObject obj)
{
obj.Kind = "Message";
obj.Mimics(IokeObject.As(IokeObject.FindCell(obj.runtime.Mixins, "Enumerable"), null), obj.runtime.nul, obj.runtime.nul);
obj.RegisterMethod(obj.runtime.NewNativeMethod("Takes one or more evaluated arguments and sends this message chain to where the first argument is ground, and if there are more arguments, the second is the receiver, and the rest will be the arguments",
new NativeMethod("evaluateOn", DefaultArgumentsDefinition.builder()
.WithRequiredPositional("ground")
.WithOptionalPositional("receiver", "ground")
.WithRest("arguments")
.Arguments,
(method, on, args, keywords, context, message) => {
IokeObject messageGround = IokeObject.As(args[0], context);
IokeObject receiver = messageGround;
int size = args.Count;
if(size > 1) {
receiver = IokeObject.As(args[1], context);
if(size > 2) {
IokeObject m = IokeObject.As(on, context).AllocateCopy(IokeObject.As(on, context), context);
m.Arguments.Clear();
for(int ix=2;ix<size;ix++) {
m.Arguments.Add(args[ix]);
}
on = m;
}
}
IokeObject msg = IokeObject.As(on, context);
return context.runtime.interpreter.Evaluate(msg, messageGround, messageGround, receiver);
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("returns a deep clone of this message chain, starting at the current point.",
new TypeCheckingNativeMethod.WithNoArguments("deepCopy", obj,
(method, on, args, keywords, context, message) => {
return Message.DeepCopy(on);
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("Returns a code representation of the object",
new TypeCheckingNativeMethod.WithNoArguments("code", obj,
(method, on, args, keywords, context, message) => {
return method.runtime.NewText(((Message)IokeObject.dataOf(on)).Code());
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("Returns the unevaluated arguments for this message",
new TypeCheckingNativeMethod.WithNoArguments("arguments", obj,
(method, on, args, keywords, context, message) => {
return context.runtime.NewList(((Message)IokeObject.dataOf(on)).arguments);
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("Returns a formatted code representation of the object",
new TypeCheckingNativeMethod.WithNoArguments("formattedCode", obj,
(method, on, args, keywords, context, message) => {
return method.runtime.NewText(Message.FormattedCode(IokeObject.As(on, context), 0, context));
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("returns the name of this message",
new TypeCheckingNativeMethod.WithNoArguments("name", obj,
(method, on, args, keywords, context, message) => {
return method.runtime.GetSymbol(((Message)IokeObject.dataOf(on)).name);
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("sets the name of the message and then returns that name",
new TypeCheckingNativeMethod("name=", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRequiredPositional("newName")
.Arguments,
(method, on, args, keywords, context, message) => {
object o = args[0];
string name = null;
if(IokeObject.dataOf(o) is Symbol) {
name = Symbol.GetText(o);
} else if(IokeObject.dataOf(o) is Text) {
name = Text.GetText(o);
} else {
name = Text.GetText(IokeObject.ConvertToText(o, message, context, true));
}
Message.SetName(IokeObject.As(on, context), name);
return o;
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("sets the next pointer of the message and then returns that pointer",
new TypeCheckingNativeMethod("next=", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRequiredPositional("newNext")
.Arguments,
(method, on, args, keywords, context, message) => {
object o = args[0];
if(o == context.runtime.nil) {
Message.SetNext(IokeObject.As(on, context), null);
} else {
o = context.runtime.Message.ConvertToThis(o, message, context);
Message.SetNext(IokeObject.As(on, context), IokeObject.As(o, context));
}
return o;
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("sets the prev pointer of the message and then returns that pointer",
new TypeCheckingNativeMethod("prev=", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
//.........这里部分代码省略.........
示例6: Init
public override void Init(IokeObject obj)
{
Runtime runtime = obj.runtime;
obj.Kind = "Dict";
obj.Mimics(IokeObject.As(runtime.Mixins.GetCell(null, null, "Enumerable"), null), runtime.nul, runtime.nul);
obj.RegisterMethod(runtime.NewNativeMethod("takes one argument, that should be a default value, and returns a new mimic of the receiver, with the default value for that new dict set to the argument",
new TypeCheckingNativeMethod("withDefault", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRequiredPositional("defaultValue")
.Arguments,
(method, on, args, keywords, context, message) => {
object newDict = IokeObject.Mimic(on, message, context);
SetDefaultValue(newDict, IokeObject.As(args[0], context));
return newDict;
})));
obj.RegisterMethod(runtime.NewNativeMethod("creates a new Dict from the arguments provided, combined with the values in the receiver. the arguments provided will override those in the receiver. the rules for arguments are the same as for dict, except that dicts can also be provided. all positional arguments will be added before the keyword arguments.",
new TypeCheckingNativeMethod("merge", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRest("pairsAndDicts")
.WithKeywordRest("keywordPairs")
.Arguments,
(method, on, args, keywords, context, message) => {
var newMap = new SaneHashtable();
foreach(DictionaryEntry de in GetMap(on)) newMap[de.Key] = de.Value;
foreach(object o in args) {
if(IokeObject.dataOf(o) is Dict) {
foreach(DictionaryEntry de in GetMap(o)) newMap[de.Key] = de.Value;
} else if(IokeObject.dataOf(o) is Pair) {
newMap[Pair.GetFirst(o)] = Pair.GetSecond(o);
} else {
newMap[o] = context.runtime.nil;
}
}
foreach(var entry in keywords) {
string s = entry.Key;
object key = context.runtime.GetSymbol(s.Substring(0, s.Length-1));
object value = entry.Value;
if(value == null) {
value = context.runtime.nil;
}
newMap[key] = value;
}
return context.runtime.NewDict(newMap);
})));
obj.AliasMethod("merge", "+", null, null);
obj.RegisterMethod(runtime.NewNativeMethod("takes one argument, the key of the element to return. if the key doesn't map to anything in the dict, returns the default value",
new TypeCheckingNativeMethod("at", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRequiredPositional("key")
.Arguments,
(method, on, args, keywords, context, message) => {
object result = Dict.GetMap(on)[args[0]];
if(result == null) {
return GetDefaultValue(on, context, message);
} else {
return result;
}
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns true if this dict is empty, false otherwise",
new TypeCheckingNativeMethod.WithNoArguments("empty?", obj,
(method, on, args, keywords, context, message) => {
return Dict.GetMap(on).Count == 0 ? context.runtime.True : context.runtime.False;
})));
obj.RegisterMethod(runtime.NewNativeMethod("takes one argument, the key to check if it is in the dict.",
new TypeCheckingNativeMethod("key?", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRequiredPositional("key")
.Arguments,
(method, on, args, keywords, context, message) => {
return (Dict.GetMap(on).Contains(args[0])) ? context.runtime.True : context.runtime.False;
})));
obj.RegisterMethod(runtime.NewNativeMethod("takes two arguments, the key of the element to set and the value to set it too. returns the value set",
new TypeCheckingNativeMethod("[]=", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRequiredPositional("key")
.WithRequiredPositional("value")
.Arguments,
(method, on, args, keywords, context, message) => {
Dict.GetMap(on)[args[0]] = args[1];
return args[1];
})));
obj.RegisterMethod(runtime.NewNativeMethod("Returns the number of pairs contained in this dict.",
new TypeCheckingNativeMethod.WithNoArguments("size", obj,
(method, on, args, keywords, context, message) => {
return runtime.NewNumber(Dict.GetMap(on).Count);
})));
obj.RegisterMethod(runtime.NewNativeMethod("Returns a text inspection of the object",
new TypeCheckingNativeMethod.WithNoArguments("inspect", obj,
(method, on, args, keywords, context, message) => {
//.........这里部分代码省略.........
示例7: Init
public override void Init(IokeObject obj)
{
Runtime runtime = obj.runtime;
obj.Kind = "Set";
obj.Mimics(IokeObject.As(runtime.Mixins.GetCell(null, null, "Sequenced"), null), runtime.nul, runtime.nul);
obj.RegisterMethod(obj.runtime.NewNativeMethod("returns a hash for the set",
new NativeMethod.WithNoArguments("hash", (method, context, message, on, outer) => {
outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);
return context.runtime.NewNumber(((IokeSet)IokeObject.dataOf(on))._set.GetHashCode());
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns true if the left hand side set is equal to the right hand side set.",
new TypeCheckingNativeMethod("==", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(runtime.Set)
.WithRequiredPositional("other")
.Arguments,
(method, on, args, keywords, context, message) => {
IokeSet d = (IokeSet)IokeObject.dataOf(on);
object other = args[0];
return ((other is IokeObject) &&
(IokeObject.dataOf(other) is IokeSet)
&& d._set.Equals(((IokeSet)IokeObject.dataOf(other))._set)) ? context.runtime.True : context.runtime.False;
})));
obj.RegisterMethod(runtime.NewNativeMethod("Returns a text inspection of the object",
new TypeCheckingNativeMethod.WithNoArguments("inspect", obj,
(method, on, args, keywords, context, message) => {
return method.runtime.NewText(IokeSet.GetInspect(on));
})));
obj.RegisterMethod(runtime.NewNativeMethod("Converts this set to use identity semantics, and then returns it.",
new TypeCheckingNativeMethod.WithNoArguments("withIdentitySemantics!", obj,
(method, on, args, keywords, context, message) => {
IokeSet ss = (IokeSet)IokeObject.dataOf(on);
ss._set = new SaneHashSet<object>(ss._set, new IdentityHashTable.IdentityEqualityComparer());
return on;
})));
obj.RegisterMethod(runtime.NewNativeMethod("Returns a brief text inspection of the object",
new TypeCheckingNativeMethod.WithNoArguments("notice", obj,
(method, on, args, keywords, context, message) => {
return method.runtime.NewText(IokeSet.GetNotice(on));
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns true if this set is empty, false otherwise",
new TypeCheckingNativeMethod.WithNoArguments("empty?", obj,
(method, on, args, keywords, context, message) => {
return ((IokeSet)IokeObject.dataOf(on)).Set.Count == 0 ? context.runtime.True : context.runtime.False;
})));
obj.RegisterMethod(runtime.NewNativeMethod("Adds the argument to this set, if it's not already in the set. Returns the set after adding the object.",
new TypeCheckingNativeMethod("<<", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRequiredPositional("value")
.Arguments,
(method, on, args, keywords, context, message) => {
((IokeSet)IokeObject.dataOf(on))._set.Add(args[0]);
return on;
})));
obj.RegisterMethod(runtime.NewNativeMethod("Removes the argument from the set, if it's in the set. Returns the set after removing the object.",
new TypeCheckingNativeMethod("remove!", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRequiredPositional("value")
.Arguments,
(method, on, args, keywords, context, message) => {
((IokeSet)IokeObject.dataOf(on))._set.Remove(args[0]);
return on;
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns a new set that contains the receivers elements and the elements of the set sent in as the argument.",
new TypeCheckingNativeMethod("+", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRequiredPositional("otherSet").WhichMustMimic(obj)
.Arguments,
(method, on, args, keywords, context, message) => {
var newSet = new SaneHashSet<object>();
newSet.UnionWith(((IokeSet)IokeObject.dataOf(on)).Set);
newSet.UnionWith(((IokeSet)IokeObject.dataOf(args[0])).Set);
return context.runtime.NewSet(newSet);
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns true if the receiver includes the evaluated argument, otherwise false",
new TypeCheckingNativeMethod("include?", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRequiredPositional("object")
.Arguments,
(method, on, args, keywords, context, message) => {
return ((IokeSet)IokeObject.dataOf(on)).Set.Contains(args[0]) ? context.runtime.True : context.runtime.False;
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns a new sequence to iterate over this set",
new TypeCheckingNativeMethod.WithNoArguments("seq", obj,
(method, on, args, keywords, context, message) => {
IokeObject ob = method.runtime.IteratorSequence.AllocateCopy(null, null);
ob.MimicsWithoutCheck(method.runtime.IteratorSequence);
ob.Data = new Sequence.IteratorSequence(((IokeSet)IokeObject.dataOf(on))._set.GetEnumerator());
return ob;
//.........这里部分代码省略.........
示例8: Init
public override void Init(IokeObject obj)
{
Runtime runtime = obj.runtime;
IokeObject number = obj;
obj.Kind = "Number";
obj.Mimics(IokeObject.As(runtime.Mixins.GetCell(null, null, "Comparing"), obj), runtime.nul, runtime.nul);
IokeObject real = new IokeObject(runtime, "A real number can be either a rational number or a decimal number", new Number());
real.MimicsWithoutCheck(number);
real.Kind = "Number Real";
number.RegisterCell("Real", real);
IokeObject rational = new IokeObject(runtime, "A rational number is either an integer or a ratio", new Number());
rational.MimicsWithoutCheck(real);
rational.Kind = "Number Rational";
number.RegisterCell("Rational", rational);
IokeObject integer = new IokeObject(runtime, "An integral number", new Number());
integer.MimicsWithoutCheck(rational);
integer.Kind = "Number Integer";
number.RegisterCell("Integer", integer);
runtime.Integer = integer;
IokeObject ratio = new IokeObject(runtime, "A ratio of two integral numbers", new Number());
ratio.MimicsWithoutCheck(rational);
ratio.Kind = "Number Ratio";
number.RegisterCell("Ratio", ratio);
runtime.Ratio = ratio;
IokeObject _decimal = new IokeObject(runtime, "An exact, unlimited representation of a decimal number", new Decimal(BigDecimal.ZERO));
_decimal.MimicsWithoutCheck(real);
_decimal.Init();
number.RegisterCell("Decimal", _decimal);
IokeObject infinity = new IokeObject(runtime, "A value representing infinity", new Number(RatNum.infinity(1)));
infinity.MimicsWithoutCheck(ratio);
infinity.Kind = "Number Infinity";
number.RegisterCell("Infinity", infinity);
runtime.Infinity = infinity;
IokeObject infinity2 = new IokeObject(runtime, "A value representing infinity", new Number(RatNum.infinity(1)));
infinity2.MimicsWithoutCheck(ratio);
infinity2.Kind = "Number \u221E";
number.RegisterCell("\u221E", infinity2);
number.RegisterMethod(runtime.NewNativeMethod("returns a hash for the number",
new NativeMethod.WithNoArguments("hash", (method, context, message, on, outer) => {
outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);
return context.runtime.NewNumber(Number.GetValue(on).GetHashCode());
})));
number.RegisterMethod(runtime.NewNativeMethod("returns the square root of the receiver. this should return the same result as calling ** with 0.5",
new NativeMethod.WithNoArguments("sqrt", (method, context, message, on, outer) => {
outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);
RatNum value = Number.GetValue(on);
if(value is IntFraction) {
IntNum num = value.numerator();
IntNum den = value.denominator();
BigDecimal nums = new BigSquareRoot().Get(num.AsBigDecimal());
BigDecimal dens = new BigSquareRoot().Get(den.AsBigDecimal());
try {
num = IntNum.valueOf(nums.toBigIntegerExact().ToString());
den = IntNum.valueOf(dens.toBigIntegerExact().ToString());
return context.runtime.NewNumber(new IntFraction(num, den));
} catch(ArithmeticException e) {
// Ignore and fall through
}
}
if(RatNum.compare(value, IntNum.zero()) < 1) {
IokeObject condition = IokeObject.As(IokeObject.GetCellChain(context.runtime.Condition,
message,
context,
"Error",
"Arithmetic"), context).Mimic(message, context);
condition.SetCell("message", message);
condition.SetCell("context", context);
condition.SetCell("receiver", on);
context.runtime.ErrorCondition(condition);
}
return context.runtime.NewDecimal(new BigSquareRoot().Get(value.AsBigDecimal()));
})));
number.RegisterMethod(runtime.NewNativeMethod("returns true if the left hand side number is equal to the right hand side number.",
new TypeCheckingNativeMethod("==", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(runtime.Number)
.WithRequiredPositional("other")
.Arguments,
(method, on, args, keywords, context, message) => {
Number d = (Number)IokeObject.dataOf(on);
object other = args[0];
return ((other is IokeObject) &&
(IokeObject.dataOf(other) is Number)
&& (((d.kind || ((Number)IokeObject.dataOf(other)).kind) ? on == other :
d.value.Equals(((Number)IokeObject.dataOf(other)).value)))) ? context.runtime.True : context.runtime.False;
})));
//.........这里部分代码省略.........
示例9: Init
public override void Init(IokeObject obj)
{
Runtime runtime = obj.runtime;
obj.Kind = "Text";
obj.Mimics(IokeObject.As(runtime.Mixins.GetCell(null, null, "Comparing"), null), runtime.nul, runtime.nul);
obj.SetCell("==", runtime.Base.Cells["=="]);
obj.RegisterMethod(runtime.NewNativeMethod("Returns a text representation of the object",
new NativeMethod.WithNoArguments("asText",
(method, context, message, on, outer) => {
outer.ArgumentsDefinition.GetEvaluatedArguments(context, message, on, new SaneArrayList(), new SaneDictionary<string, object>());
return on;
})));
obj.RegisterMethod(runtime.NewNativeMethod("Takes any number of arguments, and expects the text receiver to contain format specifications. The currently supported specifications are only %s and %{, %}. These have several parameters that can be used. See the spec for more info about these. The format method will return a new text based on the content of the receiver, and the arguments given.",
new TypeCheckingNativeMethod("format", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRest("replacements")
.Arguments,
(self, on, args, keywords, context, message) => {
StringBuilder result = new StringBuilder();
Format(on, message, context, args, result);
return context.runtime.NewText(result.ToString());
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("Converts the content of this text into a rational value",
new TypeCheckingNativeMethod.WithNoArguments("toRational", obj,
(self, on, args, keywords, context, message) => {
return Text.ToRational(on, context, message);
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("Converts the content of this text into a decimal value",
new TypeCheckingNativeMethod.WithNoArguments("toDecimal", obj,
(self, on, args, keywords, context, message) => {
return Text.ToDecimal(on, context, message);
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("Returns a text inspection of the object",
new TypeCheckingNativeMethod.WithNoArguments("inspect", obj,
(self, on, args, keywords, context, message) => {
return self.runtime.NewText(Text.GetInspect(on));
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("Returns a brief text inspection of the object",
new TypeCheckingNativeMethod.WithNoArguments("notice", obj,
(self, on, args, keywords, context, message) => {
return self.runtime.NewText(Text.GetInspect(on));
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("Returns a lower case version of this text",
new TypeCheckingNativeMethod.WithNoArguments("lower", obj,
(self, on, args, keywords, context, message) => {
return self.runtime.NewText(Text.GetText(on).ToLower());
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("Returns an upper case version of this text",
new TypeCheckingNativeMethod.WithNoArguments("upper", obj,
(self, on, args, keywords, context, message) => {
return self.runtime.NewText(Text.GetText(on).ToUpper());
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("Returns a version of this text with leading and trailing whitespace removed",
new TypeCheckingNativeMethod.WithNoArguments("trim", obj,
(self, on, args, keywords, context, message) => {
return self.runtime.NewText(Text.GetText(on).Trim());
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("Returns an array of texts split around the argument",
new TypeCheckingNativeMethod("split", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithOptionalPositional("splitAround", "")
.Arguments,
(self, on, args, keywords, context, message) => {
string real = Text.GetText(on);
var r = new SaneArrayList();
Pattern p = null;
if(args.Count == 0) {
p = new Pattern("\\s");
} else {
object arg = args[0];
if(IokeObject.dataOf(arg) is Regexp) {
p = Regexp.GetRegexp(arg);
} else {
string around = Text.GetText(arg);
p = new Pattern(Pattern.Quote(around));
}
}
RETokenizer tok = new RETokenizer(p, real);
tok.EmptyEnabled = false;
while(tok.HasMore) {
r.Add(context.runtime.NewText(tok.NextToken));
}
return context.runtime.NewList(r);
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("Takes two text arguments where the first is the substring to replace, and the second is the replacement to insert. Will only replace the first match, if any is found, and return a new Text with the result.",
//.........这里部分代码省略.........