rt_func.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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{}) (interface{}, error) {
  41. _, err := rt.baseRuntime.Eval(vs, is)
  42. if err == nil {
  43. var res interface{}
  44. if res, err = rt.node.Children[0].Runtime.Eval(vs, is); 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{}) (interface{}, error) {
  74. var fc interface{}
  75. _, err := rt.baseRuntime.Eval(vs, is)
  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}
  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. }
  97. /*
  98. Run executes this function. The function is called with parameters and might also
  99. have a reference to a context state - this.
  100. */
  101. func (f *function) Run(instanceID string, vs parser.Scope, is map[string]interface{}, args []interface{}) (interface{}, error) {
  102. var res interface{}
  103. var err error
  104. nameOffset := 0
  105. if f.declaration.Children[0].Name == parser.NodeIDENTIFIER {
  106. nameOffset = 1
  107. }
  108. params := f.declaration.Children[0+nameOffset].Children
  109. body := f.declaration.Children[1+nameOffset]
  110. // Create varscope for the body - not a child scope but a new root
  111. fvs := scope.NewScope(fmt.Sprintf("func: %v", f.name))
  112. if f.this != nil {
  113. fvs.SetValue("this", f.this)
  114. }
  115. if f.super != nil {
  116. fvs.SetValue("super", f.super)
  117. }
  118. for i, p := range params {
  119. var name string
  120. var val interface{}
  121. if err == nil {
  122. name = ""
  123. if p.Name == parser.NodeIDENTIFIER {
  124. name = p.Token.Val
  125. if i < len(args) {
  126. val = args[i]
  127. }
  128. } else if p.Name == parser.NodePRESET {
  129. name = p.Children[0].Token.Val
  130. if i < len(args) {
  131. val = args[i]
  132. } else {
  133. val, err = p.Children[1].Runtime.Eval(vs, is)
  134. }
  135. }
  136. if name != "" {
  137. fvs.SetValue(name, val)
  138. }
  139. }
  140. }
  141. if err == nil {
  142. scope.SetParentOfScope(fvs, vs)
  143. res, err = body.Runtime.Eval(fvs, make(map[string]interface{}))
  144. // Check for return value (delivered as error object)
  145. if rval, ok := err.(*returnValue); ok {
  146. res = rval.returnValue
  147. err = nil
  148. }
  149. }
  150. return res, err
  151. }
  152. /*
  153. DocString returns a descriptive string.
  154. */
  155. func (f *function) DocString() (string, error) {
  156. if len(f.declaration.Meta) > 0 {
  157. return strings.TrimSpace(f.declaration.Meta[0].Value()), nil
  158. }
  159. return fmt.Sprintf("Declared function: %v (%v)", f.name, f.declaration.Token.PosString()), nil
  160. }
  161. /*
  162. String returns a string representation of this function.
  163. */
  164. func (f *function) String() string {
  165. return fmt.Sprintf("ecal.function: %v (%v)", f.name, f.declaration.Token.PosString())
  166. }
  167. /*
  168. MarshalJSON returns a string representation of this function - a function cannot
  169. be JSON encoded.
  170. */
  171. func (f *function) MarshalJSON() ([]byte, error) {
  172. return json.Marshal(f.String())
  173. }