Updating the changelog in the VERSION file, and version_sync.
[shapes.git] / source / astvar.h
blob08544da5191dfd7515a97a2e499cd0041954d83e
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_Kernel_decls.h"
23 #include "ast.h"
25 namespace Shapes
27 namespace Ast
30 class SourceLocationMark : public Node
32 public:
33 SourceLocationMark( const Ast::SourceLocation & loc );
34 virtual ~SourceLocationMark( );
36 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
40 class CodeBracket : public Expression
42 public:
43 typedef std::map< const char *, size_t, charPtrLess > MapType;
44 private:
45 std::list< Ast::Node * > * nodes_;
46 Ast::CodeBracket * extends_;
47 public:
48 MapType * argumentOrder_;
49 MapType * dynamicMap_;
50 MapType * stateOrder_;
51 public:
52 CodeBracket( const Ast::SourceLocation & loc, std::list< Ast::Node * > * nodes, Ast::CodeBracket * extends = 0 );
53 virtual ~CodeBracket( );
55 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
56 virtual void eval( Kernel::EvalState * evalState ) const;
58 Kernel::Environment * newEnvironment( Kernel::Environment * parent, const char * debugName = "Bracket" ) const;
59 void eval( Kernel::EvalState * evalState, Kernel::Environment * extendsEnv ) const;
61 void evalAt( const RefCountPtr< const Kernel::CodeBracketContInfo > & info, const std::list< Ast::Node * >::const_iterator & i, Kernel::EvalState * evalState ) const;
63 void setDynamicMap( MapType * dynamicMap );
68 namespace Kernel
70 class CodeBracketContInfo
72 public:
73 const Ast::CodeBracket * bracketExpr_;
74 Kernel::PassedEnv env_;
75 Kernel::PassedDyn dyn_;
76 Kernel::ContRef cont_;
78 CodeBracketContInfo( const Ast::CodeBracket * bracketExpr, const Kernel::EvalState & evalState );
79 ~CodeBracketContInfo( );
81 void gcMark( Kernel::GCMarkedSet & marked );
84 class CodeBracketContinuation : public Kernel::Continuation
86 RefCountPtr< const Kernel::CodeBracketContInfo > info_;
87 std::list< Ast::Node * >::const_iterator pos_;
88 public:
89 CodeBracketContinuation( const Ast::SourceLocation & _traceLoc, const RefCountPtr< const Kernel::CodeBracketContInfo > & _info, const std::list< Ast::Node * >::const_iterator & _pos );
90 virtual ~CodeBracketContinuation( );
91 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const;
92 virtual Kernel::ContRef up( ) const;
93 virtual RefCountPtr< const char > description( ) const;
94 virtual void gcMark( Kernel::GCMarkedSet & marked );
98 class DynamicBindingContinuation : public Kernel::Continuation
100 Kernel::PassedEnv env_;
101 Kernel::Environment::LexicalKey key_;
102 const Ast::DynamicBindingExpression * bindingExpr_;
103 Kernel::ContRef cont_;
104 public:
105 DynamicBindingContinuation( const Ast::SourceLocation & traceLoc, const Kernel::PassedEnv & env, const Kernel::Environment::LexicalKey & key, const Ast::DynamicBindingExpression * bindingExpr, const Kernel::ContRef & cont );
106 virtual ~DynamicBindingContinuation( );
107 virtual void takeHandle( Kernel::VariableHandle val, Kernel::EvalState * evalState, bool dummy ) const;
108 virtual Kernel::ContRef up( ) const;
109 virtual RefCountPtr< const char > description( ) const;
110 virtual void gcMark( Kernel::GCMarkedSet & marked );
113 class WithDynamicContinuation : public Kernel::Continuation
115 Ast::Expression * expr_;
116 Kernel::PassedEnv env_;
117 Kernel::PassedDyn dyn_;
118 Kernel::ContRef cont_;
119 public:
120 WithDynamicContinuation( const Ast::SourceLocation & traceLoc, Ast::Expression * expr, const Kernel::EvalState & evalState );
121 virtual ~WithDynamicContinuation( );
122 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const;
123 virtual Kernel::ContRef up( ) const;
124 virtual RefCountPtr< const char > description( ) const;
125 virtual void gcMark( Kernel::GCMarkedSet & marked );
128 class DynamicVariableDeclContinuation : public Kernel::Continuation
130 const Ast::DynamicVariableDecl * declExpr_;
131 Kernel::PassedEnv env_;
132 Kernel::PassedDyn dyn_;
133 Kernel::ContRef cont_;
134 public:
135 DynamicVariableDeclContinuation( const Ast::SourceLocation & traceLoc, const Ast::DynamicVariableDecl * declExpr, Kernel::EvalState & evalState );
136 virtual ~DynamicVariableDeclContinuation( );
137 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const;
138 virtual Kernel::ContRef up( ) const;
139 virtual RefCountPtr< const char > description( ) const;
140 virtual void gcMark( Kernel::GCMarkedSet & marked );
143 class AssertStructureContinuation : public Kernel::Continuation
145 Kernel::ContRef cont_;
146 public:
147 AssertStructureContinuation( const Kernel::ContRef & cont, const Ast::SourceLocation & traceLoc );
148 ~AssertStructureContinuation( );
149 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const;
150 virtual Kernel::ContRef up( ) const;
151 virtual RefCountPtr< const char > description( ) const;
152 virtual void gcMark( Kernel::GCMarkedSet & marked );
155 } // End of namespace Kernel.
157 namespace Ast
159 class LexiographicVariable : public Expression
161 const char * id_;
162 mutable Kernel::Environment::LexicalKey ** idKey_;
163 public:
164 LexiographicVariable( const Ast::SourceLocation & loc, const char * id, Kernel::Environment::LexicalKey ** idKey );
165 virtual ~LexiographicVariable( );
166 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
167 virtual void eval( Kernel::EvalState * evalState ) const;
170 class EvalOutsideExpr : public Expression
172 Ast::Expression * expr_;
173 public:
174 EvalOutsideExpr( const Ast::SourceLocation & loc, Ast::Expression * expr );
175 virtual ~EvalOutsideExpr( );
176 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
177 virtual void eval( Kernel::EvalState * evalState ) const;
180 class DefineVariable : public BindNode
182 Ast::Expression * expr_;
183 mutable size_t ** idPos_;
184 public:
185 DefineVariable( const Ast::SourceLocation & idLoc, const char * id, Ast::Expression * expr, size_t ** idPos );
186 virtual ~DefineVariable( );
187 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
188 virtual void evalHelper( Kernel::EvalState * evalState ) const;
191 class LexicalVariableLocationExpr : public Expression
193 public:
194 typedef enum { INDEX, DEPTH } Kind;
195 private:
196 const char * id_;
197 Kind kind_;
198 RefCountPtr< const Lang::Value > value_;
199 public:
200 LexicalVariableLocationExpr( const Ast::SourceLocation & idLoc, const char * id, Kind kind );
201 virtual ~LexicalVariableLocationExpr( );
202 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
203 virtual void eval( Kernel::EvalState * evalState ) const;
206 class LexicalVariableNameExpr : public Expression
208 RefCountPtr< const Lang::Value > value_;
209 public:
210 LexicalVariableNameExpr( const Ast::SourceLocation & loc );
211 virtual ~LexicalVariableNameExpr( );
212 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
213 virtual void eval( Kernel::EvalState * evalState ) const;
216 class StructSplitReference : public Expression
218 Ast::SourceLocation structLoc_;
219 size_t ** structPos_;
220 const char * fieldId_;
221 size_t fieldPos_;
222 Ast::Expression * defaultExpr_;
223 public:
224 StructSplitReference( Ast::SourceLocation fieldLoc, const char * fieldId, Ast::Expression * defaultExpr );
225 StructSplitReference( Ast::SourceLocation fieldLoc, size_t fieldPos, Ast::Expression * defaultExpr );
226 virtual ~StructSplitReference( );
227 void setStruct( Ast::SourceLocation structLoc, size_t ** structPos );
228 bool isOrdered( ) const { return fieldId_ == 0; }
229 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
230 virtual void eval( Kernel::EvalState * evalState ) const;
233 class StructSplitSink : public Expression
235 Ast::SourceLocation structLoc_;
236 size_t ** structPos_;
237 size_t consumedArguments_;
238 public:
239 StructSplitSink( );
240 virtual ~StructSplitSink( );
241 void setStruct( Ast::SourceLocation structLoc, size_t ** structPos, size_t consumedArguments );
242 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
243 virtual void eval( Kernel::EvalState * evalState ) const;
246 class AssertNoSinkNeeded : public Assertion
248 size_t orderedCount_;
249 Ast::SourceLocation structLoc_;
250 size_t ** structPos_;
251 public:
252 AssertNoSinkNeeded( const Ast::SourceLocation & loc, size_t orderedCount, Ast::SourceLocation structLoc, size_t ** structPos );
253 virtual ~AssertNoSinkNeeded( );
254 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
255 virtual void eval( Kernel::EvalState * evalState ) const;
258 class SplitDefineVariables
260 static size_t splitVarCount;
261 static PtrOwner_back_Access< std::list< const char * > > mem;
262 const char * splitVarId_;
263 public:
264 std::list< std::pair< Ast::DefineVariable *, Ast::StructSplitReference * > > exprs_;
265 Ast::DefineVariable * sinkDefine_;
266 Ast::StructSplitSink * sinkExpr_;
267 bool seenNamed_;
268 bool seenDefault_;
269 SplitDefineVariables( );
270 const char * newSplitVarId( ) const;
273 class StateReference : public Node
275 protected:
276 Node * someParent_; // The inherited parent_ does not make sense for reused nodes. Use this instead.
277 Ast::AnalysisEnvironment * someAnalysisEnv_; // The inherited analysisEnv_ does not make sense for reused nodes. Use this instead.
278 public:
279 StateReference( const Ast::SourceLocation & loc );
280 virtual ~StateReference( );
281 virtual Kernel::StateHandle getHandle( Kernel::PassedEnv env, Kernel::PassedDyn dyn ) const = 0;
284 class LexiographicState : public StateReference
286 const char * id_;
287 mutable Kernel::Environment::LexicalKey ** idKey_;
288 public:
289 LexiographicState( const Ast::SourceLocation & loc, const char * id, Kernel::Environment::LexicalKey ** idKey );
290 virtual ~LexiographicState( );
291 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
292 Kernel::StateHandle getHandle( Kernel::PassedEnv env, Kernel::PassedDyn dyn ) const;
295 class DynamicState : public Ast::StateReference
297 const char * id_;
298 mutable Kernel::Environment::LexicalKey ** idKey_;
299 public:
300 DynamicState( const Ast::SourceLocation & loc, const char * id );
301 virtual ~DynamicState( );
302 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
303 Kernel::StateHandle getHandle( Kernel::PassedEnv env, Kernel::PassedDyn dyn ) const;
306 class IntroduceState : public BindNode
308 Ast::Expression * expr_;
309 mutable size_t ** idPos_;
310 Ast::StateID stateID_;
311 public:
312 IntroduceState( const Ast::SourceLocation & idLoc, const char * id, Ast::Expression * expr, size_t ** idPos );
313 virtual ~IntroduceState( );
314 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
315 virtual void evalHelper( Kernel::EvalState * evalState ) const;
316 Ast::StateID getStateID( ) const { return stateID_; } /* Make sure analyze_impl has been called first! */
319 class Insertion : public Expression
321 Ast::StateReference * stateRef_;
322 Ast::Expression * expr_;
323 public:
324 Insertion( Ast::StateReference * stateRef, Ast::Expression * expr );
325 virtual ~Insertion( );
326 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
327 virtual void eval( Kernel::EvalState * evalState ) const;
330 class Freeze : public Expression
332 const char * id_;
333 mutable size_t ** idPos_;
334 public:
335 Freeze( const Ast::SourceLocation & idLoc, const char * id, size_t ** idPos );
336 virtual ~Freeze( );
337 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
338 virtual void eval( Kernel::EvalState * evalState ) const;
341 class Peek : public Expression
343 Ast::StateReference * stateRef_;
344 public:
345 Peek( const Ast::SourceLocation & idLoc, Ast::StateReference * stateRef );
346 virtual ~Peek( );
347 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
348 virtual void eval( Kernel::EvalState * evalState ) const;
351 class EvalSymbolFunction : public Lang::Function
353 Ast::SourceLocation loc_;
354 Ast::Expression * expr_;
355 Ast::AnalysisEnvironment * analysisEnv_;
356 public:
357 EvalSymbolFunction( const Ast::SourceLocation & loc, Ast::Expression * expr );
358 virtual ~EvalSymbolFunction( );
359 void push_exprs( Ast::ArgListExprs * args ) const;
360 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
361 virtual void call( Kernel::EvalState * evalState, Kernel::Arguments & args, const Ast::SourceLocation & callLoc ) const;
362 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
363 virtual bool isTransforming( ) const { return false; }
366 class MemberReferenceFunction : public Lang::Function
368 Ast::SourceLocation loc_;
369 Ast::Expression * variable_;
370 const char * fieldID_;
371 public:
372 MemberReferenceFunction( const Ast::SourceLocation & loc, Ast::Expression * variable, const char * fieldID );
373 virtual ~MemberReferenceFunction( );
374 void push_exprs( Ast::ArgListExprs * args ) const;
375 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
376 virtual void call( Kernel::EvalState * evalState, Kernel::Arguments & args, const Ast::SourceLocation & callLoc ) const;
377 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
378 virtual bool isTransforming( ) const { return false; }
381 class MutatorReference : public Ast::Expression
383 Ast::SourceLocation mutatorLoc_;
384 Ast::StateReference * state_;
385 const char * mutatorID_;
386 public:
387 MutatorReference( const Ast::SourceLocation & mutatorLoc, Ast::StateReference * state, const char * mutatorID );
388 virtual ~MutatorReference( );
389 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
390 virtual void eval( Kernel::EvalState * evalState ) const;
393 class SpecialLength : public Expression
395 double val_;
396 int sort_;
397 public:
398 SpecialLength( const Ast::SourceLocation & loc, double val, int sort );
399 virtual ~SpecialLength( );
400 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
401 virtual void eval( Kernel::EvalState * evalState ) const;
404 class DynamicVariable : public Ast::Expression
406 const char * id_;
407 mutable Kernel::Environment::LexicalKey ** idKey_;
408 public:
409 DynamicVariable( const Ast::SourceLocation & loc, const char * id );
410 virtual ~DynamicVariable( );
411 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
412 virtual void eval( Kernel::EvalState * evalState ) const;
415 class DynamicBindingExpression : public Ast::Expression
417 Ast::SourceLocation idLoc_;
418 const char * id_;
419 Ast::Expression * expr_;
420 mutable Kernel::Environment::LexicalKey ** idKey_;
421 public:
422 DynamicBindingExpression( const Ast::SourceLocation & idLoc, const char * id, Ast::Expression * expr, Kernel::Environment::LexicalKey ** idKey );
423 virtual ~DynamicBindingExpression( );
424 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
425 virtual void eval( Kernel::EvalState * evalState ) const;
426 const Ast::SourceLocation & idLoc( ) const { return idLoc_; }
427 const Ast::SourceLocation & exprLoc( ) const { return expr_->loc( ); }
428 const char * id( ) const { return id_; }
431 class DynamicStateBindingExpression : public Ast::Expression
433 Ast::SourceLocation dstLoc_;
434 const char * dstId_;
435 mutable Kernel::Environment::LexicalKey ** dstIdKey_;
436 Ast::StateReference * src_;
437 public:
438 DynamicStateBindingExpression( const Ast::SourceLocation & loc, const Ast::SourceLocation & dstLoc, const char * dstId, Ast::StateReference * src );
439 virtual ~DynamicStateBindingExpression( );
440 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
441 virtual void eval( Kernel::EvalState * evalState ) const;
444 class WithDynamicExpr : public Ast::Expression
446 Ast::Expression * bindings_;
447 Ast::Expression * expr_;
448 public:
449 WithDynamicExpr( const Ast::SourceLocation & loc, Ast::Expression * bindings, Ast::Expression * expr );
450 virtual ~WithDynamicExpr( );
451 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
452 virtual void eval( Kernel::EvalState * evalState ) const;
455 class DynamicVariableDecl : public BindNode
457 mutable size_t ** idPos_;
458 Ast::Expression * impl_;
459 public:
460 DynamicVariableDecl( const Ast::SourceLocation & loc, const Ast::SourceLocation & idLoc, const char * id, Ast::Expression * filterExpr, Ast::Expression * defaultExpr );
461 virtual ~DynamicVariableDecl( );
462 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
463 virtual void evalHelper( Kernel::EvalState * evalState ) const;
466 class DynamicVariableDeclFunction : public Lang::Function
468 const char * id_;
469 Ast::Expression * filterExpr_;
470 Ast::Expression * defaultExpr_;
471 size_t ** idPos_;
472 public:
473 DynamicVariableDeclFunction( const char * id, Ast::Expression * filterExpr, Ast::Expression * defaultExpr, size_t ** idPos );
474 virtual ~DynamicVariableDeclFunction( );
475 void push_exprs( Ast::ArgListExprs * args ) const;
476 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
477 virtual void call( Kernel::EvalState * evalState, Kernel::Arguments & args, const Ast::SourceLocation & callLoc ) const;
478 virtual void gcMark( Kernel::GCMarkedSet & marked ){ };
479 virtual bool isTransforming( ) const { return false; }
482 class DynamicStateDecl : public BindNode
484 const char * defaultStateID_;
485 mutable size_t ** idPos_;
486 Ast::StateReference * defaultState_;
487 public:
488 DynamicStateDecl( const Ast::SourceLocation & loc, const Ast::SourceLocation & idLoc, const char * id, Ast::StateReference * defaultState, size_t ** idPos );
489 virtual ~DynamicStateDecl( );
490 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
491 virtual void evalHelper( Kernel::EvalState * evalState ) const;
494 class DynamicExpression : public Ast::Expression
496 Ast::Expression * expr_;
497 public:
498 DynamicExpression( const Ast::SourceLocation & loc, Ast::Expression * expr );
499 virtual ~DynamicExpression( );
500 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
501 virtual void eval( Kernel::EvalState * evalState ) const;
504 class LexiographicType : public Expression
506 const char * id_;
507 mutable Kernel::Environment::LexicalKey ** idKey_;
508 public:
509 LexiographicType( const Ast::SourceLocation & loc, const char * id, Kernel::Environment::LexicalKey ** idKey );
510 virtual ~LexiographicType( );
511 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
512 virtual void eval( Kernel::EvalState * evalState ) const;