rt_func.go 4.5 KB

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