20090323-VisitorTrace

20090323-VisitorTrace - How do visitors work? This set of...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: How do visitors work? This set of slides traces through the execution of a visitor weve seen before (the lengthVisitor) on a short list. It shows how the runtime stack makes recursion possible. First lets recall: how the visitor is defined how execute is defined in the two list state classes Length visitor in the empty case the answer is zero in the non-empty case the answer is one more than the length of the rest of the list public Integer emptyCase(LRStruct<?> host, Object _){ return 0; } public Integer nonEmptyCase(LRStruct<?> host, Object _){ return 1 + host.getRest().execute(this,_); } execute definition in states Empty state: public Object execute(LRS host, IAlgo algo, Object argut){ return algo.emptyCase(host, input); } NonEmpty state: public Object execute(LRS host, IAlgo algo, Object argut){ return algo.nonEmptyCase(host, input); } Now lets trace execution of the visitor LRStruct<String> list = new LRStruct<String>(); list.insertFront(Wilma).insertFront(Fred); LengthVisitor<String> visitor = new LengthVisitor<String>(); int len = list.execute(visitor,null); System.out.println(Length is + length); Focus on the evaluation of: list.execute(visitor,null) list Tracing the execution of a visitor _state _datum _rest Fred _state _datum _rest Wilma _state public Object emptyCase(){} public Object nonEmptyCase(){} visitor Driver: int len = list.execute(visitor, null) ; LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, inp, this); } EmptyState: public <I, O> O execute(IAlgo<I, E, O> algo, LRStruct<E> host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public <I, O> O execute(IAlgo<I, E, O> algo, LRStruct<E> host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct<E> host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct<E> host, Object _) { return 1 + host.getRest().execute(this, _); } algo list Tracing the execution of a visitor _state _datum _rest Fred _state _datum _rest Wilma _state public Object emptyCase(){} public Object nonEmptyCase(){} space for return value visitor arg (null) this Driver: int len = list.execute(visitor, null); LRS: public Object execute(IAlgo algo, Object arg){ return _state.execute(algo, inp, this) ; } EmptyState: public <I, O> O execute(IAlgo<I, E, O> algo, LRStruct<E> host, I arg) { return algo.emptyCase(host, arg); } NonEmptyState: public <I, O> O execute(IAlgo<I, E, O> algo, LRStruct<E> host, I arg) { return algo.nonEmptyCase(host, arg); } LengthVisitor: public Integer emptyCase(LRStruct<E> host, Object _) { return 0; // isn't autoboxing cool :-) } public Integer nonEmptyCase(LRStruct<E> host, Object _) { return 1 + host.getRest().execute(this, _); } algo list Tracing the execution of a visitor _state _datum _rest Fred _state _datum _rest Wilma _state...
View Full Document

This document was uploaded on 11/03/2011 for the course CSE 116 at SUNY Buffalo.

Page1 / 31

20090323-VisitorTrace - How do visitors work? This set of...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online