Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

conteval.hpp

Go to the documentation of this file.
00001 // +-------------------------------------------------------------------------+
00002 // |               I__n__t__e__L__i__b           0.6.10 development          |
00003 // | Copyright (c) Andrey Vikt. Stolyarov <crocodil_AT_croco.net> 2000-2007. |
00004 // |                                                                         |
00005 // | This is free software. The library part is available under              |
00006 // |                               GNU LESSER GENERAL PUBLIC LICENSE v.2.1.  |
00007 // | GNU LGPL v2.1 is found in docs/gnu_gpl2.txt,  or at  http://www.gnu.org |
00008 // |     Please see also docs/readme.txt and visit http://www.intelib.org    |
00009 // |                                                                         |
00010 // | !!! THERE IS NO WARRANTY OF ANY KIND, NEITHER EXPRESSED NOR IMPLIED !!! |
00011 // +-------------------------------------------------------------------------+
00012 
00013 
00014 
00015 
00024 #ifndef INTELIB_CONTEVAL_HPP_SENTRY
00025 #define INTELIB_CONTEVAL_HPP_SENTRY
00026 
00027 #include "../sexpress/sexpress.hpp"
00028 #include "../sexpress/iexcept.hpp"
00029 #include "../sexpress/gensref.hpp"
00030 
00031 #ifndef INTELIB_CONTINUATION_KEEPS_STACK_INFO
00032 
00033 #define INTELIB_CONTINUATION_KEEPS_STACK_INFO 1
00034 #endif
00035 
00036 
00038 
00046 class SExpressionSetfAgent : public SExpression {
00047 public:
00048     static IntelibTypeId TypeId;
00049 
00050     SExpressionSetfAgent() : SExpression(TypeId) {}
00051     virtual void Setf(const SReference &val) = 0;
00052 
00053 protected:
00054     virtual ~SExpressionSetfAgent() {}
00055 };
00056 
00057 
00059 
00115 class IntelibContinuation {
00116     SReference *result_stack;
00117     int result_stack_size;
00118     int result_stack_pointer;
00119 
00120     struct TodoItem {
00121         int opcode;
00122         SReference param;
00123 #if INTELIB_CONTINUATION_KEEPS_STACK_INFO == 1
00124         SReference stack_info;
00125 #endif
00126     };
00127     TodoItem *todo_stack;
00128     int todo_stack_size;
00129     int todo_stack_pointer;
00130 
00131     SReference current_context;
00132 
00133 protected:
00134     SReference *PTheFalseValue;
00135 
00136 public:
00138     IntelibContinuation();
00140     IntelibContinuation(const IntelibContinuation &other, bool ignored);
00142     virtual ~IntelibContinuation();
00143 
00145 
00150     virtual void JustEvaluate(const SReference& expr) = 0;
00152 
00156     virtual void CustomCommand(int opcode, const SReference& param);
00157 
00159 
00165     bool Step();
00166 
00168     int GetMark() const { return todo_stack_pointer; }
00170 
00175     bool Ready(int mark = 0) const { return todo_stack_pointer == mark; }
00177     SReference Get();
00178 
00180 
00184     void ReplaceContinuation(const IntelibContinuation &other);
00185 private:
00187     IntelibContinuation(const IntelibContinuation &other) {}
00189     void operator=(const IntelibContinuation &other) {}
00190 
00191 public:
00193 
00201     enum Instructions {
00203         just_evaluate =       -1,
00205         evaluate_prepared =   -2,
00207         evaluate_progn =      -3,
00209         quote_parameter =     -4,
00211         drop_result =         -5,
00213         return_unspecified =  -6,
00215         end_of_clauses =      -7,
00217 
00223         cond_clause =         -8,
00225 
00230         bail_on_false =       -9,
00232 
00235         set_context =        -10,
00237 
00239         assign_to =          -11, 
00241 
00244         assign_location =    -12,
00246 
00247         generic_iteration =  -13,
00249         iteration_callback = -14,
00251         max_command = -14
00252     };
00253 
00255 
00259     void PushTodo(int opcode, const SReference& param);
00261 
00264     void PushTodo(int opcode); // for the 'no parameter' case
00266 
00268     bool PopTodo(int &opcode, SReference& param);
00270     void PushResult(const SReference& param);
00272     bool PopResult(SReference& param);
00273 
00275     void RegularReturn(const SReference &ref);
00277     void ReferenceReturn(SReference &ref, const SReference &superstruct);
00279     void AgentReturn(const SReference &val, const SExpressionSetfAgent *ag);
00281     void TailReturn(const SReference &ref);
00283     void ReturnUnspecified();
00284 
00286     SReference GetContext() const { return current_context; }
00288     void SetContext(const SReference &cont) { current_context = cont; }
00289 
00290 
00291 private:
00292     static bool pending_interruption;
00293     static bool interruptions_suspended;
00294 public:
00296     static void InterruptEvaluator() { pending_interruption = true; }
00298     static void RemoveInterruption() { pending_interruption = false; }
00300     static void SuspendInterruptions() { interruptions_suspended = true; }
00302     static void ResumeInterruptions() { interruptions_suspended = false; }
00303 
00305     class Interruption {};
00306 
00307 private:
00308     bool IsTrue(const SReference& expr)
00309         { return expr.GetPtr() != PTheFalseValue->GetPtr(); }
00310     void EvaluatePrepared(const SReference& expr);
00311     void FunctionCall(int paramscount);
00312     void PlacePrognToStack(const SReference& expr);
00313     void EvaluateCondClause(const SReference& expr);
00314     void BailOnFalse();
00315     void AssignLocation();
00316     void GenericIteration(const SReference& expr);
00317     void IterationCallback(const SReference& expr);
00318 
00319     bool AcceptsLocation() const;
00320 
00321     void DoFunctionCall(const SReference &save_fun,
00322                         int paramscount,
00323                         const SReference *paramsvector);
00324 
00325 
00326 protected:
00328 
00334     void PlaceFormToStack(const SExpressionCons *form, int len);
00335 };
00336 
00338 
00358 class SExpressionGenericIterator : public SExpression {
00359 public:
00360     SExpressionGenericIterator(IntelibTypeId &tid)
00361         : SExpression(tid) {}
00362     ~SExpressionGenericIterator() {}
00363 
00365     virtual bool NeedAnotherIteration(IntelibContinuation& lf) const = 0;
00367     virtual void ScheduleIteration(IntelibContinuation &lf) = 0;
00369     virtual void CollectResultOfIteration(IntelibContinuation &lf) = 0;
00371     virtual void ReturnFinalValue(IntelibContinuation &lf) = 0;
00372 };
00373 
00375 class IntelibX_continuation_unknown_operation : public IntelibX {
00376 public:
00377     IntelibX_continuation_unknown_operation(SReference a_param);
00378 };
00379 
00381 class IntelibX_not_a_function : public IntelibX {
00382 public:
00383     IntelibX_not_a_function(SReference a_param);
00384 };
00385 
00386 
00387 #endif

Generated on Tue Dec 18 00:39:43 2007 for InteLib by  doxygen 1.4.1