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