fork download
  1. #!/bin/sh
  2.  
  3. data()
  4. {
  5. ds="$1"
  6. object="$2"
  7. method="$3"
  8. if [ "$method" = "_data" ]; then
  9. echo "$ds"
  10. fi
  11. }
  12.  
  13. get_d()
  14. {
  15. [ $# -lt 2 ] && return
  16. object="$1"
  17. d="$2"
  18. eval "echo \$$($object $d)"
  19. }
  20.  
  21. set_d()
  22. {
  23. [ $# -lt 3 ] && return
  24. object="$1"
  25. d="$2"
  26. new_val="$3"
  27. eval "$($object $d)"="$new_val"
  28. }
  29.  
  30. object_counter=0
  31.  
  32. new()
  33. {
  34. [ $# -lt 2 ] && return
  35. class="$1"
  36. object_var="$2"
  37. ds="$($class null _data)"
  38. eval 'object_'"$object_counter"'()
  39. {
  40. [ "$#" -lt 1 ] && return
  41. method="$1"
  42. case "$method" in
  43. "_class")
  44. echo '"$class"';;
  45. '"$(for d in $ds; do
  46. echo '"'$d'")
  47. echo object_'$object_counter'_'$d';;'
  48. done)"'
  49. esac
  50. }'
  51. object="object_$object_counter"
  52. object_counter="$((object_counter + 1))"
  53. call "$object" _init
  54. eval "$object_var=$object"
  55. }
  56.  
  57. call()
  58. {
  59. [ $# -lt 2 ] && return
  60. object="$1"
  61. method="$2"
  62. class="$($object _class)"
  63. shift 2
  64. "$class" "$object" "$method" "$@"
  65. }
  66.  
  67. ################################################################################
  68.  
  69. BaseYoba()
  70. {
  71. data "x y" "$@"
  72. case "$method" in
  73. "_init")
  74. set_d "$object" x 0
  75. set_d "$object" y 0 ;;
  76. "print")
  77. echo "x=$(get_d $object x) y=$(get_d $object y)" ;;
  78. esac
  79. }
  80.  
  81. DerivedYoba1()
  82. {
  83. BaseYoba "$@"
  84. case "$method" in
  85. "go")
  86. set_d "$object" x "$(($(get_d $object x) + $3))"
  87. set_d "$object" y "$(($(get_d $object y) + $3))" ;;
  88. esac
  89. }
  90.  
  91. DerivedYoba2()
  92. {
  93. case "$method" in
  94. "go")
  95. set_d "$object" x "$(($(get_d $object x) + $3 * 2))" ;;
  96. *)
  97. BaseYoba "$@" ;;
  98. esac
  99. }
  100.  
  101. DerivedYoba3()
  102. {
  103. data "z" "$@"
  104. case "$method" in
  105. "_init")
  106. DerivedYoba1 "$@"
  107. set_d "$object" z 0 ;;
  108. "go")
  109. DerivedYoba1 "$@"
  110. set_d "$object" z "$(($(get_d $object z) + $3 * 2))" ;;
  111. "print")
  112. echo "x=$(get_d $object x) y=$(get_d $object y) z=$(get_d $object z)" ;;
  113. *)
  114. DerivedYoba1 "$@"
  115. esac
  116. }
  117.  
  118. fun()
  119. {
  120. for i in `seq 5`; do
  121. call "$1" go $i
  122. call "$1" print
  123. done
  124. }
  125.  
  126. new DerivedYoba1 yoba1
  127. fun "$yoba1"
  128. new DerivedYoba2 yoba2
  129. fun "$yoba2"
  130. new DerivedYoba3 yoba3
  131. fun "$yoba3"
  132.  
Success #stdin #stdout 0.06s 5312KB
stdin
Standard input is empty
stdout
x=1 y=1
x=3 y=3
x=6 y=6
x=10 y=10
x=15 y=15
x=2 y=0
x=6 y=0
x=12 y=0
x=20 y=0
x=30 y=0
x=1 y=1 z=2
x=3 y=3 z=6
x=6 y=6 z=12
x=10 y=10 z=20
x=15 y=15 z=30