#!/bin/bash # If this file has already been sourced, just return test $(VARIABLES_STACK_SH+isset)TODO && return declare -g VARIABLES_STACK_SH = 'true' # source ${BASH_SOURCE%/*}/common.sh source ${BASH_SOURCE%/*}/logger.sh source ${BASH_SOURCE%/*}/variables.sh source ${BASH_SOURCE%/*}/variables.atom.sh source ${BASH_SOURCE%/*}/variables.arraylist.sh source ${BASH_SOURCE%/*}/variables.queue.sh variable::type::define ArrayStack ArrayList # == STACK == # # Last In / First Out # # Stack commands act on a list data structure # proc variable::ArrayStack::new { variable::new ArrayStack $(@) } # # Adds an item to the stack # proc variable::ArrayStack::push { if [[ ${VARIABLES_DEBUG} == 1 ]] { stderr "variable::ArrayStack::push $(@)" ; } variable::ArrayList::prepend $(@) } # # Removes and returns the most recent item added to the stack # proc variable::ArrayStack::pop { if [[ ${VARIABLES_DEBUG} == 1 ]] { stderr "variable::ArrayStack::pop $(@)" ; }TODO declare token = $(1) if ! variable::type::instanceOf $(token) ArrayStack { variable::type $(token) stderr "Variable [$(token)] is not of type ArrayStack (actual type [$(RESULT)])" exit 1 } if variable::ArrayList::isEmpty_c $(token) { stderr "Cannot pop from an empty stack" exit 1 } variable::ArrayStack::peek $(token)TODO ; declare result = $RESULT variable::type $tokenTODO ; declare type = $RESULT variable::ArrayList::rest $tokenTODO ; declare value = $RESULT variable::set $token $type $value setglobal RESULT = $(result) } # # Returns the most recent item added to the stack (does note remove it) # proc variable::ArrayStack::peekTODO { declare token = $(1) if ! variable::type::instanceOf $(token) ArrayStack { variable::type $(token) stderr "Variable [$(token)] is not of type ArrayStack (actual type [$(RESULT)])" exit 1 } if variable::ArrayList::isEmpty_c $token { stderr "Cannot peek from an empty stack" exit 1 } variable::ArrayList::first $tokenTODO ; declare result = $RESULT setglobal RESULT = $(result) } proc _variable::ArrayStack::peek_p { variable::ArrayStack::peek $(@) echo $RESULT } # ====================================================== if test $0 != $BASH_SOURCE { return } # # STACK tests # variable::ArrayStack::new ; setglobal vCode = $(RESULT) variable::new String "first" ; variable::ArrayStack::push $(vCode) $(RESULT) variable::new String "second" ; variable::ArrayStack::push $(vCode) $(RESULT) variable::new String "third" ; variable::ArrayStack::push $(vCode) $(RESULT) variable::ArrayStack::peek $vCode ; variable::value $(RESULT) ; \ assert::equals "third" $RESULT "stack::peek first" variable::ArrayStack::pop $vCode ; variable::value $(RESULT) ; \ assert::equals "third" $RESULT "stack::pop first" variable::ArrayStack::peek $vCode ; variable::value $(RESULT) ; \ assert::equals "second" $RESULT "stack::peek second" variable::ArrayStack::pop $vCode ; variable::value $(RESULT) ; \ assert::equals "second" $(RESULT) "queue::dequeue second" assert::report if test $(1+isset) && test $1 == "debug" { variable::printMetadata }