TCL: Tool Command Language

[top]

  1. Simple but yet powerful string processing language.
  2. Easy to embed another application.
  3. Example: Cross referencer

    #!/usr/local/bin/tclsh8.2
    # tiny cross referencer writte in TCL 8.2
    
    set name {([A-Za-z_][A-Za-z0-9_]*)(.*)$}
    
    set words {}
    set linno 0
    set maxlen 0
    
    proc process {f} {
        global xref words name linno maxlen
        while {[gets $f line] >= 0} {
    	incr linno
    	while {[regexp $name $line match w rest]} {
    	    set line $rest
    	    if {[lsearch -exact $words $w] < 0} {
    		lappend words $w
    		if {[string length $w] > $maxlen} {
    		    set maxlen [string length $w]
    		}
    	    }
    	    if {[catch {set dup [lsearch $xref($w) $linno]}]} {
    		set xref($w) $linno
    	    } elseif {$dup < 0} {
    		lappend xref($w) $linno
    	    }
    	}
        }
    }
    
    if {$argc == 0} {
        process stdin
    } else {
        foreach fn $argv {
    	if {[catch {set f [open $fn]}]} {
    	    continue
    	} else {
    	    process $f
    	    close $f
    	}
        }
    }
    set namefmt "%${maxlen}s:"
    set lnofmt "%[expr [string length $linno] + 1]d"
    foreach w [lsort -dictionary $words] {
        puts -nonewline [format $namefmt $w]
        foreach ln $xref($w) {
    	puts -nonewline [format $lnofmt $ln]
        }
        puts ""
    }
    

[top]