Updating the changelog in the VERSION file, and version_sync.
[shapes.git] / source / shapescore.cc
blobd2e4e5ff9c22deebc0f2c0cbee5c3042fe000c16
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 <cmath>
21 #include "Shapes_Helpers_decls.h"
23 #include "shapescore.h"
24 #include "globals.h"
25 #include "shapesexceptions.h"
26 #include "astexpr.h"
27 #include "astfun.h"
28 #include "consts.h"
29 #include "simplepdfi.h"
31 #include <iostream>
32 #include <sstream>
33 #include <fstream>
34 #include <vector>
35 #include <stdio.h>
37 using namespace Shapes;
40 RefCountPtr< const Lang::ElementaryPath2D >
41 Helpers::elementaryPathCast2D( const char * title, Kernel::Arguments & args, size_t argNo, const Ast::SourceLocation & callLoc )
43 RefCountPtr< const Lang::Value > ptr = args.getValue( argNo );
44 typedef const Lang::ElementaryPath2D ArgType;
45 RefCountPtr< ArgType > res = ptr.down_cast< ArgType >( );
46 if( res != NullPtr< ArgType >( ) )
48 return res;
51 typedef const Lang::CompositePath2D ArgType2;
52 ArgType2 * res2 = dynamic_cast< ArgType2 * >( ptr.getPtr( ) );
53 if( res2 == 0 )
55 throw Exceptions::CoreTypeMismatch( callLoc, title, args, argNo, ArgType::staticTypeName( ) );
58 return res2->getElementaryPath( );
61 RefCountPtr< const Lang::ElementaryPath2D >
62 Helpers::elementaryPathCast2D( const RefCountPtr< const Lang::Value > & ptr, const Kernel::Continuation * loc )
64 typedef const Lang::ElementaryPath2D ArgType;
65 RefCountPtr< ArgType > res = ptr.down_cast< ArgType >( );
66 if( res != NullPtr< ArgType >( ) )
68 return res;
71 typedef const Lang::CompositePath2D ArgType2;
72 ArgType2 * res2 = dynamic_cast< ArgType2 * >( ptr.getPtr( ) );
73 if( res2 == 0 )
75 throw Exceptions::ContinuationTypeMismatch( loc, ptr->getTypeName( ), ArgType::staticTypeName( ) );
78 return res2->getElementaryPath( );
81 RefCountPtr< const Lang::ElementaryPath2D >
82 Helpers::elementaryPathTry2D( const RefCountPtr< const Lang::Value > & ptr )
84 typedef const Lang::ElementaryPath2D ArgType;
85 RefCountPtr< ArgType > res = ptr.down_cast< ArgType >( );
86 if( res != NullPtr< ArgType >( ) )
88 return res;
91 typedef const Lang::CompositePath2D ArgType2;
92 ArgType2 * res2 = dynamic_cast< ArgType2 * >( ptr.getPtr( ) );
93 if( res2 == 0 )
95 throw NonLocalExit::NotThisType( );
98 return res2->getElementaryPath( );
102 Concrete::SplineTime
103 Helpers::pathTimeCast( const char * title, const RefCountPtr< const Lang::ElementaryPath2D > & pRef, Kernel::Arguments & args, size_t argNo, const Ast::SourceLocation & callLoc )
105 const Lang::ElementaryPath2D * p = pRef.getPtr( );
106 const Lang::Value * tPtr = args.getValue( argNo ).getPtr( );
107 typedef const Lang::Float ArgType;
108 ArgType * res = dynamic_cast< ArgType * >( tPtr );
109 if( res != 0 )
111 return Concrete::Time( res->val_ );
114 typedef const Lang::Length ArgType2;
115 ArgType2 * res2 = dynamic_cast< ArgType2 * >( tPtr );
116 if( res2 == 0 )
118 throw Exceptions::CoreTypeMismatch( callLoc, title, args, argNo, typeSetString( ArgType::staticTypeName( ), ArgType::staticTypeName( ) ) );
121 return p->arcTime( res2->get( ) );
124 Concrete::SplineTime
125 Helpers::pathTimeCast( const Lang::ElementaryPath2D * p, const Lang::Value * tPtr, const Kernel::Continuation * loc )
127 typedef const Lang::Float ArgType;
128 ArgType * res = dynamic_cast< ArgType * >( tPtr );
129 if( res != 0 )
131 return Concrete::Time( res->val_ );
134 typedef const Lang::Length ArgType2;
135 ArgType2 * res2 = dynamic_cast< ArgType2 * >( tPtr );
136 if( res2 == 0 )
138 throw Exceptions::ContinuationTypeMismatch( loc, tPtr->getTypeName( ), typeSetString( ArgType::staticTypeName( ), ArgType2::staticTypeName( ) ) );
141 return p->arcTime( res2->get( ) );
145 RefCountPtr< const Lang::ElementaryPath3D >
146 Helpers::elementaryPathCast3D( const char * title, Kernel::Arguments & args, size_t argNo, const Ast::SourceLocation & callLoc )
148 RefCountPtr< const Lang::Value > ptr = args.getValue( argNo );
149 typedef const Lang::ElementaryPath3D ArgType;
150 RefCountPtr< ArgType > res = ptr.down_cast< ArgType >( );
151 if( res != NullPtr< ArgType >( ) )
153 return res;
156 typedef const Lang::CompositePath3D ArgType2;
157 ArgType2 * res2 = dynamic_cast< ArgType2 * >( ptr.getPtr( ) );
158 if( res2 == 0 )
160 throw Exceptions::CoreTypeMismatch( callLoc, title, args, argNo, ArgType::staticTypeName( ) );
163 return res2->getElementaryPath( );
166 RefCountPtr< const Lang::ElementaryPath3D >
167 Helpers::elementaryPathCast3D( const RefCountPtr< const Lang::Value > & ptr, const Kernel::Continuation * loc )
169 typedef const Lang::ElementaryPath3D ArgType;
170 RefCountPtr< ArgType > res = ptr.down_cast< ArgType >( );
171 if( res != NullPtr< ArgType >( ) )
173 return res;
176 typedef const Lang::CompositePath3D ArgType2;
177 ArgType2 * res2 = dynamic_cast< ArgType2 * >( ptr.getPtr( ) );
178 if( res2 == 0 )
180 throw Exceptions::ContinuationTypeMismatch( loc, ptr->getTypeName( ), ArgType::staticTypeName( ) );
183 return res2->getElementaryPath( );
186 RefCountPtr< const Lang::ElementaryPath3D >
187 Helpers::elementaryPathTry3D( const RefCountPtr< const Lang::Value > & ptr )
189 typedef const Lang::ElementaryPath3D ArgType;
190 RefCountPtr< ArgType > res = ptr.down_cast< ArgType >( );
191 if( res != NullPtr< ArgType >( ) )
193 return res;
196 typedef const Lang::CompositePath3D ArgType2;
197 ArgType2 * res2 = dynamic_cast< ArgType2 * >( ptr.getPtr( ) );
198 if( res2 == 0 )
200 throw NonLocalExit::NotThisType( );
203 return res2->getElementaryPath( );
207 Concrete::SplineTime
208 Helpers::pathTimeCast( const char * title, const RefCountPtr< const Lang::ElementaryPath3D > & pRef, Kernel::Arguments & args, size_t argNo, const Ast::SourceLocation & callLoc )
210 const Lang::ElementaryPath3D * p = pRef.getPtr( );
211 const Lang::Value * tPtr = args.getValue( argNo ).getPtr( );
212 typedef const Lang::Float ArgType;
213 ArgType * res = dynamic_cast< ArgType * >( tPtr );
214 if( res != 0 )
216 return Concrete::Time( res->val_ );
219 typedef const Lang::Length ArgType2;
220 ArgType2 * res2 = dynamic_cast< ArgType2 * >( tPtr );
221 if( res2 == 0 )
223 throw Exceptions::CoreTypeMismatch( callLoc, title, args, argNo, typeSetString( ArgType::staticTypeName( ), ArgType::staticTypeName( ) ) );
226 return p->arcTime( res2->get( ) );
229 Concrete::SplineTime
230 Helpers::pathTimeCast( const Lang::ElementaryPath3D * p, const Lang::Value * tPtr, const Kernel::Continuation * loc )
232 typedef const Lang::Float ArgType;
233 ArgType * res = dynamic_cast< ArgType * >( tPtr );
234 if( res != 0 )
236 return Concrete::Time( res->val_ );
239 typedef const Lang::Length ArgType2;
240 ArgType2 * res2 = dynamic_cast< ArgType2 * >( tPtr );
241 if( res2 == 0 )
243 throw Exceptions::ContinuationTypeMismatch( loc, tPtr->getTypeName( ), typeSetString( ArgType::staticTypeName( ), ArgType2::staticTypeName( ) ) );
246 return p->arcTime( res2->get( ) );
250 RefCountPtr< const char >
251 Helpers::typeSetString( RefCountPtr< const char > type1, RefCountPtr< const char > type2 )
253 std::list< RefCountPtr< const char > > types;
254 types.push_back( type1 );
255 types.push_back( type2 );
256 return typeSetString( types );
259 RefCountPtr< const char >
260 Helpers::typeSetString( RefCountPtr< const char > type1, RefCountPtr< const char > type2, RefCountPtr< const char > type3 )
262 std::list< RefCountPtr< const char > > types;
263 types.push_back( type1 );
264 types.push_back( type2 );
265 types.push_back( type3 );
266 return typeSetString( types );
269 RefCountPtr< const char >
270 Helpers::typeSetString( RefCountPtr< const char > type1, RefCountPtr< const char > type2, RefCountPtr< const char > type3, RefCountPtr< const char > type4 )
272 std::list< RefCountPtr< const char > > types;
273 types.push_back( type1 );
274 types.push_back( type2 );
275 types.push_back( type3 );
276 types.push_back( type4 );
277 return typeSetString( types );
280 RefCountPtr< const char >
281 Helpers::typeSetString( RefCountPtr< const char > type1, RefCountPtr< const char > type2, RefCountPtr< const char > type3, RefCountPtr< const char > type4, RefCountPtr< const char > type5 )
283 std::list< RefCountPtr< const char > > types;
284 types.push_back( type1 );
285 types.push_back( type2 );
286 types.push_back( type3 );
287 types.push_back( type4 );
288 types.push_back( type5 );
289 return typeSetString( types );
292 RefCountPtr< const char >
293 Helpers::typeSetString( const std::list< RefCountPtr< const char > > types )
295 std::ostringstream oss;
296 oss << "any of {" ;
297 if( ! types.empty( ) )
299 typedef typeof types ListType;
300 ListType::const_iterator i = types.begin( );
301 oss << " " << *i ;
302 ++i;
303 for( ; i != types.end( ); ++i )
305 oss << ", " << *i ;
308 oss << " }" ;
309 return strrefdup( oss );
312 Lang::CoreFunction::CoreFunction( const char * title )
313 : Lang::Function( new Kernel::EvaluatedFormals( Ast::FileID::build_internal( title ), true ) ), title_( title )
316 Lang::CoreFunction::CoreFunction( const char * title, Kernel::EvaluatedFormals * formals )
317 : Lang::Function( formals ), title_( title )
320 void
321 Lang::CoreFunction::analyze_impl( Ast::Node * parent, Ast::AnalysisEnvironment * env, Ast::StateIDSet * freeStatesDst )
323 /* Nothing to be done! */
326 bool
327 Lang::CoreFunction::isTransforming( ) const
329 return false;
332 const char *
333 Lang::CoreFunction::getTitle( ) const
335 return title_;
338 void
339 Lang::CoreFunction::show( std::ostream & os ) const
341 os << "< core function: " << title_ << " >" ;