Ejemplo n.º 1
0
    public void Append_entries_to_a_list_and_return_the_new_list_non_empty_lists()
    {
        var list1 = new List <int> {
            1, 2
        };
        var list2 = new List <int> {
            2, 3, 4, 5
        };
        var expected = new List <int> {
            1, 2, 2, 3, 4, 5
        };

        Assert.Equal(expected, ListOps.Append(list1, list2));
    }
Ejemplo n.º 2
0
        public Node TryEval(Node node, Env env, Logger logger)
        {
            Node currentListNode = node.Tail;

            Node list = null;

            while (currentListNode != Node.NIL)
            {
                Node newEntry = Evaluator.Evaluate(currentListNode.Head, env);
                list = ListOps.AppendToList(newEntry, list);

                currentListNode = currentListNode.Tail;
            }

            return(list);
        }
Ejemplo n.º 3
0
        private static Dictionary <string, Node> CreateReplacements(Node parameters, Node node)
        {
            // Create replacements

            Dictionary <string, Node> replaceMap = new Dictionary <string, Node>();

            Node currentParameterNode = parameters;

            Node currentValueNode = node.Tail;

            while (currentParameterNode != Node.NIL)
            {
                string key = currentParameterNode.Head.Content.ToString();

                if ("&rest".Equals(key))
                {
                    string restParameterName = currentParameterNode.Tail.Head.Content.ToString();

                    Node restParameterValues = null;

                    while (currentValueNode != Node.NIL)
                    {
                        restParameterValues = ListOps.AppendToList(currentValueNode.Head, restParameterValues);
                        currentValueNode    = currentValueNode.Tail;
                    }

                    replaceMap[restParameterName] = restParameterValues;
                    return(replaceMap);
                }

                Node value = currentValueNode.Head;

                replaceMap[key] = value;

                currentParameterNode = currentParameterNode.Tail;
                currentValueNode     = currentValueNode.Tail;
            }

            int remainingLength = ListOps.CalculateListLength(currentValueNode);

            if (remainingLength > 0)
            {
                throw new ArgumentException("MacroExpander.CreateReplacements: " + remainingLength + " additional parameters found.");
            }

            return(replaceMap);
        }
Ejemplo n.º 4
0
 public void ConcatOfListOfLists()
 {
     Assert.Equal(Range(1, 6),
                  ListOps.Concat(new List <List <int> >
     {
         new List <int> {
             1, 2
         },
         new List <int> {
             3
         },
         EmptyList,
         new List <int> {
             4, 5, 6
         }
     }));
 }
Ejemplo n.º 5
0
    public void Concatenate_a_list_of_lists_list_of_lists()
    {
        var lists = new List <List <int> > {
            new List <int> {
                1, 2
            }, new List <int> {
                3
            }, new List <int>(), new List <int> {
                4, 5, 6
            }
        };
        var expected = new List <int> {
            1, 2, 3, 4, 5, 6
        };

        Assert.Equal(expected, ListOps.Concat(lists));
    }
Ejemplo n.º 6
0
        ///////////////////////////////////////////////////////////////////////////////////////////////

        #region IComparer<string> Members
        public int Compare(
            string left,
            string right
            )
        {
            ListOps.GetElementsToCompare(
                interpreter, ascending, indexText, leftOnly, false,
                cultureInfo, ref left, ref right); /* throw */

            int result = String.Compare(left, right, noCase ?
                                        StringOps.SystemNoCaseStringComparisonType :
                                        StringOps.SystemStringComparisonType);

            ListOps.UpdateDuplicateCount(this, duplicates, left, right,
                                         unique, result, ref levels); /* throw */

            return(result);
        }
