import IPushDownAutomaton from "./pda.spec" ; import { TTypeProto } from "./_.spec" ; type TStateNode < T > = { state : T ; prevNode ? : TStateNode < T > ; } ; export class PushDownAutomaton < T > implements IPushDownAutomaton < T > { protected currentState ? : T ; protected statesTail ? : TStateNode < T > ; get state ( ) : T | undefined { return this . currentState ; } clear ( ) { this . currentState = undefined ; this . statesTail = undefined ; } pop ( ) { if ( ! this . statesTail ) return ; const oldTail = this . statesTail ; this . statesTail = this . statesTail . prevNode ; this . currentState = this . statesTail ?. state ; return oldTail . state ; } push < P extends TTypeProto < T > > ( State ) { this . currentState = new State ( ) ; this . statesTail = { prevNode : this . statesTail , state : this . currentState , } ; } }