rt_func.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. "encoding/json"
  13. "fmt"
  14. "strings"
  15. "devt.de/krotik/ecal/parser"
  16. "devt.de/krotik/ecal/scope"
  17. "devt.de/krotik/ecal/util"
  18. )
  19. /*
  20. returnRuntime is a special runtime for return statements in functions.
  21. */
  22. type returnRuntime struct {
  23. *baseRuntime
  24. }
  25. /*
  26. voidRuntimeInst returns a new runtime component instance.
  27. */
  28. func returnRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  29. return &returnRuntime{newBaseRuntime(erp, node)}
  30. }
  31. /*
  32. Validate this node and all its child nodes.
  33. */
  34. func (rt *returnRuntime) Validate() error {
  35. return rt.baseRuntime.Validate()
  36. }
  37. /*
  38. Eval evaluate this runtime component.
  39. */
  40. func (rt *returnRuntime) Eval(vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  41. _, err := rt.baseRuntime.Eval(vs, is, tid)
  42. if err == nil {
  43. var res interface{}
  44. if len(rt.node.Children) > 0 {
  45. res, err = rt.node.Children[0].Runtime.Eval(vs, is, tid)
  46. } else {
  47. res = nil
  48. }
  49. if err == nil {
  50. rerr := rt.erp.NewRuntimeError(util.ErrReturn, fmt.Sprintf("Return value: %v", res), rt.node)
  51. err = &returnValue{
  52. rerr.(*util.RuntimeError),
  53. res,
  54. }
  55. }
  56. }
  57. return nil, err
  58. }
  59. type returnValue struct {
  60. *util.RuntimeError
  61. returnValue interface{}
  62. }
  63. /*
  64. funcRuntime is the runtime component for function declarations.
  65. */
  66. type funcRuntime struct {
  67. *baseRuntime
  68. }
  69. /*
  70. funcRuntimeInst returns a new runtime component instance.
  71. */
  72. func funcRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  73. return &funcRuntime{newBaseRuntime(erp, node)}
  74. }
  75. /*
  76. Eval evaluate this runtime component.
  77. */
  78. func (rt *funcRuntime) Eval(vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  79. var fc interface{}
  80. _, err := rt.baseRuntime.Eval(vs, is, tid)
  81. if err == nil {
  82. name := ""
  83. if rt.node.Children[0].Name == parser.NodeIDENTIFIER {
  84. name = rt.node.Children[0].Token.Val
  85. }
  86. fc = &function{name, nil, nil, rt.node, vs}
  87. if name != "" {
  88. vs.SetValue(name, fc)
  89. }
  90. }
  91. return fc, err
  92. }
  93. /*
  94. function models a function in ECAL. It can have a context object attached - this.
  95. */
  96. type function struct {
  97. name string
  98. super []interface{} // Super function pointer
  99. this interface{} // Function context
  100. declaration *parser.ASTNode // Function declaration node
  101. declarationVS parser.Scope // Function declaration scope
  102. }
  103. /*
  104. Run executes this function. The function is called with parameters and might also
  105. have a reference to a context state - this.
  106. */
  107. func (f *function) Run(instanceID string, vs parser.Scope, is map[string]interface{}, tid uint64, args []interface{}) (interface{}, error) {
  108. var res interface{}
  109. var err error
  110. nameOffset := 0
  111. if f.declaration.Children[0].Name == parser.NodeIDENTIFIER {
  112. nameOffset = 1
  113. }
  114. params := f.declaration.Children[0+nameOffset].Children
  115. body := f.declaration.Children[1+nameOffset]
  116. // Create varscope for the body - not a child scope but a new root
  117. fvs := scope.NewScope(fmt.Sprintf("%v %v", scope.FuncPrefix, f.name))
  118. if f.this != nil {
  119. fvs.SetValue("this", f.this)
  120. }
  121. if f.super != nil {
  122. fvs.SetValue("super", f.super)
  123. }
  124. for i, p := range params {
  125. var name string
  126. var val interface{}
  127. if err == nil {
  128. name = ""
  129. if p.Name == parser.NodeIDENTIFIER {
  130. name = p.Token.Val
  131. if i < len(args) {
  132. val = args[i]
  133. }
  134. } else if p.Name == parser.NodePRESET {
  135. name = p.Children[0].Token.Val
  136. if i < len(args) {
  137. val = args[i]
  138. } else {
  139. val, err = p.Children[1].Runtime.Eval(vs, is, tid)
  140. }
  141. }
  142. if name != "" {
  143. fvs.SetValue(name, val)
  144. }
  145. }
  146. }
  147. if err == nil {
  148. scope.SetParentOfScope(fvs, f.declarationVS)
  149. res, err = body.Runtime.Eval(fvs, make(map[string]interface{}), tid)
  150. // Check for return value (delivered as error object)
  151. if rval, ok := err.(*returnValue); ok {
  152. res = rval.returnValue
  153. err = nil
  154. }
  155. }
  156. return res, err
  157. }
  158. /*
  159. DocString returns a descriptive string.
  160. */
  161. func (f *function) DocString() (string, error) {
  162. if len(f.declaration.Meta) > 0 {
  163. return strings.TrimSpace(f.declaration.Meta[0].Value()), nil
  164. }
  165. return fmt.Sprintf("Declared function: %v (%v)", f.name, f.declaration.Token.PosString()), nil
  166. }
  167. /*
  168. String returns a string representation of this function.
  169. */
  170. func (f *function) String() string {
  171. return fmt.Sprintf("ecal.function: %v (%v)", f.name, f.declaration.Token.PosString())
  172. }
  173. /*
  174. MarshalJSON returns a string representation of this function - a function cannot
  175. be JSON encoded.
  176. */
  177. func (f *function) MarshalJSON() ([]byte, error) {
  178. return json.Marshal(f.String())
  179. }