Пример #1
0
        internal string Stringify(HashSet <NeoValue> seen)
        {
            if (data.Count == 0)
            {
                return("[]");
            }

            var builder = new StringBuilder();

            builder.Append("[ ");

            for (var i = 0; i < data.Count; i++)
            {
                builder.Append(NeoValue.StringifyElement(seen, data[i]));

                if (i + 1 < data.Count)
                {
                    builder.Append(", ");
                }
            }

            builder.Append(" ]");

            return(builder.ToString());
        }
Пример #2
0
        internal string Stringify(HashSet <NeoValue> seen)
        {
            if (data.Count == 0)
            {
                return("{}");
            }

            var builder = new StringBuilder();

            builder.Append("{ ");

            var i = 1;

            foreach (var mapping in data)
            {
                builder.Append(NeoValue.StringifyElement(seen, mapping.Key));
                builder.Append(" = ");
                builder.Append(NeoValue.StringifyElement(seen, mapping.Value));

                if (i < data.Count)
                {
                    builder.Append(", ");
                }
                i++;
            }

            builder.Append(" }");

            return(builder.ToString());
        }
Пример #3
0
 internal static string StringifyElement(HashSet <NeoValue> seen, NeoValue value)
 {
     if (value is NeoObject o)
     {
         if (seen.Contains(value))
         {
             return("<already-seen>");
         }
         else
         {
             seen.Add(value);
             return(o.Stringify(seen));
         }
     }
     else if (value is NeoArray a)
     {
         if (seen.Contains(value))
         {
             return("[already-seen]");
         }
         else
         {
             seen.Add(value);
             return(a.Stringify(seen));
         }
     }
     else
     {
         return(value.ToNeoString());
     }
 }
Пример #4
0
        public override bool Equals(NeoValue value, bool deep)
        {
            if (!value.IsArray)
            {
                return(false);
            }

            var other = value.CheckArray();

            if (other.data.Count != data.Count)
            {
                return(false);
            }

            if (deep)
            {
                for (var i = 0; i < data.Count; i++)
                {
                    if (!other[i].Equals(this[i], true))
                    {
                        return(false);
                    }
                }
                return(true);
            }
            else
            {
                return(value == this);
            }
        }
Пример #5
0
 public NeoValue RawGet(NeoValue key)
 {
     if (data.ContainsKey(key))
     {
         return(data[key]);
     }
     else
     {
         return(NeoNil.NIL);
     }
 }
Пример #6
0
 public override int Compare(NeoValue other)
 {
     if (other.IsInt)
     {
         return(((double)other.CheckInt().Value).CompareTo(Value));
     }
     else if (other.IsFloat)
     {
         return(other.CheckFloat().Value.CompareTo(Value));
     }
     throw new NeoError($"Attempt to compare float to {other.Type}");
 }
Пример #7
0
        public override NeoValue BitXor(NeoValue other)
        {
            var mm = GetMetaMethod("__bitxor", other);

            if (mm != null)
            {
                return(mm.Call(new [] { this, other }));
            }
            else
            {
                throw new NeoError($"attempt to bit-xor {Type} and {other.Type}");
            }
        }
Пример #8
0
        public override NeoValue Get(NeoValue key)
        {
            var name = key.CheckString().Value;

            if (Scope.IsExported(name))
            {
                return(Scope.Get(name));
            }
            else
            {
                return(NeoNil.NIL);
            }
        }
Пример #9
0
        public override NeoValue Pow(NeoValue other)
        {
            var mm = GetMetaMethod("__pow", other);

            if (mm != null)
            {
                return(mm.Call(new [] { this, other }));
            }
            else
            {
                throw new NeoError($"attempt to exponentiate {Type} and {other.Type}");
            }
        }
Пример #10
0
        public override NeoValue Concat(NeoValue other)
        {
            var mm = GetMetaMethod("__concat", other);

            if (mm != null)
            {
                return(mm.Call(new [] { this, other }));
            }
            else
            {
                throw new NeoError($"attempt to concat {Type} and {other.Type}");
            }
        }
Пример #11
0
        public override NeoValue Rsh(NeoValue other)
        {
            var mm = GetMetaMethod("__rsh", other);

            if (mm != null)
            {
                return(mm.Call(new [] { this, other }));
            }
            else
            {
                throw new NeoError($"attempt to right-shift {Type} and {other.Type}");
            }
        }
