Re: Implementing an "in" operator

"Charles Bryant" <n502640813.ch@chch.demon.co.uk>
20 Nov 2002 15:19:24 -0500

          From comp.compilers

Related articles
Implementing an "in" operator ed_davis2@yahoo.com (Ed Davis) (2002-11-13)
Re: Implementing an "in" operator n502640813.ch@chch.demon.co.uk (Charles Bryant) (2002-11-20)
Re: Implementing an "in" operator sos@zjod.net (Steve Siegfried) (2002-11-24)
Re: Implementing an "in" operator ed_davis2@yahoo.com (Ed Davis) (2002-11-26)
| List of all articles for this month |
From: "Charles Bryant" <n502640813.ch@chch.demon.co.uk>
Newsgroups: comp.compilers
Date: 20 Nov 2002 15:19:24 -0500
Organization: Compilers Central
References: 02-11-079
Keywords: design
Posted-Date: 20 Nov 2002 15:19:24 EST

Ed Davis <ed_davis2@yahoo.com> wrote:
>Any ideas on how one would implement an "in" operator?
>
>As in (no pun intended):
>
> if (a in b,c,d..e)
> statements;
>
>translates to:
>
> if (a == b or a == c or (a >= d and a <=e))
> statements;


I'd be inclined to make 'b,c,d..e' be a literal of some appripriate
type and make 'in' a binary operator taking an object of type T as its
left operand and an object of type 'list of T', 'set of T' or suchlike
as it right operanad. This would also allow constructs like:


x = <nasty complex range specification>
if (a in x) {
...
} else if (b in x) {
...
}


making it a lot clearer that the same list of ranges is used twice.


Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.