Beispiel #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));
        }
Beispiel #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;
        }
Beispiel #3
0
        public string AsString(Runtime runtime)
        {
            if (reference != null)
                return reference.AsString(runtime);

            return Message;
        }
Beispiel #4
0
 private P5StringNumber(Runtime runtime, int f, int ival, string sval, double fval)
 {
     flags = f;
     integerValue = ival;
     stringValue = sval;
     floatValue = fval;
 }
Beispiel #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();
        }
Beispiel #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;
        }
Beispiel #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;
        }
Beispiel #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);
        }
Beispiel #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;
        }
Beispiel #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;
        }
Beispiel #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));
        }
Beispiel #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));
        }
Beispiel #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));
        }
Beispiel #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;
        }
Beispiel #15
0
 public P5LvalueList(Runtime runtime, params IP5Any[] data)
     : base(runtime, data)
 {
     flattened = false;
 }
Beispiel #16
0
 public P5List(Runtime runtime, params IP5Any[] data)
     : base(runtime, data)
 {
 }
Beispiel #17
0
 public override P5Scalar AsScalar(Runtime runtime)
 {
     return array.Count == 0 ? new P5Scalar(runtime) : array[array.Count - 1].AsScalar(runtime);
 }
Beispiel #18
0
 public P5List(Runtime runtime, IP5Any value)
     : base(runtime, new IP5Any[] { value })
 {
 }
Beispiel #19
0
 public P5List(Runtime runtime, List<IP5Any> data)
     : base(runtime, data)
 {
 }
Beispiel #20
0
 public P5List(Runtime runtime, int size)
     : base(runtime, size)
 {
 }
Beispiel #21
0
 public P5List(Runtime runtime, bool value)
     : base(runtime)
 {
     if (value)
         array.Add(new P5Scalar(runtime, 1));
 }
Beispiel #22
0
        public override P5List Slice(Runtime runtime, P5Array keys)
        {
            if (!flattened)
                Flatten(runtime);

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

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

            flattened = true;
        }
Beispiel #24
0
        public new IEnumerator<IP5Any> GetEnumerator(Runtime runtime)
        {
            if (!flattened)
                Flatten(runtime);

            return base.GetEnumerator();
        }
Beispiel #25
0
 public P5List(Runtime runtime)
     : base(runtime)
 {
 }
Beispiel #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;
        }
Beispiel #27
0
 public P5Code GetCode(Runtime runtime)
 {
     return globBody.Code ??
         (globBody.Code = new P5Code(globBody.Name, null));
 }
Beispiel #28
0
 public override bool IsDefined(Runtime runtime)
 {
     return AsScalar(runtime).IsDefined(runtime);
 }
Beispiel #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;
        }
Beispiel #30
0
 public P5LvalueList(Runtime runtime, List<IP5Any> data)
     : base(runtime, data)
 {
     flattened = false;
 }