Update suitable examples and tests to use blank mode
[shapes.git] / source / coreoperators.cc
blob56392fee720d9ad099b81c76054b3e830a3760df
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 Henrik Tidefelt
19 #include "globals.h"
20 #include "shapescore.h"
21 #include "astfun.h"
22 #include "astexprs.h"
24 using namespace Shapes;
27 namespace Shapes
29 namespace Kernel
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_;
37 public:
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 )
40 { }
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,
47 evalState );
48 return;
51 std::vector< Kernel::VariableHandle >::const_iterator nextNext = next_;
52 ++nextNext;
53 if( nextNext == arguments_->end( ) )
55 cont_->takeHandle( *next_, evalState );
56 return;
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
65 return cont_;
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_;
87 public:
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 )
90 { }
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,
97 evalState );
98 return;
101 std::vector< Kernel::VariableHandle >::const_iterator nextNext = next_;
102 ++nextNext;
103 if( nextNext == arguments_->end( ) )
105 cont_->takeHandle( *next_, evalState );
106 return;
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
115 return cont_;
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 );
136 namespace Shapes
138 namespace Lang
141 class Core_and : public Lang::CoreFunction
143 public:
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 ) )
147 virtual void
148 call( Kernel::EvalState * evalState, Kernel::Arguments & args, const Ast::SourceLocation & callLoc ) const
150 if( args.empty( ) )
152 Kernel::ContRef cont = evalState->cont_;
153 cont->takeValue( THE_TRUE, evalState );
154 return;
157 if( args.size( ) == 1 )
159 Kernel::ContRef cont = evalState->cont_;
160 cont->takeHandle( args.getHandle( 0 ), evalState );
161 return;
164 typedef typeof *(args.getVariables( )) ListType;
165 ListType::const_iterator next = args.getVariables( )->begin( );
166 ++next;
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
175 public:
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 ) )
179 virtual void
180 call( Kernel::EvalState * evalState, Kernel::Arguments & args, const Ast::SourceLocation & callLoc ) const
182 if( args.empty( ) )
184 Kernel::ContRef cont = evalState->cont_;
185 cont->takeValue( Lang::THE_FALSE, evalState );
186 return;
189 if( args.size( ) == 1 )
191 Kernel::ContRef cont = evalState->cont_;
192 cont->takeHandle( args.getHandle( 0 ),
193 evalState );
194 return;
197 typedef typeof *(args.getVariables( )) ListType;
198 ListType::const_iterator next = args.getVariables( )->begin( );
199 ++next;
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 ),
212 "(--)" ) );
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 ),
215 "(++)" ) );
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 ),
218 "(&)" ) );
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 ),
221 "(&|)" ) );
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 ),
224 "(+)" ) );
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 ),
227 "(-)" ) );
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 ),
230 "(/_)" ) );
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 ),
233 "(*)" ) );
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 ),
236 "(/)" ) );
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 ),
239 "(*/)" ) );
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 ),
242 "(())" ) );
243 RefCountPtr< const Lang::UnaryOperatorFunction >
244 Lang::THE_OPERATOR_NEG( new Lang::UnaryOperatorFunction( new Ast::NegExpr( Ast::THE_UNKNOWN_LOCATION, new Ast::DummyExpression ),
245 "(~)" ) );
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 ),
248 "(<)" ) );
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 ),
251 "(>)" ) );
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 ),
254 "(==)" ) );
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 ),
257 "(<>)" ) );
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 ),
260 "(<=)" ) );
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 ),
263 "(>=)" ) );
264 RefCountPtr< const Lang::UnaryOperatorFunction >
265 Lang::THE_OPERATOR_NOT( new Lang::UnaryOperatorFunction( new Ast::NotExpr( Ast::THE_UNKNOWN_LOCATION, new Ast::DummyExpression ),
266 "(not)" ) );
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 ),
269 "(xor)" ) );
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" ) );