public void ShouldSetValue()
        {
            ValueEnvironment environment = new ValueEnvironment();

            environment.SetValue("foo", "bar");
            Assert.AreEqual("bar", environment.GetValue("foo"));
        }
        public void ShouldResetValueUsingResetInTrue()
        {
            ValueEnvironment environment = new ValueEnvironment();

            environment.SetValue("foo", "bar");
            environment.SetValue("foo", "rebar", true);
        }
        public void ShouldRaiseIfTryToSetAnAlreadyDefinedValueWithResetInFalse()
        {
            ValueEnvironment environment = new ValueEnvironment();

            environment.SetValue("foo", "bar");
            environment.SetValue("foo", "newbar", false);
        }
示例#4
0
        public object Apply(Machine machine, ValueEnvironment environment, object[] argumentValues)
        {
            foreach (DefinedMacro macro in this.macros)
                if ((macro.Arity == 0 && argumentValues == null) || macro.Arity == argumentValues.Length || (macro.Arity <= argumentValues.Length && macro.VariableArity))
                    return macro.Apply(machine, environment, argumentValues);

            throw new InvalidOperationException("Invalid number of parameters");
        }
        public void ShouldGetValueFromParent()
        {
            ValueEnvironment parent = new ValueEnvironment();
            ValueEnvironment environment = new ValueEnvironment(parent);

            parent.SetValue("foo", "bar");

            Assert.AreEqual("bar", environment.GetValue("foo"));
        }
        public void ShouldSetAndGetLocalValue()
        {
            ValueEnvironment parent = new ValueEnvironment();
            ValueEnvironment environment = new ValueEnvironment(parent);

            environment.SetValue("foo", "bar");

            Assert.AreEqual("bar", environment.GetValue("foo"));
            Assert.IsNull(parent.GetValue("foo"));
        }
示例#7
0
        public object Apply(Machine machine, ValueEnvironment environment, object[] argumentValues)
        {
            ValueEnvironment newenv = new ValueEnvironment(environment);

            if (argumentValues == null && this.Arity != 0 && !this.VariableArity)
                throw new InvalidOperationException("Invalid number of parameters");

            if (argumentValues != null && !this.VariableArity && argumentValues.Length != this.Arity)
                throw new InvalidOperationException("Invalid number of parameters");

            if (argumentValues != null && this.VariableArity && argumentValues.Length < this.Arity)
                throw new InvalidOperationException("Invalid number of parameters");

            if (this.name != null)
                newenv.SetValue(this.name, this);

            int k = 0;
            bool islast = false;

            // TODO refactor see DefinedFunction
            foreach (Symbol argname in this.arguments)
            {
                if (argname.Name == "&")
                    islast = true;
                else
                {
                    if (!islast)
                        newenv.SetValue(argname.Name, argumentValues[k++]);
                    else
                    {
                        IList rest = new ArrayList();

                        while (k < argumentValues.Length)
                            rest.Add(argumentValues[k++]);

                        if (rest.Count > 0)
                            newenv.SetValue(argname.Name, rest);
                        else
                            newenv.SetValue(argname.Name, null);
                    }
                }
            }

            object result = machine.Evaluate(MacroUtilities.Expand(this.body, machine, newenv), newenv);

            result = machine.Evaluate(result, environment);

            return result;
        }
示例#8
0
        public static object Expand(object obj, Machine machine, ValueEnvironment environment)
        {
            if (obj == null)
                return null;

            if (obj is string)
                return obj;

            if (!(obj is IList))
                return obj;

            IList list = (IList)obj;

            if (IsMacro(list))
                return machine.Evaluate(list[1], environment);

            ArrayList newlist = new ArrayList();

            foreach (object element in list)
            {
                if (element is IList)
                {
                    IList elementList = (IList)element;

                    if (IsListMacro(elementList))
                    {
                        newlist.AddRange((ICollection)machine.Evaluate(elementList[1], environment));
                        continue;
                    }
                }

                newlist.Add(Expand(element, machine, environment));
            }

            if (obj is System.Array)
                return newlist.ToArray();

            if (obj is IPersistentVector)
                return PersistentVector.Create(newlist);

            return newlist;
        }
