stdlib.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. var internalStdlibFuncMap = make(map[string]util.ECALFunction)
  17. var internalStdlibDocMap = make(map[string]string)
  18. /*
  19. AddStdlibPkg adds a package to stdlib. A package needs to be added before functions
  20. can be added.
  21. */
  22. func AddStdlibPkg(pkg string, docstring string) error {
  23. _, ok1 := GetPkgDocString(pkg)
  24. _, ok2 := internalStdlibDocMap[pkg]
  25. if ok1 || ok2 {
  26. return fmt.Errorf("Package %v already exists", pkg)
  27. }
  28. internalStdlibDocMap[pkg] = docstring
  29. return nil
  30. }
  31. /*
  32. AddStdlibFunc adds a function to stdlib.
  33. */
  34. func AddStdlibFunc(pkg string, name string, funcObj util.ECALFunction) error {
  35. _, ok1 := GetPkgDocString(pkg)
  36. _, ok2 := internalStdlibDocMap[pkg]
  37. if !ok1 && !ok2 {
  38. return fmt.Errorf("Package %v does not exist", pkg)
  39. }
  40. internalStdlibFuncMap[fmt.Sprintf("%v.%v", pkg, name)] = funcObj
  41. return nil
  42. }
  43. /*
  44. GetStdlibSymbols returns all available packages of stdlib and their constant
  45. and function symbols.
  46. */
  47. func GetStdlibSymbols() ([]string, []string, []string) {
  48. var constSymbols, funcSymbols []string
  49. var packageNames []string
  50. packageSet := make(map[string]bool)
  51. addSym := func(sym string, suffix string, symMap map[interface{}]interface{},
  52. ret []string) []string {
  53. if strings.HasSuffix(sym, suffix) {
  54. trimSym := strings.TrimSuffix(sym, suffix)
  55. packageSet[trimSym] = true
  56. for k := range symMap {
  57. ret = append(ret, fmt.Sprintf("%v.%v", trimSym, k))
  58. }
  59. }
  60. return ret
  61. }
  62. for k, v := range genStdlib {
  63. sym := fmt.Sprint(k)
  64. if symMap, ok := v.(map[interface{}]interface{}); ok {
  65. constSymbols = addSym(sym, "-const", symMap, constSymbols)
  66. funcSymbols = addSym(sym, "-func", symMap, funcSymbols)
  67. }
  68. }
  69. for k := range packageSet {
  70. packageNames = append(packageNames, k)
  71. }
  72. // Add internal stuff
  73. for k := range internalStdlibDocMap {
  74. packageNames = append(packageNames, k)
  75. }
  76. for k := range internalStdlibFuncMap {
  77. funcSymbols = append(funcSymbols, k)
  78. }
  79. return packageNames, constSymbols, funcSymbols
  80. }
  81. /*
  82. GetStdlibConst looks up a constant from stdlib.
  83. */
  84. func GetStdlibConst(name string) (interface{}, bool) {
  85. var res interface{}
  86. var resok bool
  87. if m, n := splitModuleAndName(name); n != "" {
  88. if cmap, ok := genStdlib[fmt.Sprintf("%v-const", m)]; ok {
  89. res, resok = cmap.(map[interface{}]interface{})[n]
  90. }
  91. }
  92. return res, resok
  93. }
  94. /*
  95. GetStdlibFunc looks up a function from stdlib.
  96. */
  97. func GetStdlibFunc(name string) (util.ECALFunction, bool) {
  98. var res util.ECALFunction
  99. var resok bool
  100. if m, n := splitModuleAndName(name); n != "" {
  101. if fmap, ok := genStdlib[fmt.Sprintf("%v-func", m)]; ok {
  102. if fn, ok := fmap.(map[interface{}]interface{})[n]; ok {
  103. res = fn.(util.ECALFunction)
  104. resok = true
  105. }
  106. }
  107. }
  108. if !resok {
  109. res, resok = internalStdlibFuncMap[name]
  110. }
  111. return res, resok
  112. }
  113. /*
  114. GetPkgDocString returns the docstring of a stdlib package.
  115. */
  116. func GetPkgDocString(name string) (string, bool) {
  117. var res = ""
  118. s, ok := genStdlib[fmt.Sprintf("%v-synopsis", name)]
  119. if ok {
  120. res = fmt.Sprint(s)
  121. } else {
  122. res, ok = internalStdlibDocMap[name]
  123. }
  124. return res, ok
  125. }
  126. /*
  127. splitModuleAndName splits up a given full function name in module and function name part.
  128. */
  129. func splitModuleAndName(fullname string) (string, string) {
  130. var module, name string
  131. ccSplit := strings.SplitN(fullname, ".", 2)
  132. if len(ccSplit) != 0 {
  133. module = ccSplit[0]
  134. name = strings.Join(ccSplit[1:], "")
  135. }
  136. return module, name
  137. }