Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

stack primitive


SourceForge.net Logo     CTTL on    
    SourceForge    
    Download    
    Latest    
    Documentation    
    Index    
    Library    
    News    
    CVS    
    Repository    
   Other    
   Links    

Due to enormous popularity of stack structure in parsing world, a special type of lambda scalar, named stack primitive, is supported by CTTL lambda library. At present time, implementation of the stack primitive is limited to objects of std::stack type only:

void f()
{
    lambda< int >::stack Stack;          // instantiates std::stack< int >
    assert( Stack.size() == 0 );         // no elements exist so far
    
    Stack.push( 3 );                     // push new element
    assert( Stack.top() == 3 );          // verify result
    
    Stack.top() = 2;                     // write access to top element
    assert( Stack.top() == 2 );          // verify result
}


overloaded stack operators


CTTL lambda implementation includes overloaded operators specific to stack primitives. The following two fragments of code achieve exact same results while working with a stack. Left sample instantiates stack primitive named Stack and handles it by means of overloaded operators. Right-hand side sample uses primitive named Scalar, which encapsulates instance of a stack and employs member functions of std::stack adaptor via predefined closures:

void f()
{
    lambda< int >::scalar Variable;
    lambda< int >::stack Stack;
    (
        Stack = 4,
        Variable = *Stack,
        *Stack = 5,
        Stack--,
        Variable = +Stack
    ).evaluate();
}
void f()
{
    lambda< int >::scalar Variable;
    lambda< std::stack< int > >::scalar Scalar;
    (
        alias::push( &Scalar, 4 ),
        Variable = alias::top( Scalar ),
        alias::top( &Scalar ) = 5,
        alias::pop( &Scalar ),
        Variable = alias::size( Scalar )
    ).evaluate();
}

If S is an instance of lambda stack primitive, and x is a scalar encapsulating an object that could be stored on the stack,

    lambda< int >::stack S;
    lambda< int >::scalar x;

then the following lambda expression formats become available:

Table of overloaded stack operators
Expression Description
  S = x
Overloaded assignment operator pushes right-hand-side value on the stack and returns copy of x as result of the assignment.

If right-hand-side of assignment is lambda composite, for example,
  S = (x^y^z)
then multiple elements are pushed on the stack. The content of lambda composite enters the stack in left-to-right order. Value of the rightmost terminal node (such as z in x^y^z composite) is pushed last. Copy of the last pushed value becomes result of the assignment.

  x = *S
Overloaded dereference operator returns copy of the value on top of the stack and has same meaning as x = S.top().
Precondition: stack must not be empty.

  x = S
Returns copy of top stack element. Same as x = *S.
Precondition: stack must not be empty.

  *S = x
Dereference operator provides mutable access to the top element of stack. Result of expression *S = x is a copy of x.
Precondition: stack must not be empty.

  --S
Overloaded prefix increment operator pops top element from the stack.
Expression --S returns stack size after pop.
Result of --S can be dereferenced:
  x = *--S // pop element from stack and assign top value to x
  *--S = x // pop element from stack and assign x to the top element
Precondition: stack must not be empty.

  S--
Overloaded postfix increment operator pops top element from the stack.
Expression S-- returns stack size before pop.
Result of S-- can be dereferenced:
  x = *S-- // assign top stack value to x, then pop top element from stack
  *S-- = x // not too practical: assign x to stack top and instantly pop it
Precondition: stack must not be empty.

  +S
Overloaded unary plus operator returns size of the stack.



Copyright © 1997-2006 Igor Kholodov mailto:cttl@users.sourceforge.net.

Permission to copy, use, modify, sell and distribute this document is granted provided this copyright notice appears in all copies. This document is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose.


Generated on Thu Nov 2 17:48:53 2006 for CTTL Lambda Expression by  doxygen 1.3.9.1