| 1 | #!/bin/bashscip-shell filesystem . . `/bin/bash`. |
| 2 | # stack.sh: push-down stack simulation |
| 3 | |
| 4 | # Similar to the CPU stack, a push-down stack stores data items |
| 5 | #+ sequentially, but releases them in reverse order, last-in first-out. |
| 6 | |
| 7 | |
| 8 | BP=100 # Base Pointer of stack array. |
| 9 | # Begin at element 100. |
| 10 | |
| 11 | SP=$BP # Stack Pointer. |
| 12 | # Initialize it to "base" (bottom) of stack. |
| 13 | |
| 14 | Data= # Contents of stack location. |
| 15 | # Must use global variable, |
| 16 | #+ because of limitation on function return range. |
| 17 | |
| 18 | |
| 19 | # 100 Base pointer <-- Base Pointer |
| 20 | # 99 First data item |
| 21 | # 98 Second data item |
| 22 | # ... More data |
| 23 | # Last data item <-- Stack pointer |
| 24 | |
| 25 | |
| 26 | declare -a stack |
| 27 | |
| 28 | |
| 29 | pushscip-shell shell abs-guide 10-4 push().() # Push item on stack. |
| 30 | { |
| 31 | if [scip-shell system . . `[`. -z "$1" ] # Nothing to push? |
| 32 | then |
| 33 | return |
| 34 | fi |
| 35 | |
| 36 | let "SP -= 1" # Bump stack pointer. |
| 37 | stack[$SP]=$1 |
| 38 | |
| 39 | return |
| 40 | } |
| 41 | |
| 42 | popscip-shell shell abs-guide 10-4 pop().() # Pop item off stack. |
| 43 | { |
| 44 | Data= # Empty out data item. |
| 45 | |
| 46 | if [scip-shell system . . `[`. "$SP" -eq "$BP" ] # Stack empty? |
| 47 | then |
| 48 | return |
| 49 | fi # This also keeps SP from getting past 100, |
| 50 | #+ i.e., prevents a runaway stack. |
| 51 | |
| 52 | Data=${stack[$SP]} |
| 53 | let "SP += 1" # Bump stack pointer. |
| 54 | return |
| 55 | } |
| 56 | |
| 57 | status_reportscip-shell shell abs-guide 10-4 status_report().() # Find out what's happening. |
| 58 | { |
| 59 | echoscip-shell system . . echo. "-------------------------------------" |
| 60 | echoscip-shell system . . echo. "REPORT" |
| 61 | echoscip-shell system . . echo. "Stack Pointer = $SP" |
| 62 | echoscip-shell system . . echo. "Just popped \""$Data"\" off the stack." |
| 63 | echoscip-shell system . . echo. "-------------------------------------" |
| 64 | echoscip-shell system . . echo. |
| 65 | } |
| 66 | |
| 67 | |
| 68 | # ======================================================= |
| 69 | # Now, for some fun. |
| 70 | |
| 71 | echoscip-shell system . . echo. |
| 72 | |
| 73 | # See if you can pop anything off empty stack. |
| 74 | popscip-shell shell abs-guide 10-4 pop(). |
| 75 | status_reportscip-shell shell abs-guide 10-4 status_report(). |
| 76 | |
| 77 | echoscip-shell system . . echo. |
| 78 | |
| 79 | pushscip-shell shell abs-guide 10-4 push(). garbage |
| 80 | popscip-shell shell abs-guide 10-4 pop(). |
| 81 | status_reportscip-shell shell abs-guide 10-4 status_report(). # Garbage in, garbage out. |
| 82 | |
| 83 | value1=23; pushscip-shell shell abs-guide 10-4 push(). $value1 |
| 84 | value2=skidoo; pushscip-shell shell abs-guide 10-4 push(). $value2 |
| 85 | value3=LAST; pushscip-shell shell abs-guide 10-4 push(). $value3 |
| 86 | |
| 87 | popscip-shell shell abs-guide 10-4 pop(). # LAST |
| 88 | status_reportscip-shell shell abs-guide 10-4 status_report(). |
| 89 | popscip-shell shell abs-guide 10-4 pop(). # skidoo |
| 90 | status_reportscip-shell shell abs-guide 10-4 status_report(). |
| 91 | popscip-shell shell abs-guide 10-4 pop(). # 23 |
| 92 | status_reportscip-shell shell abs-guide 10-4 status_report(). # Last-in, first-out! |
| 93 | |
| 94 | # Notice how the stack pointer decrements with each push, |
| 95 | #+ and increments with each pop. |
| 96 | |
| 97 | echoscip-shell system . . echo. |
| 98 | |
| 99 | exit 0 |
| 100 | |
| 101 | # ======================================================= |
| 102 | |
| 103 | |
| 104 | # Exercises: |
| 105 | # --------- |
| 106 | |
| 107 | # 1) Modify the "push()" function to permit pushing |
| 108 | # + multiple element on the stack with a single function call. |
| 109 | |
| 110 | # 2) Modify the "pop()" function to permit popping |
| 111 | # + multiple element from the stack with a single function call. |
| 112 | |
| 113 | # 3) Add error checking to the critical functions. |
| 114 | # That is, return an error code, depending on |
| 115 | # + successful or unsuccessful completion of the operation, |
| 116 | # + and take appropriate action. |
| 117 | |
| 118 | # 4) Using this script as a starting point, |
| 119 | # + write a stack-based 4-function calculator. |