Пример #1
0
        internal ValueEnvironment MakeEnvironment(List arguments, ValueEnvironment parent)
        {
            ValueEnvironment nenv = new ValueEnvironment(parent);

            if (Predicates.IsNil(this.arguments))
            {
                return(nenv);
            }

            if (Predicates.IsIdentifier(this.arguments))
            {
                nenv.SetValue(((Identifier)this.arguments).Name, arguments);
                return(nenv);
            }

            List       argnames  = (List)this.arguments;
            List       argvalues = arguments;
            Identifier argname;
            object     argvalue;

            while (!Predicates.IsNil(argnames) && !Predicates.IsNil(argvalues))
            {
                argname  = (Identifier)argnames.First;
                argvalue = argvalues.First;
                nenv.SetValue(argname.Name, argvalue);
                argnames  = argnames.Next;
                argvalues = argvalues.Next;
            }

            return(nenv);
        }
Пример #2
0
        public void ParseAndEvaluateSubtractVariablesExpression()
        {
            Parser parser = new Parser("a-b");

            IExpression      expression  = parser.ParseExpression();
            ValueEnvironment environment = new ValueEnvironment();

            environment.SetValue("a", 1);
            environment.SetValue("b", 2);

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(SubtractExpression));
            Assert.IsInstanceOfType(expression.Evaluate(environment), typeof(int));
            Assert.AreEqual(-1, (int)(expression.Evaluate(environment)));
        }
Пример #3
0
        public void SetAndGetValue()
        {
            ValueEnvironment environment = new ValueEnvironment();

            environment.SetValue("foo", "bar");
            Assert.AreEqual("bar", environment.GetValue("foo"));
        }
Пример #4
0
        public void DefineSetAndGetPublicValue()
        {
            ValueEnvironment parent      = new ValueEnvironment(ValueEnvironmentType.Public);
            ValueEnvironment environment = new ValueEnvironment(parent);

            environment.SetPublicValue("foo", null);

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

            Assert.AreEqual("bar", environment.GetValue("foo"));
            Assert.AreEqual("bar", parent.GetValue("foo"));

            Assert.AreEqual(1, environment.GetValue("one"));
            Assert.IsNull(parent.GetValue("one"));
        }
Пример #5
0
        internal ValueEnvironment MakeEnvironment(List arguments, ValueEnvironment parent)
        {
            ValueEnvironment nenv = new ValueEnvironment(parent);

            nenv.SetValue(this.argumentName.Name, arguments);
            return(nenv);
        }
Пример #6
0
        public object Apply(IList <object> parameters, ValueEnvironment environment)
        {
            ValueEnvironment normalenv = new ValueEnvironment(environment);

            if (this.parameterNames != null)
            {
                for (int k = 0; k < this.parameterNames.Count; k++)
                {
                    normalenv.SetValue(this.parameterNames[k], parameters[k]);
                }
            }

            ValueEnvironment localenv = new ValueEnvironment(normalenv, ValueEnvironmentType.Local);

            try
            {
                this.command.Execute(this.machine, localenv);
            }
            catch (ReturnException ex)
            {
                return(ex.Value);
            }

            return(null);
        }
Пример #7
0
        public override object Execute(object argument1, object argument2, ValueEnvironment environment)
        {
            Identifier atom = (Identifier)argument1;

            environment.SetValue(atom.Name, argument2);
            return(argument2);
        }
Пример #8
0
        public void SetGlobalAndLocalValue()
        {
            ValueEnvironment child = new ValueEnvironment(this.environment);

            child.SetGlobalValue("foo", "bar");
            child.SetValue("foo", "bar2");
            Assert.AreEqual("bar", this.environment.GetValue("foo"));
            Assert.AreEqual("bar2", child.GetValue("foo"));
        }
Пример #9
0
        public void ShouldEvaluateName()
        {
            IExpression      expression  = new NameExpression("foo");
            ValueEnvironment environment = new ValueEnvironment();

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

            Assert.AreEqual("bar", expression.Evaluate(environment));
        }
Пример #10
0
        public void GetValueFromParent()
        {
            ValueEnvironment parent      = new ValueEnvironment();
            ValueEnvironment environment = new ValueEnvironment(parent);

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

            Assert.AreEqual("bar", environment.GetValue("foo"));
            Assert.AreEqual("bar", environment.GetValue("FOO"));
        }
Пример #11
0
        public void ParseAndExecuteDotExpressionWithArguments()
        {
            ValueEnvironment environment = new ValueEnvironment();

            environment.SetValue("dinfo", new System.IO.DirectoryInfo("."));
            IExpression expression = ParseExpression("dinfo.GetFiles(\"*.exe\")");
            object      result     = expression.Evaluate(environment);

            Assert.IsNotNull(result);
        }
Пример #12
0
        public void GetTypeStoredInEnvironment()
        {
            ValueEnvironment environment = new ValueEnvironment();

            environment.SetValue("int", typeof(int));

            Type type = TypeUtilities.GetType(environment, "int");

            Assert.IsNotNull(type);
            Assert.AreEqual(type, typeof(int));
        }
