stdlib.go 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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(fullname string) (string, string) {
  45. var module, name string
  46. ccSplit := strings.SplitN(fullname, ".", 2)
  47. if len(ccSplit) != 0 {
  48. module = ccSplit[0]
  49. name = strings.Join(ccSplit[1:], "")
  50. }
  51. return module, name
  52. }