Exemplo n.º 1
0
        public P5MainSymbolTable(Runtime runtime, string name)
            : base(runtime, name)
        {
            var stdout = GetStashGlob(runtime, "STDOUT", true);
            stdout.Handle = new P5Handle(runtime, null, System.Console.Out);

            var stdin = GetStashGlob(runtime, "STDIN", true);
            stdin.Handle = new P5Handle(runtime, System.Console.In, null);

            var stderr = GetStashGlob(runtime, "STDERR", true);
            stderr.Handle = new P5Handle(runtime, null, System.Console.Error);

            var dquote = GetStashGlob(runtime, "\"", true);
            dquote.Scalar = new P5Scalar(runtime, " ");

            var version = GetStashGlob(runtime, "]", true);
            version.Scalar = new P5Scalar(runtime, 5.008);

            // UNIVERSAL
            universal = GetPackage(runtime, "UNIVERSAL", true);

            var isa = universal.GetStashGlob(runtime, "isa", true);
            isa.Code = new P5NativeCode("UNIVERSAL::isa", new P5Code.Sub(WrapIsa));

            // Internals
            var internals = GetPackage(runtime, "Internals", true);

            var add_overload = internals.GetStashGlob(runtime, "add_overload", true);
            add_overload.Code = new P5NativeCode("Internals::add_overload", new P5Code.Sub(WrapAddOverload));

            // Internals::Net (TODO move to external assembly)
            var internals_net = GetPackage(runtime, "Internals::Net", true);

            var get_class = internals_net.GetStashGlob(runtime, "get_class", true);
            get_class.Code = new P5NativeCode("Internals::Net::get_class", new P5Code.Sub(WrapGetClass));

            var specialize_type = internals_net.GetStashGlob(runtime, "specialize_type", true);
            specialize_type.Code = new P5NativeCode("Internals::Net::specialize_type", new P5Code.Sub(WrapSpecializeType));

            var create = internals_net.GetStashGlob(runtime, "create", true);
            create.Code = new P5NativeCode("Internals::Net::create", new P5Code.Sub(WrapCreate));

            var call_method = internals_net.GetStashGlob(runtime, "call_method", true);
            call_method.Code = new P5NativeCode("Internals::Net::call_mehtod", new P5Code.Sub(WrapCallMethod));

            var call_static = internals_net.GetStashGlob(runtime, "call_static", true);
            call_static.Code = new P5NativeCode("Internals::Net::call_static", new P5Code.Sub(WrapCallStatic));

            var get_property = internals_net.GetStashGlob(runtime, "get_property", true);
            get_property.Code = new P5NativeCode("Internals::Net::get_property", new P5Code.Sub(WrapGetProperty));

            var set_property = internals_net.GetStashGlob(runtime, "set_property", true);
            set_property.Code = new P5NativeCode("Internals::Net::set_property", new P5Code.Sub(WrapSetProperty));

            var extend = internals_net.GetStashGlob(runtime, "extend", true);
            extend.Code = new P5NativeCode("Internals::Net::extend", new P5Code.Sub(WrapExtend));

            var compile = internals_net.GetStashGlob(runtime, "compile_assembly", true);
            compile.Code = new P5NativeCode("Internals::Net::compile_assembly", new P5Code.Sub(WrapCompileAssembly));
        }
Exemplo n.º 2
0
        public override P5Scalar Assign(Runtime runtime, IP5Any other)
        {
            var ob = other.AsScalar(runtime).Body;
            var obr = ob as P5Reference;
            var obb = ob as P5TypeglobBody;

            if (obb != null)
                body = globBody = obb;
            else if (obr != null)
            {
                var referred = obr.Referred;
                var code = referred as P5Code;
                var scalar = referred as P5Scalar;
                var array = referred as P5Array;
                var hash = referred as P5Hash;

                if (code != null)
                    globBody.Code = code;
                else if (scalar != null)
                    globBody.Scalar = scalar;
                else if (array != null)
                    globBody.Array = array;
                else if (hash != null)
                    globBody.Hash = hash;
            }
            else
            {
                throw new System.NotImplementedException("Assign either glob or reference");
            }

            return this;
        }
