Mercurial > repos > tabletprog
diff 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 |
line wrap: on
line diff
--- a/cbackend.js Tue Jul 10 22:09:21 2012 -0700 +++ b/cbackend.js Tue Jul 10 23:09:44 2012 -0700 @@ -108,10 +108,18 @@ } listlit.prototype.toC = function() { - var ret = 'make_list('; - each(this.val, function(idx, el) { - ret += (idx ? ', ' : '') + el.toC(); - }); + var ret = 'make_list(' + this.val.length + ', '; + for (var i = 0; i < this.val.length; i++) { + ret += (i ? ', ' : '') + this.val[i].toC(); + } + return ret + ')'; +} + +arraylit.prototype.toC = function() { + var ret = 'make_array(' + this.val.length + ', '; + for (var i = 0; i < this.val.length; i++) { + ret += (i ? ', ' : '') + this.val[i].toC(); + } return ret + ')'; } @@ -341,9 +349,33 @@ addCompOp(int32, 'NEQ_', '!=', 'obj_int32'); addCompOp(int32, 'GEQ_', '>=', 'obj_int32'); addCompOp(int32, 'LEQ_', '<=', 'obj_int32'); + var array = new cObject('array'); + array.addProperty('size', null, 'uint32_t'); + array.addProperty('storage', null, 'uint32_t'); + array.addProperty('data', null, 'object **'); + array.addMessage('get', { + vars: {index: 'obj_int32 *'}, + lines: [ + 'index = va_arg(args, obj_int32 *);', + 'if (index->num >= 0 && index->num < self->size) {', + ' return self->data[index->num];', + '}', + 'return mcall(METHOD_ID_FALSE, 1, main_module);' + ] + }); + array.addMessage('set', { + vars: {index: 'obj_int32 *'}, + lines: [ + 'index = va_arg(args, obj_int32 *);', + 'if (index->num >= 0 && index->num < self->size) {', + ' self->data[index->num] = va_arg(args, object *);', + '}', + 'return (object *)self;' + ] + }); forwarddec = toplevelcode = ''; - forwarddec += int32.toEarlyCDef(); - toplevelcode += int32.toCDef(); + forwarddec += int32.toEarlyCDef() + array.toEarlyCDef(); + toplevelcode += int32.toCDef() + array.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' + @@ -440,13 +472,14 @@ assignment.prototype.toC = function() { var existing = this.symbols.find(this.symbol.name); var prefix = ''; - if (!existing) { - prefix = 'object * '; - } var val = this.expression.toC(); if (val === null) { return null; } + if (existing.type == 'local' && !existing.isdeclared) { + prefix = 'object *'; + this.symbols.declareVar(this.symbol.name); + } return prefix + this.symbol.toC() + ' = ' + val; }; assignment.prototype.toCObject = function(cobj) {