comparison cbackend.js @ 41:0558dad9d061

Add basic string support
author Mike Pavone <pavone@retrodev.com>
date Wed, 11 Jul 2012 21:57:38 -0700
parents 927fd7911a01
children 4e983fe32047
comparison
equal deleted inserted replaced
40:927fd7911a01 41:0558dad9d061
101 101
102 floatlit.prototype.toC = function() { 102 floatlit.prototype.toC = function() {
103 return 'make_float(' + this.val.toString() + ')'; 103 return 'make_float(' + this.val.toString() + ')';
104 } 104 }
105 105
106 var declaredStrings = {};
107 var nextStringId = 0;
108
106 strlit.prototype.toC = function() { 109 strlit.prototype.toC = function() {
107 return 'make_str("' + this.val.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r') + '")'; 110 if (!(this.val in declaredStrings)) {
111 //TODO: get the proper byte length
112 toplevelcode += 'string str_' + nextStringId + ' = {{&string_meta, NULL}, ' + this.val.length + ', ' + this.val.length + ', ' + this.val.length + ', "' + this.val.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r') + '"};\n';
113 declaredStrings[this.val] = nextStringId++;
114 }
115 return '((object *)&str_' + declaredStrings[this.val] + ')';
108 } 116 }
109 117
110 listlit.prototype.toC = function() { 118 listlit.prototype.toC = function() {
111 var ret = 'make_list(' + this.val.length; 119 var ret = 'make_list(' + this.val.length;
112 for (var i = 0; i < this.val.length; i++) { 120 for (var i = 0; i < this.val.length; i++) {
399 '}', 407 '}',
400 'self->data[self->size++] = va_arg(args, object *);', 408 'self->data[self->size++] = va_arg(args, object *);',
401 'return self;' 409 'return self;'
402 ] 410 ]
403 }); 411 });
412 var string = new cObject('string');
413 string.addProperty('length', null, 'uint32_t');
414 string.addProperty('bytes', null, 'uint32_t');
415 string.addProperty('storage', null, 'uint32_t');
416 string.addProperty('data', null, 'char *');
417 string.addMessage('length', {
418 vars: {intret: 'obj_int32 *'},
419 lines: [
420 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);',
421 'intret->num = self->length;',
422 'return &(intret->header);'
423 ]
424 });
425 string.addMessage('EQ_', {
426 vars: {argb: 'string *'},
427 lines: [
428 'argb = va_arg(args, string *);',
429 'if (self->length == argb->length && self->bytes == argb->bytes && !memcmp(self->data, argb->data, self->bytes)) {',
430 ' return mcall(METHOD_ID_TRUE, 1, main_module);',
431 '}',
432 'return mcall(METHOD_ID_FALSE, 1, main_module);'
433 ]
434 });
435 string.addMessage('print', {
436 vars: {},
437 lines: [
438 'fwrite(self->data, 1, self->bytes, stdout);',
439 'return self;'
440 ]
441 });
404 forwarddec = toplevelcode = ''; 442 forwarddec = toplevelcode = '';
405 forwarddec += int32.toEarlyCDef() + array.toEarlyCDef(); 443 forwarddec += int32.toEarlyCDef() + array.toEarlyCDef() + string.toEarlyCDef();
406 toplevelcode += int32.toCDef() + array.toCDef(); 444 toplevelcode += int32.toCDef() + array.toCDef() + string.toCDef();
407 obj.populateSymbols(toplevel); 445 obj.populateSymbols(toplevel);
408 var rest = 'object * mainModule() {\n\tmain_module = ' + obj.toC() + ';\n\treturn main_module;\n}\n'; 446 var rest = 'object * mainModule() {\n\tmain_module = ' + obj.toC() + ';\n\treturn main_module;\n}\n';
409 return '#include "runtime/proghead.inc"\n' + 447 return '#include "runtime/proghead.inc"\n' +
410 '#define METHOD_ID_MAIN ' + getMethodId('main') + '\n' + 448 '#define METHOD_ID_MAIN ' + getMethodId('main') + '\n' +
411 '#define METHOD_ID_TRUE ' + getMethodId('true') + '\n' + 449 '#define METHOD_ID_TRUE ' + getMethodId('true') + '\n' +