1
      2
      3
      4
      5
      6
      7
      8
      9
     10
     11
     12
     13
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
#ifndef STACK_HPP
#define STACK_HPP

#include <cassert>
#include "types.hpp"

/*
   All functions have exactly one parameter. Hence, the
   function call stack consists of one value per call.
   For lexical scoped variables, the parser computes
   the relative offset of a variable.

   Examples: (lambda x x)                        x has the offset 0
             (lambda x (lambda y x))             x has the offset 1
         (lambda x (lambda y (lambda x x)))  x has the offset 0
         (lambda x (lambda y (lambda z x)))  x has the offset 2

   Each function value (see value.hpp) consists of a
   pointer to a function object and a stack pointer
   that provides all variables required by the closure.

   The []-operator of Stack delivers the value with offset i for [i].
*/

namespace LambdaCalc {

   class Stack {
      public:
     StaStackPtrPtr nexValuePtrPtr value) :
        nenextext), valvaluelue), lnextexnextexlengt;len+11) {
     }
  ValuePtrbsp; ValuePtr operator[](unsigned int index) const {
        asseindexdexlent; len);
        iindexan> (index == 0retuvaluepan> value;
        returnnextp;(*nexindexdex-1];
     }
      private:
  ValuePtrbsp; ValuePtr value;
  StackPtrbsp; StackPtr next;
     unsigned int len;
   };

// namespace LambdaCalc

#endif