comparison cbackend.js @ 140:bf8f75b69048

Minor progress towards supporting imports in the C backend
author Mike Pavone <pavone@retrodev.com>
date Fri, 09 Aug 2013 01:35:29 -0700
parents 9820ecd4eed4
children 833624457b81
comparison
equal deleted inserted replaced
139:9bce890a7ff2 140:bf8f75b69048
418 } 418 }
419 } 419 }
420 420
421 cObject.prototype.addInit = function(statement) { 421 cObject.prototype.addInit = function(statement) {
422 this.init.push(statement); 422 this.init.push(statement);
423 } 423 };
424
425 cObject.prototype.addImport = function(symbols, source) {
426 this.imported.push(source);
427 var importNum = imported.length - 1;
428 if (symbols) {
429 each(symbols, function(i, sym) {
430 this.addMessage(sym.name, {
431 vars: {},
432 lines: [
433 'return self->import_' + importNum + '->meta->meth_lookup[method_id & 0xF](method_id, num_args, self->import_' + importNum + ', args);'
434 ]
435 });
436 });
437 } else {
438 //TODO: handle proxying for unimplemented methods
439 }
440 };
424 441
425 cObject.prototype.checkInitMsg = function() { 442 cObject.prototype.checkInitMsg = function() {
426 if (!this.initmsgadded && this.init.length) { 443 if (!this.initmsgadded && this.init.length) {
427 var init = this.init.slice(0, this.init.length); 444 var init = this.init.slice(0, this.init.length);
428 init.push('return (object *)self;'); 445 init.push('return (object *)self;');
510 var nextobject = 0; 527 var nextobject = 0;
511 528
512 529
513 object.prototype.toCObject = function() { 530 object.prototype.toCObject = function() {
514 var messages = this.messages; 531 var messages = this.messages;
515 var values = [];
516 var imports = [];
517 if (!this.name) { 532 if (!this.name) {
518 this.name = 'object_' + nextobject++; 533 this.name = 'object_' + nextobject++;
519 } 534 }
520 var me = new cObject(this.name); 535 var me = new cObject(this.name);
521 this.symbols.typename = me.name; 536 this.symbols.typename = me.name;
527 me.parent = '(object *)(' + (new symbol('self', this.symbols.parent)).toC() + ')'; 542 me.parent = '(object *)(' + (new symbol('self', this.symbols.parent)).toC() + ')';
528 } 543 }
529 for (var i in messages) { 544 for (var i in messages) {
530 if (messages[i] instanceof funcall) { 545 if (messages[i] instanceof funcall) {
531 if (messages[i].name == 'import:' && messages[i].args.length == 1) { 546 if (messages[i].name == 'import:' && messages[i].args.length == 1) {
532 imports.push({symbols: false, src: messages[i].args[0]}); 547 me.addImport(false, messages[i].args[0]);
533 } else if(messages[i].name == 'import:from:' && messages[i].args.length == 2) { 548 } else if(messages[i].name == 'import:from:' && messages[i].args.length == 2) {
534 var importsyms = []; 549 var importsyms = [];
535 each(messages[i].args[0].val, function(i, el) { 550 each(messages[i].args[0].val, function(i, el) {
536 if (!(el instanceof symbol)) { 551 if (!(el instanceof symbol)) {
537 throw new Error('Names in import:from statement must be symbols'); 552 throw new Error('Names in import:from statement must be symbols');
538 } 553 }
539 importsyms.push(new strlit(el.name)); 554 importsyms.push(el);
540 }); 555 });
541 imports.push({symbols: new listlit(importsyms), src: messages[i].args[1]}); 556 me.addImport(importsyms, messages[i].args[1]);
542 } else if(messages[i].name == 'llProperty:withType:' && messages[i].args.length == 2) { 557 } else if(messages[i].name == 'llProperty:withType:' && messages[i].args.length == 2) {
543 me.addProperty(messages[i].args[0].name, null, messages[i].args[1].toCTypeName()) 558 me.addProperty(messages[i].args[0].name, null, messages[i].args[1].toCTypeName())
544 } else if(messages[i].name == 'llMessage:withVars:andCode:' && messages[i].args.length == 3) { 559 } else if(messages[i].name == 'llMessage:withVars:andCode:' && messages[i].args.length == 3) {
545 var msgname = messages[i].args[0].name 560 var msgname = messages[i].args[0].name
546 var rawvars = messages[i].args[1].expressions; 561 var rawvars = messages[i].args[1].expressions;