Updating the changelog in the VERSION file, and version_sync.
[shapes.git] / source / astfun.h
blob350c33d46ea4fefac839aa9a89558ed9317768e8
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 #pragma once
21 #include "Shapes_Ast_decls.h"
22 #include "Shapes_Kernel_decls.h"
24 #include "ast.h"
27 namespace Shapes
29 namespace Kernel
31 class Formals
33 Ast::SourceLocation loc_;
34 public:
35 bool seenDefault_;
36 std::map< const char *, size_t, charPtrLess > * argumentOrder_;
37 std::vector< Ast::Expression * > defaultExprs_; // The sink does not have a default expression at all.
38 const char * sink_; // If null, there is no sink.
39 std::vector< bool > forcePos_;
40 std::map< const char *, size_t, charPtrLess > * stateOrder_;
41 Formals( );
42 Formals( size_t numberOfDummyDefaultExprs );
43 ~Formals( );
44 void setLoc( Ast::SourceLocation loc );
46 void push_exprs( Ast::ArgListExprs * args ) const;
47 Kernel::EvaluatedFormals * newEvaluatedFormals( Kernel::Arguments & args ) const;
48 Kernel::EvaluatedFormals * newEvaluatedFormals( Kernel::Arguments & args, size_t * pos ) const; // values are taken at *pos, and *pos is incremented accordingly
50 std::vector< bool > * newArgListForcePos( const Ast::ArgListExprs * argList ) const;
51 std::vector< bool > * newArgListForcePos( const Ast::ArgListExprs * argList, const Kernel::Arguments & curryArgs ) const;
53 bool hasSink( ) const { return sink_ != 0; }
55 const Ast::SourceLocation & loc( ) const;
58 class CallContInfo
60 std::vector< bool > * forcePos_;
61 bool forceAll_; /* if forcePos == 0, then either all or none are forced according to this variable */
62 public:
63 const Ast::ArgListExprs * argList_;
64 Kernel::PassedEnv env_;
65 Kernel::PassedDyn dyn_;
66 Kernel::ContRef cont_;
68 CallContInfo( const Ast::ArgListExprs * argList, const Kernel::EvalState & evalState, std::vector< bool > * forcePos );
69 CallContInfo( const Ast::ArgListExprs * argList, const Kernel::EvalState & evalState, bool forceAll );
70 ~CallContInfo( );
72 bool force( const size_t & pos ) const;
73 bool isSelective( ) const;
74 void gcMark( Kernel::GCMarkedSet & marked );
79 namespace Ast
82 class ArgListExprs
84 bool exprOwner_;
85 Ast::Node * parent_;
86 public:
87 std::list< Ast::Expression * > * orderedExprs_;
88 std::map< const char *, Ast::Expression *, charPtrLess > * namedExprs_;
89 std::list< Ast::StateReference * > * orderedStates_;
90 std::map< const char *, Ast::StateReference *, charPtrLess > * namedStates_;
91 StateIDSet * freeStates_; /* Set of free states referenced by the expressions. */
93 class ConstIterator
95 public:
96 std::list< Ast::Expression * >::const_reverse_iterator i1_;
97 std::map< const char *, Ast::Expression *, charPtrLess >::const_iterator i2_;
98 size_t index_;
100 ConstIterator( const ConstIterator & orig );
101 ConstIterator( std::list< Ast::Expression * >::const_reverse_iterator i1, std::map< const char *, Ast::Expression *, charPtrLess >::const_iterator i2, const size_t & index );
104 ArgListExprs( bool exprOwner );
105 ArgListExprs( std::list< Ast::Expression * > * orderedExprs, std::map< const char *, Ast::Expression *, charPtrLess > * namedExprs,
106 std::list< Ast::StateReference * > * orderedStates, std::map< const char *, Ast::StateReference *, charPtrLess > * namedStates );
107 ArgListExprs( size_t numberOfOrderedDummyExprs );
108 ~ArgListExprs( );
109 void analyze( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
111 ConstIterator begin( ) const;
113 void evaluate( const RefCountPtr< const Kernel::CallContInfo > & info, const ArgListExprs::ConstIterator & pos, const RefCountPtr< const Lang::SingleList > & vals, Kernel::EvalState * evalState ) const;
114 void bind( Kernel::Arguments * dst, RefCountPtr< const Lang::SingleList > vals, Kernel::PassedEnv env, Kernel::PassedDyn dyn ) const;
116 Kernel::VariableHandle findNamed( RefCountPtr< const Lang::SingleList > vals, const char * name ) const;
117 Kernel::VariableHandle getOrdered( RefCountPtr< const Lang::SingleList > vals, size_t pos ) const;
120 class FunctionFunction : public Lang::Function
122 Ast::SourceLocation loc_;
123 const Kernel::Formals * formals_;
124 Ast::Expression * body_;
125 Ast::FunctionMode functionMode_;
126 public:
127 FunctionFunction( const Ast::SourceLocation & loc, const Kernel::Formals * formals, Ast::Expression * body, const Ast::FunctionMode & functionMode );
128 virtual ~FunctionFunction( );
130 void push_exprs( Ast::ArgListExprs * args ) const;
132 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
133 virtual void call( Kernel::EvalState * evalState, Kernel::Arguments & args, const Ast::SourceLocation & callLoc ) const;
135 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
136 virtual bool isTransforming( ) const { return false; }
141 namespace Kernel
144 class CallCont_1 : public Kernel::Continuation
146 const Ast::ArgListExprs * argList_;
147 bool curry_;
148 Kernel::StateHandle mutatorSelf_;
149 Kernel::PassedEnv env_;
150 Kernel::PassedDyn dyn_;
151 Kernel::ContRef cont_;
152 const Ast::SourceLocation & callLoc_;
153 public:
154 CallCont_1( const Ast::SourceLocation & traceLoc, const Ast::ArgListExprs * argList, bool curry, Kernel::StateHandle mutatorSelf, const Kernel::EvalState & evalState, const Ast::SourceLocation & callLoc );
155 virtual ~CallCont_1( );
156 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const;
157 virtual Kernel::ContRef up( ) const;
158 virtual RefCountPtr< const char > description( ) const;
159 virtual void gcMark( Kernel::GCMarkedSet & marked );
162 class CallCont_last : public Kernel::Continuation
164 RefCountPtr< const Lang::Function > fun_;
165 const Ast::ArgListExprs * argList_;
166 bool curry_;
167 Kernel::StateHandle mutatorSelf_;
168 Kernel::PassedEnv env_;
169 Kernel::PassedDyn dyn_;
170 Kernel::ContRef cont_;
171 public:
172 CallCont_last( const RefCountPtr< const Lang::Function > & fun, const Ast::ArgListExprs * argList, bool curry, Kernel::StateHandle mutatorSelf, const Kernel::PassedEnv & env, const Kernel::PassedDyn & dyn, const Kernel::ContRef & cont, const Ast::SourceLocation & callLoc );
173 virtual ~CallCont_last( );
174 virtual void takeValue( const RefCountPtr< const Lang::Value > & valsUntyped, Kernel::EvalState * evalState, bool dummy ) const;
175 virtual Kernel::ContRef up( ) const;
176 virtual RefCountPtr< const char > description( ) const;
177 virtual void gcMark( Kernel::GCMarkedSet & marked );
180 class CallCont_n : public Kernel::Continuation
182 RefCountPtr< const Kernel::CallContInfo > info_;
183 Ast::ArgListExprs::ConstIterator pos_;
184 RefCountPtr< const Lang::SingleList > vals_;
185 public:
186 CallCont_n( const Ast::SourceLocation & traceLoc, const RefCountPtr< const Kernel::CallContInfo > & info, const Ast::ArgListExprs::ConstIterator & pos, RefCountPtr< const Lang::SingleList > vals );
187 virtual ~CallCont_n( );
188 virtual void takeHandle( Kernel::VariableHandle val, Kernel::EvalState * evalState, bool dummy ) const;
189 virtual Kernel::ContRef up( ) const;
190 virtual RefCountPtr< const char > description( ) const;
191 virtual void gcMark( Kernel::GCMarkedSet & marked );
194 class UnionCont_last : public Kernel::Continuation
196 const Ast::ArgListExprs * argList_;
197 Kernel::ContRef cont_;
198 public:
199 UnionCont_last( const Ast::ArgListExprs * argList, const Kernel::ContRef & cont, const Ast::SourceLocation & callLoc );
200 virtual ~UnionCont_last( );
201 virtual void takeValue( const RefCountPtr< const Lang::Value > & valsUntyped, Kernel::EvalState * evalState, bool dummy ) const;
202 virtual Kernel::ContRef up( ) const;
203 virtual RefCountPtr< const char > description( ) const;
204 virtual void gcMark( Kernel::GCMarkedSet & marked );
207 class SplitCont_1 : public Kernel::Continuation
209 Ast::Expression * argList_;
210 bool curry_;
211 Kernel::PassedEnv env_;
212 Kernel::PassedDyn dyn_;
213 Kernel::ContRef cont_;
214 const Ast::SourceLocation & callLoc_;
215 public:
216 SplitCont_1( const Ast::SourceLocation & traceLoc, Ast::Expression * argList, bool curry, const Kernel::EvalState & evalState, const Ast::SourceLocation & callLoc );
217 virtual ~SplitCont_1( );
218 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const;
219 virtual Kernel::ContRef up( ) const;
220 virtual RefCountPtr< const char > description( ) const;
221 virtual void gcMark( Kernel::GCMarkedSet & marked );
224 class SplitCont_2 : public Kernel::Continuation
226 RefCountPtr< const Lang::Function > fun_;
227 bool curry_;
228 Kernel::PassedEnv env_;
229 Kernel::PassedDyn dyn_;
230 Kernel::ContRef cont_;
231 public:
232 SplitCont_2( const RefCountPtr< const Lang::Function > & fun, bool curry, const Kernel::PassedEnv & env, const Kernel::PassedDyn & dyn, const Kernel::ContRef & cont, const Ast::SourceLocation & callLoc );
233 virtual ~SplitCont_2( );
234 virtual void takeValue( const RefCountPtr< const Lang::Value > & valsUntyped, Kernel::EvalState * evalState, bool dummy ) const;
235 virtual Kernel::ContRef up( ) const;
236 virtual RefCountPtr< const char > description( ) const;
237 virtual void gcMark( Kernel::GCMarkedSet & marked );
242 namespace Ast
245 class CallExpr : public Expression
247 bool curry_;
248 RefCountPtr< const Lang::Function > constFun_;
249 Ast::StateReference * mutatorSelf_;
250 public:
251 Ast::Expression * funExpr_;
252 Ast::ArgListExprs * argList_;
254 CallExpr( const Ast::SourceLocation & loc, Ast::Expression * funExpr, Ast::ArgListExprs * argList, bool curry = false );
255 CallExpr( const Ast::SourceLocation & loc, const RefCountPtr< const Lang::Function > & constFun, Ast::ArgListExprs * argList, bool curry = false );
256 virtual ~CallExpr( );
257 void setMutatorSelf( Ast::StateReference * mutatorSelf );
258 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
259 virtual void eval( Kernel::EvalState * evalState ) const;
262 class UnionExpr : public Expression
264 public:
265 Ast::ArgListExprs * argList_;
267 UnionExpr( const Ast::SourceLocation & loc, Ast::ArgListExprs * argList );
268 virtual ~UnionExpr( );
269 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
270 virtual void eval( Kernel::EvalState * evalState ) const;
273 class CallSplitExpr : public Expression
275 bool curry_;
276 Ast::Expression * funExpr_;
277 Ast::Expression * argList_;
279 public:
280 CallSplitExpr( const Ast::SourceLocation & loc, Ast::Expression * funExpr, Ast::Expression * argList, bool curry = false );
281 virtual ~CallSplitExpr( );
282 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
283 virtual void eval( Kernel::EvalState * evalState ) const;
286 class DummyExpression : public Expression
288 public:
289 DummyExpression( );
290 DummyExpression( const Ast::SourceLocation & loc );
291 virtual ~DummyExpression( );
292 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
293 virtual void eval( Kernel::EvalState * evalState ) const;