rt_assign.go 4.8 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. "fmt"
  13. "devt.de/krotik/ecal/parser"
  14. "devt.de/krotik/ecal/util"
  15. )
  16. /*
  17. assignmentRuntime is the runtime component for assignment of values.
  18. */
  19. type assignmentRuntime struct {
  20. *baseRuntime
  21. leftSide []*identifierRuntime
  22. }
  23. /*
  24. assignmentRuntimeInst returns a new runtime component instance.
  25. */
  26. func assignmentRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  27. return &assignmentRuntime{newBaseRuntime(erp, node), nil}
  28. }
  29. /*
  30. Validate this node and all its child nodes.
  31. */
  32. func (rt *assignmentRuntime) Validate() error {
  33. err := rt.baseRuntime.Validate()
  34. if err == nil {
  35. leftVar := rt.node.Children[0]
  36. if _, ok := leftVar.Runtime.(*letRuntime); ok {
  37. leftVar = leftVar.Children[0]
  38. }
  39. if leftRuntime, ok := leftVar.Runtime.(*identifierRuntime); ok {
  40. rt.leftSide = []*identifierRuntime{leftRuntime}
  41. } else if leftVar.Name == parser.NodeLIST {
  42. rt.leftSide = make([]*identifierRuntime, 0, len(leftVar.Children))
  43. for _, child := range leftVar.Children {
  44. childRuntime, ok := child.Runtime.(*identifierRuntime)
  45. if !ok {
  46. err = rt.erp.NewRuntimeError(util.ErrVarAccess,
  47. "Must have a list of variables on the left side of the assignment", rt.node)
  48. break
  49. }
  50. rt.leftSide = append(rt.leftSide, childRuntime)
  51. }
  52. } else {
  53. err = rt.erp.NewRuntimeError(util.ErrVarAccess,
  54. "Must have a variable or list of variables on the left side of the assignment", rt.node)
  55. }
  56. }
  57. return err
  58. }
  59. /*
  60. Eval evaluate this runtime component.
  61. */
  62. func (rt *assignmentRuntime) Eval(vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  63. _, err := rt.baseRuntime.Eval(vs, is, tid)
  64. if err == nil {
  65. // Execute let statements on the right before evaluating the left side
  66. if _, err = rt.node.Children[0].Runtime.Eval(vs, is, tid); err == nil {
  67. var val interface{}
  68. val, err = rt.node.Children[1].Runtime.Eval(vs, is, tid)
  69. if err == nil {
  70. if len(rt.leftSide) == 1 {
  71. err = rt.leftSide[0].Set(vs, is, tid, val)
  72. } else if valList, ok := val.([]interface{}); ok {
  73. if len(rt.leftSide) != len(valList) {
  74. err = rt.erp.NewRuntimeError(util.ErrInvalidState,
  75. fmt.Sprintf("Assigned number of variables is different to "+
  76. "number of values (%v variables vs %v values)",
  77. len(rt.leftSide), len(valList)), rt.node)
  78. } else {
  79. for i, v := range rt.leftSide {
  80. if err = v.Set(vs, is, tid, valList[i]); err != nil {
  81. err = rt.erp.NewRuntimeError(util.ErrVarAccess,
  82. err.Error(), rt.node)
  83. break
  84. }
  85. }
  86. }
  87. } else {
  88. err = rt.erp.NewRuntimeError(util.ErrInvalidState,
  89. fmt.Sprintf("Result is not a list (value is %v)", val),
  90. rt.node)
  91. }
  92. }
  93. }
  94. }
  95. return nil, err
  96. }
  97. /*
  98. letRuntime is the runtime component for let statements
  99. */
  100. type letRuntime struct {
  101. *baseRuntime
  102. declared []*identifierRuntime
  103. }
  104. /*
  105. letRuntimeInst returns a new runtime component instance.
  106. */
  107. func letRuntimeInst(erp *ECALRuntimeProvider, node *parser.ASTNode) parser.Runtime {
  108. return &letRuntime{newBaseRuntime(erp, node), nil}
  109. }
  110. /*
  111. Validate this node and all its child nodes.
  112. */
  113. func (rt *letRuntime) Validate() error {
  114. err := rt.baseRuntime.Validate()
  115. if err == nil {
  116. leftVar := rt.node.Children[0]
  117. if leftRuntime, ok := leftVar.Runtime.(*identifierRuntime); ok {
  118. rt.declared = []*identifierRuntime{leftRuntime}
  119. } else if leftVar.Name == parser.NodeLIST {
  120. rt.declared = make([]*identifierRuntime, 0, len(leftVar.Children))
  121. for _, child := range leftVar.Children {
  122. childRuntime, ok := child.Runtime.(*identifierRuntime)
  123. if !ok {
  124. err = rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  125. "Let can only declare variables within a list", rt.node)
  126. break
  127. }
  128. rt.declared = append(rt.declared, childRuntime)
  129. }
  130. } else {
  131. err = rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  132. "Let must declare a variable or list of variables", rt.node)
  133. }
  134. }
  135. return err
  136. }
  137. /*
  138. Eval evaluate this runtime component.
  139. */
  140. func (rt *letRuntime) Eval(vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  141. var res interface{}
  142. _, err := rt.baseRuntime.Eval(vs, is, tid)
  143. if err == nil {
  144. for _, v := range rt.declared {
  145. if len(v.node.Children) == 0 {
  146. vs.SetLocalValue(v.node.Token.Val, nil)
  147. } else {
  148. err = rt.erp.NewRuntimeError(util.ErrInvalidConstruct,
  149. "Let can only declare simple variables", rt.node)
  150. break
  151. }
  152. }
  153. if err == nil {
  154. res, err = rt.node.Children[0].Runtime.Eval(vs, is, tid)
  155. }
  156. }
  157. return res, err
  158. }