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
19 class UserLength
: public Expression
21 Ast::SourceLocation loc
;
25 UserLength( const Ast::SourceLocation
& _loc
, double _val
, const char * _unit
);
26 virtual ~UserLength( );
27 virtual void eval( Kernel::EvalState
* evalState
) const;
28 virtual const Ast::SourceLocation
& firstLoc( ) const;
29 virtual const Ast::SourceLocation
& lastLoc( ) const;
32 class IntroduceUnitStmt
: public Expression
34 Ast::SourceLocation idLoc
;
36 Ast::Expression
* expr
;
38 IntroduceUnitStmt( const Ast::SourceLocation
& _idLoc
, const char * _id
, Ast::Expression
* _expr
);
39 virtual ~IntroduceUnitStmt( );
40 virtual void eval( Kernel::EvalState
* evalState
) const;
41 virtual const Ast::SourceLocation
& firstLoc( ) const;
42 virtual const Ast::SourceLocation
& lastLoc( ) const;
48 Shapes::UserLength::UserLength( const Ast::SourceLocation
& _loc
, double _val
, const char * _unit
)
49 : loc( _loc
), val( _val
), unit( _unit
)
52 Shapes::UserLength::~UserLength( )
58 Shapes::UserLength::eval( Kernel::EvalState
* evalState
) const
60 Kernel::ContRef cont
= evalState
->cont_
;
61 (*cont
)( Kernel::ValueRef( new Lang::Length( val
* evalState
->env
->lookupUnit( loc
, unit
) ) ),
65 const Ast::SourceLocation
&
66 Shapes::UserLength::firstLoc( ) const
71 const Ast::SourceLocation
&
72 Shapes::UserLength::lastLoc( ) const
78 Shapes::IntroduceUnitStmt::IntroduceUnitStmt( const Ast::SourceLocation
& _idLoc
, const char * _id
, Ast::Expression
* _expr
)
79 : idLoc( _idLoc
), id( _id
), expr( _expr
)
82 Shapes::IntroduceUnitStmt::~IntroduceUnitStmt( )
88 RefCountPtr
< const Lang::Value
>
89 Shapes::IntroduceUnitStmt::value( Kernel::VariableHandle dstgroup
, SimplePDF::PDF_out
* pdfo
, Kernel::GraphicsState
* metaState
, Kernel::PassedEnv env
) const
91 RefCountPtr
< const Lang::Value
> untypedVal
= expr
->value( dstgroup
, pdfo
, metaState
, env
);
92 typedef const Lang::Length ArgType
;
93 ArgType
* val
= dynamic_cast< ArgType
* >( untypedVal
.getPtr( ) );
96 throw Exceptions::TypeMismatch( expr
, untypedVal
->getTypeName( ), ArgType::staticTypeName( ) );
98 env
->defineUnit( idLoc
, id
, val
->getVal( ) );
99 return Shapes::THE_VOID
;
102 const Ast::SourceLocation
&
103 Shapes::IntroduceUnitStmt::firstLoc( ) const
108 const Ast::SourceLocation
&
109 Shapes::IntroduceUnitStmt::lastLoc( ) const
111 return expr
->lastLoc( );
115 Shapes::AssignUnitStmt::AssignUnitStmt( const Ast::SourceLocation
& _idLoc
, const char * _id
, Ast::Expression
* _expr
)
116 : idLoc( _idLoc
), id( _id
), expr( _expr
)
119 Shapes::AssignUnitStmt::~AssignUnitStmt( )
125 RefCountPtr
< const Lang::Value
>
126 Shapes::AssignUnitStmt::value( Kernel::VariableHandle dstgroup
, SimplePDF::PDF_out
* pdfo
, Kernel::GraphicsState
* metaState
, Kernel::PassedEnv env
) const
128 RefCountPtr
< const Lang::Value
> untypedVal
= expr
->value( dstgroup
, pdfo
, metaState
, env
);
129 typedef const Lang::Length ArgType
;
130 ArgType
* val
= dynamic_cast< ArgType
* >( untypedVal
.getPtr( ) );
133 throw Exceptions::TypeMismatch( expr
, untypedVal
->getTypeName( ), ArgType::staticTypeName( ) );
135 env
->redefineUnit( idLoc
, id
, val
->getVal( ) );
136 return Shapes::THE_VOID
;
139 const Ast::SourceLocation
&
140 Shapes::AssignUnitStmt::firstLoc( ) const
145 const Ast::SourceLocation
&
146 Shapes::AssignUnitStmt::lastLoc( ) const
148 return expr
->lastLoc( );