Advertisement
lilo_booter

Bash stack implementation

Mar 8th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.77 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # stack constructor: stack name [ value ]*
  4. #
  5. # This creates a global stack object and a collection of functions of the form
  6. # name.method which are defined below.
  7. #
  8. # Example:
  9. #
  10. # $ source split_stack
  11. # $ stack name
  12. # $ name.push 20 30 40
  13. # $ echo $( name.dump )
  14. # 20 30 40
  15. # $ echo $( name.tos )
  16. # 40
  17. # $ name.pop
  18. # $ echo $( name.dump )
  19. # 20 30
  20. #
  21. # This type of use is also valid:
  22. #
  23. # $ object=name
  24. # $ $object.push 50
  25. # $ echo $( $object.dump )
  26. # 20 30 50
  27.  
  28. function stack( ) {
  29.     local name=$1
  30.     shift
  31.     unset stack_$name
  32.     declare -a -g stack_$name
  33.     object_create "$name" "_stack_"
  34.     $name.push "$@"
  35. }
  36.  
  37. # name.forget
  38. #
  39. # Destroys the named stack
  40.  
  41. function _stack_forget( ) {
  42.     local name=$1
  43.     unset stack_$name
  44.     object_destroy "$name"
  45. }
  46.  
  47. # name.copy new
  48. #
  49. # Creates a copy of the named stack as new
  50.  
  51. function _stack_copy( ) {
  52.     local name=$1
  53.     local new_name=$2
  54.     stack "$new_name"
  55.     $new_name.push $( $name.dump )
  56. }
  57.  
  58. # name.push [ value ]*
  59. #
  60. # Pushes values to the stack.
  61.  
  62. function _stack_push( ) {
  63.     local -n stack=stack_$1
  64.     shift  
  65.     while (( $# > 0 ))
  66.     do
  67.         local value=$1
  68.         local count=${#stack[@]}
  69.         stack[$count]=$value
  70.         shift
  71.     done
  72. }
  73.  
  74. # name.tos
  75. #
  76. # Outputs the current top of stack
  77.  
  78. function _stack_tos( ) {
  79.     local -n stack=stack_$1
  80.     local count=${#stack[@]}
  81.     let index="count - 1"
  82.     (( index < 0 )) && return 1
  83.     echo "${stack[$index]}"
  84. }
  85.  
  86. # name.pop
  87. #
  88. # Removes the current top of stack
  89.  
  90. function _stack_pop( ) {
  91.     local -n stack=stack_$1
  92.     local count=${#stack[@]}
  93.     let index="count - 1"
  94.     (( index < 0 )) && return 1
  95.     unset stack[$index]
  96. }
  97.  
  98. # name.depth
  99. #
  100. # Outputs the number of items on the stack
  101.  
  102. function _stack_depth( ) {
  103.     local -n stack=stack_$1
  104.     echo ${#stack[@]}
  105. }
  106.  
  107. # name.dump
  108. #
  109. # Convenience function to output the contents of the stack
  110.  
  111. function _stack_dump( ) {
  112.     local -n stack=stack_$1
  113.     for value in "${stack[@]}"
  114.     do
  115.         echo "$value"
  116.     done
  117. }
  118.  
  119. # Object functions
  120.  
  121. # object_create name pattern
  122.  
  123. function object_create( ) {
  124.     local name=$1
  125.     local pattern=$2
  126.     local methods=$( declare -F | grep " $pattern" | sed "s/^declare -f $pattern//" )
  127.     for method in ${methods[@]}
  128.     do
  129.         method_create "$name.$method" "$pattern$method" "$name"
  130.     done
  131. }
  132.  
  133. # object_destroy name
  134.  
  135. function object_destroy( ) {
  136.     local name=$1
  137.     local methods=$( declare -F | grep " $name\." | sed "s/^declare -f //" )
  138.     for method in ${methods[@]}
  139.     do
  140.         unset -f "$method"
  141.     done
  142. }
  143.  
  144. # method_create name function args ...
  145.  
  146. function method_create( ) {
  147.     local name=$1
  148.     shift
  149.     eval "$name( ) { "$@" \"\$@\" ; }"
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement