Ejemplo n.º 1
0
        public void ShouldResetValueUsingResetInTrue()
        {
            ValueEnvironment environment = new ValueEnvironment();

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

            environment.SetValue("foo", "bar");
            environment.SetValue("foo", "newbar", false);
        }
Ejemplo n.º 3
0
        public void ShouldSetValue()
        {
            ValueEnvironment environment = new ValueEnvironment();

            environment.SetValue("foo", "bar");
            Assert.AreEqual("bar", environment.GetValue("foo"));
        }
Ejemplo n.º 4
0
        public void ShouldGetValueFromParent()
        {
            ValueEnvironment parent = new ValueEnvironment();
            ValueEnvironment environment = new ValueEnvironment(parent);

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

            Assert.AreEqual("bar", environment.GetValue("foo"));
        }
Ejemplo n.º 5
0
        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"));
        }
Ejemplo n.º 6
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;
        }
Ejemplo n.º 7
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;
        }
Ejemplo n.º 8
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;
        }