Пример #1
0
        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();
        }
Пример #2
0
 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);
 }
Пример #3
0
 public override void Bundle(iObject argument, ArgumentBundle bundle)
 {
     foreach (var item in (Array)argument)
     {
         bundle.Splat.Add(item);
     }
 }
Пример #4
0
        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));
        }
Пример #5
0
 public override void Bundle(iObject argument, ArgumentBundle bundle)
 {
     foreach (var pair in (Hash)argument)
     {
         var array = (Array)pair;
         bundle.Keywords[array[0]] = array[1];
     }
 }
Пример #6
0
        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;
        }
Пример #7
0
        private static Class SuperclassCast(iObject super)
        {
            if (super is Class c)
            {
                return(c);
            }

            throw new TypeError($"superclass must be a Class ({super.Class} given)");
        }
Пример #8
0
 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>();
 }
Пример #9
0
        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)));
        }
Пример #10
0
        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");
        }
Пример #11
0
        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");
            }
        }
Пример #12
0
        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();
            }
        }
Пример #13
0
        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);
        }
Пример #14
0
        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");
            }
        }
Пример #15
0
        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);
        }
Пример #16
0
        // 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;
        }
Пример #17
0
            public override void Bundle(iObject argument, ArgumentBundle bundle)
            {
                var labeledArg = (Array)argument;

                bundle.Keywords[labeledArg[0]] = labeledArg[1];
            }
Пример #18
0
 public LocalVariable(Symbol name, iObject value = null)
 {
     Name  = name;
     Value = value;
 }
Пример #19
0
 public override void Bundle(iObject argument, ArgumentBundle bundle)
 {
     bundle.Splat.Add(argument);
 }
Пример #20
0
 public override void Bundle(iObject argument, ArgumentBundle bundle)
 {
     bundle.Block = argument;
 }
Пример #21
0
 public static CallFrame Push(CallSite callSite,
                              iObject instance,
                              ArgumentBundle arguments = null)
 => Current = new CallFrame(callSite, instance, arguments);
Пример #22
0
 public bool SomeMethod(iObject obj)
 {
     return(false);
 }
Пример #23
0
 public abstract void Bundle(iObject argument, ArgumentBundle bundle);
Пример #24
0
 public static bool RespondTo(iObject instance, Symbol methodName)
 => instance.EffectiveClass.Methods.ContainsKey(methodName);
Пример #25
0
 public iObject InstanceVariableSet(Symbol name, iObject obj)
 {
     Object.ValidateInstanceVariableName(name.Name);
     throw new RuntimeError($"can't modify frozen {EffectiveClass.Name}");
 }
Пример #26
0
 public static iObject Box(iObject obj) => obj ?? new NilClass();
Пример #27
0
 internal static string MethodMissingInspect(iObject obj) => $"{obj.Inspect()}:{obj.Class.Name}";
Пример #28
0
        //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);
Пример #29
0
 public iObject InstanceVariableSet(string name, iObject obj) => InstanceVariableSet(new Symbol(name), obj);
Пример #30
0
 public Compiler(string filename, iObject instance)
     : this(filename, new CallFrame(null, instance))
 {
 }