Updating the changelog in the VERSION file, and version_sync.
[shapes.git] / source / ast.h
blobb0e1cc9bf4152249b800937cb39fc3d86fc4da2d
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, 2009 Henrik Tidefelt
19 #pragma once
21 #include "Shapes_Ast_decls.h"
22 #include "Shapes_Kernel_decls.h"
24 #include "refcount.h"
25 #include "shapestypes.h"
26 #include "dynamicenvironment.h"
27 #include "sourcelocation.h"
29 #include <list>
30 #include <iostream>
31 #include <string>
33 namespace Shapes
36 namespace Kernel
39 class EvalState
41 public:
42 Ast::Expression * expr_;
43 Kernel::PassedEnv env_;
44 Kernel::PassedDyn dyn_;
45 Kernel::ContRef cont_;
47 EvalState( Ast::Expression * expr, const Kernel::PassedEnv & env, const Kernel::PassedDyn & dyn, const Kernel::ContRef & cont )
48 : expr_( expr ), env_( env ), dyn_( dyn ), cont_( cont )
49 { }
52 class Continuation
54 protected:
55 const Ast::SourceLocation & traceLoc_;
56 public:
57 Continuation( const Ast::SourceLocation & traceLoc )
58 : traceLoc_( traceLoc )
59 { }
60 virtual ~Continuation( )
61 { }
62 virtual void takeHandle( Kernel::VariableHandle val, Kernel::EvalState * evalState, bool callingMyself = false ) const;
63 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool callingMyself = false ) const;
64 virtual void gcMark( Kernel::GCMarkedSet & marked ) = 0;
65 virtual Kernel::ContRef up( ) const = 0; // Returns a NullPtr when there is nothing to return.
66 virtual RefCountPtr< const char > description( ) const = 0; // outer continuation before ourselves in the list, that is first push_front ourselves, then recurse.
67 const Ast::SourceLocation & traceLoc( ) const;
68 void backTrace( std::ostream & os ) const;
71 class ForcedStructureContinuation : public Continuation
73 protected:
74 const char * continuationName_;
75 public:
76 ForcedStructureContinuation( const char * continuationName, const Ast::SourceLocation & traceLoc );
77 virtual ~ForcedStructureContinuation( );
78 virtual void takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const;
79 virtual void takeStructure( const RefCountPtr< const Lang::Structure > & structure, Kernel::EvalState * evalState ) const = 0;
81 static RefCountPtr< const Lang::SingleList > findUnforced( RefCountPtr< const Lang::SingleList > lst );
86 namespace Ast
89 class Node
91 public:
92 typedef std::vector< Ast::Node * > ChildrenType;
93 protected:
94 Node * parent_;
95 Ast::AnalysisEnvironment * analysisEnv_;
96 const Ast::SourceLocation loc_;
97 ChildrenType children_;
98 public:
99 Node( const Ast::SourceLocation & loc );
100 virtual ~Node( );
101 void analyze( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
102 const Ast::SourceLocation & loc( ) const;
103 Ast::Node * parent( );
104 ChildrenType & children( );
105 Ast::AnalysisEnvironment * getEnv( );
106 Ast::Expression * findExpressionSameFile( const Ast::SourceLocation & loc );
107 protected:
108 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst ) = 0;
111 class Expression : public Node
113 public:
114 /* This flag serves two purposes.
115 * 1) Users may use it to prevent delayed evaluation for efficiency reasons. (There is no way users
116 * can force evaluation to be delayed.)
117 * 2) The kernel may set it during the analysis of the program, for example, to prevent delayed
118 * evaluation of expressions with free states.
119 * Since the computation of free states can be (just a little bit) costly, the analysis is not performed
120 * for every single expressions -- care must be taken during the analysis to ensure it is set correctly
121 * wherever needed.
123 bool immediate_;
124 Kernel::BreakpointInfo * breakpoint_; /* Pointer is null if there is no breakpoint. */
126 Expression( const Ast::SourceLocation & loc );
127 virtual ~Expression( );
128 virtual void eval( Kernel::EvalState * evalState ) const = 0;
131 class BindNode : public Node
133 protected:
134 Ast::SourceLocation idLoc_;
135 const char * id_;
136 public:
137 BindNode( const Ast::SourceLocation & loc, const Ast::SourceLocation & idLoc, const char * id );
138 virtual ~BindNode( );
139 const char * id( ) const;
140 const Ast::SourceLocation & idLoc( ) const;
141 virtual void evalHelper( Kernel::EvalState * evalState ) const = 0; /* Avoid the name <eval>, to make it easy to keep track of all places in the source where Expression::eval is invoked. */
144 class Assertion : public Expression
146 public:
147 Assertion( const Ast::SourceLocation & loc );
148 virtual ~Assertion( );
151 class ErrorExpression : public Expression
153 public:
154 ErrorExpression( const Ast::SourceLocation & loc );
155 virtual ~ErrorExpression( );
156 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
157 virtual void eval( Kernel::EvalState * evalState ) const;