#!/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 global 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 global RESULT := $(result) } proc _variable::ArrayStack::peek_p { variable::ArrayStack::peek $(@) echo $RESULT } # ====================================================== if test $0 != $BASH_SOURCE { return } # # STACK tests # variable::ArrayStack::new ; global 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 } (CommandList children: [ (AndOr children: [ (C {(Lit_Other "[")} { (BracedVarSub token: suffix_op: (StringUnary op_id:VTest_Plus arg_word:{(isset)}) spids: [9 13] ) } {(Lit_Other "]")} ) (ControlFlow token:) ] op_id: Op_DAmp ) (Assignment keyword: Assign_Declare flags: ["'-g'"] pairs: [(assign_pair lhs:(LhsName name:VARIABLES_STACK_SH) op:Equal rhs:{(true)} spids:[25])] spids: [21] ) (C {(.)} { (BracedVarSub token: suffix_op: (StringUnary op_id:VOp1_Percent arg_word:{(Lit_Slash /) ("*")}) spids: [35 40] ) (/common.sh) } ) (C {(.)} { (BracedVarSub token: suffix_op: (StringUnary op_id:VOp1_Percent arg_word:{(Lit_Slash /) ("*")}) spids: [45 50] ) (/logger.sh) } ) (C {(.)} { (BracedVarSub token: suffix_op: (StringUnary op_id:VOp1_Percent arg_word:{(Lit_Slash /) ("*")}) spids: [55 60] ) (/variables.sh) } ) (C {(.)} { (BracedVarSub token: suffix_op: (StringUnary op_id:VOp1_Percent arg_word:{(Lit_Slash /) ("*")}) spids: [65 70] ) (/variables.atom.sh) } ) (C {(.)} { (BracedVarSub token: suffix_op: (StringUnary op_id:VOp1_Percent arg_word:{(Lit_Slash /) ("*")}) spids: [75 80] ) (/variables.arraylist.sh) } ) (C {(.)} { (BracedVarSub token: suffix_op: (StringUnary op_id:VOp1_Percent arg_word:{(Lit_Slash /) ("*")}) spids: [85 90] ) (/variables.queue.sh) } ) (C {(variable) (Lit_Other ":") (Lit_Other ":") (type) (Lit_Other ":") (Lit_Other ":") (define)} {(ArrayStack)} {(ArrayList)} ) (FuncDef name: "variable::ArrayStack::new" body: (BraceGroup children: [ (C {(variable) (Lit_Other ":") (Lit_Other ":") (new)} {(ArrayStack)} {(DQ (${ VSub_At "@"))}) ] spids: [138] ) spids: [126 137] ) (FuncDef name: "variable::ArrayStack::push" body: (BraceGroup children: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(${ VSub_Name VARIABLES_DEBUG)} right: {(1)} ) ) terminator: ) ] action: [ (Sentence child: (C {(stderr)} {(DQ ("variable::ArrayStack::push ") (${ VSub_At "@"))}) terminator: ) ] spids: [-1 196] ) ] spids: [-1 209] ) (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayList) (Lit_Other ":") (Lit_Other ":") (prepend) } {(DQ (${ VSub_At "@"))} ) ] spids: [178] ) spids: [166 177] ) (FuncDef name: "variable::ArrayStack::pop" body: (BraceGroup children: [ (If arms: [ (if_arm cond: [ (Sentence child: (DBracket expr: (BoolBinary op_id: BoolBinary_GlobDEqual left: {(${ VSub_Name VARIABLES_DEBUG)} right: {(1)} ) ) terminator: ) ] action: [ (Sentence child: (C {(stderr)} {(DQ ("variable::ArrayStack::pop ") (${ VSub_At "@"))}) terminator: ) ] spids: [-1 268] ) ] spids: [-1 281] ) (Assignment keyword: Assign_Declare pairs: [ (assign_pair lhs: (LhsName name:token) op: Equal rhs: {(DQ (${ VSub_Number 1))} spids: [286] ) ] spids: [284] ) (If arms: [ (if_arm cond: [ (Sentence child: (Pipeline children: [ (C {(variable) (Lit_Other ":") (Lit_Other ":") (type) (Lit_Other ":") (Lit_Other ":") (instanceOf) } {(DQ (${ VSub_Name token))} {(ArrayStack)} ) ] negated: True ) terminator: ) ] action: [ (C {(variable) (Lit_Other ":") (Lit_Other ":") (type)} {(DQ (${ VSub_Name token))}) (C {(stderr)} { (DQ ("Variable [") (${ VSub_Name token) ("] is not of type ArrayStack (actual type [") (${ VSub_Name RESULT) ("])") ) } ) (C {(exit)} {(1)}) ] spids: [-1 317] ) ] spids: [-1 352] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayList) (Lit_Other ":") (Lit_Other ":") (isEmpty_c) } {(DQ (${ VSub_Name token))} ) terminator: ) ] action: [(C {(stderr)} {(DQ ("Cannot pop from an empty stack"))}) (C {(exit)} {(1)})] spids: [-1 374] ) ] spids: [-1 389] ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayStack) (Lit_Other ":") (Lit_Other ":") (peek) } {(DQ (${ VSub_Name token))} ) terminator: ) (Assignment keyword: Assign_Declare pairs: [ (assign_pair lhs: (LhsName name:result) op: Equal rhs: {($ VSub_Name "$RESULT")} spids: [411] ) ] spids: [409] ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (type)} {($ VSub_Name "$token")}) terminator: ) (Assignment keyword: Assign_Declare pairs: [ (assign_pair lhs: (LhsName name:type) op: Equal rhs: {($ VSub_Name "$RESULT")} spids: [426] ) ] spids: [424] ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayList) (Lit_Other ":") (Lit_Other ":") (rest) } {($ VSub_Name "$token")} ) terminator: ) (Assignment keyword: Assign_Declare pairs: [ (assign_pair lhs: (LhsName name:value) op: Equal rhs: {($ VSub_Name "$RESULT")} spids: [444] ) ] spids: [442] ) (C {(variable) (Lit_Other ":") (Lit_Other ":") (set)} {(DQ ($ VSub_Name "$token"))} {(DQ ($ VSub_Name "$type"))} {(DQ ($ VSub_Name "$value"))} ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:RESULT) op: Equal rhs: {(DQ (${ VSub_Name result))} spids: [466] ) ] spids: [466] ) ] spids: [250] ) spids: [238 249] ) (FuncDef name: "variable::ArrayStack::peek" body: (BraceGroup children: [ (Assignment keyword: Assign_Declare pairs: [ (assign_pair lhs: (LhsName name:token) op: Equal rhs: {(DQ (${ VSub_Number 1))} spids: [502] ) ] spids: [500] ) (If arms: [ (if_arm cond: [ (Sentence child: (Pipeline children: [ (C {(variable) (Lit_Other ":") (Lit_Other ":") (type) (Lit_Other ":") (Lit_Other ":") (instanceOf) } {(DQ (${ VSub_Name token))} {(ArrayStack)} ) ] negated: True ) terminator: ) ] action: [ (C {(variable) (Lit_Other ":") (Lit_Other ":") (type)} {(DQ (${ VSub_Name token))}) (C {(stderr)} { (DQ ("Variable [") (${ VSub_Name token) ("] is not of type ArrayStack (actual type [") (${ VSub_Name RESULT) ("])") ) } ) (C {(exit)} {(1)}) ] spids: [-1 533] ) ] spids: [-1 568] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayList) (Lit_Other ":") (Lit_Other ":") (isEmpty_c) } {($ VSub_Name "$token")} ) terminator: ) ] action: [(C {(stderr)} {(DQ ("Cannot peek from an empty stack"))}) (C {(exit)} {(1)})] spids: [-1 586] ) ] spids: [-1 601] ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayList) (Lit_Other ":") (Lit_Other ":") (first) } {($ VSub_Name "$token")} ) terminator: ) (Assignment keyword: Assign_Declare pairs: [ (assign_pair lhs: (LhsName name:result) op: Equal rhs: {($ VSub_Name "$RESULT")} spids: [618] ) ] spids: [616] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:RESULT) op: Equal rhs: {(DQ (${ VSub_Name result))} spids: [622] ) ] spids: [622] ) ] spids: [497] ) spids: [485 496] ) (FuncDef name: "_variable::ArrayStack::peek_p" body: (BraceGroup children: [ (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayStack) (Lit_Other ":") (Lit_Other ":") (peek) } {(DQ (${ VSub_At "@"))} ) (C {(echo)} {(DQ ($ VSub_Name "$RESULT"))}) ] spids: [643] ) spids: [631 642] ) (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {($ VSub_Number "$0")} {(KW_Bang "!") (Lit_Other "=")} {($ VSub_Name "$BASH_SOURCE")} {(Lit_Other "]")} ) terminator: ) ] action: [(ControlFlow token:)] spids: [-1 687] ) ] spids: [-1 692] ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayStack) (Lit_Other ":") (Lit_Other ":") (new)} ) terminator: ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:vCode) op:Equal rhs:{(${ VSub_Name RESULT)} spids:[715])] spids: [715] ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (new)} {(String)} {(DQ (first))}) terminator: ) (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayStack) (Lit_Other ":") (Lit_Other ":") (push)} {(${ VSub_Name vCode)} {(${ VSub_Name RESULT)} ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (new)} {(String)} {(DQ (second))}) terminator: ) (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayStack) (Lit_Other ":") (Lit_Other ":") (push)} {(${ VSub_Name vCode)} {(${ VSub_Name RESULT)} ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (new)} {(String)} {(DQ (third))}) terminator: ) (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayStack) (Lit_Other ":") (Lit_Other ":") (push)} {(${ VSub_Name vCode)} {(${ VSub_Name RESULT)} ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayStack) (Lit_Other ":") (Lit_Other ":") (peek)} {($ VSub_Name "$vCode")} ) terminator: ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (value)} {(DQ (${ VSub_Name RESULT))}) terminator: ) (C {(assert) (Lit_Other ":") (Lit_Other ":") (equals)} {(DQ (third))} {(DQ ($ VSub_Name "$RESULT"))} {(DQ ("stack::peek first"))} ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayStack) (Lit_Other ":") (Lit_Other ":") (pop)} {($ VSub_Name "$vCode")} ) terminator: ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (value)} {(DQ (${ VSub_Name RESULT))}) terminator: ) (C {(assert) (Lit_Other ":") (Lit_Other ":") (equals)} {(DQ (third))} {(DQ ($ VSub_Name "$RESULT"))} {(DQ ("stack::pop first"))} ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayStack) (Lit_Other ":") (Lit_Other ":") (peek)} {($ VSub_Name "$vCode")} ) terminator: ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (value)} {(DQ (${ VSub_Name RESULT))}) terminator: ) (C {(assert) (Lit_Other ":") (Lit_Other ":") (equals)} {(DQ (second))} {(DQ ($ VSub_Name "$RESULT"))} {(DQ ("stack::peek second"))} ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayStack) (Lit_Other ":") (Lit_Other ":") (pop)} {($ VSub_Name "$vCode")} ) terminator: ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (value)} {(DQ (${ VSub_Name RESULT))}) terminator: ) (C {(assert) (Lit_Other ":") (Lit_Other ":") (equals)} {(DQ (second))} {(DQ (${ VSub_Name RESULT))} {(DQ ("queue::dequeue second"))} ) (C {(assert) (Lit_Other ":") (Lit_Other ":") (report)}) (If arms: [ (if_arm cond: [ (Sentence child: (AndOr children: [ (C {(Lit_Other "[")} { (BracedVarSub token: suffix_op: (StringUnary op_id:VTest_Plus arg_word:{(isset)}) spids: [996 1000] ) } {(Lit_Other "]")} ) (C {(Lit_Other "[")} {(DQ ($ VSub_Number "$1"))} {(Lit_Other "=") (Lit_Other "=")} {(DQ (debug))} {(Lit_Other "]")} ) ] op_id: Op_DAmp ) terminator: ) ] action: [(C {(variable) (Lit_Other ":") (Lit_Other ":") (printMetadata)})] spids: [-1 1022] ) ] spids: [-1 1031] ) ] )