Nested func definitions have worked in the past, but the script below has a problem the use of an isident func from with another func. Worth debugging.
# cat findstruct
#! /usr/bin/env comterp_run
#
# findstruct analyze go code to find use of a particular struct
#
/* command line help */
if(arg(1)=="help" || arg(1)=="-h" || arg(1)==nil || arg(1)=="?" ||
arg(1)=="-help" || arg(1)=="--help" || arg(1)=="-?" :then
print("findstruct analyze go code to find use of a particular struct\n");
print("Usage: findstruct structname [dir] :grep\n\n");
print("--grep\t\tshow raw grep output\n");
exit)
/* argument processing */
structname=arg(1)
dirname="."
firstkey=2
if(arg(2)!=nil && substr(arg(2) 2)!="--" :then
dirname=arg(2);
firstkey=3)
grep_flag=false
dummy2_flag=false
for(i=firstkey i<narg() i++
switch(substr(arg(i) 2 :after)
:grep grep_flag=true
:dummy2 dummy2_flag=true;if(substr(arg(i+1) 2)=="--" :then continue);i++;dummy2_val=arg(i)
:default print("findstruct: Unknown argument %s\n" arg(i) :err)))
// retrieve list of go files
gofiles=list(stream(open(print("ls %s/*.go" dirname :str) :pipe)))
// trim newlines
for(i=0 i<size(gofiles) i++
at(at(gofiles i) size(at(gofiles i))-1 :set '\0'))
// retrieve list of list of lines of struct uses
structuses=list
for(i=0 i<size(gofiles) i++
cmd=print("grep %s %s" structname at(gofiles i) :str);
l=list(stream(open(cmd :pipe)));
structuses,l)
// raw grep output
if(grep_flag :then
for(i=0 i<size(gofiles) i++
for(j=0 j<size(at(structuses i)) j++
print("%s: %s" at(gofiles i) at(at(structuses i) j))));
exit)
// define func to recognize identifier character
isident=func( // :c input char
c=='_'||c>='0'&&c<='9'||isalpha(c))
// define func to extract field name
fname=func( // :s input string
fstr=substr(s structname+"." :after);
cs=stream(split(fstr));
nl=list;
while((newc=next(cs))!=nil
if(!isident(:c newc) :then break); // problem is here
nl,newc);
join(nl))
// build map of struct field to file name
for(i=0 i<size(gofiles) i++
for(j=0 j<size(at(structuses i)) j++
print("%s" fname(:s at(at(structuses i) j)))))
Nested func definitions have worked in the past, but the script below has a problem the use of an isident func from with another func. Worth debugging.