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

            return v;
        }
Esempio n. 2
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;
        }
Esempio n. 3
0
 internal static void AddTo(Scope scope)
 {
     foreach (var kvp in Funcs)
         scope.Add(kvp.Key, kvp.Value, ScopeMode.Public);
 }
Esempio n. 4
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;
        }
Esempio n. 5
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;
        }