本文整理汇总了C#中Ioke.Lang.IokeObject.RegisterMethod方法的典型用法代码示例。如果您正苦于以下问题:C# IokeObject.RegisterMethod方法的具体用法?C# IokeObject.RegisterMethod怎么用?C# IokeObject.RegisterMethod使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Ioke.Lang.IokeObject
的用法示例。
在下文中一共展示了IokeObject.RegisterMethod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Init
public override void Init(IokeObject obj)
{
Runtime runtime = obj.runtime;
obj.Kind = "DateTime";
// obj.mimics(IokeObject.as(runtime.mixins.getCell(null, null, "Comparing")), runtime.nul, runtime.nul);
obj.RegisterMethod(runtime.NewNativeMethod("Returns a new DateTime representing the current instant in time in the default TimeZone.",
new TypeCheckingNativeMethod.WithNoArguments("now", obj,
(method, on, args, keywords, context, message) => {
return method.runtime.NewDateTime(System.DateTime.Now);
})));
obj.RegisterMethod(runtime.NewNativeMethod("Expects to get one DateTime as argument, and returns the difference between this instant and that instant, in milliseconds.",
new TypeCheckingNativeMethod("-", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRequiredPositional("subtrahend").WhichMustMimic(obj)
.Arguments,
(method, on, args, keywords, context, message) => {
long diff = System.Convert.ToInt64(GetDateTime(on).Subtract(GetDateTime(args[0])).TotalMilliseconds);
return context.runtime.NewNumber(diff);
})));
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(DateTime.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(DateTime.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 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));
})));
}
示例3: Init
public override void Init(IokeObject obj)
{
obj.Kind = "Sequence Iterator";
obj.MimicsWithoutCheck(obj.runtime.Sequence);
obj.RegisterMethod(obj.runtime.NewNativeMethod("returns the next object from this sequence if it exists. the behavior otherwise is undefined",
new TypeCheckingNativeMethod.WithNoArguments("next", obj,
(method, on, args, keywords, context, message) => {
return ((Iterator2Sequence)IokeObject.dataOf(on)).iter.next();
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("returns true if there is another object in this sequence.",
new TypeCheckingNativeMethod.WithNoArguments("next?", obj,
(method, on, args, keywords, context, message) => {
return ((Iterator2Sequence)IokeObject.dataOf(on)).iter.hasNext() ? method.runtime.True : method.runtime.False;
})));
}
示例4: 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));
})));
}
示例5: Init
public static void Init(IokeObject iokeGround, IokeObject ground)
{
Runtime runtime = ground.runtime;
iokeGround.Kind = "IokeGround";
ground.Kind = "Ground";
iokeGround.RegisterCell("Base", runtime.Base);
iokeGround.RegisterCell("DefaultBehavior", runtime.DefaultBehavior);
iokeGround.RegisterCell("IokeGround", runtime.IokeGround);
iokeGround.RegisterCell("Ground", runtime.Ground);
iokeGround.RegisterCell("Origin", runtime.Origin);
iokeGround.RegisterCell("System", runtime.System);
iokeGround.RegisterCell("Runtime", runtime._Runtime);
iokeGround.RegisterCell("Text", runtime.Text);
iokeGround.RegisterCell("Symbol", runtime.Symbol);
iokeGround.RegisterCell("Number", runtime.Number);
iokeGround.RegisterCell("nil", runtime.nil);
iokeGround.RegisterCell("true", runtime.True);
iokeGround.RegisterCell("false", runtime.False);
iokeGround.RegisterCell("Arity", runtime.Arity);
iokeGround.RegisterCell("Method", runtime.Method);
iokeGround.RegisterCell("DefaultMethod", runtime.DefaultMethod);
iokeGround.RegisterCell("NativeMethod", runtime.NativeMethod);
iokeGround.RegisterCell("LexicalBlock", runtime.LexicalBlock);
iokeGround.RegisterCell("DefaultMacro", runtime.DefaultMacro);
iokeGround.RegisterCell("LexicalMacro", runtime.LexicalMacro);
iokeGround.RegisterCell("DefaultSyntax", runtime.DefaultSyntax);
iokeGround.RegisterCell("Mixins", runtime.Mixins);
iokeGround.RegisterCell("Restart", runtime.Restart);
iokeGround.RegisterCell("List", runtime.List);
iokeGround.RegisterCell("Dict", runtime.Dict);
iokeGround.RegisterCell("Set", runtime.Set);
iokeGround.RegisterCell("Range", runtime.Range);
iokeGround.RegisterCell("Pair", runtime.Pair);
iokeGround.RegisterCell("DateTime", runtime.DateTime);
iokeGround.RegisterCell("Message", runtime.Message);
iokeGround.RegisterCell("Call", runtime.Call);
iokeGround.RegisterCell("Condition", runtime.Condition);
iokeGround.RegisterCell("Rescue", runtime.Rescue);
iokeGround.RegisterCell("Handler", runtime.Handler);
iokeGround.RegisterCell("IO", runtime.Io);
iokeGround.RegisterCell("FileSystem", runtime.FileSystem);
iokeGround.RegisterCell("Regexp", runtime.Regexp);
iokeGround.RegisterCell("Sequence", runtime.Sequence);
iokeGround.RegisterMethod(runtime.NewNativeMethod("will return a text representation of the current stack trace",
new NativeMethod.WithNoArguments("stackTraceAsText",
(method, context, message, on, outer) => {
outer.ArgumentsDefinition.GetEvaluatedArguments(context, message, on, new SaneArrayList(), new SaneDictionary<string, object>());
return context.runtime.NewText("");
})));
}
示例6: Init
public static void Init(IokeObject bm)
{
Runtime runtime = bm.runtime;
bm.Kind = "Benchmark";
runtime.Ground.SetCell("Benchmark", bm);
bm.MimicsWithoutCheck(runtime.Origin);
bm.RegisterMethod(runtime.NewNativeMethod("expects two optional numbers, x (default 10) and y (default 1), and a block of code to run, and will run benchmark this block x times, while looping y times in each benchmark. after each loop will print the timings for this loop",
new NativeMethod("report", DefaultArgumentsDefinition.builder()
.WithOptionalPositional("repetitions", "10")
.WithOptionalPositional("loops", "1")
.WithRequiredPositionalUnevaluated("code")
.Arguments,
(method, context, message, on, outer) => {
outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);
int count = message.Arguments.Count;
int bmRounds = 10;
long iterations = 1;
int index = 0;
if(count > 1) {
bmRounds = ((Number)IokeObject.dataOf(IokeObject.ConvertToNumber(((Message)IokeObject.dataOf(message)).GetEvaluatedArgument(message, index, context), message, context))).AsNativeInteger();
index++;
if(count > 2) {
iterations = ((Number)IokeObject.dataOf(IokeObject.ConvertToNumber(((Message)IokeObject.dataOf(message)).GetEvaluatedArgument(message, index, context), message, context))).AsNativeLong();
index++;
}
}
for(int i=0;i<bmRounds;i++) {
long before = System.DateTime.Now.Ticks;
for(int j=0;j<iterations;j++) {
((Message)IokeObject.dataOf(message)).GetEvaluatedArgument(message, index, context);
}
long after = System.DateTime.Now.Ticks;
long time = after-before;
long secs = time/10000000;
long rest = time%10000000;
string theCode = Message.ThisCode(((IokeObject)message.Arguments[index]));
((Message)IokeObject.dataOf(context.runtime.printlnMessage)).SendTo(context.runtime.printlnMessage, context, ((Message)IokeObject.dataOf(context.runtime.outMessage)).SendTo(context.runtime.outMessage, context, context.runtime.System), context.runtime.NewText(string.Format("{0,-32} {1:d6}.{2:d9}", theCode, secs, rest)));
}
return context.runtime.nil;
})));
}
示例7: InitRuntime
public static void InitRuntime(IokeObject obj)
{
obj.Kind = "Runtime";
obj.RegisterMethod(obj.runtime.NewNativeMethod("returns the node id for the runtime it's called on",
new TypeCheckingNativeMethod.WithNoArguments("nodeId", obj,
(method, on, args, keywords, context, message) => {
Runtime r = (Runtime)IokeObject.dataOf(on);
return method.runtime.NewNumber(r.id);
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("creates a new runtime and returns that. be careful using this since it will result in some fairly strange behavior if used incorrectly. it will not copy the state of this runtime, but just create a new one from scratch.",
new TypeCheckingNativeMethod.WithNoArguments("create", obj,
(method, on, args, keywords, context, message) => {
Runtime r = new Runtime(method.runtime.Out, method.runtime.In, method.runtime.Error);
r.Init();
IokeObject o = method.runtime._Runtime.AllocateCopy(null, null);
o.MimicsWithoutCheck(method.runtime._Runtime);
o.Data = r;
return o;
})));
}
示例8: Init
public override void Init(IokeObject obj)
{
Runtime runtime = obj.runtime;
obj.Kind = "Number Decimal";
runtime.Decimal = obj;
obj.RegisterMethod(runtime.NewNativeMethod("returns true if the left hand side decimal is equal to the right hand side decimal.",
new TypeCheckingNativeMethod("==", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(runtime.Decimal)
.WithRequiredPositional("other")
.Arguments,
(method, on, args, keywords, context, message) => {
Decimal d = (Decimal)IokeObject.dataOf(on);
object other = args[0];
return ((other is IokeObject) &&
(IokeObject.dataOf(other) is Decimal)
&& ((on == context.runtime.Decimal && other == on) ||
d.value.Equals(((Decimal)IokeObject.dataOf(other)).value))) ? context.runtime.True : context.runtime.False;
})));
obj.RegisterMethod(runtime.NewNativeMethod("Returns a text representation of the object",
new TypeCheckingNativeMethod.WithNoArguments("asText", obj,
(method, on, args, keywords, context, message) => {
return runtime.NewText(on.ToString());
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("Returns a text inspection of the object",
new TypeCheckingNativeMethod.WithNoArguments("inspect", obj,
(method, on, args, keywords, context, message) => {
return method.runtime.NewText(Decimal.GetInspect(on));
})));
obj.RegisterMethod(obj.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(Decimal.GetInspect(on));
})));
obj.RegisterMethod(obj.runtime.NewNativeMethod("returns a hash for the decimal number",
new NativeMethod.WithNoArguments("hash", (method, context, message, on, outer) => {
outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);
return context.runtime.NewNumber(Decimal.GetValue(on).GetHashCode());
})));
obj.RegisterMethod(runtime.NewNativeMethod("compares this number against the argument, true if this number is the same, otherwise false",
new TypeCheckingNativeMethod("==", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRequiredPositional("other")
.Arguments,
(method, on, args, keywords, context, message) => {
object arg = args[0];
if(IokeObject.dataOf(arg) is Number) {
return (Decimal.GetValue(on).CompareTo(Number.GetValue(arg).AsBigDecimal()) == 0) ? context.runtime.True : context.runtime.False;
} else if(IokeObject.dataOf(arg) is Decimal) {
return (Decimal.GetValue(on).CompareTo(Decimal.GetValue(arg)) == 0) ? context.runtime.True : context.runtime.False;
} else {
return context.runtime.False;
}
})));
obj.RegisterMethod(runtime.NewNativeMethod("compares this number against the argument, returning -1, 0 or 1 based on which one is larger. if the argument is a rational, it will be converted into a form suitable for comparing against a decimal, and then compared. if the argument is neither a Rational nor a Decimal, it tries to call asDecimal, and if that doesn't work it returns nil.",
new TypeCheckingNativeMethod("<=>", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRequiredPositional("other")
.Arguments,
(method, on, args, keywords, context, message) => {
object arg = args[0];
IokeData data = IokeObject.dataOf(arg);
if(data is Number) {
return context.runtime.NewNumber(Decimal.GetValue(on).CompareTo(Number.GetValue(arg).AsBigDecimal()));
} else {
if(!(data is Decimal)) {
arg = IokeObject.ConvertToDecimal(arg, message, context, false);
if(!(IokeObject.dataOf(arg) is Decimal)) {
// Can't compare, so bail out
return context.runtime.nil;
}
}
if(on == context.runtime.Decimal || arg == context.runtime.Decimal) {
if(arg == on) {
return context.runtime.NewNumber(0);
}
return context.runtime.nil;
}
return context.runtime.NewNumber(Decimal.GetValue(on).CompareTo(Decimal.GetValue(arg)));
}
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns the difference between this number and the argument. if the argument is a rational, it will be converted into a form suitable for subtracting against a decimal, and then subtracted. if the argument is neither a Rational nor a Decimal, it tries to call asDecimal, and if that fails it signals a condition.",
new TypeCheckingNativeMethod("-", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRequiredPositional("subtrahend")
.Arguments,
(method, on, args, keywords, context, message) => {
object arg = args[0];
//.........这里部分代码省略.........
示例9: 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;
})));
//.........这里部分代码省略.........
示例10: Init
public override void Init(IokeObject obj)
{
Runtime runtime = obj.runtime;
obj.Kind = "Call";
obj.RegisterMethod(runtime.NewNativeMethod("takes one evaluated text or symbol argument and resends the current message to that method/macro on the current receiver.",
new TypeCheckingNativeMethod("resendToMethod", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(runtime.Call)
.WithRequiredPositional("cellName")
.Arguments,
(self, _on, args, keywords, context, _message) => {
Call c = (Call)IokeObject.dataOf(_on);
string name = Text.GetText(((Message)IokeObject.dataOf(runtime.asText)).SendTo(runtime.asText, context, args[0]));
IokeObject m = Message.Copy(c.message);
Message.SetName(m, name);
return ((Message)IokeObject.dataOf(m)).SendTo(m, c.surroundingContext, c.on);
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns a list of all the unevaluated arguments",
new TypeCheckingNativeMethod.WithNoArguments("arguments",
runtime.Call,
(method, _on, args, keywords, context, _message) => {
return context.runtime.NewList(((Call)IokeObject.dataOf(_on)).message.Arguments);
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns the ground of the place this call originated",
new TypeCheckingNativeMethod.WithNoArguments("ground",
runtime.Call,
(method, _on, args, keywords, context, _message) => {
return ((Call)IokeObject.dataOf(_on)).surroundingContext;
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns the receiver of the call",
new TypeCheckingNativeMethod.WithNoArguments("receiver",
runtime.Call,
(method, _on, args, keywords, context, _message) => {
return ((Call)IokeObject.dataOf(_on)).on;
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns the currently executing context",
new TypeCheckingNativeMethod.WithNoArguments("currentContext",
runtime.Call,
(method, _on, args, keywords, context, _message) => {
return ((Call)IokeObject.dataOf(_on)).ctx;
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns the message that started this call",
new TypeCheckingNativeMethod.WithNoArguments("message",
runtime.Call,
(method, _on, args, keywords, context, _message) => {
return ((Call)IokeObject.dataOf(_on)).message;
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns a list of the result of evaluating all the arguments to this call",
new TypeCheckingNativeMethod.WithNoArguments("evaluatedArguments",
runtime.Call,
(method, _on, args, keywords, context, _message) => {
IokeObject msg = ((Call)IokeObject.dataOf(_on)).message;
return context.runtime.NewList(((Message)IokeObject.dataOf(msg)).GetEvaluatedArguments(msg, ((Call)IokeObject.dataOf(_on)).surroundingContext));
})));
obj.RegisterMethod(runtime.NewNativeMethod("uhm. this is a strange one. really.",
new TypeCheckingNativeMethod("resendToValue", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(runtime.Call)
.WithRequiredPositional("value")
.WithOptionalPositional("newSelf", "nil")
.Arguments,
(method, _on, args, keywords, context, _message) => {
Call c = (Call)IokeObject.dataOf(_on);
object self = c.on;
if(args.Count > 1) {
self = args[1];
}
return IokeObject.GetOrActivate(args[0], c.surroundingContext, c.message, self);
})));
obj.RegisterMethod(runtime.NewNativeMethod("uhm. this one isn't too bad.",
new TypeCheckingNativeMethod("activateValue", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(runtime.Call)
.WithRequiredPositional("value")
.WithOptionalPositional("newSelf", "nil")
.WithKeywordRest("valuesToAdd")
.Arguments,
(method, _on, args, keys, context, _message) => {
Call c = (Call)IokeObject.dataOf(_on);
object self = c.on;
if(args.Count > 1) {
self = args[1];
}
return IokeObject.As(args[0], context).ActivateWithData(c.surroundingContext, c.message, self, keys);
})));
obj.RegisterMethod(runtime.NewNativeMethod("I really ought to write documentation for these methods, but I don't know how to describe what they do.",
new TypeCheckingNativeMethod("activateValueWithCachedArguments", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(runtime.Call)
.WithRequiredPositional("value")
.WithOptionalPositional("newSelf", "nil")
.WithKeywordRest("valuesToAdd")
//.........这里部分代码省略.........
示例11: Init
public static void Init(IokeObject obj)
{
Runtime runtime = obj.runtime;
obj.Kind = "DefaultBehavior Reflection";
obj.RegisterMethod(runtime.NewNativeMethod("Takes one evaluated argument and returns either true or false if this object or one of it's mimics mimics that argument",
new NativeMethod("mimics?", DefaultArgumentsDefinition.builder()
.WithRequiredPositional("potentialMimic")
.Arguments,
(method, context, message, on, outer) => {
var args = new SaneArrayList();
outer.ArgumentsDefinition.GetEvaluatedArguments(context, message, on, args, new SaneDictionary<string, object>());
IokeObject arg = IokeObject.As(args[0], context);
if(IokeObject.IsMimic(on, arg, context)) {
return context.runtime.True;
} else {
return context.runtime.False;
}
})));
obj.RegisterMethod(runtime.NewNativeMethod("modifies the receiver to be in all ways identical to the argument. if the receiver is nil, true or false, this method can't be used - but those are the only exceptions. it's generally not recommended to use it on kinds and objects that are important for the Ioke runtime, since the result might be highly unpredictable.",
new NativeMethod("become!", DefaultArgumentsDefinition.builder()
.WithRequiredPositional("objectToBecome")
.Arguments,
(method, context, message, on, outer) => {
var args = new SaneArrayList();
outer.ArgumentsDefinition.GetEvaluatedArguments(context, message, on, args, new SaneDictionary<string, object>());
IokeObject me = IokeObject.As(on, context);
IokeObject other = IokeObject.As(args[0], context);
if(on == context.runtime.nil || on == context.runtime.True || on == context.runtime.False) {
IokeObject condition = IokeObject.As(IokeObject.GetCellChain(context.runtime.Condition,
message,
context,
"Error",
"CantMimicOddball"), context).Mimic(message, context);
condition.SetCell("message", message);
condition.SetCell("context", context);
condition.SetCell("receiver", on);
context.runtime.ErrorCondition(condition);
}
me.Become(other, message, context);
return on;
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns a text hex representation of the receiver in upper case hex literal, starting with 0x. This value is based on System.identityHashCode, and as such is not totally guaranteed to be totally unique. but almost.",
new NativeMethod.WithNoArguments("uniqueHexId",
(method, context, message, on, outer) => {
outer.ArgumentsDefinition.GetEvaluatedArguments(context, message, on, new SaneArrayList(), new SaneDictionary<string, object>());
return context.runtime.NewText("0x" + System.Convert.ToString(System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(IokeObject.As(on, context).Cells), 16).ToUpper());
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns a textual representation of the object called on.",
new NativeMethod.WithNoArguments("asText",
(method, context, message, on, outer) => {
outer.ArgumentsDefinition.GetEvaluatedArguments(context, message, on, new SaneArrayList(), new SaneDictionary<string, object>());
return method.runtime.NewText(on.ToString());
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns true if the evaluated argument is the same reference as the receiver, false otherwise.",
new NativeMethod("same?", DefaultArgumentsDefinition.builder()
.WithRequiredPositional("other")
.Arguments,
(method, context, message, on, outer) => {
var args = new SaneArrayList();
outer.ArgumentsDefinition.GetEvaluatedArguments(context, message, on, args, new SaneDictionary<string, object>());
return IokeObject.Same(on, args[0]) ? context.runtime.True : context.runtime.False;
})));
obj.RegisterMethod(runtime.NewNativeMethod("takes the name of a message to send, and the arguments to give it. send should generally behave exactly as if you had sent the message itself - except that you can give a variable containing the name.",
new NativeMethod("send", DefaultArgumentsDefinition.builder()
.WithRequiredPositional("messageName")
.WithRestUnevaluated("arguments")
.WithKeywordRestUnevaluated("keywordArguments")
.Arguments,
(method, context, message, on, outer) => {
outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);
object _name = ((Message)IokeObject.dataOf(message)).GetEvaluatedArgument(message, 0, context);
string name = Text.GetText(((Message)IokeObject.dataOf(runtime.asText)).SendTo(runtime.asText, context, _name));
IokeObject newMessage = Message.DeepCopy(message);
newMessage.Arguments.RemoveAt(0);
Message.SetName(newMessage, name);
return ((Message)IokeObject.dataOf(newMessage)).SendTo(newMessage, context, on);
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns false if the left hand side is equal to the right hand side. exactly what this means depend on the object. the default behavior of Ioke objects is to only be equal if they are the same instance.",
new NativeMethod("!=", DefaultArgumentsDefinition.builder()
.WithRequiredPositional("other")
.Arguments,
(method, context, message, on, outer) => {
var args = new SaneArrayList();
outer.ArgumentsDefinition.GetEvaluatedArguments(context, message, on, args, new SaneDictionary<string, object>());
return !IokeObject.Equals(on, ((Message)IokeObject.dataOf(message)).GetEvaluatedArgument(message, 0, context)) ? context.runtime.True : context.runtime.False;
})));
obj.RegisterMethod(runtime.NewNativeMethod("Takes one evaluated Text argument and returns either true or false if this object or one of it's mimics have the kind of the name specified",
//.........这里部分代码省略.........
示例12: Init
public static void Init(IokeObject obj)
{
Runtime runtime = obj.runtime;
obj.Kind = "DefaultBehavior Definitions";
obj.RegisterMethod(runtime.NewNativeMethod("expects any number of unevaluated arguments. if no arguments at all are given, will just return nil. creates a new method based on the arguments. this method will be evaluated using the context of the object it's called on, and thus the definition can not refer to the outside scope where the method is defined. (there are other ways of achieving this). all arguments except the last one is expected to be names of arguments that will be used in the method. there will possible be additions to the format of arguments later on - including named parameters and optional arguments. the actual code is the last argument given.",
new NativeMethod("method", DefaultArgumentsDefinition.builder()
.WithOptionalPositionalUnevaluated("documentation")
.WithRestUnevaluated("argumentsAndBody")
.Arguments,
(method, context, message, on, outer) => {
outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);
var args = message.Arguments;
if(args.Count == 0) {
Message mx = new Message(context.runtime, "nil", null, Message.Type.MESSAGE);
mx.File = Message.GetFile(message);
mx.Line = Message.GetLine(message);
mx.Position = Message.GetPosition(message);
IokeObject mmx = context.runtime.CreateMessage(mx);
return runtime.NewMethod(null, runtime.DefaultMethod, new DefaultMethod(context, DefaultArgumentsDefinition.Empty(), mmx));
}
string doc = null;
int start = 0;
if(args.Count > 1 && ((IokeObject)Message.GetArguments(message)[0]).Name.Equals("internal:createText")) {
start++;
string s = (string)((IokeObject)args[0]).Arguments[0];
doc = s;
}
DefaultArgumentsDefinition def = DefaultArgumentsDefinition.CreateFrom(args, start, args.Count-1, message, on, context);
return runtime.NewMethod(doc, runtime.DefaultMethod, new DefaultMethod(context, def, (IokeObject)args[args.Count-1]));
})));
obj.RegisterMethod(runtime.NewNativeMethod("expects one code argument, optionally preceeded by a documentation string. will create a new DefaultMacro based on the code and return it.",
new NativeMethod("macro", DefaultArgumentsDefinition.builder()
.WithOptionalPositionalUnevaluated("documentation")
.WithOptionalPositionalUnevaluated("body")
.Arguments,
(method, context, message, on, outer) => {
outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);
var args = message.Arguments;
if(args.Count == 0) {
Message mx = new Message(context.runtime, "nil", null, Message.Type.MESSAGE);
mx.File = Message.GetFile(message);
mx.Line = Message.GetLine(message);
mx.Position = Message.GetPosition(message);
IokeObject mmx = context.runtime.CreateMessage(mx);
return runtime.NewMacro(null, runtime.DefaultMacro, new DefaultMacro(context, mmx));
}
string doc = null;
int start = 0;
if(args.Count > 1 && ((IokeObject)Message.GetArguments(message)[0]).Name.Equals("internal:createText")) {
start++;
string s = (string)(((IokeObject)args[0]).Arguments[0]);
doc = s;
}
return runtime.NewMacro(doc, runtime.DefaultMacro, new DefaultMacro(context, (IokeObject)args[start]));
})));
obj.RegisterMethod(runtime.NewNativeMethod("creates a new lexical block that can be executed at will, while retaining a reference to the lexical closure it was created in. it will always update variables if they exist. there is currently no way of introducing shadowing variables in the local context. new variables can be created though, just like in a method. a lexical block mimics LexicalBlock, and can take arguments. at the moment these are restricted to required arguments, but support for the same argument types as DefaultMethod will come.",
new NativeMethod("fn", DefaultArgumentsDefinition.builder()
.WithOptionalPositionalUnevaluated("documentation")
.WithRestUnevaluated("argumentsAndBody")
.Arguments,
(method, context, message, on, outer) => {
outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);
var args = message.Arguments;
if(args.Count == 0) {
return runtime.NewLexicalBlock(null, runtime.LexicalBlock, new LexicalBlock(context, DefaultArgumentsDefinition.Empty(), method.runtime.nilMessage));
}
string doc = null;
int start = 0;
if(args.Count > 1 && ((IokeObject)Message.GetArguments(message)[0]).Name.Equals("internal:createText")) {
start++;
string s = ((string)((IokeObject)args[0]).Arguments[0]);
doc = s;
}
IokeObject code = IokeObject.As(args[args.Count-1], context);
DefaultArgumentsDefinition def = DefaultArgumentsDefinition.CreateFrom(args, start, args.Count-1, message, on, context);
return runtime.NewLexicalBlock(doc, runtime.LexicalBlock, new LexicalBlock(context, def, code));
})));
obj.RegisterMethod(runtime.NewNativeMethod("creates a new lexical block that can be executed at will, while retaining a reference to the lexical closure it was created in. it will always update variables if they exist. there is currently no way of introducing shadowing variables in the local context. new variables can be created though, just like in a method. a lexical block mimics LexicalBlock, and can take arguments. at the moment these are restricted to required arguments, but support for the same argument types as DefaultMethod will come. same as fn()",
new NativeMethod("\u028E", DefaultArgumentsDefinition.builder()
.WithOptionalPositionalUnevaluated("documentation")
//.........这里部分代码省略.........
示例13: Init
public override void Init(IokeObject obj)
{
Runtime runtime = obj.runtime;
obj.Kind = "FileSystem File";
obj.RegisterMethod(runtime.NewNativeMethod("Closes any open stream to this file",
new TypeCheckingNativeMethod.WithNoArguments("close", obj,
(method, on, args, keywords, context, message) => {
try {
TextWriter writer = IokeIO.GetWriter(on);
if(writer != null) {
writer.Close();
}
} catch(IOException) {
}
return context.runtime.nil;
})));
}
示例14: 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",
//.........这里部分代码省略.........
示例15: Init
public override void Init(IokeObject obj)
{
Runtime runtime = obj.runtime;
obj.Kind = "Regexp";
IokeObject regexpMatch = new IokeObject(runtime, "contains behavior related to assignment", new RegexpMatch(obj, null, null));
regexpMatch.MimicsWithoutCheck(runtime.Origin);
regexpMatch.Init();
obj.RegisterCell("Match", regexpMatch);
obj.RegisterMethod(runtime.NewNativeMethod("returns a hash for the regexp",
new NativeMethod.WithNoArguments("hash", (method, context, message, on, outer) => {
outer.ArgumentsDefinition.CheckArgumentCount(context, message, on);
Regexp r = (Regexp)IokeObject.dataOf(on);
return context.runtime.NewNumber(r.pattern.GetHashCode() + 13 * r.flags.GetHashCode());
})));
obj.RegisterMethod(runtime.NewNativeMethod("returns true if the left hand side pattern is equal to the right hand side pattern.",
new TypeCheckingNativeMethod("==", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(runtime.Regexp)
.WithRequiredPositional("other")
.Arguments,
(method, on, args, keywords, context, message) => {
Regexp d = (Regexp)IokeObject.dataOf(on);
object other = args[0];
return ((other is IokeObject) &&
(IokeObject.dataOf(other) is Regexp) &&
((on == context.runtime.Regexp || other == context.runtime.Regexp) ? on == other :
(d.pattern.Equals(((Regexp)IokeObject.dataOf(other)).pattern) &&
d.flags.Equals(((Regexp)IokeObject.dataOf(other)).flags)))) ? context.runtime.True : context.runtime.False;
})));
obj.RegisterMethod(runtime.NewNativeMethod("Returns the pattern use for this regular expression",
new TypeCheckingNativeMethod.WithNoArguments("pattern", obj,
(method, on, args, keywords, context, message) => {
return context.runtime.NewText(GetPattern(on));
})));
obj.RegisterMethod(runtime.NewNativeMethod("Takes one argument and tries to match that argument against the current pattern. Returns nil if no match can be done, or a Regexp Match object if a match succeeds",
new TypeCheckingNativeMethod("match", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRequiredPositional("other")
.Arguments,
(method, on, args, keywords, context, message) => {
IokeObject target = IokeObject.As(Interpreter.Send(context.runtime.asText, context, args[0]), context);
string arg = Text.GetText(target);
Matcher m = ((Regexp)IokeObject.dataOf(on)).regexp.Matcher(arg);
if(m.Find()) {
IokeObject match = regexpMatch.AllocateCopy(message, context);
match.MimicsWithoutCheck(regexpMatch);
match.Data = new RegexpMatch(IokeObject.As(on, context), m, target);
return match;
} else {
return context.runtime.nil;
}
})));
obj.AliasMethod("match", "=~", null, null);
obj.RegisterMethod(runtime.NewNativeMethod("Takes one argument that should be a text and returns a text that has all regexp meta characters quoted",
new NativeMethod("quote", DefaultArgumentsDefinition.builder()
.WithRequiredPositional("text")
.Arguments,
(method, on, args, keywords, context, message) => {
return context.runtime.NewText(Pattern.Quote(Text.GetText(Interpreter.Send(context.runtime.asText, context, args[0]))));
})));
obj.RegisterMethod(runtime.NewNativeMethod("Takes one or two text arguments that describes the regular expression to create. the first text is the pattern and the second is the flags.",
new NativeMethod("from", DefaultArgumentsDefinition.builder()
.WithRequiredPositional("pattern")
.WithOptionalPositional("flags", "")
.Arguments,
(method, on, args, keywords, context, message) => {
string pattern = Text.GetText(Interpreter.Send(context.runtime.asText, context, args[0]));
string flags = "";
if(args.Count > 1) {
flags = Text.GetText(Interpreter.Send(context.runtime.asText, context, args[1]));
}
return context.runtime.NewRegexp(pattern, flags, context, message);
})));
obj.RegisterMethod(runtime.NewNativeMethod("Takes one argument and tries to match that argument against the current pattern. Returns a list of all the texts that were matched.",
new TypeCheckingNativeMethod("allMatches", TypeCheckingArgumentsDefinition.builder()
.ReceiverMustMimic(obj)
.WithRequiredPositional("other")
.Arguments,
(method, on, args, keywords, context, message) => {
string arg = Text.GetText(Interpreter.Send(context.runtime.asText, context, args[0]));
Matcher m = ((Regexp)IokeObject.dataOf(on)).regexp.Matcher(arg);
var result = new SaneArrayList();
MatchIterator iter = m.FindAll();
while(iter.HasMore) {
result.Add(runtime.NewText(iter.NextMatch.Group(0)));
}
return runtime.NewList(result);
//.........这里部分代码省略.........