Example #1
0
        public Value Evaluate(Scope scope)
        {
            Value v = Body.Evaluate(scope);
            scope.Add(Name, v, Mode);

            return v;
        }
Example #2
0
        public Value Evaluate(Scope scope)
        {
            foreach (var c in Cases)
                if (c.Item1.Evaluate(scope).IsTrue())
                    return c.Item2.Evaluate(scope);

            return Else.Evaluate(scope);
        }
Example #3
0
        public override Value Evaluate(Scope scope)
        {
            Value ret = new GTTree();

            foreach (var v in body.Evaluate(scope).Enumerate())
            {
                Scope s = new Scope(scope);
                s.Add(name, v);

                ret = ret.AddRange(child.Evaluate(s));
            }

            return ret;
        }
Example #4
0
        public Value Evaluate(Scope scope)
        {
            Scope s;

            if (Parent == null)
                s = new Scope(scope, true);
            else
                s = new Scope(from v in Parent.Evaluate(scope).Enumerate()
                              where v.Type == GTType.Scope
                              select (Scope)v, true);

            Body.Evaluate(s); // return value is ignored

            return s;
        }
Example #5
0
        public Value Evaluate(IExpression left, IExpression right, Scope scope)
        {
            Value l = left.Evaluate(scope);
            Value r = right.Evaluate(scope);

            if (l.Count > 1 && l.Count == r.Count)
            {
                var lv = new List<Value>(l.Enumerate());
                var rv = new List<Value>(r.Enumerate());
                var res = new List<Value>(lv.Count);

                for (int i = 0; i < lv.Count(); i++)
                    res.Add(Evaluate(lv[i], rv[i], scope));

                return new GTTree(res);
            }

            if (l.Count > 1 && r.Count == 1)
                return new GTTree(from x in l.Enumerate()
                                  select Evaluate(x, r, scope));

            if (r.Count > 1 && l.Count == 1)
                return new GTTree(from x in r.Enumerate()
                                  select Evaluate(l, x, scope));

            if (l is IGTAdapter && r is IGTAdapter)
            {
                dynamic res = Fun.Invoke(((IGTAdapter)l).Get(), ((IGTAdapter)r).Get());

                if (l.Type == GTType.Float || r.Type == GTType.Float)
                    return new GTFloat(res);
                else
                    return new GTInt(res);
            }

            throw new InvalidOperationException(String.Format("Cannot use operator {0} on types {1} and {2}", this, l.Type, r.Type));
        }
Example #6
0
 public Value Evaluate(Scope scope)
 {
     return new GTFunction(Body, scope.Close(), Parameters);
 }
Example #7
0
 public Value Evaluate(Scope scope)
 {
     if (Condition.Evaluate(scope).IsTrue())
         return Body.Evaluate(scope);
     else
         return Else.Evaluate(scope);
 }
Example #8
0
 public override Value Evaluate(Scope scope)
 {
     return Left.Evaluate(scope).AddRange(Right.Evaluate(scope));
 }
Example #9
0
 public Value Evaluate(Scope scope)
 {
     return new GTTree(Expressions.Select(e => e.Evaluate(scope)));
 }
Example #10
0
        public Value Evaluate(Scope scope)
        {
            if (Scope == null)
                return scope.Find(Name);
            else
            {
                Value v = Scope.Evaluate(scope).Self();

                if (v.Type == GTType.List)
                    return new GTTree(from s in v.Enumerate()
                                      select new Usage(Name, s).Evaluate(scope));
                else if (v.Type == GTType.Scope)
                    return ((Scope)v).Find(Name);
                else
                    throw new InvalidOperationException("Cannot dereference from non-scope value: " + v.ToString());
            }
        }
Example #11
0
        public override Value Evaluate(Scope scope)
        {
            int res = Left.Evaluate(scope).CompareTo(Right.Evaluate(scope));

            return new GTBool(res == -1 || res == 0); // can't be -2
        }
Example #12
0
 public Value Evaluate(Scope scope)
 {
     return root.Evaluate(scope);
 }
Example #13
0
 public abstract Value Evaluate(Scope scope);