Exemplo n.º 3
0
        public string AsString(Runtime runtime)
        {
            if (reference != null)
                return reference.AsString(runtime);

            return Message;
        }
Exemplo n.º 4
0
 private P5StringNumber(Runtime runtime, int f, int ival, string sval, double fval)
 {
     flags = f;
     integerValue = ival;
     stringValue = sval;
     floatValue = fval;
 }
Exemplo n.º 5
0
        public virtual int AsInteger(Runtime runtime)
        {
            if ((flags & HasString) != 0) return Builtins.ParseInteger(stringValue);
            if ((flags & HasInteger) != 0) return integerValue;
            if ((flags & HasFloat) != 0) return (int)floatValue;

            throw new System.Exception();
        }
Exemplo n.º 6
0
        public virtual IP5ScalarBody Assign(Runtime runtime, IP5ScalarBody other)
        {
            var osn = other as P5Reference;

            if (osn == null)
                return other.CloneBody(runtime);

            referred = osn.referred;

            return this;
        }
Exemplo n.º 7
0
        public override IP5Any AssignIterator(Runtime runtime, IEnumerator<IP5Any> e)
        {
            for (int i = 0; i < array.Count; ++i)
            {
                if (e.MoveNext())
                    array[i] = NetGlue.UnwrapValue<object>(runtime, e.Current);
                else
                    array[i] = null;
            }

            return this;
        }
Exemplo n.º 8
0
        public virtual int AsInteger(Runtime runtime)
        {
            if (referred as P5Scalar != null)
            {
                var wrapper = (referred as P5Scalar).Body as P5NetWrapper;

                if (wrapper != null)
                    return (int)IdDispenser.GetId(wrapper.Object);
            }

            // TODO maybe use long everywhere
            return (int)IdDispenser.GetId(referred);
        }
Exemplo n.º 9
0
        public bool AsBoolean(Runtime runtime)
        {
            var type = obj.GetType();

            if (type == typeof(double))
                return (double)obj != 0.0;
            if (type == typeof(int))
                return (int)obj != 0;
            if (type == typeof(string))
                return (string)obj != "";
            if (type == typeof(bool))
                return (bool)obj;

            return obj != null;
        }
Exemplo n.º 10
0
        public virtual IP5ScalarBody Assign(Runtime runtime, IP5ScalarBody other)
        {
            var osb = other as P5StringNumber;

            if (osb == null)
                return other.CloneBody(runtime);

            flags = osb.flags;
            pos = -1;
            pos_set = false;
            stringValue = osb.stringValue;
            integerValue = osb.integerValue;
            floatValue = osb.floatValue;

            return this;
        }
Exemplo n.º 11
0
        public virtual string AsString(Runtime runtime)
        {
            var rx = referred as IP5Regex;

            // TODO use overloading
            if (rx != null)
                return rx.GetOriginal();
            if (referred.IsBlessed(runtime))
                return string.Format("{2:s}={0:s}(0x{1:x8})",
                                     referred.ReferenceTypeString(runtime),
                                     AsInteger(runtime),
                                     referred.Blessed(runtime).GetName(runtime));
            else
                return string.Format("{0:s}(0x{1:x8})",
                                     referred.ReferenceTypeString(runtime),
                                     AsInteger(runtime));
        }
Exemplo n.º 12
0
        public int AsInteger(Runtime runtime)
        {
            var type = obj.GetType();

            if (type == typeof(int))
                return (int)obj;
            if (type == typeof(double))
                return (int)(double)obj;
            if (type == typeof(char))
                return (int)(char)obj;
            if (type == typeof(bool))
                return (bool)obj ? 1 : 0;
            if (type == typeof(string))
                return Builtins.ParseInteger((string)obj);
            if (type.IsEnum)
                return System.Convert.ToInt32(obj);

            throw new System.NotImplementedException(string.Format("Integer coercion not implemented for {0:S}", type));
        }
Exemplo n.º 13
0
        public double AsFloat(Runtime runtime)
        {
            var type = obj.GetType();

            if (type == typeof(double))
                return (double)obj;
            if (type == typeof(int))
                return (double)(int)obj;
            if (type == typeof(char))
                return (double)(char)obj;
            if (type == typeof(bool))
                return (bool)obj ? 1.0 : 0.0;
            if (type.IsEnum)
                return System.Convert.ToDouble(obj);

            // TODO string

            throw new System.NotImplementedException(string.Format("Float coercion not implemented for {0:S}", type));
        }
