Exemplo 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;
            }
        }
Exemplo 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;
        }
Exemplo 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;
        }
Exemplo 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;
        }
Exemplo 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));
        }
Exemplo 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));
 }
Exemplo 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));
 }
Exemplo 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;
        }
Exemplo n.º 9
0
 public override Value InsertBefore(int i, Value v)
 {
     return Val.InsertBefore(i, v);
 }
Exemplo n.º 10
0
 public override Value AddRange(Value v)
 {
     return new GTTree().AddRange(v);
 }
Exemplo n.º 11
0
 public GTTree(Value val, Value left, Value right)
 {
     this.Value = val;
     this.Left = left;
     this.Right = right;
 }
Exemplo n.º 12
0
 public static Value mul(Value left, Value right)
 {
     return new MulOperator(left, right).Evaluate(null);
 }
Exemplo n.º 13
0
 public static Value div(Value left, Value right)
 {
     return new DivOperator(left, right).Evaluate(null);
 }
Exemplo n.º 14
0
 public static Value add(Value left, Value right)
 {
     return new AddOperator(left, right).Evaluate(null);
 }
Exemplo n.º 15
0
 public static Value stringify(Value s)
 {
     return new GTString(s.ToString());
 }
Exemplo n.º 16
0
 public override Value Set(int i, Value v)
 {
     throw new IndexOutOfRangeException();
 }
Exemplo n.º 17
0
 public override Value InsertBefore(int i, Value v)
 {
     throw new IndexOutOfRangeException();
 }
Exemplo 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));
        }
Exemplo n.º 19
0
 public GTTree(Value val)
     : this()
 {
     this.Value = val;
     this.ThisCache = 1;
 }
Exemplo n.º 20
0
 public override Value Add(Value v)
 {
     return Val.Add(v);
 }
Exemplo n.º 21
0
 public static Value sub(Value left, Value right)
 {
     return new SubOperator(left, right).Evaluate(null);
 }
Exemplo n.º 22
0
 public override Value Add(Value v)
 {
     return new GTTree(v);
 }
Exemplo n.º 23
0
 public virtual Value AddRange(Value v)
 {
     return new GTTree(this).AddRange(v);
 }
Exemplo n.º 24
0
 public virtual Value InsertBefore(int i, Value val)
 {
     return new GTTree(this).InsertBefore(i, val);
 }
Exemplo n.º 25
0
 public override Value InsertAfter(int i, Value v)
 {
     return Val.InsertAfter(i, v);
 }
Exemplo n.º 26
0
 public virtual Value Set(int i, Value val)
 {
     return new GTTree(this).Set(i, val);
 }
Exemplo n.º 27
0
 public override Value Set(int i, Value v)
 {
     return Val.Set(i, v);
 }
Exemplo 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;
        }