Пример #1
0
        public void TestMapParams()
        {
            ScopeChain scope = new ScopeChain();
            scope.SetValue("+", new TestSum());
            ValueDelimiter square = new ValueDelimiter("]", DelimiterType.AsArray);
            scope.SetValue("[", square);
            ValueDelimiter curly = new ValueDelimiter("}", DelimiterType.AsArray, new CreateMap());
            scope.SetValue("{", curly);

            {	// create postfix function that takes { :a :b }
                Map map = new Map();
                map["a"] = PatternData.Single("a", ValueType.Int);
                map["b"] = PatternData.Single("b", ValueType.Int);
                ValueMap vMap = new ValueMap(map);

                List<string> body = new List<string>();
                body.Add("a + b");

                CreateFunction.Do(scope, "add", null, vMap, body);
            }
            {	// try out the function
                DelimiterList list = ParseLine.Do("add { :a 2 :b 5 }", scope);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(7, value.AsInt);
            }

            {	// try it as a partial
                DelimiterList list = ParseLine.Do("add { :a 2 } { :b 5 }", scope);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(7, value.AsInt);
            }
        }
Пример #2
0
        static IScope CreateScope()
        {
            ScopeChain scope = new ScopeChain();
            scope.SetValue("subtract", new SubtractArray());
            scope.SetValue("add", new AddUpBody());

            ValueDelimiter square = new ValueDelimiter("]", DelimiterType.AsArray);
            scope.SetValue("[", square);

            return scope;
        }
Пример #3
0
        static IScope CreateScope()
        {
            ScopeChain scope = new ScopeChain();
            scope.SetValue("subtract-array", new SubtractArray());
            scope.SetValue("subtract-map", new SubtractMap());
            scope.SetValue("create-map", new CreateMap());

            ValueDelimiter square = new ValueDelimiter("]", DelimiterType.AsArray);
            scope.SetValue("[", square);
            ValueDelimiter curly = new ValueDelimiter("}", DelimiterType.AsArray, new CreateMap());
            scope.SetValue("{", curly);

            return scope;
        }
Пример #4
0
        public void TestDelimiterFunction()
        {
            ValueFunction function = new AddFunction();
            ValueDelimiter delim = new ValueDelimiter(">", DelimiterType.AsArray, function);
            List<DelimiterNode> nodes = new List<DelimiterNode>();
            nodes.Add(ToNode("3"));
            nodes.Add(ToNode("6"));
            nodes.Add(ToNode("9"));
            DelimiterList list = new DelimiterList(delim, nodes, 0, "<", "3 6 9", null);
            DelimiterNodeList nodelist = new DelimiterNodeList(list);

            IScope scope = new TestScope();
            INodeRequestor values = new TestValueNodeRequestor(nodelist);

            Value result = EvalNode.Do(nodelist, scope, values, null);
            Assert.AreEqual(18, result.AsInt);
        }
Пример #5
0
        public void TestDelimitedString()
        {
            ValueDelimiter delim = new ValueDelimiter("'", DelimiterType.AsString);
            List<DelimiterNode> nodes = new List<DelimiterNode>();
            string str = "this is a test";
            nodes.Add(ToNode(str));
            DelimiterList list = new DelimiterList(delim, nodes, 0, "'", str, null);
            DelimiterNodeList nodelist = new DelimiterNodeList(list);

            IScope scope = new TestScope();
            INodeRequestor values = new TestValueNodeRequestor(nodelist);

            {	// delimited string
                Value result = EvalNode.Do(nodelist, scope, values, null);
                Assert.AreEqual("this is a test", result.AsString);
            }

            {	// function that takes a delimited string
                Value result = EvalNode.Do(ToNode("caps"), scope, values, null);
                Assert.AreEqual("THIS IS A TEST", result.AsString);
            }
        }
Пример #6
0
        public void TestArray()
        {
            ValueDelimiter delim = new ValueDelimiter("]", DelimiterType.AsArray);
            List<DelimiterNode> nodes = new List<DelimiterNode>();
            nodes.Add(ToNode("3"));
            nodes.Add(ToNode("7"));
            nodes.Add(ToNode("3"));
            DelimiterList list = new DelimiterList(delim, nodes, 0, "[", "3 7 3", null);
            DelimiterNodeList nodelist = new DelimiterNodeList(list);

            IScope scope = new TestScope();
            INodeRequestor values = new TestValueNodeRequestor(nodelist);

            {	// [3 7 3]
                Value result = EvalNode.Do(nodelist, scope, values, null);
                Assert.AreEqual(result.Type, ValueType.Array);
                List<Value> array = result.AsArray;
                Assert.AreEqual(3, array.Count);
                Assert.AreEqual(3, array[0].AsInt);
                Assert.AreEqual(7, array[1].AsInt);
                Assert.AreEqual(3, array[2].AsInt);
            }
        }
Пример #7
0
        public void TestArrayParams()
        {
            ScopeChain scope = new ScopeChain();
            scope.SetValue("+", new TestSum());
            ValueDelimiter square = new ValueDelimiter("]", DelimiterType.AsArray);
            scope.SetValue("[", square);

            {	// create postfix function that takes [ a b ]
                List<Value> list = new List<Value>();
                list.Add(PatternData.Single("a", ValueType.Int));
                list.Add(PatternData.Single("b", ValueType.Int));
                ValueArray array = new ValueArray(list);

                List<string> body = new List<string>();
                body.Add("a + b");

                CreateFunction.Do(scope, "add", null, array, body);
            }
            {	// try out the function
                DelimiterList list = ParseLine.Do("add [ 2 5 ]", scope);
                Value value = EvalList.Do(list.Nodes, scope);
                Assert.AreEqual(7, value.AsInt);
            }
        }