Beispiel #1
0
        public INode Walk(WalkFunc fn)
        {
            // Visit this node
            INode rewritten;

            if (!fn(this, out rewritten))
            {
                return(rewritten);
            }
            var objectList = rewritten as ObjectList;

            if (objectList == null)
            {
                throw new InvalidOperationException("Walk function returned wrong type");
            }

            // Visit the child nodes of this node
            for (int i = 0; i < objectList.Items.Length; i++)
            {
                objectList.Items[i] = (ObjectItem)objectList.Items[i].Walk(fn);
            }

            INode _;

            fn(null, out _);

            return(objectList);
        }
Beispiel #2
0
        public INode Walk(WalkFunc fn)
        {
            // Visit this node
            INode rewritten;

            if (!fn(this, out rewritten))
            {
                return(rewritten);
            }
            var objectType = rewritten as ObjectType;

            if (objectType == null)
            {
                throw new InvalidOperationException("Walk function returned wrong type");
            }

            // Visit the child node of this node
            objectType._list = (ObjectList)objectType._list.Walk(fn);

            INode _;

            fn(null, out _);

            return(objectType);
        }
Beispiel #3
0
        // Walk traverses an AST in depth-first order: It starts by calling fn(node);
        // node must not be nil. If fn returns true, Walk invokes fn recursively for
        // each of the non-nil children of node, followed by a call of fn(nil). The
        // returned node of fn can be used to rewrite the passed node to fn.
        public static INode Walk(INode node, WalkFunc fn)
        {
            var(rewritten, ok) = fn(node);
            if (!ok)
            {
                return(rewritten);
            }

            switch (node)
            {
            case File n:
                n.Node = Walk(n.Node, fn);
                break;

            case ObjectList n:
                foreach (var(i, item) in n.Items.Range())
                {
                    n.Items[i] = Walk(item, fn) as ObjectItem;
                }
                break;

            case ObjectKey n:
                // nothing to do
                break;

            case ObjectItem n:
                foreach (var(i, k) in n.Keys.Range())
                {
                    n.Keys[i] = Walk(k, fn) as ObjectKey;
                }
                if (n.Val != null)
                {
                    n.Val = Walk(n.Val, fn);
                }
                break;

            case LiteralType n:
                // nothing to do
                break;

            case ListType n:
                foreach (var(i, l) in n.List.Range())
                {
                    n.List[i] = Walk(l, fn);
                }
                break;

            case ObjectType n:
                n.List = Walk(n.List, fn) as ObjectList;
                break;

            default:
                // should we panic here?
                Console.Error.WriteLine("unknown type: {0}", node.GetType());
                break;
            }

            fn(null);
            return(rewritten);
        }
Beispiel #4
0
        public INode Walk(WalkFunc fn)
        {
            // Visit this node
            INode rewritten;

            if (!fn(this, out rewritten))
            {
                return(rewritten);
            }
            var listType = rewritten as ListType;

            if (listType == null)
            {
                throw new InvalidOperationException("Walk function returned wrong type");
            }

            // Visit the child nodes of this node
            for (int i = 0; i < listType.List.Length; i++)
            {
                listType.List[i] = listType.List[i].Walk(fn);
            }

            INode _;

            fn(null, out _);

            return(listType);
        }
Beispiel #5
0
        public INode Walk(WalkFunc fn)
        {
            // Visit this node
            INode rewritten;

            if (!fn(this, out rewritten))
            {
                return(rewritten);
            }
            INode _;

            fn(null, out _);
            return(rewritten);
        }
Beispiel #6
0
 public INode Walk(WalkFunc fn)
 {
     throw new System.NotSupportedException();
 }