Exemplo n.º 14
0
        public override int AssignArray(Runtime runtime, IP5Value other)
        {
            // FIXME multiple dispatch
            P5Scalar s = other as P5Scalar;
            P5Array a = other as P5Array;
            IEnumerator<IP5Any> e = null;
            int c = 0;

            if (s != null)
            {
                e = s.GetEnumerator(runtime);
                c = 1;
            }
            else if (a != null)
            {
                e = a.GetEnumerator(runtime);
                c = a.GetCount(runtime);
            }

            foreach (var i in array)
                i.AssignIterator(runtime, e);

            return c;
        }
Exemplo n.º 15
0
 public P5LvalueList(Runtime runtime, params IP5Any[] data)
     : base(runtime, data)
 {
     flattened = false;
 }
Exemplo n.º 16
0
 public P5List(Runtime runtime, params IP5Any[] data)
     : base(runtime, data)
 {
 }
Exemplo n.º 17
0
 public override P5Scalar AsScalar(Runtime runtime)
 {
     return array.Count == 0 ? new P5Scalar(runtime) : array[array.Count - 1].AsScalar(runtime);
 }
Exemplo n.º 18
0
 public P5List(Runtime runtime, IP5Any value)
     : base(runtime, new IP5Any[] { value })
 {
 }
Exemplo n.º 19
0
 public P5List(Runtime runtime, List<IP5Any> data)
     : base(runtime, data)
 {
 }
Exemplo n.º 20
0
 public P5List(Runtime runtime, int size)
     : base(runtime, size)
 {
 }
Exemplo n.º 21
0
 public P5List(Runtime runtime, bool value)
     : base(runtime)
 {
     if (value)
         array.Add(new P5Scalar(runtime, 1));
 }
Exemplo n.º 22
0
        public override P5List Slice(Runtime runtime, P5Array keys)
        {
            if (!flattened)
                Flatten(runtime);

            return base.Slice(runtime, keys);
        }
Exemplo n.º 23
0
        private void Flatten(Runtime runtime)
        {
            var old = array;

            array = new List<IP5Any>();
            foreach (var i in old)
                PushFlatten(runtime, i);

            flattened = true;
        }
Exemplo n.º 24
0
        public new IEnumerator<IP5Any> GetEnumerator(Runtime runtime)
        {
            if (!flattened)
                Flatten(runtime);

            return base.GetEnumerator();
        }
Exemplo n.º 25
0
 public P5List(Runtime runtime)
     : base(runtime)
 {
 }
Exemplo n.º 26
0
        public override IP5Any Clone(Runtime runtime, int depth)
        {
            P5Array clone = new P5Array(runtime, array.Count);

            if (depth > 0)
            {
                foreach (var i in array)
                {
                    var enumerable = i as IP5Enumerable;

                    if (enumerable != null)
                        clone.PushFlatten(runtime, i);
                    else
                        clone.Push(runtime, i.Clone(runtime, depth - 1));
                }
            }
            else
                foreach (var i in array)
                    clone.PushFlatten(runtime, i);

            return clone;
        }
Exemplo n.º 27
0
 public P5Code GetCode(Runtime runtime)
 {
     return globBody.Code ??
         (globBody.Code = new P5Code(globBody.Name, null));
 }
Exemplo n.º 28
0
 public override bool IsDefined(Runtime runtime)
 {
     return AsScalar(runtime).IsDefined(runtime);
 }
Exemplo n.º 29
0
        public virtual P5List Slice(Runtime runtime, P5Array keys)
        {
            var res = new P5List(runtime);
            var list = new List<IP5Any>(keys.GetCount(runtime));
            bool found = false;

            foreach (var key in keys)
            {
                int i = key.AsInteger(runtime);

                if (i < array.Count)
                    found = true;
                list.Add(GetItemOrUndef(runtime, key, false));
            }

            if (found)
                res.SetArray(list);

            return res;
        }
Exemplo n.º 30
0
 public P5LvalueList(Runtime runtime, List<IP5Any> data)
     : base(runtime, data)
 {
     flattened = false;
 }