generate.go 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package main
  11. import (
  12. "bytes"
  13. "flag"
  14. "fmt"
  15. "go/ast"
  16. "go/build"
  17. "go/doc"
  18. "go/importer"
  19. goparser "go/parser"
  20. "go/token"
  21. "go/types"
  22. "io/ioutil"
  23. "os"
  24. "path/filepath"
  25. "sort"
  26. "unicode"
  27. "devt.de/krotik/common/errorutil"
  28. "devt.de/krotik/common/stringutil"
  29. )
  30. //go:generate echo Generating ECAL stdlib from Go functions ...
  31. //go:generate go run devt.de/krotik/ecal/stdlib/generate $PWD/stdlib
  32. /*
  33. Stdlib candidates modules:
  34. go list std | grep -v internal | grep -v '\.' | grep -v unsafe | grep -v syscall
  35. */
  36. // List underneath the Go symbols which should be available in ECAL's stdlib
  37. // e.g. var pkgNames = map[string][]string{ "fmt": {"Println", "Sprint"} }
  38. // Turn the generateDoc switch on or off to extract documentation from the
  39. // Go source.
  40. // =============EDIT HERE START=============
  41. var pkgNames = map[string][]string{
  42. // "math": {"Pi", "E", "Phi", "Pow"},
  43. // "fmt": {"Println", "Sprint"},
  44. }
  45. var generateDoc = false
  46. // ==============EDIT HERE END==============
  47. var filename = filepath.Join(os.Args[1], "stdlib_gen.go")
  48. var stderrPrint = fmt.Println
  49. var stdoutPrint = fmt.Println
  50. func main() {
  51. var err error
  52. var outbuf bytes.Buffer
  53. synopsis := make(map[string]string)
  54. pkgDocs := make(map[string]*doc.Package)
  55. flag.Parse()
  56. // Make sure we have at least an empty pkgName
  57. if len(pkgNames) == 0 {
  58. pkgNames["math"] = []string{"Pi", "E", "Phi"}
  59. }
  60. // Make sure pkgNames is sorted
  61. var importList []string
  62. for pkgName, names := range pkgNames {
  63. sort.Strings(names)
  64. importList = append(importList, pkgName)
  65. synopsis["math"] = "Mathematics-related constants and functions"
  66. }
  67. sort.Strings(importList)
  68. outbuf.WriteString(`
  69. // Code generated by ecal/stdlib/generate; DO NOT EDIT.
  70. package stdlib
  71. `)
  72. outbuf.WriteString("import (\n")
  73. for _, pkgName := range importList {
  74. if generateDoc {
  75. syn, pkgDoc, err := getPackageDocs(pkgName)
  76. errorutil.AssertOk(err) // If this throws try not generating the docs!
  77. synopsis[pkgName] = syn
  78. pkgDocs[pkgName] = pkgDoc
  79. } else if _, ok := synopsis[pkgName]; !ok {
  80. synopsis[pkgName] = fmt.Sprintf("Package %v", pkgName)
  81. }
  82. outbuf.WriteString(fmt.Sprintf("\t\"%v\"\n", pkgName))
  83. }
  84. if stringutil.IndexOf("fmt", importList) == -1 {
  85. outbuf.WriteString(` "fmt"
  86. `)
  87. }
  88. if stringutil.IndexOf("reflect", importList) == -1 {
  89. outbuf.WriteString(` "reflect"
  90. )
  91. `)
  92. }
  93. outbuf.WriteString(`/*
  94. genStdlib contains all generated stdlib constructs.
  95. */
  96. `)
  97. outbuf.WriteString("var genStdlib = map[interface{}]interface{}{\n")
  98. for _, pkgName := range importList {
  99. if s, ok := synopsis[pkgName]; ok {
  100. outbuf.WriteString(fmt.Sprintf("\t\"%v-synopsis\" : %#v,\n", pkgName, s))
  101. }
  102. outbuf.WriteString(fmt.Sprintf("\t\"%v-const\" : %vConstMap,\n", pkgName, pkgName))
  103. outbuf.WriteString(fmt.Sprintf("\t\"%v-func\" : %vFuncMap,\n", pkgName, pkgName))
  104. outbuf.WriteString(fmt.Sprintf("\t\"%v-func-doc\" : %vFuncDocMap,\n", pkgName, pkgName))
  105. }
  106. outbuf.WriteString("}\n\n")
  107. for _, pkgName := range importList {
  108. var pkg *types.Package
  109. pkgSymbols := pkgNames[pkgName]
  110. if err == nil {
  111. pkg, err = importer.ForCompiler(fset, "source", nil).Import(pkgName)
  112. if err == nil {
  113. stdoutPrint("Generating adapter functions for", pkg)
  114. scope := pkg.Scope()
  115. // Write constants
  116. outbuf.WriteString(fmt.Sprintf(`/*
  117. %vConstMap contains the mapping of stdlib %v constants.
  118. */
  119. var %vConstMap = map[interface{}]interface{}{
  120. `, pkgName, pkgName, pkgName))
  121. for _, name := range scope.Names() {
  122. if !containsSymbol(pkgSymbols, name) {
  123. continue
  124. }
  125. switch obj := scope.Lookup(name).(type) {
  126. case *types.Const:
  127. if unicode.IsUpper([]rune(name)[0]) {
  128. line := fmt.Sprintf(` "%v": %v.%v,
  129. `, name, pkgName, obj.Name())
  130. if basicType, ok := obj.Type().(*types.Basic); ok {
  131. // Convert number constants so they can be used in calculations
  132. switch basicType.Kind() {
  133. case types.Int,
  134. types.Int8,
  135. types.Int16,
  136. types.Int32,
  137. types.Int64,
  138. types.Uint,
  139. types.Uint8,
  140. types.Uint16,
  141. types.Uint32,
  142. types.Uint64,
  143. types.Uintptr,
  144. types.Float32,
  145. types.UntypedInt,
  146. types.UntypedFloat:
  147. line = fmt.Sprintf(` "%v": float64(%v.%v),
  148. `, name, pkgName, obj.Name())
  149. }
  150. }
  151. outbuf.WriteString(line)
  152. }
  153. }
  154. }
  155. outbuf.WriteString("}\n\n")
  156. // Write function documentation
  157. outbuf.WriteString(fmt.Sprintf(`/*
  158. %vFuncDocMap contains the documentation of stdlib %v functions.
  159. */
  160. var %vFuncDocMap = map[interface{}]interface{}{
  161. `, pkgName, pkgName, pkgName))
  162. if pkgDoc, ok := pkgDocs[pkgName]; ok {
  163. for _, name := range scope.Names() {
  164. if !containsSymbol(pkgSymbols, name) {
  165. continue
  166. }
  167. for _, f := range pkgDoc.Funcs {
  168. if f.Name == name {
  169. outbuf.WriteString(
  170. fmt.Sprintf(` "%v": %#v,
  171. `, name, f.Doc))
  172. }
  173. }
  174. }
  175. } else {
  176. for _, name := range pkgSymbols {
  177. switch scope.Lookup(name).(type) {
  178. case *types.Func:
  179. outbuf.WriteString(
  180. fmt.Sprintf(` "%v": "Function: %v",
  181. `, name, name))
  182. }
  183. }
  184. }
  185. outbuf.WriteString("}\n\n")
  186. // Write functions
  187. outbuf.WriteString(fmt.Sprintf(`/*
  188. %vFuncMap contains the mapping of stdlib %v functions.
  189. */
  190. var %vFuncMap = map[interface{}]interface{}{
  191. `, pkgName, pkgName, pkgName))
  192. for _, name := range scope.Names() {
  193. if !containsSymbol(pkgSymbols, name) {
  194. continue
  195. }
  196. switch obj := scope.Lookup(name).(type) {
  197. case *types.Func:
  198. if unicode.IsUpper([]rune(name)[0]) {
  199. outbuf.WriteString(
  200. fmt.Sprintf(` %#v: &ECALFunctionAdapter{reflect.ValueOf(%v), fmt.Sprint(%vFuncDocMap[%#v])},
  201. `, name, obj.FullName(), pkgName, name))
  202. }
  203. }
  204. }
  205. outbuf.WriteString("}\n\n")
  206. }
  207. }
  208. }
  209. // Write dummy statement
  210. outbuf.WriteString("// Dummy statement to prevent declared and not used errors\n")
  211. outbuf.WriteString("var Dummy = fmt.Sprint(reflect.ValueOf(fmt.Sprint))\n\n")
  212. if err == nil {
  213. err = ioutil.WriteFile(filename, outbuf.Bytes(), 0644)
  214. }
  215. if err != nil {
  216. stderrPrint("Error:", err)
  217. }
  218. }
  219. var (
  220. fset = token.NewFileSet()
  221. ctx = &build.Default
  222. )
  223. /*
  224. getPackageDocs returns the source code documentation of as given Go package.
  225. Returns a short synopsis and a documentation object.
  226. */
  227. func getPackageDocs(pkgName string) (string, *doc.Package, error) {
  228. var synopsis string
  229. var pkgDoc *doc.Package
  230. var filenames []string
  231. bp, err := ctx.Import(pkgName, ".", 0)
  232. if err == nil {
  233. synopsis = bp.Doc
  234. // Get all go files of the package
  235. filenames = append(filenames, bp.GoFiles...)
  236. filenames = append(filenames, bp.CgoFiles...)
  237. // Build the ast package from Go source
  238. astPkg := &ast.Package{
  239. Name: bp.Name,
  240. Files: make(map[string]*ast.File),
  241. }
  242. for _, filename := range filenames {
  243. filepath := filepath.Join(bp.Dir, filename)
  244. astFile, _ := goparser.ParseFile(fset, filepath, nil, goparser.ParseComments)
  245. astPkg.Files[filepath] = astFile
  246. }
  247. // Build the package doc object
  248. pkgDoc = doc.New(astPkg, bp.Dir, doc.AllDecls)
  249. }
  250. return synopsis, pkgDoc, err
  251. }
  252. func containsSymbol(symbols []string, item string) bool {
  253. i := sort.SearchStrings(symbols, item)
  254. return i < len(symbols) && symbols[i] == item
  255. }