Mercurial > repos > tabletprog
diff cbackend.js @ 122:9820ecd4eed4
Add support for implementing operators on user defined objects
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 03 Aug 2013 00:13:09 -0700 |
parents | 1a4446f573d3 |
children | 34e0befbdd77 bf8f75b69048 |
line wrap: on
line diff
--- a/cbackend.js Thu Aug 01 19:10:48 2013 -0700 +++ b/cbackend.js Sat Aug 03 00:13:09 2013 -0700 @@ -12,10 +12,19 @@ return methodIds[methodName]; } -op.prototype.toC = function(isReceiver) { +function getOpMethodName(opname) +{ var optoMeth = {'+': 'ADD_', '-': 'SUB_', '*': 'MUL_', '/': 'DIV_', '%': 'MOD_', '=': 'EQ_', '!=': 'NEQ_', '<': 'LT_', '>': 'GT_', '>=': 'GEQ_', '<=': 'LEQ_', '.': 'CAT_', '&&':'if', '||':'ifnot'}; - var method = optoMeth[this.op]; - return 'mcall(' + getMethodId(method) + '/* ' + method + ' */, 2, (object *)' + this.left.toC() + ', ' + this.right.toC() + ')\n'; + if (opname in optoMeth) { + return optoMeth[opname]; + } else { + return opname; + } +} + +op.prototype.toC = function(isReceiver) { + var method = getOpMethodName(this.op); + return 'mcall(' + getMethodId(method) + '/* operator ' + method + ' */, 2, (object *)' + this.left.toC() + ', ' + this.right.toC() + ')\n'; }; op.prototype.toCLLExpr = function(vars) { var opmap = {'=': '==', 'xor': '^'}; @@ -1005,7 +1014,7 @@ paramget += escaped + ' = va_arg(args, object *); '; } } - cobj.addMessage(this.symbol.name, { + cobj.addMessage(getOpMethodName(this.symbol.name), { vars: messagevars, lines: [paramget + 'return ' + val + '(' + (cobj.hasenv ? 'self->env' : 'NULL') + ', ' + params.length + (params.length ? ', ' : '') + params.join(', ') + ');'] });