rt_assign.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 leftRuntime, ok := leftVar.Runtime.(*identifierRuntime); ok {
  37. rt.leftSide = []*identifierRuntime{leftRuntime}
  38. } else if leftVar.Name == parser.NodeLIST {
  39. rt.leftSide = make([]*identifierRuntime, 0, len(leftVar.Children))
  40. for _, child := range leftVar.Children {
  41. childRuntime, ok := child.Runtime.(*identifierRuntime)
  42. if !ok {
  43. err = rt.erp.NewRuntimeError(util.ErrVarAccess,
  44. "Must have a list of variables on the left side of the assignment", rt.node)
  45. break
  46. }
  47. rt.leftSide = append(rt.leftSide, childRuntime)
  48. }
  49. } else {
  50. err = rt.erp.NewRuntimeError(util.ErrVarAccess,
  51. "Must have a variable or list of variables on the left side of the assignment", rt.node)
  52. }
  53. }
  54. return err
  55. }
  56. /*
  57. Eval evaluate this runtime component.
  58. */
  59. func (rt *assignmentRuntime) Eval(vs parser.Scope, is map[string]interface{}, tid uint64) (interface{}, error) {
  60. _, err := rt.baseRuntime.Eval(vs, is, tid)
  61. if err == nil {
  62. var val interface{}
  63. val, err = rt.node.Children[1].Runtime.Eval(vs, is, tid)
  64. if err == nil {
  65. if len(rt.leftSide) == 1 {
  66. err = rt.leftSide[0].Set(vs, is, tid, val)
  67. } else if valList, ok := val.([]interface{}); ok {
  68. if len(rt.leftSide) != len(valList) {
  69. err = rt.erp.NewRuntimeError(util.ErrInvalidState,
  70. fmt.Sprintf("Assigned number of variables is different to "+
  71. "number of values (%v variables vs %v values)",
  72. len(rt.leftSide), len(valList)), rt.node)
  73. } else {
  74. for i, v := range rt.leftSide {
  75. if err = v.Set(vs, is, tid, valList[i]); err != nil {
  76. err = rt.erp.NewRuntimeError(util.ErrVarAccess,
  77. err.Error(), rt.node)
  78. break
  79. }
  80. }
  81. }
  82. } else {
  83. err = rt.erp.NewRuntimeError(util.ErrInvalidState,
  84. fmt.Sprintf("Result is not a list (value is %v)", val),
  85. rt.node)
  86. }
  87. }
  88. }
  89. return nil, err
  90. }