示例#1
0
文件: Scope.cs 项目: bash0r/Green-Tea
        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;
            }
        }
示例#2
0
文件: IO.cs 项目: bash0r/Green-Tea
        public static GTVoid print(Value v)
        {
            if (v.Type == GTType.String)
                Console.WriteLine(((GTString)v).Value);
            else
                Console.WriteLine(v);

            return GTVoid.Void;
        }
示例#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;
        }
示例#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;
        }
示例#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));
        }
示例#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));
 }
示例#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));
 }
示例#8
0
文件: Scope.cs 项目: bash0r/Green-Tea
        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;
        }
示例#9
0
 public override Value InsertBefore(int i, Value v)
 {
     return Val.InsertBefore(i, v);
 }
示例#10
0
 public override Value AddRange(Value v)
 {
     return new GTTree().AddRange(v);
 }
示例#11
0
 public GTTree(Value val, Value left, Value right)
 {
     this.Value = val;
     this.Left = left;
     this.Right = right;
 }
示例#12
0
 public static Value mul(Value left, Value right)
 {
     return new MulOperator(left, right).Evaluate(null);
 }
示例#13
0
 public static Value div(Value left, Value right)
 {
     return new DivOperator(left, right).Evaluate(null);
 }
示例#14
0
 public static Value add(Value left, Value right)
 {
     return new AddOperator(left, right).Evaluate(null);
 }
示例#15
0
 public static Value stringify(Value s)
 {
     return new GTString(s.ToString());
 }
示例#16
0
 public override Value Set(int i, Value v)
 {
     throw new IndexOutOfRangeException();
 }
示例#17
0
 public override Value InsertBefore(int i, Value v)
 {
     throw new IndexOutOfRangeException();
 }
示例#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));
        }
示例#19
0
 public GTTree(Value val)
     : this()
 {
     this.Value = val;
     this.ThisCache = 1;
 }
示例#20
0
 public override Value Add(Value v)
 {
     return Val.Add(v);
 }
示例#21
0
 public static Value sub(Value left, Value right)
 {
     return new SubOperator(left, right).Evaluate(null);
 }
示例#22
0
 public override Value Add(Value v)
 {
     return new GTTree(v);
 }
示例#23
0
 public virtual Value AddRange(Value v)
 {
     return new GTTree(this).AddRange(v);
 }
示例#24
0
 public virtual Value InsertBefore(int i, Value val)
 {
     return new GTTree(this).InsertBefore(i, val);
 }
示例#25
0
 public override Value InsertAfter(int i, Value v)
 {
     return Val.InsertAfter(i, v);
 }
示例#26
0
 public virtual Value Set(int i, Value val)
 {
     return new GTTree(this).Set(i, val);
 }
示例#27
0
 public override Value Set(int i, Value v)
 {
     return Val.Set(i, v);
 }
示例#28
0
文件: Scope.cs 项目: bash0r/Green-Tea
        public Value Find(string name, Value newval = null)
        {
            Value ret;

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

            return ret;
        }