Feature: Allow program to result in a Drawable.
[shapes.git] / source / continuations.h
blobda6d282cce95ca40af5186fc1f2273c01560843c
1 /* This file is part of Shapes.
3 * Shapes is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * any later version.
8 * Shapes is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with Shapes. If not, see <http://www.gnu.org/licenses/>.
16 * Copyright 2008 Henrik Tidefelt
19 #ifndef continuations_h
20 #define continuations_h
22 #include "ast.h"
24 namespace Shapes
26 namespace Helpers
29 template< class T >
30 RefCountPtr< T >
31 down_cast_ContinuationArgument( const RefCountPtr< const Lang::Value > & val, const Kernel::Continuation * locCont, bool voidIsNull = false )
33 RefCountPtr< T > res = val.down_cast< T >( );
34 if( res == NullPtr< T >( ) )
36 if( ! voidIsNull ||
37 dynamic_cast< const Lang::Void * >( val.getPtr( ) ) == 0 )
39 throw Exceptions::ContinuationTypeMismatch( locCont, val->getTypeName( ), T::staticTypeName( ) );
42 return res;
47 namespace Kernel
50 class ExitContinuation : public Kernel::Continuation
52 bool * done_;
53 public:
54 ExitContinuation( bool * done, const Ast::SourceLocation & traceLoc );
55 ~ExitContinuation( );
56 virtual void takeHandle( Kernel::VariableHandle val, Kernel::EvalState * evalState, bool dummy ) const;
57 virtual void backTrace( std::list< Kernel::Continuation::BackTraceElem > * trace ) const;
58 virtual void gcMark( Kernel::GCMarkedSet & marked );
61 class IfContinuation : public Kernel::Continuation
63 Kernel::VariableHandle consequence_;
64 Kernel::VariableHandle alternative_;
65 Kernel::ContRef cont_;
66 public:
67 IfContinuation( const Kernel::VariableHandle & consequence, const Kernel::VariableHandle & alternative, const Kernel::ContRef & cont, const Ast::SourceLocation & traceLoc );
68 ~IfContinuation( );
69 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const;
70 virtual void backTrace( std::list< Kernel::Continuation::BackTraceElem > * trace ) const;
71 virtual void gcMark( Kernel::GCMarkedSet & marked );
74 /* DefineVariableContinuation is used by the expression DefineVariable.
76 class DefineVariableContinuation : public Kernel::Continuation
78 Kernel::PassedEnv env_;
79 size_t * pos_;
80 Kernel::ContRef cont_;
81 public:
82 DefineVariableContinuation( const Kernel::PassedEnv & _env, size_t * _pos, const Kernel::ContRef & _cont, const Ast::SourceLocation & _traceLoc );
83 ~DefineVariableContinuation( );
84 virtual void takeHandle( Kernel::VariableHandle val, Kernel::EvalState * evalState, bool dummy ) const;
85 virtual void backTrace( std::list< Kernel::Continuation::BackTraceElem > * trace ) const;
86 virtual void gcMark( Kernel::GCMarkedSet & marked );
89 /* IntroduceStateContinuation is used by the expression IntroduceState.
91 class IntroduceStateContinuation : public Kernel::Continuation
93 Kernel::PassedEnv env_;
94 size_t * pos_;
95 Kernel::ContRef cont_;
96 public:
97 IntroduceStateContinuation( const Kernel::PassedEnv & _env, size_t * _pos, const Kernel::ContRef & _cont, const Ast::SourceLocation & _traceLoc );
98 ~IntroduceStateContinuation( );
99 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const;
100 virtual void backTrace( std::list< Kernel::Continuation::BackTraceElem > * trace ) const;
101 virtual void gcMark( Kernel::GCMarkedSet & marked );
104 /* StoreValueContinuation will store the returned object at the specified address, and then pass it on
105 * to the next continuation.
106 * Note that it is the responsibility of someone else to make sure that *res still exists when the continuation is invoked.
107 * Generally, StoreVariableContinuation shall be preferred since it has no such problem.
109 class StoreValueContinuation : public Kernel::Continuation
111 Kernel::ValueRef * res_;
112 Kernel::ContRef cont_;
113 public:
114 StoreValueContinuation( Kernel::ValueRef * res, const Kernel::ContRef & cont, const Ast::SourceLocation & traceLoc );
115 ~StoreValueContinuation( );
116 virtual void takeHandle( Kernel::VariableHandle val, Kernel::EvalState * evalState, bool dummy ) const;
117 virtual void backTrace( std::list< Kernel::Continuation::BackTraceElem > * trace ) const;
118 virtual void gcMark( Kernel::GCMarkedSet & marked );
121 class StoreVariableContinuation : public Kernel::Continuation
123 Kernel::VariableHandle dst_;
124 Kernel::ContRef cont_;
125 public:
126 StoreVariableContinuation( const Kernel::VariableHandle & res, const Kernel::ContRef & cont, const Ast::SourceLocation & traceLoc );
127 ~StoreVariableContinuation( );
128 virtual void takeHandle( Kernel::VariableHandle val, Kernel::EvalState * evalState, bool dummy ) const;
129 virtual void backTrace( std::list< Kernel::Continuation::BackTraceElem > * trace ) const;
130 virtual void gcMark( Kernel::GCMarkedSet & marked );
133 /* The InsertionContinuation sends the returned value to a warm variable, and passes THE_SLOT_VARIABLE (a null value) to the next continuation.
135 class InsertionContinuation : public Kernel::Continuation
137 mutable Kernel::StateHandle dst_; /* This being mutable is actually quite ugly... */
138 Kernel::PassedDyn dyn_;
139 Kernel::ContRef cont_;
140 public:
141 InsertionContinuation( const Kernel::StateHandle & dst, const Kernel::ContRef & cont, const Kernel::PassedDyn & dyn, const Ast::SourceLocation & traceLoc );
142 ~InsertionContinuation( );
143 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const;
144 virtual void backTrace( std::list< Kernel::Continuation::BackTraceElem > * trace ) const;
145 virtual void gcMark( Kernel::GCMarkedSet & marked );
148 /* StmtStoreValueContinuation is like StoreValueContinuation, except that it passes THE_SLOT_VARIABLE (a null value) to the next continuation.
150 class StmtStoreValueContinuation : public Kernel::Continuation
152 Kernel::ValueRef * res_;
153 Kernel::ContRef cont_;
154 public:
155 StmtStoreValueContinuation( Kernel::ValueRef * res, const Kernel::ContRef & cont, const Ast::SourceLocation & traceLoc );
156 ~StmtStoreValueContinuation( );
157 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const;
158 virtual void backTrace( std::list< Kernel::Continuation::BackTraceElem > * trace ) const;
159 virtual void gcMark( Kernel::GCMarkedSet & marked );
162 class StmtStoreVariableContinuation : public Kernel::Continuation
164 Kernel::VariableHandle dst_;
165 Kernel::ContRef cont_;
166 public:
167 StmtStoreVariableContinuation( const Kernel::VariableHandle & res, const Kernel::ContRef & cont, const Ast::SourceLocation & traceLoc );
168 ~StmtStoreVariableContinuation( );
169 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const;
170 virtual void backTrace( std::list< Kernel::Continuation::BackTraceElem > * trace ) const;
171 virtual void gcMark( Kernel::GCMarkedSet & marked );
174 class ForcingContinuation : public Kernel::Continuation
176 Kernel::ContRef cont_;
177 public:
178 ForcingContinuation( const Kernel::ContRef & cont, const Ast::SourceLocation & traceLoc );
179 ~ForcingContinuation( );
180 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const;
181 virtual void backTrace( std::list< Kernel::Continuation::BackTraceElem > * trace ) const;
182 virtual void gcMark( Kernel::GCMarkedSet & marked );
185 class Transform2DCont : public Kernel::Continuation
187 Lang::Transform2D tf_;
188 Kernel::ContRef cont_;
189 public:
190 Transform2DCont( Lang::Transform2D tf, const Kernel::ContRef & cont, const Ast::SourceLocation & traceLoc );
191 virtual ~Transform2DCont( );
192 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const;
193 virtual void backTrace( std::list< Kernel::Continuation::BackTraceElem > * trace ) const;
194 virtual void gcMark( Kernel::GCMarkedSet & marked );
197 class Transform3DCont : public Kernel::Continuation
199 Lang::Transform3D tf_;
200 Kernel::ContRef cont_;
201 public:
202 Transform3DCont( Lang::Transform3D tf, const Kernel::ContRef & cont, const Ast::SourceLocation & traceLoc );
203 virtual ~Transform3DCont( );
204 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const;
205 virtual void backTrace( std::list< Kernel::Continuation::BackTraceElem > * trace ) const;
206 virtual void gcMark( Kernel::GCMarkedSet & marked );
209 class PathApplication2DCont : public Kernel::Continuation
211 RefCountPtr< const Lang::ElementaryPath2D > path_;
212 Kernel::ContRef cont_;
213 public:
214 PathApplication2DCont( RefCountPtr< const Lang::ElementaryPath2D > path, const Kernel::ContRef & cont, const Ast::SourceLocation & traceLoc );
215 virtual ~PathApplication2DCont( );
216 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const;
217 virtual void backTrace( std::list< Kernel::Continuation::BackTraceElem > * trace ) const;
218 virtual void gcMark( Kernel::GCMarkedSet & marked );
221 class PathApplication3DCont : public Kernel::Continuation
223 RefCountPtr< const Lang::ElementaryPath3D > path_;
224 Kernel::ContRef cont_;
225 public:
226 PathApplication3DCont( RefCountPtr< const Lang::ElementaryPath3D > path, const Kernel::ContRef & cont, const Ast::SourceLocation & traceLoc );
227 virtual ~PathApplication3DCont( );
228 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const;
229 virtual void backTrace( std::list< Kernel::Continuation::BackTraceElem > * trace ) const;
230 virtual void gcMark( Kernel::GCMarkedSet & marked );
233 class ComposedFunctionCall_cont : public Kernel::Continuation
235 RefCountPtr< const Lang::Function > second_;
236 Kernel::PassedDyn dyn_;
237 Kernel::ContRef cont_;
238 public:
239 ComposedFunctionCall_cont( const RefCountPtr< const Lang::Function > & second, const Kernel::PassedDyn & dyn, const Kernel::ContRef & cont, const Ast::SourceLocation & callLoc );
240 virtual ~ComposedFunctionCall_cont( );
241 virtual void takeHandle( Kernel::VariableHandle val, Kernel::EvalState * evalState, bool dummy ) const;
242 virtual void backTrace( std::list< Kernel::Continuation::BackTraceElem > * trace ) const;
243 virtual void gcMark( Kernel::GCMarkedSet & marked );
251 #endif