Пример #1
0
		public static LNode static_matchCode(LNode node, IMacroContext context)
		{
			if (node.AttrNamed(S.Static) == null && !node.HasSpecialName)
				return null; // handled by normal matchCode macro

			var args_body = context.GetArgsAndBody(false);
			VList<LNode> args = args_body.Item1, body = args_body.Item2;
			if (args.Count != 1)
				return Reject(context, args[1], "Expected only one expression to match");

			var expression = context.PreProcess(AutoStripBraces(args[0]));

			var cases = GetCases(body, context.Sink);
			// The `default:` case is represented by an empty list of patterns.
			if (cases.WithoutLast(1).Any(pair => pair.Key.IsEmpty))
				context.Write(Severity.Error, node, "The `default:` case must be the last one, because the cases are tested in the order they appear, so no case after `default:` can be matched.");

			MMap<Symbol, LNode> captures = new MMap<Symbol, LNode>();
			foreach (Pair<VList<LNode>, VList<LNode>> pair in cases)
			{
				var patterns = pair.Key.IsEmpty ? new VList<LNode>((LNode)null) : pair.Key;
				foreach (var pattern in patterns)
				{
					captures.Clear();
					VList<LNode> _;
					if (pattern == null || LNodeExt.MatchesPattern(expression, pattern, ref captures, out _)) {
						captures[_hash] = expression; // define $#
						captures.Remove(__);
						return ReplaceCaptures(pair.Value.AsLNode(S.Splice), captures);
					}
				}
			}
			return F.Call(S.Splice); // none of the cases matched
		}
Пример #2
0
        void TrialRemoves(T[] data, int start, int stop, bool randomOrder)
        {
            var indexes = GetIndexes(start, stop, randomOrder);
            int hCount = 0, oCount = 0, oldICount = _iSet.Count;

            _timer.Restart();
            for (int i = 0; i < indexes.Count; i++)
            {
                if (_hSet.Remove(data[indexes[i]]))
                {
                    hCount++;
                }
            }
            _hTime += _timer.Restart();
            for (int i = 0; i < indexes.Count; i++)
            {
                if (_mSet.Remove(data[indexes[i]]))
                {
                    oCount++;
                }
            }
            _mTime += _timer.Restart();
            for (int i = 0; i < indexes.Count; i++)
            {
                _iSet = _iSet.Without(data[indexes[i]]);
            }
            _iTime += _timer.Restart();

            Debug.Assert(hCount == oCount);
            Debug.Assert(hCount == oldICount - _iSet.Count);
            TallyMemory();
        }
Пример #3
0
        public static LNode static_matchCode(LNode node, IMacroContext context)
        {
            if (node.AttrNamed(S.Static) == null && !node.HasSpecialName)
            {
                return(null);                // handled by normal matchCode macro
            }
            var           args_body = context.GetArgsAndBody(false);
            VList <LNode> args = args_body.Item1, body = args_body.Item2;

            if (args.Count != 1)
            {
                return(Reject(context, args[1], "Expected only one expression to match"));
            }

            var expression = context.PreProcess(AutoStripBraces(args[0]));

            var cases = GetCases(body, context.Sink);

            // The `default:` case is represented by an empty list of patterns.
            if (cases.WithoutLast(1).Any(pair => pair.Key.IsEmpty))
            {
                context.Write(Severity.Error, node, "The `default:` case must be the last one, because the cases are tested in the order they appear, so no case after `default:` can be matched.");
            }

            MMap <Symbol, LNode> captures = new MMap <Symbol, LNode>();

            foreach (Pair <VList <LNode>, VList <LNode> > pair in cases)
            {
                var patterns = pair.Key.IsEmpty ? new VList <LNode>((LNode)null) : pair.Key;
                foreach (var pattern in patterns)
                {
                    captures.Clear();
                    VList <LNode> _;
                    if (pattern == null || LNodeExt.MatchesPattern(expression, pattern, ref captures, out _))
                    {
                        captures[_hash] = expression;                         // define $#
                        captures.Remove(__);
                        return(ReplaceCaptures(pair.Value.AsLNode(S.Splice), captures));
                    }
                }
            }
            return(F.Call(S.Splice));            // none of the cases matched
        }