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 Henrik Tidefelt
20 #include "shapescore.h"
24 using namespace Shapes
;
32 class AndContinuation
: public Kernel::Continuation
34 RefCountPtr
< std::vector
< Kernel::VariableHandle
> > arguments_
;
35 std::vector
< Kernel::VariableHandle
>::const_iterator next_
;
36 Kernel::ContRef cont_
;
38 AndContinuation( const RefCountPtr
< std::vector
< Kernel::VariableHandle
> > & arguments
, const std::vector
< Kernel::VariableHandle
>::const_iterator
& next
, Kernel::ContRef cont
, const Ast::SourceLocation
& traceLoc
)
39 : Kernel::Continuation( traceLoc
), arguments_( arguments
), next_( next
), cont_( cont
)
41 virtual ~AndContinuation( ) { }
42 virtual void takeValue( const RefCountPtr
< const Lang::Value
> & val
, Kernel::EvalState
* evalState
, bool dummy
) const
44 if( ! Helpers::down_cast
< const Lang::Boolean
>( val
, traceLoc_
)->val_
)
46 cont_
->takeValue( Lang::THE_FALSE
,
51 std::vector
< Kernel::VariableHandle
>::const_iterator nextNext
= next_
;
53 if( nextNext
== arguments_
->end( ) )
55 cont_
->takeHandle( *next_
, evalState
);
59 Kernel::ContRef newCont
= Kernel::ContRef( new Kernel::AndContinuation( arguments_
, nextNext
, cont_
, traceLoc_
) );
60 evalState
->cont_
= newCont
;
61 newCont
->takeHandle( *next_
, evalState
);
63 virtual Kernel::ContRef
up( ) const
67 virtual RefCountPtr
< const char > description( ) const
69 return strrefdup( "and" );
71 virtual void gcMark( Kernel::GCMarkedSet
& marked
)
73 typedef typeof next_ I
;
74 for( I i
= next_
; i
!= arguments_
->end( ); ++i
)
76 const_cast< Kernel::Variable
* >( i
->getPtr( ) )->gcMark( marked
);
78 cont_
->gcMark( marked
);
82 class OrContinuation
: public Kernel::Continuation
84 RefCountPtr
< std::vector
< Kernel::VariableHandle
> > arguments_
;
85 std::vector
< Kernel::VariableHandle
>::const_iterator next_
;
86 Kernel::ContRef cont_
;
88 OrContinuation( const RefCountPtr
< std::vector
< Kernel::VariableHandle
> > & arguments
, const std::vector
< Kernel::VariableHandle
>::const_iterator
& next
, Kernel::ContRef cont
, const Ast::SourceLocation
& traceLoc
)
89 : Kernel::Continuation( traceLoc
), arguments_( arguments
), next_( next
), cont_( cont
)
91 virtual ~OrContinuation( ) { }
92 virtual void takeValue( const RefCountPtr
< const Lang::Value
> & val
, Kernel::EvalState
* evalState
, bool dummy
) const
94 if( Helpers::down_cast
< const Lang::Boolean
>( val
, traceLoc_
)->val_
)
96 cont_
->takeValue( Lang::THE_TRUE
,
101 std::vector
< Kernel::VariableHandle
>::const_iterator nextNext
= next_
;
103 if( nextNext
== arguments_
->end( ) )
105 cont_
->takeHandle( *next_
, evalState
);
109 Kernel::ContRef newCont
= Kernel::ContRef( new Kernel::OrContinuation( arguments_
, nextNext
, cont_
, traceLoc_
) );
110 evalState
->cont_
= newCont
;
111 newCont
->takeHandle( *next_
, evalState
);
113 virtual Kernel::ContRef
up( ) const
117 virtual RefCountPtr
< const char > description( ) const
119 return strrefdup( "or" );
121 virtual void gcMark( Kernel::GCMarkedSet
& marked
)
123 typedef typeof next_ I
;
124 for( I i
= next_
; i
!= arguments_
->end( ); ++i
)
126 const_cast< Kernel::Variable
* >( i
->getPtr( ) )->gcMark( marked
);
128 cont_
->gcMark( marked
);
141 class Core_and
: public Lang::CoreFunction
144 Core_and( const RefCountPtr
< const Ast::NamespacePath
> & ns
, const char * name
)
145 : CoreFunction( ns
, name
, new Kernel::EvaluatedFormals( Ast::FileID::build_internal( name
), false ) )
148 call( Kernel::EvalState
* evalState
, Kernel::Arguments
& args
, const Ast::SourceLocation
& callLoc
) const
152 Kernel::ContRef cont
= evalState
->cont_
;
153 cont
->takeValue( THE_TRUE
, evalState
);
157 if( args
.size( ) == 1 )
159 Kernel::ContRef cont
= evalState
->cont_
;
160 cont
->takeHandle( args
.getHandle( 0 ), evalState
);
164 typedef typeof *(args
.getVariables( )) ListType
;
165 ListType::const_iterator next
= args
.getVariables( )->begin( );
167 evalState
->cont_
= Kernel::ContRef( new Kernel::AndContinuation( args
.getVariables( ), next
, evalState
->cont_
, callLoc
) );
168 Kernel::ContRef cont
= evalState
->cont_
;
169 cont
->takeHandle( args
.getHandle( 0 ), evalState
);
173 class Core_or
: public Lang::CoreFunction
176 Core_or( const RefCountPtr
< const Ast::NamespacePath
> & ns
, const char * name
)
177 : CoreFunction( ns
, name
, new Kernel::EvaluatedFormals( Ast::FileID::build_internal( name
), false ) )
180 call( Kernel::EvalState
* evalState
, Kernel::Arguments
& args
, const Ast::SourceLocation
& callLoc
) const
184 Kernel::ContRef cont
= evalState
->cont_
;
185 cont
->takeValue( Lang::THE_FALSE
, evalState
);
189 if( args
.size( ) == 1 )
191 Kernel::ContRef cont
= evalState
->cont_
;
192 cont
->takeHandle( args
.getHandle( 0 ),
197 typedef typeof *(args
.getVariables( )) ListType
;
198 ListType::const_iterator next
= args
.getVariables( )->begin( );
200 evalState
->cont_
= Kernel::ContRef( new Kernel::OrContinuation( args
.getVariables( ), next
, evalState
->cont_
, callLoc
) );
201 Kernel::ContRef cont
= evalState
->cont_
;
202 cont
->takeHandle( args
.getHandle( 0 ), evalState
);
210 RefCountPtr
< const Lang::BinaryOperatorFunction
>
211 Lang::THE_OPERATOR_MINUSMINUS( new Lang::BinaryOperatorFunction( new Ast::MinusMinusExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
213 RefCountPtr
< const Lang::BinaryOperatorFunction
>
214 Lang::THE_OPERATOR_PLUSPLUS( new Lang::BinaryOperatorFunction( new Ast::PlusPlusExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
216 RefCountPtr
< const Lang::BinaryOperatorFunction
>
217 Lang::THE_OPERATOR_AMPERSAND( new Lang::BinaryOperatorFunction( new Ast::AmpersandExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
219 RefCountPtr
< const Lang::BinaryOperatorFunction
>
220 Lang::THE_OPERATOR_AMPERSAND_MORE( new Lang::BinaryOperatorFunction( new Ast::AmpersandMoreExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
222 RefCountPtr
< const Lang::BinaryOperatorFunction
>
223 Lang::THE_OPERATOR_PLUS( new Lang::BinaryOperatorFunction( new Ast::PlusExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
225 RefCountPtr
< const Lang::BinaryOperatorFunction
>
226 Lang::THE_OPERATOR_MINUS( new Lang::BinaryOperatorFunction( new Ast::MinusExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
228 RefCountPtr
< const Lang::BinaryOperatorFunction
>
229 Lang::THE_OPERATOR_ANGLE( new Lang::BinaryOperatorFunction( new Ast::AngleExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
231 RefCountPtr
< const Lang::BinaryOperatorFunction
>
232 Lang::THE_OPERATOR_STAR( new Lang::BinaryOperatorFunction( new Ast::StarExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
234 RefCountPtr
< const Lang::BinaryOperatorFunction
>
235 Lang::THE_OPERATOR_SLASH( new Lang::BinaryOperatorFunction( new Ast::SlashExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
237 RefCountPtr
< const Lang::BinaryOperatorFunction
>
238 Lang::THE_OPERATOR_PROJECTION( new Lang::BinaryOperatorFunction( new Ast::ProjectionExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
240 RefCountPtr
< const Lang::BinaryOperatorFunction
>
241 Lang::THE_OPERATOR_COMPOSE( new Lang::BinaryOperatorFunction( new Ast::ComposeExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
243 RefCountPtr
< const Lang::UnaryOperatorFunction
>
244 Lang::THE_OPERATOR_NEG( new Lang::UnaryOperatorFunction( new Ast::NegExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
),
246 RefCountPtr
< const Lang::BinaryOperatorFunction
>
247 Lang::THE_OPERATOR_LESS( new Lang::BinaryOperatorFunction( new Ast::LessExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
249 RefCountPtr
< const Lang::BinaryOperatorFunction
>
250 Lang::THE_OPERATOR_GREATER( new Lang::BinaryOperatorFunction( new Ast::GreaterExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
252 RefCountPtr
< const Lang::BinaryOperatorFunction
>
253 Lang::THE_OPERATOR_EQEQ( new Lang::BinaryOperatorFunction( new Ast::EqualExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
255 RefCountPtr
< const Lang::BinaryOperatorFunction
>
256 Lang::THE_OPERATOR_EQNEQ( new Lang::BinaryOperatorFunction( new Ast::NotEqualExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
258 RefCountPtr
< const Lang::BinaryOperatorFunction
>
259 Lang::THE_OPERATOR_LESSEQ( new Lang::BinaryOperatorFunction( new Ast::LessEqualExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
261 RefCountPtr
< const Lang::BinaryOperatorFunction
>
262 Lang::THE_OPERATOR_GREATEREQ( new Lang::BinaryOperatorFunction( new Ast::GreaterEqualExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
264 RefCountPtr
< const Lang::UnaryOperatorFunction
>
265 Lang::THE_OPERATOR_NOT( new Lang::UnaryOperatorFunction( new Ast::NotExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
),
267 RefCountPtr
< const Lang::BinaryOperatorFunction
>
268 Lang::THE_OPERATOR_XOR( new Lang::BinaryOperatorFunction( new Ast::XorExpr( Ast::THE_UNKNOWN_LOCATION
, new Ast::DummyExpression
, new Ast::DummyExpression
),
271 RefCountPtr
< const Lang::CoreFunction
> Lang::THE_FUNCTION_AND( new Lang::Core_and( Lang::THE_NAMESPACE_Shapes
, "and" ) );
272 RefCountPtr
< const Lang::CoreFunction
> Lang::THE_FUNCTION_OR( new Lang::Core_or( Lang::THE_NAMESPACE_Shapes
, "or" ) );