Operator overloading

Stefano Corsi <[email protected]> Sun, 16 Mar 2003 12:36:00 +0000
Newsgroups gmane.comp.lang.moto.devel
Organization Moto Project
Message-ID <[email protected]>
Dave,

as I told you I take the task of implementing operator overloading.
Here are my initial notes.  Let me know if I'm on the right path.

Goal
--------------

Operator overloading should be provided at the moment for externally defined 
objects. We could use a C like syntax in mxc .i files. Something like:

Object operator SymbolTable::[] (String) =>
	void *stab_get(SymbolTable *this, char *key);

where substantially the operator is defined like a method or function with the 
addition of the operator keyword. (Do we need the keyword? I suppose yes. 
Otherwise, could we distinguish methods from operators only by the character 
set used in the name definition?)

For the time being it will be possible to overload ONLY a small set of 
operators. We will define a set of rules in mx.y and mx.l so that the 
programmer will get an error message if he tries to overload operators that 
at the present cannot be overloaded.

[ Question: will it be possible in the future to let the programmer overload 
whatever operator and even new ones (those that are not actually handled by 
moto)? What changes would be needed in moto.y and moto.l? ]

We want to let programmers overload:

	+ (arg1, arg2)
	+= (arg1, arg2)
	- (arg1, arg2) 
	-= (arg1, arg2)
	++ (arg)
	-- (arg)
	[] (arg)		get 
	[] (arg1, arg2)	set

Changes needed
-------------------

- Add new syntax for operator in mx.y.
- Modify mxcg.c and other mxc files to handle new syntax and rules.
- Modify motov.c, motoi.c, motoc.c to handle overloading in 
	
	motoX_domath();
	motoX_lval 
	motoX_rval	

- Add tests

Mx.y syntax changes
------------------------

1) Add a new terminal "operator"
2) Add new non-terminal:
	- m_operator_name (the name of the operator could be restricted at the flex 
level to only the operators that we allow to overload??)
3) Modify non-terminal
	- m_function_declaration
	- m_function_declarator
to include also the case when the operator keyword is used. 

Other mxc changes
---------------------

Overloaded operators could be implemented as functions or methods that take 
operands as parameters and return a value. 
So we modify the code for M_FUNCTION_DECLARATION and M_FUNCTION_DECLARATOR 
case labels in mxcg.c to set a proper function name in case the function or 
method is an operator. Characters as "+ - += -= []" etc... should not be part 
of a function name, so we have to find a sustitute name for them (plus, 
minus, pluseq, etc...) We should modify also mfn_symbolName mfn.c for 
handling operator functions and methods.

MotoX changes
------------------

In motoX.c we have to discover, for any single case, if there's an operator 
for the object that handles the specific operation. How could we do it? 

I have found this piece of code in motoi_new:

/* Fill in the types array for constructor identification */
motoi_fillArgs(argc,args,argv,types);

/* Generate the Moto name for this Constructor */
fname = moto_createMotonameForFn(classname,classname);

/* Retrieve the MXFN record */
code = ftab_cacheGet(env->ftable, &mfn, fname, argc, types);
free(fname);

We could create a function get_operator or similar that handles all this:

MFN * get_operator(char * oper_name, MotoType * op1, MotoType * op2);

This function should return a pointer to a operator function or NULL if no 
operator is defined for those operands.

... to be continued ...