finally working

This commit is contained in:
Christopher Jeffrey 2013-02-17 13:25:58 -06:00
parent fe2d881a08
commit 80b330a2be
2 changed files with 51 additions and 30 deletions

View File

@ -5,4 +5,5 @@ tput.colors();
console.log(tput.info);
tput._compile('%?%p9%t\u001b(0%e\u001b(B%;\u001b[0%?%p6%t;1%;%?%p2%t;4%;%?%p1%p3%|%t;7%;%?%p4%t;5%;%?%p7%t;8%;m');
tput.compile();
//tput._compile('%?%p9%t\u001b(0%e\u001b(B%;\u001b[0%?%p6%t;1%;%?%p2%t;4%;%?%p1%p3%|%t;7%;%?%p4%t;5%;%?%p7%t;8%;m');

View File

@ -261,18 +261,19 @@ Tput.prototype.compile = function(key) {
this.info.all = {};
Object.keys(info.bools).forEach(function(key) {
info.all[key] = info.bools;
info.all[key] = info.bools[key];
});
Object.keys(info.numbers).forEach(function(key) {
info.all[key] = info.numbers;
info.all[key] = info.numbers[key];
});
Object.keys(info.strings).forEach(function(key) {
info.all[key] = info.strings;
info.all[key] = info.strings[key];
});
Object.keys(info.all).forEach(function(key) {
console.log('Compiling key: ' + JSON.stringify(info.all[key]));
self.methods[key] = self._compile(info.all[key]);
});
};
@ -314,7 +315,7 @@ Tput.prototype._compile = function(val) {
if (buff) {
code += 'o("';
code += buff.replace(/([\r\n"])/g, '\\$1');
code += '")';
code += '"),';
buff = '';
}
}
@ -326,7 +327,7 @@ Tput.prototype._compile = function(val) {
val = val.substring(cap[0].length);
code += 'o("';
code += '\x1b';
code += '")';
code += '"),';
continue;
}
@ -437,7 +438,7 @@ Tput.prototype._compile = function(val) {
code += '\x7f';
break;
}
code += '")';
code += '"),';
continue;
}
@ -474,7 +475,7 @@ Tput.prototype._compile = function(val) {
//return '\0';
return '\200';
}
code += '")';
code += '"),';
continue;
}
@ -485,7 +486,7 @@ Tput.prototype._compile = function(val) {
ch = cap[1];
code += 'o("';
code += String.fromCharCode(parseInt(ch, 8));
code += '")';
code += '"),';
continue;
}
@ -496,7 +497,7 @@ Tput.prototype._compile = function(val) {
ch = cap[1];
code += 'o("';
code += Array(+ch + 1).join(' '); // "padding" characters?
code += '")';
code += '"),';
continue;
}
@ -507,7 +508,7 @@ Tput.prototype._compile = function(val) {
val = val.substring(cap[0].length);
code += 'o("';
code += '%';
code += '")';
code += '"),';
continue;
}
@ -520,7 +521,7 @@ Tput.prototype._compile = function(val) {
val = val.substring(cap[0].length);
code += 'o("';
code += 'TODO';
code += '")';
code += '"),';
continue;
}
@ -530,7 +531,7 @@ Tput.prototype._compile = function(val) {
val = val.substring(cap[0].length);
code += 'o(';
code += 'stack.pop()'; // TODO: FORMAT
code += ')';
code += '),';
continue;
}
@ -541,7 +542,7 @@ Tput.prototype._compile = function(val) {
val = val.substring(cap[0].length);
code += 'o(';
code += 'stack.pop()'; // TODO: FORMAT
code += ')';
code += '),';
continue;
}
@ -551,7 +552,7 @@ Tput.prototype._compile = function(val) {
val = val.substring(cap[0].length);
code += 'o(';
code += 'stack.pop()'; // TODO: FORMAT
code += ')';
code += '),';
continue;
}
@ -562,7 +563,9 @@ Tput.prototype._compile = function(val) {
val = val.substring(cap[0].length);
i = cap[1];
//code += 'o(';
code += 'params[' + (i - 1) + ']';
//code += 'params[' + (i - 1) + ']';
//code += '(stack.push(v = params[' + (i - 1) + ']), v)';
code += '(stack.push(v = params[' + (i - 1) + ']), v),';
//code += ')';
continue;
}
@ -573,7 +576,7 @@ Tput.prototype._compile = function(val) {
b();
val = val.substring(cap[0].length);
v = cap[1];
code += 'dyn.' + v + ' = stack.pop()';
code += 'dyn.' + v + ' = stack.pop(),';
continue;
}
@ -583,7 +586,7 @@ Tput.prototype._compile = function(val) {
b();
val = val.substring(cap[0].length);
v = cap[1];
code += '(stack.push(dyn.' + v + '), dyn.' + v + ')';
code += '(stack.push(dyn.' + v + '), dyn.' + v + '),';
continue;
}
@ -593,7 +596,7 @@ Tput.prototype._compile = function(val) {
b();
val = val.substring(cap[0].length);
v = cap[1];
code += 'stat.' + v + ' = stack.pop()';
code += 'stat.' + v + ' = stack.pop(),';
continue;
}
@ -610,7 +613,7 @@ Tput.prototype._compile = function(val) {
b();
val = val.substring(cap[0].length);
v = cap[1];
code += '(stack.push(v = stat.' + v + '), v)';
code += '(stack.push(v = stat.' + v + '), v),';
continue;
}
@ -619,7 +622,7 @@ Tput.prototype._compile = function(val) {
b();
val = val.substring(cap[0].length);
ch = cap[1];
code += '"' + ch + '"';
code += '(stack.push(v = "' + ch + '", v),';
continue;
}
@ -629,7 +632,7 @@ Tput.prototype._compile = function(val) {
b();
val = val.substring(cap[0].length);
ch = cap[1];
code += '(' + ch + ')';
code += '(stack.push(v = ' + ch + '), v),';
continue;
}
@ -637,7 +640,7 @@ Tput.prototype._compile = function(val) {
if (cap = /^%l/g.exec(val)) {
b();
val = val.substring(cap[0].length);
code += '(stack.push(v = stack.pop().length), v)';
code += '(stack.push(v = stack.pop().length), v),';
continue;
}
@ -653,7 +656,7 @@ Tput.prototype._compile = function(val) {
op = cap[1];
if (op === '=') op = '===';
else if (op === 'm') op = '%';
code += '(stack.push(v = stack.pop() ' + op + ' stack.pop()), v)';
code += '(stack.push(v = (stack.pop() ' + op + ' stack.pop())), v),';
continue;
}
@ -673,7 +676,7 @@ Tput.prototype._compile = function(val) {
b();
val = val.substring(cap[0].length);
op = cap[1];
code += '(stack.push(v = ' + op + 'stack.pop()), v)';
code += '(stack.push(v = ' + op + 'stack.pop()), v),';
continue;
}
@ -681,7 +684,7 @@ Tput.prototype._compile = function(val) {
if (cap = /^%i/g.exec(val)) {
b();
val = val.substring(cap[0].length);
code += '(params[0]++, params[1]++)';
code += '(params[0]++, params[1]++),';
continue;
}
@ -704,7 +707,8 @@ Tput.prototype._compile = function(val) {
b();
val = val.substring(cap[0].length);
//code += '"); if (';
code += 'if (';
if (code[code.length-1] === ',') code = code.slice(0, -1);
code += ';if (';
continue;
}
@ -712,6 +716,7 @@ Tput.prototype._compile = function(val) {
b();
val = val.substring(cap[0].length);
//code += ') { out.push("';
if (code[code.length-1] === ',') code = code.slice(0, -1);
code += ') {';
continue;
}
@ -720,7 +725,16 @@ Tput.prototype._compile = function(val) {
b();
val = val.substring(cap[0].length);
//code += '"); } else { out.push("';
code += '} else {';
if (code[code.length-1] === ',') code = code.slice(0, -1);
var end = val.indexOf('%;');
var els = val.indexOf('%e');
var then = val.indexOf('%t');
// does else if's like this: %?[expr]%t...%e[expr]%t...%;
if (then < end && then < els) {
code += '} else if (';
} else {
code += '} else {';
}
continue;
}
@ -728,6 +742,7 @@ Tput.prototype._compile = function(val) {
b();
val = val.substring(cap[0].length);
//code += '"); } out.push("';
if (code[code.length-1] === ',') code = code.slice(0, -1);
code += '}';
continue;
}
@ -764,14 +779,19 @@ Tput.prototype._compile = function(val) {
val = val.substring(1);
}
code += 'return out.join("");';
b();
if (code[code.length-1] === ',') code = code.slice(0, -1);
//code = code.replace(/(;|})/g, '\n$1\n');
code += ';return out.join("");';
console.log(code.replace(/\x1b/g, '\\x1b'));
try {
return new Function('params', code);
} catch (e) {
console.log(e.message);
//console.log(JSON.stringify(e.stack).replace(/\\n/g, '\n'));
console.log(e.stack.replace(/\x1b/g, '\\x1b'));
}
};