comparison cbackend.js @ 38:e7be612fd3ae

Very basic array support
author Mike Pavone <pavone@retrodev.com>
date Tue, 10 Jul 2012 23:09:44 -0700
parents a6bf4869fcbe
children a997e42b9051
comparison
equal deleted inserted replaced
37:a6bf4869fcbe 38:e7be612fd3ae
106 strlit.prototype.toC = function() { 106 strlit.prototype.toC = function() {
107 return 'make_str("' + this.val.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r') + '")'; 107 return 'make_str("' + this.val.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r') + '")';
108 } 108 }
109 109
110 listlit.prototype.toC = function() { 110 listlit.prototype.toC = function() {
111 var ret = 'make_list('; 111 var ret = 'make_list(' + this.val.length + ', ';
112 each(this.val, function(idx, el) { 112 for (var i = 0; i < this.val.length; i++) {
113 ret += (idx ? ', ' : '') + el.toC(); 113 ret += (i ? ', ' : '') + this.val[i].toC();
114 }); 114 }
115 return ret + ')';
116 }
117
118 arraylit.prototype.toC = function() {
119 var ret = 'make_array(' + this.val.length + ', ';
120 for (var i = 0; i < this.val.length; i++) {
121 ret += (i ? ', ' : '') + this.val[i].toC();
122 }
115 return ret + ')'; 123 return ret + ')';
116 } 124 }
117 125
118 funcall.prototype.toC = function() { 126 funcall.prototype.toC = function() {
119 var name = this.name[this.name.length-1] == ':' ? this.name.substr(0, this.name.length-1) : this.name; 127 var name = this.name[this.name.length-1] == ':' ? this.name.substr(0, this.name.length-1) : this.name;
339 addCompOp(int32, 'GT_', '>', 'obj_int32'); 347 addCompOp(int32, 'GT_', '>', 'obj_int32');
340 addCompOp(int32, 'EQ_', '==', 'obj_int32'); 348 addCompOp(int32, 'EQ_', '==', 'obj_int32');
341 addCompOp(int32, 'NEQ_', '!=', 'obj_int32'); 349 addCompOp(int32, 'NEQ_', '!=', 'obj_int32');
342 addCompOp(int32, 'GEQ_', '>=', 'obj_int32'); 350 addCompOp(int32, 'GEQ_', '>=', 'obj_int32');
343 addCompOp(int32, 'LEQ_', '<=', 'obj_int32'); 351 addCompOp(int32, 'LEQ_', '<=', 'obj_int32');
352 var array = new cObject('array');
353 array.addProperty('size', null, 'uint32_t');
354 array.addProperty('storage', null, 'uint32_t');
355 array.addProperty('data', null, 'object **');
356 array.addMessage('get', {
357 vars: {index: 'obj_int32 *'},
358 lines: [
359 'index = va_arg(args, obj_int32 *);',
360 'if (index->num >= 0 && index->num < self->size) {',
361 ' return self->data[index->num];',
362 '}',
363 'return mcall(METHOD_ID_FALSE, 1, main_module);'
364 ]
365 });
366 array.addMessage('set', {
367 vars: {index: 'obj_int32 *'},
368 lines: [
369 'index = va_arg(args, obj_int32 *);',
370 'if (index->num >= 0 && index->num < self->size) {',
371 ' self->data[index->num] = va_arg(args, object *);',
372 '}',
373 'return (object *)self;'
374 ]
375 });
344 forwarddec = toplevelcode = ''; 376 forwarddec = toplevelcode = '';
345 forwarddec += int32.toEarlyCDef(); 377 forwarddec += int32.toEarlyCDef() + array.toEarlyCDef();
346 toplevelcode += int32.toCDef(); 378 toplevelcode += int32.toCDef() + array.toCDef();
347 obj.populateSymbols(toplevel); 379 obj.populateSymbols(toplevel);
348 var rest = 'object * mainModule() {\n\tmain_module = ' + obj.toC() + ';\n\treturn main_module;\n}\n'; 380 var rest = 'object * mainModule() {\n\tmain_module = ' + obj.toC() + ';\n\treturn main_module;\n}\n';
349 return '#include "runtime/proghead.inc"\n' + 381 return '#include "runtime/proghead.inc"\n' +
350 '#define METHOD_ID_MAIN ' + getMethodId('main') + '\n' + 382 '#define METHOD_ID_MAIN ' + getMethodId('main') + '\n' +
351 '#define METHOD_ID_TRUE ' + getMethodId('true') + '\n' + 383 '#define METHOD_ID_TRUE ' + getMethodId('true') + '\n' +
438 } 470 }
439 471
440 assignment.prototype.toC = function() { 472 assignment.prototype.toC = function() {
441 var existing = this.symbols.find(this.symbol.name); 473 var existing = this.symbols.find(this.symbol.name);
442 var prefix = ''; 474 var prefix = '';
443 if (!existing) {
444 prefix = 'object * ';
445 }
446 var val = this.expression.toC(); 475 var val = this.expression.toC();
447 if (val === null) { 476 if (val === null) {
448 return null; 477 return null;
478 }
479 if (existing.type == 'local' && !existing.isdeclared) {
480 prefix = 'object *';
481 this.symbols.declareVar(this.symbol.name);
449 } 482 }
450 return prefix + this.symbol.toC() + ' = ' + val; 483 return prefix + this.symbol.toC() + ' = ' + val;
451 }; 484 };
452 assignment.prototype.toCObject = function(cobj) { 485 assignment.prototype.toCObject = function(cobj) {
453 if (this.expression.toCObject) { 486 if (this.expression.toCObject) {