tcg
Version:
The Code Generator
72 lines (65 loc) • 2.82 kB
JavaScript
;
var CGenerator = require(__dirname + '/../lib/CGenerator.js');
var util = require('util');
var suffix = ".h";
module.exports = function() {
};
util.inherits(module.exports, CGenerator);
module.exports.prototype.suffix = suffix;
module.exports.prototype.render = function(jo) {
var last_dt = null;
var str = "";
str += "/*********************************************************************************\n" +
"** This file is generated by program, **\n" +
"** Please don't change it directly. **\n" +
"**********************************************************************************/\n";
str += util.format("#ifndef _H_%s\n", this.getFileTag(jo.file));
str += util.format("#define _H_%s\n", this.getFileTag(jo.file));
str += "\n";
str += "#include <stdbool.h>\n";
str += "#include <stdint.h>\n";
str += "\n\n";
for(var d of jo.definitions) {
if((last_dt != null) && (d.dt != last_dt)) {
str += "\n\n";
}
switch (d.dt) {
case "include":
str += '#include \"' + d.file + suffix + '\"\n';
break;
case "const":
str += util.format('#define %s %s\n', d.name, this.getValue(d.value));
break;
case "typedef":
str += util.format('typedef %s %s;\n', this.getType(d.type), d.name);
break;
case "enum":
str += util.format("typedef enum %s_e {\n", d.name);
for(var e of d.entities) {
str += util.format(" %s = %s,%s\n", e.name, this.getValue(e.value), e.comment?'//'+e.comment:"");
}
str += util.format("}%s_t;\n\n", d.name);
break;
case "struct":
str += util.format("typedef struct %s_s {\n", d.name);
for(var e of d.entities) {
str += util.format(" %s %s%s;%s\n"
, this.getType(e.type), e.name, e.repeat?'['+e.repeat+']':"", e.comment?'//'+e.comment:"");
}
str += util.format("}%s_t;\n\n", d.name);
break;
case "union":
str += util.format("typedef union %s_u {\n", d.name);
for(var e of d.entities) {
str += util.format(" %s %s%s;%s\n"
, this.getType(e.type), e.name, e.repeat?'['+e.repeat+']':"", e.comment?'//'+e.comment:"");
}
str += util.format("}%s_t;\n\n", d.name);
break;
}
last_dt = d.dt;
}
str += "\n";
str += util.format("#endif// _H_%s\n", this.getFileTag(jo.file));
return str;
};