Ejemplo n.º 7
0
        ///////////////////////////////////////////////////////////////////////////////////////////////

        #region IComparer<string> Members
        public int Compare(
            string left,
            string right
            )
        {
            Result error = null;

            ListOps.GetElementsToCompare(
                interpreter, ascending, indexText, leftOnly, false,
                cultureInfo, ref left, ref right); /* throw */

            Number leftNumber = null;

            if ((Value.GetNumber(left, ValueFlags.AnyNumberAnyRadix, cultureInfo,
                                 ref leftNumber, ref error) == ReturnCode.Ok) &&
                leftNumber.ConvertTo(typeof(double)))
            {
                double leftDouble  = (double)leftNumber.Value;
                Number rightNumber = null;

                if ((Value.GetNumber(right, ValueFlags.AnyNumberAnyRadix, cultureInfo,
                                     ref rightNumber, ref error) == ReturnCode.Ok) &&
                    rightNumber.ConvertTo(typeof(double)))
                {
                    double rightDouble = (double)rightNumber.Value;

                    int result = LogicOps.Compare(leftDouble, rightDouble);

                    ListOps.UpdateDuplicateCount(this, duplicates, leftDouble.ToString(),
                                                 rightDouble.ToString(), unique, result, ref levels); /* throw */

                    return(result);
                }
            }

            if (error != null)
            {
                throw new ScriptException(error);
            }
            else
            {
                throw new ScriptException();
            }
        }
Ejemplo n.º 8
0
        public Node TryEval(Node node, Env env, Logger logger)
        {
            // Append values (second to n-th parameter) to a list (first parameter).

            Node list = Evaluator.Evaluate(node.Tail.Head, env);

            Node listOfValues = node.Tail.Tail;

            while (listOfValues != Node.NIL)
            {
                Node n      = listOfValues.Head;
                Node result = Evaluator.Evaluate(n, env);
                list = ListOps.AppendToList(list, result);

                listOfValues = listOfValues.Tail;
            }

            return(list);
        }
Ejemplo n.º 9
0
        public Node TryEval(Node node, Env env, Logger logger)
        {
            double result = 0.0;

            int nodeChildrenCount = ListOps.CalculateListLength(node);

            if (nodeChildrenCount > 1)
            {
                Node parameter0 = Evaluator.Evaluate(NodeOps.GetChild(node, 1), env);
                result = double.Parse(parameter0.Content.ToString(), CultureInfo.InvariantCulture);
                for (int i = 2; i < nodeChildrenCount; i++)
                {
                    Node parameter = Evaluator.Evaluate(NodeOps.GetChild(node, i), env);
                    result -= double.Parse(parameter.Content.ToString(), CultureInfo.InvariantCulture);
                }
            }

            string resultStr = result.ToString(CultureInfo.InvariantCulture);

            return(new Node(StdNodeTypes.Identifier, resultStr));
        }
Ejemplo n.º 10
0
        ///////////////////////////////////////////////////////////////////////////////////////////////

        #region IComparer<string> Members
        public int Compare(
            string left,
            string right
            )
        {
            Result error = null;

            ListOps.GetElementsToCompare(
                interpreter, ascending, indexText, leftOnly, false,
                cultureInfo, ref left, ref right); /* throw */

            long leftWide = 0;

            if (Value.GetWideInteger2(left, ValueFlags.AnyWideInteger, cultureInfo,
                                      ref leftWide, ref error) == ReturnCode.Ok)
            {
                long rightWide = 0;

                if (Value.GetWideInteger2(right, ValueFlags.AnyWideInteger, cultureInfo,
                                          ref rightWide, ref error) == ReturnCode.Ok)
                {
                    int result = LogicOps.Compare(leftWide, rightWide);

                    ListOps.UpdateDuplicateCount(this, duplicates, leftWide.ToString(),
                                                 rightWide.ToString(), unique, result, ref levels); /* throw */

                    return(result);
                }
            }

            if (error != null)
            {
                throw new ScriptException(error);
            }
            else
            {
                throw new ScriptException();
            }
        }
Ejemplo n.º 11
0
    public void Reverse_the_elements_of_the_list_list_of_lists_is_not_flattened()
    {
        var list = new List <List <int> > {
            new List <int> {
                1, 2
            }, new List <int> {
                3
            }, new List <int>(), new List <int> {
                4, 5, 6
            }
        };
        var expected = new List <List <int> > {
            new List <int> {
                4, 5, 6
            }, new List <int>(), new List <int> {
                3
            }, new List <int> {
                1, 2
            }
        };

        Assert.Equal(expected, ListOps.Reverse(list));
    }
