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
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"
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
)
34 Ast::Constant::Constant( const Ast::SourceLocation
& loc
, RefCountPtr
< const Lang::Value
> val
)
35 : Ast::Expression( loc
), val_( Kernel::VariableHandle( new Kernel::Variable( val
) ) )
38 Ast::Constant::Constant( const Ast::SourceLocation
& loc
, const Lang::Value
* val
)
39 : Ast::Expression( loc
), val_( Helpers::newValHandle( val
) )
42 Ast::Constant::~Constant( )
46 Ast::Constant::analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
)
52 Ast::Constant::eval( Kernel::EvalState
* evalState
) const
54 Kernel::ContRef cont
= evalState
->cont_
;
55 cont
->takeHandle( val_
,
60 Ast::PolarHandle2DExpr::PolarHandle2DExpr( const Ast::SourceLocation
& loc
, Ast::Expression
* rExpr
, Ast::Expression
* aExpr
)
61 : Ast::Expression( loc
), rExpr_( rExpr
), aExpr_( aExpr
)
64 Ast::PolarHandle2DExpr::~PolarHandle2DExpr( )
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
);
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_
) ) ),
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
)
92 Kernel::PolarHandle2DCont::~PolarHandle2DCont( )
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_
) ),
104 Kernel::PolarHandle2DCont::up( ) const
109 RefCountPtr
< const char >
110 Kernel::PolarHandle2DCont::description( ) const
112 return strrefdup( "( <> ^ <> ) angle" );
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( )
132 Ast::PolarHandle2DExprFree_a::analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
)
134 rExpr_
->analyze( this, env
, freeStatesDst
);
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_
) ) ) ) ),
146 Ast::SpanLastExpr::SpanLastExpr( const Ast::SourceLocation
& loc
)
147 : Ast::Expression( loc
)
150 Ast::SpanLastExpr::~SpanLastExpr( )
154 Ast::SpanLastExpr::analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
)
160 Ast::SpanLastExpr::eval( Kernel::EvalState
* evalState
) const
162 Kernel::ContRef cont
= evalState
->cont_
;
163 cont
->takeValue( evalState
->dyn_
->getSpanLast( ),
168 Ast::EmptyExpression::EmptyExpression( const Ast::SourceLocation
& loc
)
169 : Ast::Expression( loc
)
174 Ast::EmptyExpression::~EmptyExpression( )
178 Ast::EmptyExpression::analyze_impl( Ast::Node
* parent
, Ast::AnalysisEnvironment
* env
, Ast::StateIDSet
* freeStatesDst
)
184 Ast::EmptyExpression::eval( Kernel::EvalState
* evalState
) const
186 Kernel::ContRef cont
= evalState
->cont_
;
187 cont
->takeHandle( Kernel::THE_VOID_VARIABLE
,
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
;