Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Type of element on stack

42 views
Skip to first unread message

Cecil Westerhof

unread,
Nov 10, 2012, 9:24:48 PM11/10/12
to
Is there a way to check for the type of an element on the stack?

--
Cecil Westerhof
Senior Software Engineer
LinkedIn: http://www.linkedin.com/in/cecilwesterhof

Dan Coby

unread,
Nov 11, 2012, 12:15:51 AM11/11/12
to
On 11/10/2012 6:24 PM, Cecil Westerhof wrote:
> Is there a way to check for the type of an element on the stack?
>

Yes. The type operator. See page 705 of the PLRM 3rd edition.
All of the operators are listed in the PLRM.


GS>1 type =
integertype
GS>[1] type =
arraytype
GS>(aaa) type =
stringtype

Cecil Westerhof

unread,
Nov 11, 2012, 4:34:13 AM11/11/12
to
Op zondag 11 nov 2012 06:15 CET schreef Dan Coby:

>> Is there a way to check for the type of an element on the stack?
>>
>
> Yes. The type operator. See page 705 of the PLRM 3rd edition.
> All of the operators are listed in the PLRM.
>
>
> GS>1 type =
> integertype
> GS>[1] type =
> arraytype
> GS>(aaa) type =
> stringtype

Works like a charm. Thanks.

Don Lancaster

unread,
Nov 12, 2012, 11:41:32 PM11/12/12
to
On 11/10/2012 6:24 PM, Cecil Westerhof wrote:
> 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

Please visit my GURU's LAIR web site at http://www.tinaja.com

Cecil Westerhof

unread,
Nov 13, 2012, 1:31:13 AM11/13/12
to
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.

luser.droog

unread,
Nov 16, 2012, 7:19:15 PM11/16/12
to
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).

HTH

Cecil Westerhof

unread,
Nov 17, 2012, 8:39:00 AM11/17/12
to
Op zaterdag 17 nov 2012 01:19 CET schreef luser droog:

> 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. ;-}
0 new messages