// Convert analyzed data to javascript. Everything has already been calculated
// before this stage, which just does the final conversion to JavaScript.
// Main function
function JSify(data, functionsOnly, givenFunctions, givenGlobalVariables) {
// Does simple 'macro' substitution, using Django-like syntax,
// {{{ code }}} will be replaced with |eval(code)|.
function processMacros(text) {
return text.replace(/{{{[^}]+}}}/g, function(str) {
str = str.substr(3, str.length-6);
return eval(str).toString();
});
}
substrate = new Substrate('JSifyer');
var GLOBAL_VARIABLES = functionsOnly ? givenGlobalVariables : data.globalVariables;
var FUNCTIONS = functionsOnly ? givenFunctions : {};
// Now that analysis has completed, we can get around to handling unparsedFunctions
(functionsOnly ? data.functions : data.unparsedFunctions.concat(data.functions)).forEach(function(func) {
// Save just what we need, to save memory - whether there are varargs, and the # of parameters
FUNCTIONS[func.ident] = {
hasVarArgs: func.hasVarArgs,
numParams: func.params.length
};
});
for (var i = 0; i < data.unparsedFunctions.length; i++) {
var func = data.unparsedFunctions[i];
dprint('unparsedFunctions', '====================\n// Processing |' + func.ident + '|, ' + i + '/' + data.unparsedFunctions.length);
//var t = Date.now();
func.JS = JSify(analyzer(intertyper(func.lines, true, func.lineNum-1)), true, FUNCTIONS, GLOBAL_VARIABLES);
//t = (Date.now()-t)/1000;
//dprint('unparsedFunctions', 'unparsedFunction took ' + t + ' seconds.');
delete func.lines; // clean up memory as much as possible
}
// Load library
// TODO: optimize this so it isn't done over and over for each unparsedFunction
for (suffix in set('', '_sdl', '_gl')) {
eval(processMacros(preprocess(read('library' + suffix + '.js'), CONSTANTS)));
}
// Actors
// type
substrate.addActor('Type', {
processItem: function(item) {
var type = Types.types[item.name_];
var niceName = toNiceIdent(item.name_);
// We might export all of Types.types, cleaner that way, but do not want slowdowns in accessing flatteners
item.JS = 'var ' + niceName + '___SIZE = ' + Types.types[item.name_].flatSize + '; // ' + item.name_ + '\n';
if (type.needsFlattening && !type.flatFactor) {
item.JS += 'var ' + niceName + '___FLATTENER = ' + JSON.stringify(Types.types[item.name_].flatIndexes) + ';';
}
return [item];
}
});
function makePointer(slab, pos, allocator, type) { // type is FFU
if (slab in set('HEAP', 'IHEAP', 'FHEAP')) return pos;
return 'Pointer_make(' + slab + ', ' + (pos ? pos : 0) + (allocator ? ', ' +