Patch:    ivtools-131029-scott-002
For:      ivtools-1.2.11
Author:   scott@wavesemi.com
Subject:  depth-first traversal extension to stream operator

This is an intermediate patch to ivtools-1.2.11.  To apply, cd to the
top-level directory of the ivtools source tree (the directory with src
and config subdirs), and apply like this:

	patch -p0 <ThisFile

Then rebuild with "make;make install"


Index: src/ComUnidraw/grstrmfunc.h
===================================================================
--- src/ComUnidraw/grstrmfunc.h	(revision 8208)
+++ src/ComUnidraw/grstrmfunc.h	(revision 8254)
@@ -44,4 +44,30 @@
 };
 
 
+
+//: stream depth-first command
+class GrDepthFunc : public StreamFunc {
+public:
+    GrDepthFunc(ComTerp*);
+
+    virtual void execute();
+    virtual const char* docstring() { 
+      return "strm=depth(compview) -- depth-first walk of comp tree using stream"; }
+
+    CLASS_SYMID("GrDepthFunc");
+
+};
+
+//: hidden func used by next command for stream depth command
+class GrDepthNextFunc : public StrmFunc {
+public:
+    GrDepthNextFunc(ComTerp*);
+
+    virtual void execute();
+    virtual const char* docstring() { 
+      return "hidden func used by next command for depth command"; }
+
+    CLASS_SYMID("GrDepthNextFunc");
+
+};
 #endif /* !defined(_grstrmfunc_h) */
Index: src/ComUnidraw/comeditor.c
===================================================================
--- src/ComUnidraw/comeditor.c	(revision 8208)
+++ src/ComUnidraw/comeditor.c	(revision 8254)
@@ -133,6 +133,7 @@
 void ComEditor::AddCommands(ComTerp* comterp) {
     ((ComTerpServ*)comterp)->add_defaults();
 
+    comterp->add_command("depth", new GrDepthFunc(comterp));
     comterp->add_command("parent", new GrParentFunc(comterp));
     comterp->add_command("rect", new CreateRectFunc(comterp, this));
     comterp->add_command("rectangle", new CreateRectFunc(comterp, this));
Index: src/ComUnidraw/grstrmfunc.c
===================================================================
--- src/ComUnidraw/grstrmfunc.c	(revision 8208)
+++ src/ComUnidraw/grstrmfunc.c	(revision 8254)
@@ -121,3 +121,98 @@
 
 #endif
 
+/*****************************************************************************/
+
+int GrDepthFunc::_symid;
+
+GrDepthFunc::GrDepthFunc(ComTerp* comterp) : StreamFunc(comterp) {
+}
+
+void GrDepthFunc::execute() {
+  ComValue compsv(stack_arg_post_eval(0));
+  reset_stack();
+  
+  if (compsv.object_compview()) {
+    
+    static GrDepthNextFunc* snfunc = nil;
+    if (!snfunc) {
+      snfunc = new GrDepthNextFunc(comterp());
+      snfunc->funcid(symbol_add("depthnext"));
+    }
+
+    AttributeValueList* avl = new AttributeValueList();
+    Component* comp = ((ComponentView*)compsv.obj_val())->GetSubject();
+    if (!comp->IsA(OVERLAYS_COMP)) {
+      push_stack(ComValue::nullval());
+      return;
+    }
+    OverlaysComp* ovcomps = (OverlaysComp*)comp;
+
+    ComValue* av = 
+      new ComValue(new OverlayViewRef(ovcomps), ovcomps->classid());
+    avl->Append(av);
+    avl->Append(new ComValue());
+    ComValue* av2 = 
+      new ComValue(new OverlayViewRef((OverlaysComp*)ovcomps->GetParent()), ovcomps->classid());
+    avl->Append(av2);
+    
+    ComValue stream(snfunc, avl);
+    stream.stream_mode(-1); // for internal use (use by this func)
+    stream.type(ComValue::StreamType);
+    push_stack(stream);
+    
+  }
+}
+
+/*****************************************************************************/
+
+int GrDepthNextFunc::_symid;
+
+GrDepthNextFunc::GrDepthNextFunc(ComTerp* comterp) : StrmFunc(comterp) {
+}
+
+void GrDepthNextFunc::execute() {
+  ComValue operand1(stack_arg(0));
+
+  /* invoked by next func */
+  reset_stack();
+  AttributeValueList* avl = operand1.stream_list();
+  if (avl) {
+    Iterator i;
+    avl->First(i);
+    ComValue* currval = (ComValue*)avl->GetAttrVal(i);
+    avl->Next(i);
+    ComValue* prevval = (ComValue*)avl->GetAttrVal(i);
+    avl->Next(i);
+    ComValue* stopval = (ComValue*)avl->GetAttrVal(i);
+    
+    /* filter stream */
+    if (currval->is_known()) {
+      OverlayComp* currcomp = (OverlayComp*)currval->geta(OverlayComp::class_symid(), OVERLAY_COMP);
+      OverlayComp* prevcomp = (OverlayComp*)prevval->geta(OverlayComp::class_symid(), OVERLAY_COMP);
+      OverlayComp* stopcomp = (OverlayComp*)stopval->geta(OverlayComp::class_symid(), OVERLAY_COMP);
+      OverlayComp* nextcomp = currcomp->DepthNext(prevcomp);
+      while(nextcomp && nextcomp==(OverlayComp*)currcomp->GetParent()) {
+	prevcomp=currcomp;
+	currcomp=nextcomp;
+        nextcomp = currcomp->DepthNext(prevcomp);
+      }
+      if(nextcomp && nextcomp!=stopcomp) {
+        ((OverlayViewRef*)currval->obj_val())->SetSubject(nextcomp);
+        if (!prevcomp) {
+          ComValue new_prevval(new OverlayViewRef(currcomp), currcomp->classid());;
+          *prevval = new_prevval;
+	} else
+	  ((OverlayViewRef*)prevval->obj_val())->SetSubject(currcomp);
+        ComValue av(new OverlayViewRef(nextcomp), nextcomp->classid());
+        push_stack(av);
+        return;
+      }
+    }
+    
+  }
+  push_stack(ComValue::nullval());
+
+  return;
+}
+
Index: src/ComTerp/strmfunc.c
===================================================================
--- src/ComTerp/strmfunc.c	(revision 8208)
+++ src/ComTerp/strmfunc.c	(revision 8254)
@@ -489,10 +489,10 @@
 
 void FilterFunc::execute() {
   ComValue streamv(stack_arg_post_eval(0));
-  ComValue filterv(stack_arg(1));
+  ComValue filterv(stack_arg_post_eval(1));
   reset_stack();
 
-  /* setup for filterenation */
+  /* setup for filtering */
   static FilterNextFunc* flfunc = nil;
   
   if (!flfunc) {
Index: src/ComTerp/strmfunc.h
===================================================================
--- src/ComTerp/strmfunc.h	(revision 8208)
+++ src/ComTerp/strmfunc.h	(revision 8254)
@@ -76,7 +76,7 @@
     virtual void execute();
     virtual boolean post_eval() { return true; }
     virtual const char* docstring() { 
-      return ",, is the concat operator"; }
+      return ",, is the stream concat operator"; }
 
     CLASS_SYMID("ConcatFunc");
 
@@ -89,7 +89,7 @@
 
     virtual void execute();
     virtual const char* docstring() { 
-      return "hidden func used by next command for ,, (concat) operator."; }
+      return "hidden func used by next command for ,, (stream concat) operator."; }
 
     CLASS_SYMID("ConcatNextFunc");
 
@@ -172,5 +172,4 @@
     CLASS_SYMID("FilterNextFunc");
 
 };
