stdlib.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 stdlib
  11. import (
  12. "fmt"
  13. "strings"
  14. "devt.de/krotik/ecal/util"
  15. )
  16. /*
  17. GetStdlibSymbols returns all available packages of stdlib and their constant
  18. and function symbols.
  19. */
  20. func GetStdlibSymbols() ([]string, []string, []string) {
  21. var constSymbols, funcSymbols []string
  22. var packageNames []string
  23. packageSet := make(map[string]bool)
  24. addSym := func(sym string, suffix string, symMap map[interface{}]interface{},
  25. ret []string) []string {
  26. if strings.HasSuffix(sym, suffix) {
  27. trimSym := strings.TrimSuffix(sym, suffix)
  28. packageSet[trimSym] = true
  29. for k := range symMap {
  30. ret = append(ret, fmt.Sprintf("%v.%v", trimSym, k))
  31. }
  32. }
  33. return ret
  34. }
  35. for k, v := range genStdlib {
  36. sym := fmt.Sprint(k)
  37. if symMap, ok := v.(map[interface{}]interface{}); ok {
  38. constSymbols = addSym(sym, "-const", symMap, constSymbols)
  39. funcSymbols = addSym(sym, "-func", symMap, funcSymbols)
  40. }
  41. }
  42. for k := range packageSet {
  43. packageNames = append(packageNames, k)
  44. }
  45. return packageNames, constSymbols, funcSymbols
  46. }
  47. /*
  48. GetStdlibConst looks up a constant from stdlib.
  49. */
  50. func GetStdlibConst(name string) (interface{}, bool) {
  51. var res interface{}
  52. var resok bool
  53. if m, n := splitModuleAndName(name); n != "" {
  54. if cmap, ok := genStdlib[fmt.Sprintf("%v-const", m)]; ok {
  55. res, resok = cmap.(map[interface{}]interface{})[n]
  56. }
  57. }
  58. return res, resok
  59. }
  60. /*
  61. GetStdlibFunc looks up a function from stdlib.
  62. */
  63. func GetStdlibFunc(name string) (util.ECALFunction, bool) {
  64. var res util.ECALFunction
  65. var resok bool
  66. if m, n := splitModuleAndName(name); n != "" {
  67. if fmap, ok := genStdlib[fmt.Sprintf("%v-func", m)]; ok {
  68. if fn, ok := fmap.(map[interface{}]interface{})[n]; ok {
  69. res = fn.(util.ECALFunction)
  70. resok = true
  71. }
  72. }
  73. }
  74. return res, resok
  75. }
  76. /*
  77. GetPkgDocString returns the docstring of a stdlib package.
  78. */
  79. func GetPkgDocString(name string) (string, bool) {
  80. var res = ""
  81. s, ok := genStdlib[fmt.Sprintf("%v-synopsis", name)]
  82. if ok {
  83. res = fmt.Sprint(s)
  84. }
  85. return res, ok
  86. }
  87. /*
  88. splitModuleAndName splits up a given full function name in module and function name part.
  89. */
  90. func splitModuleAndName(fullname string) (string, string) {
  91. var module, name string
  92. ccSplit := strings.SplitN(fullname, ".", 2)
  93. if len(ccSplit) != 0 {
  94. module = ccSplit[0]
  95. name = strings.Join(ccSplit[1:], "")
  96. }
  97. return module, name
  98. }