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

"Richard Cartwright" <news@chunk.com.au>
20 Aug 2003 01:33:30 -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 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: "Richard Cartwright" <news@chunk.com.au>
Newsgroups: comp.compilers
Date: 20 Aug 2003 01:33:30 -0400
Organization: Compilers Central
Keywords: design, OOP, comment
Posted-Date: 20 Aug 2003 01:33:30 EDT

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?


Thanks,
Richard Cartwright
[Write your parser with generic expression rules like this:


expr: expr OP5 expr /* precedence 5 operator */


Then when you define a new operator at level 5, have the lexer return
an OP5 for it with semantic info so the parser can figure out which
operator it was. I'm pretty sure this trick was used in extensible
languages before 1980. -John]


Post a followup to this message

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