stdlib.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. GetStdlibConst looks up a constant from stdlib.
  18. */
  19. func GetStdlibConst(name string) (interface{}, bool) {
  20. m, n := splitModuleAndName(name)
  21. if n != "" {
  22. if cmap, ok := genStdlib[fmt.Sprintf("%v-const", m)]; ok {
  23. if cv, ok := cmap.(map[interface{}]interface{})[n]; ok {
  24. return cv.(interface{}), true
  25. }
  26. }
  27. }
  28. return nil, false
  29. }
  30. /*
  31. GetStdlibFunc looks up a function from stdlib.
  32. */
  33. func GetStdlibFunc(name string) (util.ECALFunction, bool) {
  34. m, n := splitModuleAndName(name)
  35. if n != "" {
  36. if fmap, ok := genStdlib[fmt.Sprintf("%v-func", m)]; ok {
  37. if fn, ok := fmap.(map[interface{}]interface{})[n]; ok {
  38. return fn.(util.ECALFunction), true
  39. }
  40. }
  41. }
  42. return nil, false
  43. }
  44. func splitModuleAndName(name string) (string, string) {
  45. ccSplit := strings.SplitN(name, ".", 2)
  46. if len(ccSplit) == 0 {
  47. return "", ""
  48. }
  49. return ccSplit[0], strings.Join(ccSplit[1:], "")
  50. }