Function frequency: Difference between revisions

Content added Content deleted
(Added PicoLisp)
(Go solution)
Line 5: Line 5:
actually executed at runtime, but how often it is used by the programmer.
actually executed at runtime, but how often it is used by the programmer.


=={{header|Go}}==
Only crude approximation is currently easy in Go. The following parses source code, looks for function call syntax (an expression followed by an argument list) and prints the expression.
<lang go>package main

import (
"fmt"
"go/ast"
"go/parser"
"go/token"
"io/ioutil"
"os"
)

func main() {
if len(os.Args) != 2 {
fmt.Println("usage ff <go source filename>")
return
}
src, err := ioutil.ReadFile(os.Args[1])
if err != nil {
fmt.Println(err)
return
}
fs := token.NewFileSet()
a, err := parser.ParseFile(fs, os.Args[1], src, 0)
if err != nil {
fmt.Println(err)
return
}
f := fs.File(a.Pos())
m := make(map[string]int)
ast.Inspect(a, func(n ast.Node) bool {
if ce, ok := n.(*ast.CallExpr); ok {
start := f.Offset(ce.Pos())
end := f.Offset(ce.Lparen)
m[string(src[start:end])]++
}
return true
})
for k, v := range m {
fmt.Println(v, k)
}
}</lang>
Output, when run on source code above:
<pre>
4 fmt.Println
1 string
1 a.Pos
1 ast.Inspect
1 ioutil.ReadFile
1 len
2 f.Offset
1 token.NewFileSet
1 ce.Pos
1 make
1 fs.File
1 parser.ParseFile
</pre>
=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(let Freq NIL
<lang PicoLisp>(let Freq NIL