Пример #13
0
        public void ShouldExpandUnquotedSymbol()
        {
            Parser           parser      = new Parser("(unquote x)");
            object           list        = parser.Compile();
            ValueEnvironment environment = new ValueEnvironment();

            environment.SetValue("x", "y");

            object result = MacroUtilities.Expand(list, environment);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(string));
            Assert.AreEqual("y", result);
        }
Пример #14
0
        public void ParseAndEvaluateNameExpression()
        {
            Parser parser = new Parser("foo");

            IExpression      expression  = parser.ParseExpression();
            ValueEnvironment environment = new ValueEnvironment();

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

            Assert.IsNotNull(expression);
            Assert.IsInstanceOfType(expression, typeof(NameExpression));
            Assert.IsInstanceOfType(((NameExpression)expression).Evaluate(environment), typeof(string));
            Assert.AreEqual("bar", (string)((NameExpression)expression).Evaluate(environment));
        }
Пример #15
0
        private static ValueEnvironment MakeEnvironment(List arguments, ValueEnvironment parent)
        {
            ValueEnvironment newenv = new ValueEnvironment(parent);

            while (!Predicates.IsNil(arguments))
            {
                List       argument = (List)arguments.First;
                Identifier id       = (Identifier)argument.First;
                object     expr     = argument.Next.First;
                newenv.SetValue(id.Name, Machine.Evaluate(expr, parent));
                arguments = arguments.Next;
            }

            return(newenv);
        }
Пример #16
0
        public void SetAndGetValueInParentEnvironment()
        {
            ValueEnvironment parent      = new ValueEnvironment();
            ValueEnvironment environment = new ValueEnvironment(parent);

            parent.SetValue("foo", "bar");
            environment.SetValue("foo", "newbar");
            Assert.AreEqual("newbar", environment.GetValue("foo"));
            Assert.AreEqual("newbar", parent.GetValue("foo"));

            Assert.IsTrue(parent.ContainsValue("foo"));
            Assert.IsFalse(environment.ContainsValue("foo"));

            Assert.IsTrue(parent.ContainsValue("Foo"));
            Assert.IsFalse(environment.ContainsValue("Foo"));
        }
Пример #17
0
        public object Apply(Machine machine, ValueEnvironment environment, object[] arguments)
        {
            Symbol symbol = (Symbol)arguments[0];

            object value = arguments[1];

            if (Utilities.IsEvaluable(value))
            {
                IExpression expression = Utilities.ToExpression(arguments[1]);

                value = expression.Evaluate(machine, environment);
            }

            environment.SetValue(symbol.FullName, value);

            return value;
        }
Пример #18
0
        public void ExecuteSimpleWhile()
        {
            Parser parser = new Parser("while a\r\n a:=0\r\nenddo");

            ICommand         command     = parser.ParseCommand();
            ValueEnvironment environment = new ValueEnvironment();

            environment.SetValue("a", 1);

            command.Execute(null, environment);

            object value = environment.GetValue("a");

            Assert.IsNotNull(value);
            Assert.IsInstanceOfType(value, typeof(int));
            Assert.AreEqual(0, (int)value);
        }
Пример #19
0
        public object Apply(Machine machine, ValueEnvironment environment, object[] arguments)
        {
            if (arguments == null || arguments.Length == 0)
                return null;

            object result = null;
            ValueEnvironment newenv = null;
            string[] names = null;

            newenv = new ValueEnvironment(environment);

            object argument = arguments[0];

            if (argument != null)
            {
                if (!(argument is ICollection))
                    throw new InvalidOperationException("Let must receive a list as first argument");

                names = Utilities.EvaluateBindings(machine, newenv, (ICollection)argument);
            }

            for (int k = 1; k < arguments.Length; k++)
                result = machine.Evaluate(arguments[k], newenv);

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

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

                newenv = new ValueEnvironment(environment);
                result = null;

                for (int k = 0; k < names.Length; k++)
                    newenv.SetValue(names[k], data.Arguments[k]);

                for (int k = 1; k < arguments.Length; k++)
                    result = machine.Evaluate(arguments[k], newenv);
            }

            return result;
        }
Пример #20
0
        public void ShouldExpandImplicitUnquotedSymbolInList()
        {
            Parser           parser      = new Parser("(1 ~x 3)");
            object           list        = parser.Compile();
            ValueEnvironment environment = new ValueEnvironment();

            environment.SetValue("x", 2);

            object result = MacroUtilities.Expand(list, environment);

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(List));

            List resultList = (List)result;

            Assert.AreEqual(1, resultList.First);
            Assert.AreEqual(2, resultList.Next.First);
            Assert.AreEqual(3, resultList.Next.Next.First);
            Assert.IsNull(resultList.Next.Next.Next);
        }
Пример #21
0
 public override void Execute(Machine machine, ValueEnvironment environment)
 {
     environment.SetValue(this.name, this.expression.Evaluate(environment));
 }