#!/bin/bash # If this file has already been sourced, just return test $(VARIABLES_QUEUE_SH+true)TODO && return declare -g VARIABLES_QUEUE_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 variable::type::define ArrayQueue ArrayList # == QUEUE == # # First In / First Out # # Queue commands act on a list data structure # proc variable::ArrayQueue::new { variable::new ArrayQueue $(@) } # # Adds an item to the queue # proc variable::ArrayQueue::enqueue { if [[ ${VARIABLES_DEBUG} == 1 ]] { stderr "variable::ArrayQueue::enqueue $(@)" ; }TODO declare token = $(1) if ! variable::type::instanceOf $(token) ArrayQueue { stderr "Variable [$(token)] is not of type ArrayQueue (actual type [$(RESULT)])" exit 1 } variable::ArrayList::append $(@) } # # Removes and returns the oldest item added to the queue # proc variable::ArrayQueue::dequeue { if [[ ${VARIABLES_DEBUG} == 1 ]] { stderr "variable::ArrayList::isEmpty_c $(@)" ; }TODO declare token = $(1) if ! variable::type::instanceOf $(token) ArrayQueue { stderr "Variable [$(token)] is not of type ArrayQueue (actual type [$(RESULT)])" exit 1 } if variable::ArrayList::isEmpty_c $(token) { stderr "Cannot dequeue from an empty queue" exit 1 } variable::ArrayQueue::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 oldest item added to the queue (does note remove it) # proc variable::ArrayQueue::peekTODO { declare token = $(1) if ! variable::type::instanceOf $(token) ArrayQueue { stderr "Variable [$(token)] is not of type ArrayQueue (actual type [$(RESULT)])" exit 1 } if variable::ArrayList::isEmpty_c $token { stderr "Cannot peek from an empty queue" exit 1 } # stderr "peeking at list [$(variable::value_p $token)] / first=$(variable::ArrayList::first_p $token)" variable::ArrayList::first $tokenTODO ; declare result = $RESULT global RESULT := $(result) } proc _variable::ArrayQueue::peek_p { variable::ArrayQueue::peek $(@) echo $RESULT } # ====================================================== if test $0 != $BASH_SOURCE { return } # # QUEUE tests # variable::ArrayQueue::new ; global vCode := $(RESULT) variable::new String "first" ; variable::ArrayQueue::enqueue $(vCode) $(RESULT) variable::new String "second" ; variable::ArrayQueue::enqueue $(vCode) $(RESULT) variable::new String "third" ; variable::ArrayQueue::enqueue $(vCode) $(RESULT) variable::ArrayQueue::peek $vCode ; variable::value $(RESULT) ; \ assert::equals "first" $RESULT "queue:peek first" variable::ArrayQueue::dequeue $vCode ; variable::value $(RESULT) ; \ assert::equals "first" $RESULT "queue::dequeue first" variable::ArrayQueue::peek $vCode ; variable::value $(RESULT) ; \ assert::equals "second" $RESULT "queue:peek second" variable::ArrayQueue::dequeue $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:{(true)}) spids: [9 13] ) } {(Lit_Other "]")} ) (ControlFlow token:) ] op_id: Op_DAmp ) (Assignment keyword: Assign_Declare flags: ["'-g'"] pairs: [(assign_pair lhs:(LhsName name:VARIABLES_QUEUE_SH) op:Equal rhs:{(true)} spids:[25])] spids: [21] ) (C {(.)} { (BracedVarSub token: suffix_op: (StringUnary op_id:VOp1_Percent arg_word:{(Lit_Slash /) ("*")}) spids: [31 36] ) (/common.sh) } ) (C {(.)} { (BracedVarSub token: suffix_op: (StringUnary op_id:VOp1_Percent arg_word:{(Lit_Slash /) ("*")}) spids: [41 46] ) (/logger.sh) } ) (C {(.)} { (BracedVarSub token: suffix_op: (StringUnary op_id:VOp1_Percent arg_word:{(Lit_Slash /) ("*")}) spids: [51 56] ) (/variables.sh) } ) (C {(.)} { (BracedVarSub token: suffix_op: (StringUnary op_id:VOp1_Percent arg_word:{(Lit_Slash /) ("*")}) spids: [61 66] ) (/variables.atom.sh) } ) (C {(.)} { (BracedVarSub token: suffix_op: (StringUnary op_id:VOp1_Percent arg_word:{(Lit_Slash /) ("*")}) spids: [71 76] ) (/variables.arraylist.sh) } ) (C {(variable) (Lit_Other ":") (Lit_Other ":") (type) (Lit_Other ":") (Lit_Other ":") (define)} {(ArrayQueue)} {(ArrayList)} ) (FuncDef name: "variable::ArrayQueue::new" body: (BraceGroup children: [ (C {(variable) (Lit_Other ":") (Lit_Other ":") (new)} {(ArrayQueue)} {(DQ (${ VSub_At "@"))}) ] spids: [124] ) spids: [112 123] ) (FuncDef name: "variable::ArrayQueue::enqueue" 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::ArrayQueue::enqueue ") (${ VSub_At "@"))}) terminator: ) ] spids: [-1 182] ) ] spids: [-1 195] ) (Assignment keyword: Assign_Declare pairs: [ (assign_pair lhs: (LhsName name:token) op: Equal rhs: {(DQ (${ VSub_Number 1))} spids: [201] ) ] spids: [199] ) (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))} {(ArrayQueue)} ) ] negated: True ) terminator: ) ] action: [ (C {(stderr)} { (DQ ("Variable [") (${ VSub_Name token) ("] is not of type ArrayQueue (actual type [") (${ VSub_Name RESULT) ("])") ) } ) (C {(exit)} {(1)}) ] spids: [-1 231] ) ] spids: [-1 254] ) (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayList) (Lit_Other ":") (Lit_Other ":") (append) } {(DQ (${ VSub_At "@"))} ) ] spids: [164] ) spids: [152 163] ) (FuncDef name: "variable::ArrayQueue::dequeue" 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::ArrayList::isEmpty_c ") (${ VSub_At "@"))}) terminator: ) ] spids: [-1 314] ) ] spids: [-1 327] ) (Assignment keyword: Assign_Declare pairs: [ (assign_pair lhs: (LhsName name:token) op: Equal rhs: {(DQ (${ VSub_Number 1))} spids: [332] ) ] spids: [330] ) (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))} {(ArrayQueue)} ) ] negated: True ) terminator: ) ] action: [ (C {(stderr)} { (DQ ("Variable [") (${ VSub_Name token) ("] is not of type ArrayQueue (actual type [") (${ VSub_Name RESULT) ("])") ) } ) (C {(exit)} {(1)}) ] spids: [-1 363] ) ] spids: [-1 386] ) (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 dequeue from an empty queue"))}) (C {(exit)} {(1)}) ] spids: [-1 408] ) ] spids: [-1 423] ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayQueue) (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: [444] ) ] spids: [442] ) (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: [458] ) ] spids: [456] ) (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: [475] ) ] spids: [473] ) (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: [497] ) ] spids: [497] ) ] spids: [296] ) spids: [284 295] ) (FuncDef name: "variable::ArrayQueue::peek" body: (BraceGroup children: [ (Assignment keyword: Assign_Declare pairs: [ (assign_pair lhs: (LhsName name:token) op: Equal rhs: {(DQ (${ VSub_Number 1))} spids: [533] ) ] spids: [531] ) (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))} {(ArrayQueue)} ) ] negated: True ) terminator: ) ] action: [ (C {(stderr)} { (DQ ("Variable [") (${ VSub_Name token) ("] is not of type ArrayQueue (actual type [") (${ VSub_Name RESULT) ("])") ) } ) (C {(exit)} {(1)}) ] spids: [-1 564] ) ] spids: [-1 587] ) (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 queue"))}) (C {(exit)} {(1)})] spids: [-1 605] ) ] spids: [-1 620] ) (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: [640] ) ] spids: [638] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:RESULT) op: Equal rhs: {(DQ (${ VSub_Name result))} spids: [644] ) ] spids: [644] ) ] spids: [528] ) spids: [516 527] ) (FuncDef name: "_variable::ArrayQueue::peek_p" body: (BraceGroup children: [ (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayQueue) (Lit_Other ":") (Lit_Other ":") (peek) } {(DQ (${ VSub_At "@"))} ) (C {(echo)} {(DQ ($ VSub_Name "$RESULT"))}) ] spids: [665] ) spids: [653 664] ) (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 711] ) ] spids: [-1 716] ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayQueue) (Lit_Other ":") (Lit_Other ":") (new)} ) terminator: ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:vCode) op:Equal rhs:{(${ VSub_Name RESULT)} spids:[738])] spids: [738] ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (new)} {(String)} {(DQ (first))}) terminator: ) (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayQueue) (Lit_Other ":") (Lit_Other ":") (enqueue)} {(${ 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 ":") (ArrayQueue) (Lit_Other ":") (Lit_Other ":") (enqueue)} {(${ 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 ":") (ArrayQueue) (Lit_Other ":") (Lit_Other ":") (enqueue)} {(${ VSub_Name vCode)} {(${ VSub_Name RESULT)} ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayQueue) (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 (first))} {(DQ ($ VSub_Name "$RESULT"))} {(DQ ("queue:peek first"))} ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayQueue) (Lit_Other ":") (Lit_Other ":") (dequeue) } {($ 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 (first))} {(DQ ($ VSub_Name "$RESULT"))} {(DQ ("queue::dequeue first"))} ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayQueue) (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 ("queue:peek second"))} ) (Sentence child: (C {(variable) (Lit_Other ":") (Lit_Other ":") (ArrayQueue) (Lit_Other ":") (Lit_Other ":") (dequeue) } {($ 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: [1018 1022] ) } {(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 1044] ) ] spids: [-1 1053] ) ] )