<<< Scalar primitive     Lambda Home     Scalar interface >>>

Common Text Transformation Library http://cttl.sourceforge.net/

Stack primitive


  1. Stack primitive typedefs
  2. stack_typedefs.cpp
  3. Inline instantiation of stack primitives
  4. make_stack_demo.cpp
  5. Overloaded stack operators
  6. lambda_stack_primitive.cpp
  7. Stack operator semantics
  8. lambda_stack_decrement.cpp


Stack primitive typedefs


Keywords: stack_typedefs.cpp, type conversion translator, make_reference


Inline instantiation of stack primitives



Overloaded stack operators


  • A program using std::stack directly:

  • Lambda stack primitive handled by the overloaded operators:

  • Similar manipulation via std::stack member functions accessed by closures:

  • 
    #include <stack> 
    
    
    int main(/*int argc, char* argv[]*/)
    {
        int Variable;
        std::stack< int > Stack;
        size_t StackSize;
    
        Stack.push( 4 );
        Variable = Stack.top();
        Stack.top() = 5;
        Stack.pop();
        StackSize = Stack.size();
    
    
        return 0;
    }
    
    
  • 
    #include "cttl/cttl.h" 
    #include "lambda/lambda.h"
    using namespace cttl;
    int main(/*int argc, char* argv[]*/)
    {
        lambda< int >::scalar Variable;
        lambda< int >::stack Stack;
        lambda< size_t >::scalar StackSize;
        (
            Stack = 4,
            Variable = *Stack,
            *Stack = 5,
            Stack--,
            StackSize = +Stack
        ).evaluate();
    
        return 0;
    }
    
    
  • 
    #include "cttl/cttl.h" 
    #include "lambda/lambda.h"
    using namespace cttl;
    int main(/*int argc, char* argv[]*/)
    {
        lambda< int >::scalar Variable;
        lambda< std::stack< int > >::scalar Scalar;
        lambda< size_t >::scalar StackSize;
        (
            alias::push( &Scalar, 4 ),
            Variable = alias::top( Scalar ),
            alias::top( &Scalar ) = 5,
            alias::pop( &Scalar ),
            StackSize = alias::size( Scalar )
        ).evaluate();
    
        return 0;
    }
    
    

Stack operator semantics


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

 S = (x^y^z)
 
If right-hand-side expression is lambda composite, 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) is pushed last. Copy of the last pushed value becomes the result of the assignment.

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

   x = S
 
Returns copy of the 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 the stack. Result of expression
   *S = x
 
is a copy of x.

Precondition: stack must not be empty.

   --S
 
Overloaded prefix decrement operator-- pops top element from the stack.
Expression
   --S
 
returns the stack size after pop.

Result of --S can be dereferenced:
   x = *--S // 1. pops element from the stack;
            // 2. assigns the top value to x.
 
   *--S = x // 1. pops element from the stack;
            // 2. assigns x to the top element.
 
Precondition: stack must not be empty.

   S--
 
Overloaded postfix decrement operator-- pops top element from the stack.
Expression
   S--
 
returns the stack size before pop.

Result of S-- can be dereferenced:
   x = *S-- // 1. assigns top stack value to x;
            // 2. pops top element from stack.
 
   *S-- = x // not very practical:
            // 1. assigns x to the stack top;
            // 2. pops the top element.
 
Precondition: stack must not be empty.

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

Keywords: lambda_stack_decrement.cpp, overloaded prefix postfix decrement operators, stack, const_scalar


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

Permission to copy 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.


 

 

<<< Scalar primitive     Lambda Home     Scalar interface >>>