Esempio n. 1
0
        public void Add(string name, Value val, ScopeMode mode = ScopeMode.Local)
        {
            switch (mode)
            {
                case ScopeMode.Local:
                    Local.Add(name, val);
                    break;

                case ScopeMode.Public:
                case ScopeMode.Private:
                    if (HasStatic)
                        (mode == ScopeMode.Public ? Public : Private).Add(name, val);
                    else
                    {
                        foreach (var p in Parents)
                            try
                            {
                                p.Add(name, val, mode);
                                return;
                            }
                            catch (InvalidOperationException)
                            {
                                continue;
                            }

                        // End of list reached
                        throw new InvalidOperationException("Cannot add a static variable to an orphan scope");
                    }
                    break;
            }
        }
Esempio n. 2
0
        public static GTVoid print(Value v)
        {
            if (v.Type == GTType.String)
                Console.WriteLine(((GTString)v).Value);
            else
                Console.WriteLine(v);

            return GTVoid.Void;
        }
Esempio n. 3
0
        public GTTree(IEnumerable<Value> list)
            : this()
        {
            var v = new GTTree();

            foreach (Value ex in list)
                v = (GTTree)v.Add(ex);

            // fake mutator
            this.Value = v.Value;
            this.Left = v.Left;
            this.Right = v.Right;
        }
Esempio n. 4
0
        internal static int CompareTo(this Value a, Value b)
        {
            a = a.Self();
            b = b.Self();

            if (a.IsNumber() && b.IsNumber())
                return a.AsNumber().CompareTo(b.AsNumber());

            if (a.Type != b.Type)
                return -2; // invalid

            switch (a.Type)
            {
                case GTType.Bool:
                    return ((GTBool)a).Value.CompareTo(((GTBool)b).Value);

                case GTType.Function:
                    return a == b ? 0 : -1;

                case GTType.List:
                    if (a.Count != b.Count)
                        return a.Count.CompareTo(b.Count);

                    for (int i = 0; i < a.Count; i++)
                    {
                        var comp = a[i].CompareTo(b[i]);

                        if (comp != 0)
                            return comp;
                    }

                    return 0;

                case GTType.String:
                    return ((GTString)a).Value.CompareTo(((GTString)b).Value);

                case GTType.Void:
                    return -1;
            }

            return -2;
        }
Esempio n. 5
0
        public override Value InsertBefore(int i, Value v)
        {
            if (i < LeftCount)
                return new GTTree(Value, Left.InsertBefore(i, v), Right);

            else if (i == LeftCount && ThisCount != 0)
            {
                if (Left == null)
                    return new GTTree(Value, new GTTree(v), Right);
                else
                    return new GTTree(Value, Left.InsertAfter(i, v), Right);
            }

            else if (RightCount == 0)
                throw new IndexOutOfRangeException();

            else
                return new GTTree(Value, Left, Right.InsertBefore(i - LeftCount - ThisCount, v));
        }
Esempio n. 6
0
 public override Value AddRange(Value v)
 {
     if (Right == null)
         return new GTTree(Value, Left, v);
     else
         return new GTTree(Value, Left, Right.AddRange(v));
 }
Esempio n. 7
0
 public override Value Add(Value v)
 {
     if (LeftSize <= RightSize)
         return new GTTree(v, this, null);
     else
         if (Right == null)
             return new GTTree(Value, Left, new GTTree(v));
         else
             return new GTTree(Value, Left, Right.Add(v));
 }
Esempio n. 8
0
        public bool TryFind(string name, out Value ret, Value newval = null)
        {
            // check for local copy
            if (Local.ContainsKey(name))
            {
                if (newval != null)
                    Local[name] = newval;

                ret = Local[name];
                return true;
            }

            if (HasStatic)
            {
                // check for private copy
                if (Private.ContainsKey(name))
                {
                    if (newval != null)
                        Private[name] = newval;

                    ret = Private[name];
                    return true;
                }

                // check for public copy
                if (Public.ContainsKey(name))
                {
                    if (newval != null)
                        Public[name] = newval;

                    ret = Public[name];
                    return true;
                }
            }

            // otherwise recurse upwards
            foreach (var p in Parents)
                if (p.TryFind(name, out ret, newval))
                    return true;

            ret = GTVoid.Void;
            return false;
        }
Esempio n. 9
0
 public override Value InsertBefore(int i, Value v)
 {
     return Val.InsertBefore(i, v);
 }
Esempio n. 10
0
 public override Value AddRange(Value v)
 {
     return new GTTree().AddRange(v);
 }
Esempio n. 11
0
 public GTTree(Value val, Value left, Value right)
 {
     this.Value = val;
     this.Left = left;
     this.Right = right;
 }
Esempio n. 12
0
 public static Value mul(Value left, Value right)
 {
     return new MulOperator(left, right).Evaluate(null);
 }
Esempio n. 13
0
 public static Value div(Value left, Value right)
 {
     return new DivOperator(left, right).Evaluate(null);
 }
Esempio n. 14
0
 public static Value add(Value left, Value right)
 {
     return new AddOperator(left, right).Evaluate(null);
 }
Esempio n. 15
0
 public static Value stringify(Value s)
 {
     return new GTString(s.ToString());
 }
Esempio n. 16
0
 public override Value Set(int i, Value v)
 {
     throw new IndexOutOfRangeException();
 }
Esempio n. 17
0
 public override Value InsertBefore(int i, Value v)
 {
     throw new IndexOutOfRangeException();
 }
Esempio n. 18
0
        public override Value Set(int i, Value v)
        {
            if (i < LeftCount)
                return new GTTree(Value, Left.Set(i, v), Right);

            else if (i == LeftCount && ThisCount != 0)
                return new GTTree(v, Left, Right);

            else if (RightCount == 0)
                throw new IndexOutOfRangeException();

            else
                return new GTTree(Value, Left, Right.Set(i - LeftCount - ThisCount, v));
        }
Esempio n. 19
0
 public GTTree(Value val)
     : this()
 {
     this.Value = val;
     this.ThisCache = 1;
 }
Esempio n. 20
0
 public override Value Add(Value v)
 {
     return Val.Add(v);
 }
Esempio n. 21
0
 public static Value sub(Value left, Value right)
 {
     return new SubOperator(left, right).Evaluate(null);
 }
Esempio n. 22
0
 public override Value Add(Value v)
 {
     return new GTTree(v);
 }
Esempio n. 23
0
 public virtual Value AddRange(Value v)
 {
     return new GTTree(this).AddRange(v);
 }
Esempio n. 24
0
 public virtual Value InsertBefore(int i, Value val)
 {
     return new GTTree(this).InsertBefore(i, val);
 }
Esempio n. 25
0
 public override Value InsertAfter(int i, Value v)
 {
     return Val.InsertAfter(i, v);
 }
Esempio n. 26
0
 public virtual Value Set(int i, Value val)
 {
     return new GTTree(this).Set(i, val);
 }
Esempio n. 27
0
 public override Value Set(int i, Value v)
 {
     return Val.Set(i, v);
 }
Esempio n. 28
0
        public Value Find(string name, Value newval = null)
        {
            Value ret;

            if (!TryFind(name, out ret, newval))
                throw new KeyNotFoundException("Unknown variable: " + name);

            return ret;
        }