# HG changeset patch # User Mike Pavone # Date 1342069058 25200 # Node ID 0558dad9d061ce23f12a3cb06c1ff31f9e1bc1a2 # Parent 927fd7911a016ce5c6448568980875f6509bc57f Add basic string support diff -r 927fd7911a01 -r 0558dad9d061 cbackend.js --- a/cbackend.js Wed Jul 11 19:17:24 2012 -0700 +++ b/cbackend.js Wed Jul 11 21:57:38 2012 -0700 @@ -103,8 +103,16 @@ return 'make_float(' + this.val.toString() + ')'; } +var declaredStrings = {}; +var nextStringId = 0; + strlit.prototype.toC = function() { - return 'make_str("' + this.val.replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n').replace('\r', '\\r') + '")'; + if (!(this.val in declaredStrings)) { + //TODO: get the proper byte length + 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'; + declaredStrings[this.val] = nextStringId++; + } + return '((object *)&str_' + declaredStrings[this.val] + ')'; } listlit.prototype.toC = function() { @@ -401,9 +409,39 @@ 'return self;' ] }); + var string = new cObject('string'); + string.addProperty('length', null, 'uint32_t'); + string.addProperty('bytes', null, 'uint32_t'); + string.addProperty('storage', null, 'uint32_t'); + string.addProperty('data', null, 'char *'); + string.addMessage('length', { + vars: {intret: 'obj_int32 *'}, + lines: [ + 'intret = (obj_int32 *)make_object(&obj_int32_meta, NULL, 0);', + 'intret->num = self->length;', + 'return &(intret->header);' + ] + }); + string.addMessage('EQ_', { + vars: {argb: 'string *'}, + lines: [ + 'argb = va_arg(args, string *);', + 'if (self->length == argb->length && self->bytes == argb->bytes && !memcmp(self->data, argb->data, self->bytes)) {', + ' return mcall(METHOD_ID_TRUE, 1, main_module);', + '}', + 'return mcall(METHOD_ID_FALSE, 1, main_module);' + ] + }); + string.addMessage('print', { + vars: {}, + lines: [ + 'fwrite(self->data, 1, self->bytes, stdout);', + 'return self;' + ] + }); forwarddec = toplevelcode = ''; - forwarddec += int32.toEarlyCDef() + array.toEarlyCDef(); - toplevelcode += int32.toCDef() + array.toCDef(); + forwarddec += int32.toEarlyCDef() + array.toEarlyCDef() + string.toEarlyCDef(); + toplevelcode += int32.toCDef() + array.toCDef() + string.toCDef(); obj.populateSymbols(toplevel); var rest = 'object * mainModule() {\n\tmain_module = ' + obj.toC() + ';\n\treturn main_module;\n}\n'; return '#include "runtime/proghead.inc"\n' + diff -r 927fd7911a01 -r 0558dad9d061 samples/hello Binary file samples/hello has changed