Ejemplo n.º 12
0
    public void Returns_the_length_of_a_list_empty_list()
    {
        var list = new List <int>();

        Assert.Equal(0, ListOps.Length(list));
    }
Ejemplo n.º 13
0
 public void FoldlOfHugeList()
 {
     Assert.Equal(Big * (Big + 1L) / 2L, ListOps.Foldl((acc, x) => acc + x, 0L, Range(1, Big)));
 }
Ejemplo n.º 14
0
 public void AppendOfNonEmptyAndEmptyLists()
 {
     Assert.Equal(Range(1, 4), ListOps.Append(Range(1, 4), EmptyList));
 }
Ejemplo n.º 15
0
 public void FoldlOfEmptylist()
 {
     Assert.Equal(0, ListOps.Foldl((acc, x) => acc + x, 0, EmptyList));
 }
Ejemplo n.º 16
0
 public void FoldlOfNonEmptyList()
 {
     Assert.Equal(7, ListOps.Foldl((acc, x) => acc + x, -3, Range(1, 4)));
 }
Ejemplo n.º 17
0
 public void AppendOfLargeLists()
 {
     Assert.Equal(BigList, ListOps.Append(Range(1, Big / 2), Range(1 + Big / 2, Big)));
 }
Ejemplo n.º 18
0
 public void FilterOfNormalList()
 {
     Assert.Equal(new List <int> {
         1, 3
     }, ListOps.Filter(Odd, Range(1, 4)));
 }
Ejemplo n.º 19
0
 public void ReverseOfNonEmptyList()
 {
     Assert.Equal(Range(1, 100).OrderByDescending(x => x).ToList(), ListOps.Reverse(Range(1, 100)));
 }
Ejemplo n.º 20
0
 public void MapOfEmptylist()
 {
     Assert.Equal(EmptyList, ListOps.Map(x => x + 1, EmptyList));
 }
Ejemplo n.º 21
0
 public void LengthOfLargeList()
 {
     Assert.Equal(Big, ListOps.Length(Range(1, Big)));
 }
Ejemplo n.º 22
0
 public void ReverseOfEmptylist()
 {
     Assert.Equal(EmptyList, ListOps.Reverse(EmptyList));
 }
Ejemplo n.º 23
0
 public void AppendOfNonEmptylists()
 {
     Assert.Equal(Range(1, 5), ListOps.Append(Range(1, 3), new List <int> {
         4, 5
     }));
 }
Ejemplo n.º 24
0
    public void Concatenate_a_list_of_lists_empty_list()
    {
        var lists = new List <List <int> >();

        Assert.Empty(ListOps.Concat(lists));
    }
Ejemplo n.º 25
0
 public void ConcatOfLargeListOfSmallLists()
 {
     Assert.Equal(BigList, ListOps.Concat(ListOps.Map(x => new List <int> {
         x
     }, Range(1, Big))));
 }
Ejemplo n.º 26
0
    public void Reverse_the_elements_of_the_list_empty_list()
    {
        var list = new List <int>();

        Assert.Empty(ListOps.Reverse(list));
    }
Ejemplo n.º 27
0
 public void FilterOfEmptylist()
 {
     Assert.Equal(EmptyList, ListOps.Filter(x => true, EmptyList));
 }
Ejemplo n.º 28
0
 public void LengthOfEmptyList()
 {
     Assert.Equal(0, ListOps.Length(EmptyList));
 }
Ejemplo n.º 29
0
 public void ConcatOfNoLists()
 {
     Assert.Equal(EmptyList, ListOps.Concat(new List <List <int> >()));
 }
Ejemplo n.º 30
0
 public void LengthOfNonEmptyList()
 {
     Assert.Equal(4, ListOps.Length(Range(1, 4)));
 }