示例#9
0
        public object Evaluate(object obj, ValueEnvironment environment)
        {
            if (obj == null)
                return null;

            if (Utilities.IsEvaluable(obj))
                return Utilities.ToExpression(obj).Evaluate(this, environment);

            return obj;
        }
示例#10
0
        public static Variable ToVariable(Machine machine, ValueEnvironment environment, Symbol symbol)
        {
            string ns = symbol.Namespace;

            if (String.IsNullOrEmpty(ns))
                ns = (string)environment.GetValue(Machine.CurrentNamespaceKey);

            string name = symbol.Name;

            Variable variable = machine.GetVariable(ns, name);

            if (variable == null)
                variable = Variable.Intern(machine, ns, name);

            return variable;
        }
示例#11
0
        public static string[] EvaluateBindings(Machine machine, ValueEnvironment newenv, ICollection bindings)
        {
            if ((bindings.Count % 2) != 0)
                throw new InvalidOperationException("Let should receive a collection as first argument with even length");

            int k = 0;
            string name = null;
            string[] names = new string[bindings.Count / 2];

            foreach (object obj in bindings)
            {
                if ((k % 2) == 0)
                {
                    // TODO review if Name or FullName
                    if (obj is INamed)
                        name = ((INamed)obj).FullName;
                    else if (obj is string)
                        name = (string)obj;
                    else
                        throw new InvalidOperationException("Let expect a symbol or a string to name a value");

                    names[k / 2] = name;
                }
                else
                    newenv.SetValue(name, machine.Evaluate(obj, newenv), true);

                k++;
            }

            return names;
        }
示例#12
0
        public void ShouldGetNullIfNoValue()
        {
            ValueEnvironment environment = new ValueEnvironment();

            Assert.IsNull(environment.GetValue("foo"));
        }
示例#13
0
        public object Apply(Machine machine, ValueEnvironment environment, object[] argumentValues)
        {
            ValueEnvironment newenv = new ValueEnvironment(environment);

            if (argumentValues == null && this.Arity != 0 && !this.VariableArity)
                throw new InvalidOperationException("Invalid number of parameters");

            if (argumentValues != null && !this.VariableArity && argumentValues.Length != this.Arity)
                throw new InvalidOperationException("Invalid number of parameters");

            if (argumentValues != null && this.VariableArity && argumentValues.Length < this.Arity)
                throw new InvalidOperationException("Invalid number of parameters");

            if (this.name != null)
                newenv.SetValue(this.name, this);

            int k = 0;
            bool islast = false;

            foreach (Symbol argname in this.arguments)
            {
                if (argname.Name == "&")
                    islast = true;
                else
                {
                    if (!islast)
                    {
                        newenv.SetValue(argname.Name, argumentValues[k]);
                        k++;
                    }
                    else
                    {
                        IList rest = new ArrayList();

                        while (argumentValues != null && k < argumentValues.Length)
                            rest.Add(argumentValues[k++]);

                        if (rest.Count > 0)
                            newenv.SetValue(argname.Name, rest);
                        else
                            newenv.SetValue(argname.Name, null);
                    }
                }
            }

            object result = this.expression.Evaluate(machine, newenv);

            while (result != null && result is RecursionData)
            {
                RecursionData data = (RecursionData)result;

                if (Utilities.GetArity(data.Arguments) != Utilities.GetArity(this.arguments))
                    throw new InvalidOperationException("Invalid recursion data");

                newenv = new ValueEnvironment(environment);

                k = 0;

                foreach (Symbol argname in this.arguments)
                    newenv.SetValue(argname.Name, data.Arguments[k++]);

                result = this.expression.Evaluate(machine, newenv);
            }

            return result;
        }
示例#14
0
 public ValueEnvironment(ValueEnvironment parent)
 {
     this.parent = parent;
 }