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; } }
public static GTVoid print(Value v) { if (v.Type == GTType.String) Console.WriteLine(((GTString)v).Value); else Console.WriteLine(v); return GTVoid.Void; }
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; }
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; }
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)); }
public override Value AddRange(Value v) { if (Right == null) return new GTTree(Value, Left, v); else return new GTTree(Value, Left, Right.AddRange(v)); }
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)); }
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; }
public override Value InsertBefore(int i, Value v) { return Val.InsertBefore(i, v); }
public override Value AddRange(Value v) { return new GTTree().AddRange(v); }
public GTTree(Value val, Value left, Value right) { this.Value = val; this.Left = left; this.Right = right; }
public static Value mul(Value left, Value right) { return new MulOperator(left, right).Evaluate(null); }
public static Value div(Value left, Value right) { return new DivOperator(left, right).Evaluate(null); }
public static Value add(Value left, Value right) { return new AddOperator(left, right).Evaluate(null); }
public static Value stringify(Value s) { return new GTString(s.ToString()); }
public override Value Set(int i, Value v) { throw new IndexOutOfRangeException(); }
public override Value InsertBefore(int i, Value v) { throw new IndexOutOfRangeException(); }
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)); }
public GTTree(Value val) : this() { this.Value = val; this.ThisCache = 1; }
public override Value Add(Value v) { return Val.Add(v); }
public static Value sub(Value left, Value right) { return new SubOperator(left, right).Evaluate(null); }
public override Value Add(Value v) { return new GTTree(v); }
public virtual Value AddRange(Value v) { return new GTTree(this).AddRange(v); }
public virtual Value InsertBefore(int i, Value val) { return new GTTree(this).InsertBefore(i, val); }
public override Value InsertAfter(int i, Value v) { return Val.InsertAfter(i, v); }
public virtual Value Set(int i, Value val) { return new GTTree(this).Set(i, val); }
public override Value Set(int i, Value v) { return Val.Set(i, v); }
public Value Find(string name, Value newval = null) { Value ret; if (!TryFind(name, out ret, newval)) throw new KeyNotFoundException("Unknown variable: " + name); return ret; }