-
 #endif /* !defined(_strmfunc_h) */
Index: src/ComTerp/comvalue.h
===================================================================
--- src/ComTerp/comvalue.h	(revision 8208)
+++ src/ComTerp/comvalue.h	(revision 8254)
@@ -101,7 +101,7 @@
     // assignment operator.
 
     virtual void* geta(int type, int compid=-1); 
-    // return a pointer if ObjectType matches
+    // return a pointer if ObjectType matches or compid is superclass
 
     int narg() const;
     // number of arguments associated with this command or keyword.
Index: src/man/man1/comterp.1
===================================================================
--- src/man/man1/comterp.1	(revision 8208)
+++ src/man/man1/comterp.1	(revision 8254)
@@ -121,7 +121,7 @@
     &&         and            41          L-to-R      binary
     ||         or             40          L-to-R      binary
     ,          tuple          35          L-to-R      binary
-    ,,         concat         33          L-to-R      binary
+    ,,         stream concat  33          L-to-R      binary
     %=         mod_assign     30          R-to-L      binary
     *=         mpy_assign     30          R-to-L      binary
     +=         add_assign     30          R-to-L      binary
@@ -218,6 +218,8 @@
 
  cnt=each(strm) -- traverse stream returning its length
 
+ comp=filter(comps classid) -- filter stream of comps for matching classid
+
 .SH CONTROL COMMANDS (using post evaluation):
 
  val=cond(testexpr trueexpr falseexpr) -- evaluate testexpr, and if true, evaluate and return trueexpr, otherwise evaluate and return falseexpr
Index: src/man/man1/comdraw.1
===================================================================
--- src/man/man1/comdraw.1	(revision 8208)
+++ src/man/man1/comdraw.1	(revision 8254)
@@ -93,7 +93,9 @@
  val=at(list|attrlist|compview n :set val :ins val) -- return (or set or insert after) the nth item in a list.
  num=size(list|attrlist|compview) -- return size of a list.
  compview=parent(compview) -- get parent of graphic
+ comp=depth(comps) -- depth-first walk of comp tree
 
+
 .SH VIEWER COMMANDS
 
  update() -- update viewer
Index: src/comdraw/README
===================================================================
--- src/comdraw/README	(revision 8208)
+++ src/comdraw/README	(revision 8254)
@@ -85,7 +85,9 @@
 val=at(list|attrlist|compview n :set val :ins val) -- return (or set or insert after) the nth item in a list.
 num=size(list|attrlist|str|compview) -- return size of a list (or string)
 compview=parent(compview) -- get parent of graphic
+strm=depth(comps) -- depth-first walk of comp tree using stream
 
+
 VIEWER COMMANDS
 
 update() -- update viewer
Index: src/comterp_/README
===================================================================
--- src/comterp_/README	(revision 8208)
+++ src/comterp_/README	(revision 8254)
@@ -110,7 +110,7 @@
     &&            and                  41          L-to-R      binary
     ||            or                   40          L-to-R      binary
     ,             tuple                35          L-to-R      binary
-    ,,            concat               33          L-to-R      binary
+    ,,            stream concat        33          L-to-R      binary
     %=            mod_assign           30          R-to-L      binary
     *=            mpy_assign           30          R-to-L      binary
     +=            add_assign           30          R-to-L      binary
@@ -205,6 +205,8 @@
 
 cnt=each(strm) -- traverse stream returning its length
 
+comp=filter(comps classid) -- filter stream of comps for matching classid
+
 CONTROL COMMANDS (using post evaluation):
 
 val=cond(testexpr trueexpr falseexpr) -- evaluate testexpr, and if true,