Ejemplo n.º 31
0
		public ManagedObject (object obj)
		{
//			Console.WriteLine ("new ManagedObject created wrapping object of type {0}, handle == {1}", obj.GetType(), Handle);

			managed = obj;

			Type type = obj.GetType ();

			bool isScriptable = type.IsDefined (typeof(ScriptableTypeAttribute), true);

			// add properties

			foreach (PropertyInfo pi in type.GetProperties ()) {
				if (!isScriptable && !pi.IsDefined (typeof(ScriptableMemberAttribute), true))
					continue;
				RegisterScriptableProperty (pi);
				if (RegisterScriptableTypes (pi))
					HasTypes = true;
			}

			// add events
			foreach (EventInfo ei in type.GetEvents ()) {
				if (!isScriptable && !ei.IsDefined (typeof(ScriptableMemberAttribute), true))
					continue;
				RegisterScriptableEvent (ei);
				HasEvents = true;

				// XXX toshok - do we need to RegisterScriptableTypes for parameters on the delegate?
			}

			// add functions
			foreach (MethodInfo mi in type.GetMethods ()) {
				if (!isScriptable && !mi.IsDefined (typeof(ScriptableMemberAttribute), true))
					continue;
				RegisterScriptableMethod (mi);
				if (RegisterScriptableTypes (mi))
					HasTypes = true;
			}

			if (HasTypes) {
				TypeOps typeOps = new TypeOps ();
				Type typeOpsType = typeof (TypeOps);
				RegisterBuiltinScriptableMethod (typeOpsType.GetMethod ("CreateManagedObject"), "createManagedObject", typeOps);
			}

			if (HasEvents) {
				EventOps eventOps = new EventOps (this);
				Type eventOpsType = typeof (EventOps);
				RegisterBuiltinScriptableMethod (eventOpsType.GetMethod ("AddEventListener"), "addEventListener", eventOps);
				RegisterBuiltinScriptableMethod (eventOpsType.GetMethod ("RemoveEventListener"), "removeEventListener", eventOps);
			}

			RegisterScriptableMethod (type.GetMethod ("ToString"), "toString");

			if (ManagedObject is IList) {
				if (type.GetProperty ("Length") != null)
					RegisterScriptableProperty (type.GetProperty ("Length"), "length");
				else if (type.GetProperty ("Count") != null)
					RegisterScriptableProperty (type.GetProperty ("Count"), "length");

				foreach (MethodInfo mi in type.GetMethods ()) {
					switch (mi.Name) {
					case "IndexOf":
						RegisterScriptableMethod (mi, "indexOf");
						break;
					case "LastIndexOf":
						RegisterScriptableMethod (mi, "lastIndexOf");
						break;
					case "ToArray":
						RegisterScriptableMethod (mi, "toArray");
						break;
					}
				}

				ListOps listOps = new ListOps ((IList)ManagedObject);
				Type listOpsType = typeof (ListOps);

				if (type.GetProperty ("Item") != null)
					RegisterScriptableProperty (type.GetProperty ("Item"), "item");
				else
					RegisterScriptableProperty (listOpsType.GetProperty ("Item"), "item", listOps);

				RegisterScriptableMethod (listOpsType.GetMethod ("Pop"), "pop", listOps);
				RegisterScriptableMethod (listOpsType.GetMethod ("Push"), "push", listOps);
				RegisterScriptableMethod (listOpsType.GetMethod ("Reverse"), "reverse", listOps);
				RegisterScriptableMethod (listOpsType.GetMethod ("Shift"), "shift", listOps);
				RegisterScriptableMethod (listOpsType.GetMethod ("Unshift"), "unshift", listOps);
				RegisterScriptableMethod (listOpsType.GetMethod ("Splice"), "splice", listOps);
			} else if (ManagedObject is IDictionary) {
				DictOps dictOps = new DictOps ((IDictionary)ManagedObject);
				Type dictOpsType = typeof (DictOps);

				RegisterScriptableProperty (dictOpsType.GetProperty ("Item"), "item", dictOps);
			}
		}