Example #14
0
 // AddLazy
 public virtual Value AddExp(IExpression exp, Scope scope)
 {
     return new GTTree(this, null, new GTLazy(exp, scope));
 }
Example #15
0
 public Value Evaluate(Scope scope)
 {
     return this;
 }
Example #16
0
        public Value Evaluate(Scope scope)
        {
            // Evaluate the function
            Value v = Function.Evaluate(scope);
            Value ret = new GTTree();
            int c = v.Count;

            foreach (var res in v.Enumerate())
            {
                var r = res.Self();

                if (r.Type != GTType.Function)
                    if (c == 1)
                        throw new InvalidOperationException("Cannot call non-function type");
                    else
                        continue;

                GTFunction f = (GTFunction)res;

                // Check the parameter count
                if (Parameters.Count > f.Parameters.Count)
                    if (c == 1)
                        throw new InvalidOperationException("Too many arguments in call to function: " + Function.ToString());
                    else
                        continue;

                // Create evaluation context
                Scope s = new Scope(f.Container);

                // Add this() for recursion
                s.Add("this", f);

                for (int i = 0; i < Parameters.Count; i++)
                    s.Add(f.Parameters[i], Parameters[i].Evaluate(scope));

                // All parameters filled
                if (Parameters.Count == f.Parameters.Count)
                {
                    Value o;

                    if (f.Body == null)
                    {
                        // Invoke the method
                        var w = (WrapperFunc)f;
                        o = (Value)w.Method.Invoke(null, (from par in w.Parameters
                                                          select new Usage(par).Evaluate(s)).ToArray());
                    }
                    else
                        o = f.Body.Evaluate(s);

                    ret = ret.Add(o);
                    continue;
                }

                // No partial application in list context
                if (c > 1)
                    continue;

                // Partial application: Create a new function
                var newparams = new List<string>();

                for (int i = f.Parameters.Count - Parameters.Count; i < f.Parameters.Count; i++)
                    newparams.Add(f.Parameters[i]);

                return new GTFunction(f.Body, s, newparams);
            }

            if (ret.Count == 1)
                return ret[0]; // single values -> skip the list stuff
            else
                return ret;
        }
Example #17
0
 internal static void AddTo(Scope scope)
 {
     foreach (var kvp in Funcs)
         scope.Add(kvp.Key, kvp.Value, ScopeMode.Public);
 }
Example #18
0
 public Value Evaluate(Scope scope)
 {
     return Body.Evaluate(scope);
 }
Example #19
0
 public override Value Evaluate(Scope scope)
 {
     return new GTBool(Left.Evaluate(scope).IsTrue() || Right.Evaluate(scope).IsTrue());
 }
Example #20
0
 public GTFunction(IExpression body, Scope parent, IEnumerable<string> args)
 {
     this.Body = body;
     this.Container = parent;
     this.Parameters = new List<string>(args);
 }
Example #21
0
 public override Value Evaluate(Scope scope)
 {
     return new GTTree(body.Evaluate(scope));
 }
Example #22
0
 public Scope(Scope parent, bool HasStatic = false)
     : this(new Scope[] { parent }, HasStatic)
 {
 }
Example #23
0
 public override Value Evaluate(Scope scope)
 {
     if (body.Evaluate(scope).IsTrue())
         return child.Evaluate(scope);
     else
         return new GTTree();
 }
Example #24
0
 public Value Evaluate(Scope scope)
 {
     return scope.Find(Name, Body.Evaluate(scope));
 }
Example #25
0
 public override Value Evaluate(Scope scope)
 {
     return Evaluate(Left, Right, scope);
 }
Example #26
0
 public override Value Evaluate(Scope scope)
 {
     return new GTBool(Left.Evaluate(scope).CompareTo(Right.Evaluate(scope)) == 1);
 }
Example #27
0
 public GTLazy(IExpression exp, Scope scope)
 {
     this.Body = exp;
     this.Container = scope;
 }
Example #28
0
        public Scope Close()
        {
            // create a new child scope
            Scope s = new Scope(from p in Parents
                                select p.Close());

            // Duplicate all variables
            foreach (var v in Local)
                s.Add(v.Key, v.Value);

            // Apend self for static referencing
            s.Parents.Add(this);

            return s;
        }