ammend merge: now compiles
[shapes.git] / source / ast.h
blob9fc87eac4798fcf5f6217b93a19e4818c054d86c
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, 2014 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_; /* Note: Not a const reference. */
97 ChildrenType children_;
98 public:
99 Node( const Ast::SourceLocation & loc );
100 Node( const Ast::SourceLocation & firstLoc, const Ast::SourceLocation & lastLoc ); /* Call SourceLocation two argument constructor on the fly. */
101 virtual ~Node( );
102 void analyze( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
103 /* The setParent and changeParent methods shall only be used when manipulating already analyzed expression,
104 * for example, when wrapping an already analyzed expression in Ast::Force.
106 void setParent( Ast::Node * parent, Ast::AnalysisEnvironment * env );
107 void changeParent( Ast::Node * parent );
108 const Ast::SourceLocation & loc( ) const;
109 Ast::Node * parent( );
110 ChildrenType & children( );
111 Ast::AnalysisEnvironment * getEnv( );
112 Ast::Expression * findExpressionSameFile( const Ast::SourceLocation & loc );
113 protected:
114 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst ) = 0;
117 class Expression : public Node
119 public:
120 /* The immediate_ flag means that the expression should not be delayed. This is not the same as forcing
121 * the expression (compare Ast::Force), which keeps forcing the result of the expression until a value
122 * is obtained.
124 bool immediate_;
125 Kernel::BreakpointInfo * breakpoint_; /* Pointer is null if there is no breakpoint. */
127 Expression( const Ast::SourceLocation & loc );
128 Expression( const Ast::SourceLocation & firstLoc, const Ast::SourceLocation & lastLoc ); /* Call SourceLocation two argument constructor on the fly. */
129 virtual ~Expression( );
130 virtual void eval( Kernel::EvalState * evalState ) const = 0;
133 class BindNode : public Node
135 protected:
136 const Ast::SourceLocation idLoc_; /* Note: Not a const reference. */
137 const Ast::PlacedIdentifier * id_;
138 public:
139 BindNode( const Ast::SourceLocation & loc, const Ast::SourceLocation & idLoc, const Ast::PlacedIdentifier * id );
140 BindNode( const Ast::SourceLocation & firstLoc, const Ast::SourceLocation & lastLoc, const Ast::SourceLocation & idLoc, const Ast::PlacedIdentifier * id ); /* Call SourceLocation two argument constructor on the fly. */
141 virtual ~BindNode( );
142 const Ast::PlacedIdentifier * id( ) const;
143 const Ast::SourceLocation & idLoc( ) const;
144 virtual void initializeEnvironment( Kernel::PassedEnv env, Kernel::PassedDyn dyn ) const = 0;
145 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. */
148 class Assertion : public Expression
150 public:
151 Assertion( const Ast::SourceLocation & loc );
152 virtual ~Assertion( );
155 class ErrorExpression : public Expression
157 public:
158 ErrorExpression( const Ast::SourceLocation & loc );
159 virtual ~ErrorExpression( );
160 virtual void analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst );
161 virtual void eval( Kernel::EvalState * evalState ) const;