Re: Designing a language in which a class can define (not just overload) operators

joshualevy@yahoo.com (Joshua Levy)
23 Aug 2003 23:14:22 -0400

          From comp.compilers

Related articles
Designing a language in which a class can define (not just overload) news@chunk.com.au (Richard Cartwright) (2003-08-20)
Re: Designing a language in which a class can define (not just overloa joshualevy@yahoo.com (2003-08-23)
Re: Designing a language in which a class can define (not just overloa tmk@netvision.net.il (2003-08-23)
Re: Designing a language in which a class can define (not just overloa rkrayhawk@aol.com (2003-09-04)
Re: Designing a language in which a class can define (not just overl toon@moene.indiv.nluug.nl (Toon Moene) (2003-09-04)
Re: Designing a language in which a class can define (not just overloa vidar@hokstad.name (2003-09-04)
Re: Designing a language in which a class can define (not just overloa nmm1@cus.cam.ac.uk (2003-09-09)
Re: Designing a language in which a class can define (not just overloa derkgwen@HotPOP.com (Derk Gwen) (2003-09-09)
| List of all articles for this month |
From: joshualevy@yahoo.com (Joshua Levy)
Newsgroups: comp.compilers
Date: 23 Aug 2003 23:14:22 -0400
Organization: http://groups.google.com/
References: 03-08-062
Keywords: design
Posted-Date: 23 Aug 2003 23:14:21 EDT

"Richard Cartwright" <news@chunk.com.au> wrote
> I have OO language design in my head in which it is possible for a
> class to define new operators. I'm thinking of something like this
> (adapted to C++-like syntax for purposes of illustration):
>
> class vector3
> {
> public:
> float operator "dot" (const vector3& v) const precedence 5;
> vector3 operator "cross" (const vector3& v) const precedence 5;
> };
>
> Which could then be used like this:
> vector3 a, b, c;
> float f;
> c = a cross b;
> f = a dot b;
>
> Since the grammar for this language is very much context sensitive,
> rather than context free, I can't use the traditional finite state
> machine approach of YACC or Bison to implement a compiler for this
> language.
>
> Could anyone point me toward any resources for implementing such a language?


I have a dim memory of a paper published in CACM in the 1984-1987 time
frame, which solved this problem by forcing new infix operators to
start and end with dots, while new prefix operators would end with a
dot and postfix operators would start with a dot.


In your example "cross" and "dot" would actually be .cross. and .dot.,
like this:
        c = a .cross. b;
        f = a .dot. b;


If you wanted to define a prefix operator which doubled its argument, it
would be called double., and would be used like this:
        x = double. 17 ;
If you wanted a posfix, round down operator, it could be this:
        i = 1.345 .roundedDown ;


They were able to build their parser using yacc and lex, as I remember.
The key is that the lexer can identify any new operator, including the
not-yet-defined ones.


Joshua Levy


Post a followup to this message

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