rt_value.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 interpreter
  11. import (
  12. "strconv"
  13. "devt.de/krotik/ecal/parser"
  14. )
  15. /*
  16. numberValueRuntime is the runtime component for constant numeric values.
  17. */
  18. type numberValueRuntime struct {
  19. *baseRuntime
  20. numValue float64 // Numeric value
  21. }
  22. /*
  23. numberValueRuntimeInst returns a new runtime component instance.
  24. */
  25. func numberValueRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  26. return &numberValueRuntime{newBaseRuntime(erp, node), 0}
  27. }
  28. /*
  29. Validate this node and all its child nodes.
  30. */
  31. func (rt *numberValueRuntime) Validate() error {
  32. err := rt.baseRuntime.Validate()
  33. if err == nil {
  34. rt.numValue, err = strconv.ParseFloat(rt.node.Token.Val, 64)
  35. }
  36. return err
  37. }
  38. /*
  39. Eval evaluate this runtime component.
  40. */
  41. func (rt *numberValueRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  42. _, err := rt.baseRuntime.Eval(vs, is)
  43. return rt.numValue, err
  44. }
  45. /*
  46. stringValueRuntime is the runtime component for constant string values.
  47. */
  48. type stringValueRuntime struct {
  49. *baseRuntime
  50. }
  51. /*
  52. stringValueRuntimeInst returns a new runtime component instance.
  53. */
  54. func stringValueRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  55. return &stringValueRuntime{newBaseRuntime(erp, node)}
  56. }
  57. /*
  58. Eval evaluate this runtime component.
  59. */
  60. func (rt *stringValueRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  61. _, err := rt.baseRuntime.Eval(vs, is)
  62. // Do some string interpolation
  63. return rt.node.Token.Val, err
  64. }
  65. /*
  66. mapValueRuntime is the runtime component for map values.
  67. */
  68. type mapValueRuntime struct {
  69. *baseRuntime
  70. }
  71. /*
  72. mapValueRuntimeInst returns a new runtime component instance.
  73. */
  74. func mapValueRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  75. return &mapValueRuntime{newBaseRuntime(erp, node)}
  76. }
  77. /*
  78. Eval evaluate this runtime component.
  79. */
  80. func (rt *mapValueRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  81. _, err := rt.baseRuntime.Eval(vs, is)
  82. m := make(map[interface{}]interface{})
  83. if err == nil {
  84. for _, kvp := range rt.node.Children {
  85. key, err := kvp.Children[0].Runtime.Eval(vs, is)
  86. if err != nil {
  87. return nil, err
  88. }
  89. val, err := kvp.Children[1].Runtime.Eval(vs, is)
  90. if err != nil {
  91. return nil, err
  92. }
  93. m[key] = val
  94. }
  95. }
  96. return m, err
  97. }
  98. /*
  99. listValueRuntime is the runtime component for list values.
  100. */
  101. type listValueRuntime struct {
  102. *baseRuntime
  103. }
  104. /*
  105. listValueRuntimeInst returns a new runtime component instance.
  106. */
  107. func listValueRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  108. return &listValueRuntime{newBaseRuntime(erp, node)}
  109. }
  110. /*
  111. Eval evaluate this runtime component.
  112. */
  113. func (rt *listValueRuntime) Eval(vs parser.Scope, is map[string]interface{}) (interface{}, error) {
  114. _, err := rt.baseRuntime.Eval(vs, is)
  115. var l []interface{}
  116. if err == nil {
  117. for _, item := range rt.node.Children {
  118. val, err := item.Runtime.Eval(vs, is)
  119. if err != nil {
  120. return nil, err
  121. }
  122. l = append(l, val)
  123. }
  124. }
  125. return l, nil
  126. }