more cleanup

This commit is contained in:
Christopher Jeffrey 2013-02-18 14:43:53 -06:00
parent ef991d5a26
commit 29720f05d1
2 changed files with 32 additions and 31 deletions

View File

@ -6,4 +6,7 @@ tput.colors();
console.log(tput.info);
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');
//console.log(tput.methods.set_attributes([34]) + 'foo' + tput.methods.set_attributes([0]));

View File

@ -305,11 +305,12 @@ Tput.prototype._compile = function(val) {
code += 'o("';
code += buff.replace(/([\r\n"])/g, '\\$1');
code += '"),';
//code += 'o(' + JSON.stringify(buff) + '),';
buff = '';
}
}
function exec(regex) {
function read(regex) {
cap = regex.exec(val);
if (!cap) return;
val = val.substring(cap[0].length);
@ -320,7 +321,7 @@ Tput.prototype._compile = function(val) {
while (val) {
// '\e' -> ^[
if (exec(/^\\e/gi)) {
if (read(/^\\e/i)) {
code += 'o("';
code += '\\x1b';
code += '"),';
@ -328,7 +329,7 @@ Tput.prototype._compile = function(val) {
}
// '^A' -> ^A
if (exec(/^\^(.)/gi)) { // case-insensitive?
if (read(/^\^(.)/i)) { // case-insensitive?
code += 'o("';
switch (ch) {
case '@':
@ -438,7 +439,7 @@ Tput.prototype._compile = function(val) {
// '\n' -> \n
// '\r' -> \r
// '\0' -> \200 (special case)
if (exec(/^\\([nlrtbfs\^\\,:0])/g)) {
if (read(/^\\([nlrtbfs\^\\,:0])/)) {
code += 'o("';
switch (ch) {
case 'n':
@ -474,7 +475,7 @@ Tput.prototype._compile = function(val) {
}
// 3 octal digits -> character
if (exec(/^\\(\d\d\d)/g)) {
if (read(/^\\(\d\d\d)/)) {
code += 'o("';
code += '\\' + ch;
//code += String.fromCharCode(parseInt(ch, 8));
@ -483,7 +484,7 @@ Tput.prototype._compile = function(val) {
}
// $<5> -> padding
if (exec(/^\$<(\d+)>(\*|\/)/g)) {
if (read(/^\$<(\d+)>(\*|\/)/)) {
code += 'o("';
code += Array(+ch + 1).join(' '); // "padding" characters?
code += '"),';
@ -491,7 +492,7 @@ Tput.prototype._compile = function(val) {
}
// %% outputs `%'
if (exec(/^%%/g)) {
if (read(/^%%/)) {
code += 'o("';
code += '%';
code += '"),';
@ -502,7 +503,8 @@ Tput.prototype._compile = function(val) {
// as in printf, flags are [-+#] and space. Use a `:' to allow the
// next character to be a `-' flag, avoiding interpreting "%-" as an
// operator.
if (exec(/^%(?:(:)?([\-+# ]+))(?:(\d+)(\.\d+)?)?([doxXs])?/g)) {
// if (read(/^%(?:(:)?([\-+# ]+))(?:(\d+)(\.\d+)?)?([doxXs])?/)) {
if (read(/^%(:-|[+# ])(?:(\d+)(\.\d+)?)?([doxXs])?/)) {
code += 'o("';
code += 'TODO';
code += '"),';
@ -510,7 +512,7 @@ Tput.prototype._compile = function(val) {
}
// %c print pop() like %c in printf
if (exec(/^%c/g)) {
if (read(/^%c/)) {
code += 'o(';
code += 'stack.pop()'; // TODO: FORMAT
code += '),';
@ -519,7 +521,7 @@ Tput.prototype._compile = function(val) {
// %d print pop() like %d in printf
// NOT SURE ABOUT %d being print!
if (exec(/^%d/g)) {
if (read(/^%d/)) {
code += 'o(';
code += 'stack.pop()'; // TODO: FORMAT
code += '),';
@ -527,7 +529,7 @@ Tput.prototype._compile = function(val) {
}
// %s print pop() like %s in printf
if (exec(/^%s/g)) {
if (read(/^%s/)) {
code += 'o(';
code += 'stack.pop()'; // TODO: FORMAT
code += '),';
@ -536,32 +538,28 @@ Tput.prototype._compile = function(val) {
// %p[1-9]
// push i'th parameter
if (exec(/^%p([1-9])/g)) {
//code += 'o(';
//code += 'params[' + (i - 1) + ']';
//code += '(stack.push(v = params[' + (i - 1) + ']), v)';
if (read(/^%p([1-9])/)) {
code += '(stack.push(v = params[' + (i - 1) + ']), v),';
//code += ')';
continue;
}
// %P[a-z]
// set dynamic variable [a-z] to pop()
if (exec(/^%P([a-z])/g)) {
if (read(/^%P([a-z])/)) {
code += 'dyn.' + v + ' = stack.pop(),';
continue;
}
// %g[a-z]
// get dynamic variable [a-z] and push it
if (exec(/^%g([a-z])/g)) {
if (read(/^%g([a-z])/)) {
code += '(stack.push(dyn.' + v + '), dyn.' + v + '),';
continue;
}
// %P[A-Z]
// set static variable [a-z] to pop()
if (exec(/^%P([A-Z])/g)) {
if (read(/^%P([A-Z])/)) {
code += 'stat.' + v + ' = stack.pop(),';
continue;
}
@ -575,26 +573,26 @@ Tput.prototype._compile = function(val) {
// documented in other implementations. Relying on it will adversely
// impact portability to other implementations.
if (exec(/^%g([A-Z])/g)) {
if (read(/^%g([A-Z])/)) {
code += '(stack.push(v = stat.' + v + '), v),';
continue;
}
// %'c' char constant c
if (exec(/^%'(\w)'/g)) {
if (read(/^%'(\w)'/)) {
code += '(stack.push(v = "' + ch + '", v),';
continue;
}
// %{nn}
// integer constant nn
if (exec(/^%\{(\d+)\}/g)) {
if (read(/^%\{(\d+)\}/)) {
code += '(stack.push(v = ' + ch + '), v),';
continue;
}
// %l push strlen(pop)
if (exec(/^%l/g)) {
if (read(/^%l/)) {
code += '(stack.push(v = stack.pop().length), v),';
continue;
}
@ -605,7 +603,7 @@ Tput.prototype._compile = function(val) {
// bit operations (AND, OR and exclusive-OR): push(pop() op pop())
// %= %> %<
// logical operations: push(pop() op pop())
if (exec(/^%([+\-*\/m&|\^=><])/g)) {
if (read(/^%([+\-*\/m&|\^=><])/)) {
if (op === '=') op = '===';
else if (op === 'm') op = '%';
code += '(stack.push(v = (stack.pop() ' + op + ' stack.pop())), v),';
@ -614,7 +612,7 @@ Tput.prototype._compile = function(val) {
// %A, %O
// logical AND and OR operations (for conditionals)
if (exec(/^%([AO])/g)) {
if (read(/^%([AO])/)) {
if (code[code.length-1] === ',') code = code.slice(0, -1);
code += op === ' A ' ? ' && ' : ' || ';
continue;
@ -622,13 +620,13 @@ Tput.prototype._compile = function(val) {
// %! %~
// unary operations (logical and bit complement): push(op pop())
if (exec(/^%([!~])/g)) {
if (read(/^%([!~])/)) {
code += '(stack.push(v = ' + op + 'stack.pop()), v),';
continue;
}
// %i add 1 to first two parameters (for ANSI terminals)
if (exec(/^%i/g)) {
if (read(/^%i/)) {
code += '(params[0]++, params[1]++),';
continue;
}
@ -648,19 +646,19 @@ Tput.prototype._compile = function(val) {
// if-then-else's. Some strings, e.g., sgr can be very complicated when
// written on one line. The -f option splits the string into lines with
// the parts indented.
if (exec(/^%\?/g)) {
if (read(/^%\?/)) {
if (code[code.length-1] === ',') code = code.slice(0, -1);
code += ';if (';
continue;
}
if (exec(/^%t/g)) {
if (read(/^%t/)) {
if (code[code.length-1] === ',') code = code.slice(0, -1);
code += ') {';
continue;
}
if (exec(/^%e/g)) {
if (read(/^%e/)) {
if (code[code.length-1] === ',') code = code.slice(0, -1);
var end = val.indexOf('%;');
var els = val.indexOf('%e');
@ -674,7 +672,7 @@ Tput.prototype._compile = function(val) {
continue;
}
if (exec(/^%;/g)) {
if (read(/^%;/)) {
if (code[code.length-1] === ',') code = code.slice(0, -1);
code += '}';
continue;