rt_func.go 4.7 KB

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