Пример #12
0
        public override NeoValue Slice(NeoValue start, NeoValue end)
        {
            var mm = GetMetaMethod("__slice");

            if (mm != null)
            {
                return(mm.Call(new [] { this, start, end }));
            }
            else
            {
                throw new NeoError($"attenpt to slice {Type}");
            }
        }
Пример #13
0
 public override bool Equals(NeoValue other, bool deep)
 {
     if (other.IsInt)
     {
         return(((double)other.CheckInt().Value) == Value);
     }
     else if (other.IsFloat)
     {
         return(other.CheckFloat().Value == Value);
     }
     else
     {
         return(false);
     }
 }
Пример #14
0
        public override void Set(NeoValue key, NeoValue value)
        {
            var mm = GetMetaMethod("__set");

            if (mm != null)
            {
                mm.Call(new [] { this, key, value });
            }
            else
            {
                if (!data.ContainsKey(key))
                {
                    keys.Add(key);
                }
                data[key] = value;
            }
        }
Пример #15
0
        public override NeoValue Slice(NeoValue start, NeoValue end)
        {
            var s = start.CheckInt().Value;
            var e = end.CheckInt().Value;
            var d = s > e ? -1 : 1;

            var a = new NeoArray();
            var i = s;

            while (i != e)
            {
                a.Insert(this[i]);
                i += d;
            }
            a.Insert(this[i]);

            return(a);
        }
Пример #16
0
        public override int Compare(NeoValue other)
        {
            var mm = GetMetaMethod("__compare", other);

            if (mm != null)
            {
                var r = mm.Call(new [] { this, other });
                return(r.CheckInt().Value);
            }
            else
            {
                if (!other.IsObject)
                {
                    return(base.Compare(other));
                }

                return(base.Compare(other));
            }
        }
Пример #17
0
        public override int Compare(NeoValue other)
        {
            var otherNumber = other.CheckNumber();
            var a           = AsDouble;
            var b           = otherNumber.AsDouble;

            if (a > b)
            {
                return(1);
            }
            else if (a < b)
            {
                return(1);
            }
            else
            {
                return(0);
            }
        }
Пример #18
0
        public override NeoValue Get(NeoValue key)
        {
            var mm = GetMetaMethod("__get");

            if (mm != null)
            {
                return(mm.Call(new [] { this, key }));
            }
            else
            {
                if (data.ContainsKey(key))
                {
                    return(data[key]);
                }
                else
                {
                    return(NeoNil.NIL);
                }
            }
        }
Пример #19
0
        public override bool Equals(NeoValue value, bool deep)
        {
            var mm = GetMetaMethod("__equals", value);

            if (mm != null)
            {
                var r = mm.Call(new [] { this, value, NeoBool.ValueOf(deep) });
                return(r.CheckBool().Value);
            }
            else
            {
                if (!value.IsObject)
                {
                    return(false);
                }

                var other = value.CheckObject();
                if (other.data.Count != data.Count)
                {
                    return(false);
                }

                if (deep)
                {
                    foreach (var mapping in data)
                    {
                        if (!mapping.Value.Equals(other.Get(mapping.Key), true))
                        {
                            return(false);
                        }
                    }
                    return(true);
                }
                else
                {
                    return(this == other);
                }
            }
        }
Пример #20
0
 public override NeoValue BitXor(NeoValue other) => obj.BitXor(other);
Пример #21
0
 public override NeoValue Concat(NeoValue other) => obj.Concat(other);
Пример #22
0
 public override NeoValue BitAnd(NeoValue other) => obj.BitAnd(other);
Пример #23
0
 public override NeoValue Rsh(NeoValue other) => obj.Rsh(other);
Пример #24
0
 public override NeoValue Lsh(NeoValue other) => obj.Lsh(other);
Пример #25
0
 public override NeoValue Mod(NeoValue other) => obj.Mod(other);
Пример #26
0
 public override NeoValue Slice(NeoValue start, NeoValue end) => obj.Slice(start, end);
Пример #27
0
 public override void Remove(NeoValue key) => throw new NeoError("attempt to modify frozen object");
Пример #28
0
 public override NeoValue Div(NeoValue other) => obj.Div(other);
Пример #29
0
 public void RawSet(NeoValue key, NeoValue value)
 {
     data[key] = value;
 }
Пример #30
0
 public override NeoValue Pow(NeoValue other) => obj.Pow(other);