Update procedures
[shapes.git] / source / astvalues.cc
blobf4fa3e1b7902ab244cd446146d7bad7f608144f4
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, 2014 Henrik Tidefelt
19 #include "astvalues.h"
20 #include "shapesexceptions.h"
21 #include "shapescore.h"
22 #include "astfun.h"
23 #include "consts.h"
24 #include "globals.h"
25 #include "continuations.h"
27 using namespace Shapes;
30 Ast::Constant::Constant( const Ast::SourceLocation & loc, const Kernel::VariableHandle & val )
31 : Ast::Expression( loc ), val_( val )
32 { }
34 Ast::Constant::Constant( const Ast::SourceLocation & loc, RefCountPtr< const Lang::Value > val )
35 : Ast::Expression( loc ), val_( Kernel::VariableHandle( new Kernel::Variable( val ) ) )
36 { }
38 Ast::Constant::Constant( const Ast::SourceLocation & loc, const Lang::Value * val )
39 : Ast::Expression( loc ), val_( Helpers::newValHandle( val ) )
40 { }
42 Ast::Constant::~Constant( )
43 { }
45 void
46 Ast::Constant::analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst )
48 /* Nothing to do! */
51 void
52 Ast::Constant::eval( Kernel::EvalState * evalState ) const
54 Kernel::ContRef cont = evalState->cont_;
55 cont->takeHandle( val_,
56 evalState );
60 Ast::PolarHandle2DExpr::PolarHandle2DExpr( const Ast::SourceLocation & loc, Ast::Expression * rExpr, Ast::Expression * aExpr )
61 : Ast::Expression( loc ), rExpr_( rExpr ), aExpr_( aExpr )
62 { }
64 Ast::PolarHandle2DExpr::~PolarHandle2DExpr( )
66 delete rExpr_;
67 delete aExpr_;
70 void
71 Ast::PolarHandle2DExpr::analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst )
73 rExpr_->analyze( this, env, freeStatesDst );
74 aExpr_->analyze( this, env, freeStatesDst );
77 void
78 Ast::PolarHandle2DExpr::eval( Kernel::EvalState * evalState ) const
80 Kernel::ContRef cont = evalState->cont_;
81 evalState->cont_ = Kernel::ContRef
82 ( new Kernel::PolarHandle2DCont( aExpr_->loc( ),
83 RefCountPtr< Kernel::PolarHandlePromise >( new Kernel::PolarHandleTruePromise( new Kernel::Thunk( evalState->env_, evalState->dyn_, rExpr_ ) ) ),
84 cont ) );
85 evalState->expr_ = aExpr_;
88 Kernel::PolarHandle2DCont::PolarHandle2DCont( const Ast::SourceLocation & traceLoc, const RefCountPtr< Kernel::PolarHandlePromise > & rPromise, const Kernel::ContRef & cont )
89 : Kernel::Continuation( traceLoc ), rPromise_( rPromise ), cont_( cont )
90 { }
92 Kernel::PolarHandle2DCont::~PolarHandle2DCont( )
93 { }
95 void
96 Kernel::PolarHandle2DCont::takeValue( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState, bool dummy ) const
98 cont_->takeValue( Kernel::ValueRef( new Lang::PolarHandle2D( rPromise_,
99 Helpers::down_cast_ContinuationArgument< const Lang::Float >( val, this )->val_ ) ),
100 evalState );
103 Kernel::ContRef
104 Kernel::PolarHandle2DCont::up( ) const
106 return cont_;
109 RefCountPtr< const char >
110 Kernel::PolarHandle2DCont::description( ) const
112 return strrefdup( "( <> ^ <> ) angle" );
115 void
116 Kernel::PolarHandle2DCont::gcMark( Kernel::GCMarkedSet & marked )
118 cont_->gcMark( marked );
122 Ast::PolarHandle2DExprFree_a::PolarHandle2DExprFree_a( const Ast::SourceLocation & loc, Ast::Expression * rExpr )
123 : Ast::Expression( loc ), rExpr_( rExpr )
126 Ast::PolarHandle2DExprFree_a::~PolarHandle2DExprFree_a( )
128 delete rExpr_;
131 void
132 Ast::PolarHandle2DExprFree_a::analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst )
134 rExpr_->analyze( this, env, freeStatesDst );
137 void
138 Ast::PolarHandle2DExprFree_a::eval( Kernel::EvalState * evalState ) const
140 Kernel::ContRef cont = evalState->cont_;
141 cont->takeValue( Kernel::ValueRef( new Lang::PolarHandle2DFree_a( RefCountPtr< Kernel::PolarHandlePromise >( new Kernel::PolarHandleTruePromise( new Kernel::Thunk( evalState->env_, evalState->dyn_, rExpr_ ) ) ) ) ),
142 evalState );
146 Ast::SpanLastExpr::SpanLastExpr( const Ast::SourceLocation & loc )
147 : Ast::Expression( loc )
150 Ast::SpanLastExpr::~SpanLastExpr( )
153 void
154 Ast::SpanLastExpr::analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst )
156 /* Nothing to do! */
159 void
160 Ast::SpanLastExpr::eval( Kernel::EvalState * evalState ) const
162 Kernel::ContRef cont = evalState->cont_;
163 cont->takeValue( evalState->dyn_->getSpanLast( ),
164 evalState );
168 Ast::EmptyExpression::EmptyExpression( const Ast::SourceLocation & loc )
169 : Ast::Expression( loc )
171 immediate_ = true;
174 Ast::EmptyExpression::~EmptyExpression( )
177 void
178 Ast::EmptyExpression::analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst )
180 /* Nothing to do! */
183 void
184 Ast::EmptyExpression::eval( Kernel::EvalState * evalState ) const
186 Kernel::ContRef cont = evalState->cont_;
187 cont->takeHandle( Kernel::THE_VOID_VARIABLE,
188 evalState );
192 void
193 Helpers::relax( const RefCountPtr< const Lang::Value > & val, Kernel::EvalState * evalState )
195 /* Piggy-back on the mechanism for the <end> expression. */
197 static Ast::SourceLocation internalLoc( Ast::FileID::build_internal( "< core internal helper 'relax' >" ) );
198 static Ast::SpanLastExpr spanLast( internalLoc );
200 evalState->dyn_ = Kernel::PassedDyn( new Kernel::DynamicEnvironment( evalState->dyn_, val ) );
201 evalState->expr_ = & spanLast;