> Is there a way to check for the type of an element on the stack?
like dup type ?
-- Many thanks,
Don Lancaster voice phone: (928)428-4073
Synergetics 3860 West First Street Box 809 Thatcher, AZ 85552
rss: http://www.tinaja.com/whtnu.xml email: d...@tinaja.com
Cecil Westerhof wrote:
> Op dinsdag 13 nov 2012 05:41 CET schreef Don Lancaster:
>>> Is there a way to check for the type of an element on the stack?
>> like dup type ?
> Yep.
The next cool bit is 'exec'.
'type' returns an executable name, so you can do type-dispatch with a
dictionary. eg.
/do { %different things to different types
<<
/integertype{1 add}
/booleantype{not}
/realtype{2 exp}
/stringtype{cvr 1 add}
>> 1 index type % x dict x-type
2 copy known not {pop/default} if
get exec % x'
} def
Wait. rats. that doesn't use the 'executable' part.
How about...
/do2 { %coerce input to integer
<<
/integertype{}
/realtype{cvi}
/booleantype{{1}{0}ifelse}
/stringtype{cvr round cvi}
>> begin
dup type exec
end
} def
This is used by the = and == operators to operate on multiple types in a
sensible manner (without a nasty shrub of ifelse nonsense).
> The next cool bit is 'exec'.
> 'type' returns an executable name, so you can do type-dispatch with a
> dictionary. eg.
> /do { %different things to different types
> <<
> /integertype{1 add}
> /booleantype{not}
> /realtype{2 exp}
> /stringtype{cvr 1 add}
>>> 1 index type % x dict x-type
> 2 copy known not {pop/default} if
> get exec % x'
> } def
> Wait. rats. that doesn't use the 'executable' part.
> How about...
> /do2 { %coerce input to integer
> <<
> /integertype{}
> /realtype{cvi}
> /booleantype{{1}{0}ifelse}
> /stringtype{cvr round cvi}
>>> begin
> dup type exec
> end
> } def
I have to look into this, to understand what is happening here, but I
gladly will do so. ;-}