"Fossies" - the Fresh Open Source Software Archive

Member "asymptote-2.60/stack.h" (6 Nov 2019, 3328 Bytes) of package /linux/misc/asymptote-2.60.src.tgz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "stack.h" see the Fossies "Dox" file reference documentation.

    1 /*****
    2  * stack.h
    3  * Andy Hammerlindl 2002/06/27
    4  * 
    5  * The general stack machine  used to run compiled camp code.
    6  *****/
    7 
    8 #ifndef STACK_H
    9 #define STACK_H
   10 
   11 #include <iostream>
   12 
   13 #include "errormsg.h"
   14 #include "vm.h"
   15 #include "item.h"
   16 #include "absyn.h"
   17 
   18 namespace vm {
   19 
   20 struct func;
   21 class program;
   22 struct lambda;
   23 class importInitMap;
   24 
   25 struct bpinfo : public gc {
   26   fileinfo f;
   27   absyntax::runnable *r;
   28   
   29   bpinfo(const string& filename, size_t lineNum,
   30          absyntax::runnable *r=NULL) :
   31     f(fileinfo(filename,lineNum)), r(r) {}
   32 };
   33   
   34 inline bool operator == (const bpinfo& a, const bpinfo& b)
   35 {
   36   return a.f == b.f;
   37 }
   38   
   39 extern mem::list<bpinfo> bplist;
   40   
   41 class runnable;
   42   
   43 extern bool indebugger;  
   44   
   45 class stack {
   46 public:
   47   typedef frame* vars_t;
   48 
   49   struct importInitMap {
   50     virtual ~importInitMap() {}
   51     virtual lambda *operator[](string) = 0;
   52   };
   53 
   54 private:
   55   // stack for operands
   56   typedef mem::vector<item> stack_t;
   57   stack_t theStack;
   58 
   59   void draw(ostream& out);
   60 
   61   // The initializer functions for imports, indexed by name.
   62   importInitMap *initMap;
   63 
   64   // The stack stores a map of initialized imported modules by name, so that
   65   // each module is initialized only once and each import refers to the same
   66   // instance.
   67   typedef mem::map<CONST string,frame *> importInstanceMap;
   68   importInstanceMap instMap;
   69   
   70   // One can associate an environment to embedded code while running.
   71   trans::coenv *e;
   72 
   73   // Debugger variables:
   74   char debugOp;
   75   position lastPos, breakPos;
   76   bool newline;
   77   
   78   // Move arguments from stack to frame.
   79   void marshall(size_t args, stack::vars_t vars);
   80 
   81 public:
   82   stack() : e(0), debugOp(0), lastPos(nullPos),
   83             breakPos(nullPos), newline(false) {};
   84   
   85   virtual ~stack() {};
   86 
   87   void setInitMap(importInitMap *i) {
   88     initMap=i;
   89   }
   90 
   91   void setEnvironment(trans::coenv *e) {
   92     this->e=e;
   93   }
   94 
   95   trans::coenv *getEnvironment() {
   96     return e;
   97   }
   98 
   99   // Runs a lambda.  If vars is non-null, it is used to store the variables of
  100   // the lambda.  Otherwise, the method allocates a closure only if needed.
  101   void runWithOrWithoutClosure(lambda *l, vars_t vars, vars_t parent);
  102 
  103   // Executes a function on top of the stack.
  104   void run(func *f);
  105 
  106   void breakpoint(absyntax::runnable *r=NULL);
  107   void debug();
  108   
  109   // Put an import (indexed by name) on top of the stack, initializing it if
  110   // necessary.
  111   void load(string index);
  112 
  113   // These are so that built-in functions can easily manipulate the stack
  114   void push(item next) {
  115     theStack.push_back(next);
  116   }
  117   template <typename T>
  118   void push(T next) {
  119     push((item)next);
  120   }
  121   item top() {
  122     return theStack.back();
  123   }
  124   item pop() {
  125     item ret = theStack.back();
  126     theStack.pop_back();
  127     return ret;
  128   }
  129   template <typename T>
  130   T pop()
  131   {
  132     return get<T>(pop());
  133   }
  134 };
  135 
  136 inline item pop(stack* s)
  137 {
  138   return s->pop();
  139 }
  140 
  141 template <typename T>
  142 inline T pop(stack* s)
  143 {
  144   return get<T>(pop(s));
  145 }
  146   
  147 template <typename T>
  148 inline T pop(stack* s, T defval)
  149 {
  150   item it=pop(s);
  151   return isdefault(it) ? defval : get<T>(it);
  152 }
  153   
  154 class interactiveStack : public stack {
  155   vars_t globals;
  156   size_t globals_size;
  157 public:
  158   interactiveStack();
  159     
  160   // Run a codelet, a small piece of code that uses globals as its frame.
  161   void run(lambda *codelet);
  162 };
  163 
  164 } // namespace vm
  165 
  166 #endif // STACK_H
  167