public bool Include(iObject value) { if (Begin is Fixnum fixnumBegin && End is Fixnum fixnumEnd) { return(value is Fixnum fixnumValue && fixnumBegin <= fixnumValue && (ExcludeEnd ? fixnumValue < fixnumEnd : fixnumValue <= fixnumEnd)); } if (Begin is String && End is String) { if (!(value is String)) { return(false); } var text = value.ToString(); return(string.Compare(Begin.ToString(), text, StringComparison.Ordinal) <= 0 && ( ExcludeEnd ? string.Compare(End.ToString(), text, StringComparison.Ordinal) > 0 : string.Compare(End.ToString(), text, StringComparison.Ordinal) >= 0 )); } throw new NotImplementedException(); }
private static void DummyMethod( iObject instance, iObject prefixRequired, [Optional] iObject optional, [Rest] iObject rest, iObject suffixRequired, [Key] iObject keyRequired1, [Key][Optional] iObject keyOptional1, [Key] iObject keyRequired2, [Key][Optional] iObject keyOptional2, [Key][Rest] iObject keyRest, [Block] iObject block ) { Console.WriteLine(instance); Console.WriteLine(prefixRequired); Console.WriteLine(optional); Console.WriteLine(rest); Console.WriteLine(suffixRequired); Console.WriteLine(keyRequired1); Console.WriteLine(keyOptional1); Console.WriteLine(keyRequired2); Console.WriteLine(keyOptional2); Console.WriteLine(keyRest); Console.WriteLine(block); }
public override void Bundle(iObject argument, ArgumentBundle bundle) { foreach (var item in (Array)argument) { bundle.Splat.Add(item); } }
public static iObject Send(iObject instance, iObject methodName, params iObject[] arguments) { var methodNameAsSymbol = MethodNameAsSymbol(methodName); var argumentKinds = Enumerable.Range(0, arguments.Length).Select(_ => ArgumentKind.Simple); var callSite = new CallSite(methodNameAsSymbol, Visibility.Private, argumentKinds); return(callSite.Call(instance, arguments)); }
public override void Bundle(iObject argument, ArgumentBundle bundle) { foreach (var pair in (Hash)argument) { var array = (Array)pair; bundle.Keywords[array[0]] = array[1]; } }
public Range(iObject begin, iObject end, bool excludeEnd = false) : base(Class.RANGE) { Debug.Assert(begin != null); Debug.Assert(end != null); Begin = begin; End = end; ExcludeEnd = excludeEnd; }
private static Class SuperclassCast(iObject super) { if (super is Class c) { return(c); } throw new TypeError($"superclass must be a Class ({super.Class} given)"); }
public CallFrame(CallSite callSite, iObject instance, ArgumentBundle arguments = null, CallFrame caller = null) { CallSite = callSite; Instance = instance; Caller = caller ?? Current; Arguments = arguments ?? new ArgumentBundle(); Locals = new LinkedDictionary <Symbol, LocalVariable>(); }
public static bool IsA(this iObject instance, iObject arg) { if (!(arg is Module module)) { throw new TypeError("class or module required"); } var instanceClass = instance as Class ?? instance.EffectiveClass; return(instanceClass.Ancestors.Any(c => c.Equals(module))); }
protected static Class GetOrCreateClassWithParentCast(iObject parent, Symbol name, Class superclass, IEnumerable <Module> nesting) { if (parent is Module mod) { return(mod.GetOrCreateClass(name, superclass, nesting)); } throw new TypeError($"{parent.Inspect()} is not a class/module"); }
private static Symbol MethodNameAsSymbol(iObject methodName) { switch (methodName) { case Symbol symName: return(symName); case String name: return(new Symbol(name.Value)); default: throw new TypeError($"{methodName.Inspect()} is not a symbol nor a string"); } }
public iObject Call(iObject instance, params iObject[] arguments) { if (arguments.Length != ArgumentKinds.Count) { throw new ArgumentException( $"{MethodName}: Number of arguments ({arguments.Length}) doesn't match expected number ({ArgumentKinds.Count})." ); } var bundle = new ArgumentBundle(ArgumentKinds.Zip(arguments, (kind, arg) => (kind, arg))); CallFrame.Push(this, instance, bundle); try { try { return(CallCache.Call()); } catch (RecompilationRequiredException) { // caught while a method was being undefined. // give a second chance to recover (which will possibly call method_missing). return(CallCache.Call()); } } catch (Exception e) { if (!e.Data.Contains(RB_STACK_KEY)) { e.Data[RB_STACK_KEY] = CallFrame.Current; } throw; } catch (System.Exception e) { if (!e.Data.Contains(RB_STACK_KEY)) { e.Data[RB_STACK_KEY] = CallFrame.Current.CallSite.MethodName.Name; } throw; } finally { CallFrame.Pop(); } }
public iObject SetLocalValue(Symbol local, iObject value) { var variable = Locals.FirstOrDefault(_ => _.Name == local); if (variable != null) { variable.Value = value; } else { dynamicLocals.Add(new LocalVariable(local, value)); } return(value); }
protected static Module GetOrCreateModuleWithParentCast(iObject parent, Symbol name, IEnumerable <Module> nesting) { switch (parent) { case Module mod: return(mod.GetOrCreateModule(name, nesting)); case iObject vmObj: throw new TypeError($"{vmObj.Inspect()} is not a class/module"); default: throw new TypeError($"{parent} is not a class/module"); } }
public iObject SetConstant(Symbol name, iObject value) { if (IsConstantDefined(name, false)) { var fullName = ReferenceEquals(this, Class.OBJECT) ? name.Name : $"{Name}::{name}"; Console.Error.WriteLine($"warning: already initialized constant {fullName}"); } ValidateConstantName(name.Name); if (value is Module module && module.BaseName == null) { module.BaseName = name; module.container = this; } return(constants[name] = value); }
// TODO accept string public Complex(iObject real, iObject imag) : base(Class.COMPLEX) { if (NilClass.IsNil(real) || NilClass.IsNil(imag)) { throw new TypeError("can't convert nil into Complex"); } if (!real.IsA(Class.INTEGER) && !real.IsA(Class.FLOAT)) { throw new TypeError($"can't convert {real.Class} into Complex"); } if (!imag.IsA(Class.INTEGER) && !imag.IsA(Class.FLOAT)) { throw new TypeError($"can't convert {imag.Class} into Complex"); } Real = real; Imag = imag; }
public override void Bundle(iObject argument, ArgumentBundle bundle) { var labeledArg = (Array)argument; bundle.Keywords[labeledArg[0]] = labeledArg[1]; }
public LocalVariable(Symbol name, iObject value = null) { Name = name; Value = value; }
public override void Bundle(iObject argument, ArgumentBundle bundle) { bundle.Splat.Add(argument); }
public override void Bundle(iObject argument, ArgumentBundle bundle) { bundle.Block = argument; }
public static CallFrame Push(CallSite callSite, iObject instance, ArgumentBundle arguments = null) => Current = new CallFrame(callSite, instance, arguments);
public bool SomeMethod(iObject obj) { return(false); }
public abstract void Bundle(iObject argument, ArgumentBundle bundle);
public static bool RespondTo(iObject instance, Symbol methodName) => instance.EffectiveClass.Methods.ContainsKey(methodName);
public iObject InstanceVariableSet(Symbol name, iObject obj) { Object.ValidateInstanceVariableName(name.Name); throw new RuntimeError($"can't modify frozen {EffectiveClass.Name}"); }
public static iObject Box(iObject obj) => obj ?? new NilClass();
internal static string MethodMissingInspect(iObject obj) => $"{obj.Inspect()}:{obj.Class.Name}";
//public static bool ToBool(object obj) => obj is iObject instance ? ToBool(instance) : true.Equals(obj); public static bool ToBool(iObject obj) => !(obj == null || obj is NilClass || obj is FalseClass);
public iObject InstanceVariableSet(string name, iObject obj) => InstanceVariableSet(new Symbol(name), obj);
public Compiler(string filename, iObject instance) : this(filename, new CallFrame(null, instance)) { }