more code refactoring

This commit is contained in:
Christopher Jeffrey 2013-02-16 12:15:10 -06:00
parent c083164e33
commit 214c10e547

View File

@ -294,14 +294,14 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
while (val) { while (val) {
// '\e' -> ^[ // '\e' -> ^[
if (cap = /\\e/gi.exec(val)) { if (cap = /^\\e/gi.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
code += '\x1b'; code += '\x1b';
continue; continue;
} }
// '^A' -> ^A // '^A' -> ^A
if (cap = /\^(.)/gi.exec(val)) { // case-insensitive? if (cap = /^\^(.)/gi.exec(val)) { // case-insensitive?
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
ch = cap[1]; ch = cap[1];
switch (ch) { switch (ch) {
@ -411,7 +411,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// '\n' -> \n // '\n' -> \n
// '\r' -> \r // '\r' -> \r
// '\0' -> \200 (special case) // '\0' -> \200 (special case)
if (cap = /\\([nlrtbfs\^\\,:0])/g.exec(val)) { if (cap = /^\\([nlrtbfs\^\\,:0])/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
ch = cap[1]; ch = cap[1];
switch (ch) { switch (ch) {
@ -443,7 +443,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
} }
// 3 octal digits -> character // 3 octal digits -> character
if (cap = /\\(\d\d\d)/g.exec(val)) { if (cap = /^\\(\d\d\d)/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
ch = cap[1]; ch = cap[1];
code += String.fromCharCode(parseInt(ch, 8)); code += String.fromCharCode(parseInt(ch, 8));
@ -451,7 +451,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
} }
// $<5> -> padding // $<5> -> padding
if (cap = /\$<(\d+)>(\*|\/)/g.exec(val)) { if (cap = /^\$<(\d+)>(\*|\/)/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
ch = cap[1]; ch = cap[1];
code += Array(+ch + 1).join(' '); // "padding" characters? code += Array(+ch + 1).join(' '); // "padding" characters?
@ -460,7 +460,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// man terminfo, around page 1034 // man terminfo, around page 1034
// %% outputs `%' // %% outputs `%'
if (cap = /%%/g.exec(val)) { if (cap = /^%%/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
code += '%'; code += '%';
continue; continue;
@ -470,21 +470,29 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// 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 (cap = /%(?:(:)?([\-+# ]+)?)(?:(\d+)(\.\d+)?)?([doxXs])?/g.exec(val)) { if (cap = /^%(?:(:)?([\-+# ]+)?)(?:(\d+)(\.\d+)?)?([doxXs])?/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
code += 'TODO'; code += 'TODO';
continue; continue;
} }
// %c print pop() like %c in printf // %c print pop() like %c in printf
if (cap = /%c/g.exec(val)) { if (cap = /^%c/g.exec(val)) {
val = val.substring(cap[0].length);
code += 'stack.pop()'; // TODO: FORMAT
continue;
}
// %d print pop() like %d in printf
// NOT SURE ABOUT %d being print!
if (cap = /^%d/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
code += 'stack.pop()'; // TODO: FORMAT code += 'stack.pop()'; // TODO: FORMAT
continue; continue;
} }
// %s print pop() like %s in printf // %s print pop() like %s in printf
if (cap = /%s/g.exec(val)) { if (cap = /^%s/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
code += 'stack.pop()'; // TODO: FORMAT code += 'stack.pop()'; // TODO: FORMAT
continue; continue;
@ -492,7 +500,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// %p[1-9] // %p[1-9]
// push i'th parameter // push i'th parameter
if (cap = /%p([1-9])/g.exec(val)) { if (cap = /^%p([1-9])/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
code += 'params[i]'; code += 'params[i]';
continue; continue;
@ -500,28 +508,28 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// %P[a-z] // %P[a-z]
// set dynamic variable [a-z] to pop() // set dynamic variable [a-z] to pop()
if (cap = /%P([a-z])/g.exec(val)) { if (cap = /^%P([a-z])/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
v = cap[1]; v = cap[1];
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 (cap = /%g([a-z])/g.exec(val)) { if (cap = /^%g([a-z])/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
v = cap[1]; v = cap[1];
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 (cap = /%P([A-Z])/g.exec(val)) { if (cap = /^%P([A-Z])/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
v = cap[1]; v = cap[1];
code += 'stat[' + v + '] = stack.pop()'; code += 'stat.' + v + ' = stack.pop()';
continue; continue;
} }
@ -534,15 +542,15 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// 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 (cap = /%g([A-Z])/g.exec(val)) { if (cap = /^%g([A-Z])/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
v = cap[1]; v = cap[1];
code += 'stack.push(stat[' + v + '])'; code += 'stack.push(stat.' + v + ')';
continue; continue;
} }
// %'c' char constant c // %'c' char constant c
if (cap = /%'(\w)'/g.exec(val)) { if (cap = /^%'(\w)'/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
ch = cap[1]; ch = cap[1];
code += '"' + ch + '"'; code += '"' + ch + '"';
@ -551,7 +559,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// %{nn} // %{nn}
// integer constant nn // integer constant nn
if (cap = /%\{(\d+)\}/g.exec(val)) { if (cap = /^%\{(\d+)\}/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
ch = cap[1]; ch = cap[1];
code += '(' + ch + ')'; code += '(' + ch + ')';
@ -559,7 +567,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
} }
// %l push strlen(pop) // %l push strlen(pop)
if (cap = /%l/g.exec(val)) { if (cap = /^%l/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
code += 'stack.push(stack.pop().length)'; code += 'stack.push(stack.pop().length)';
continue; continue;
@ -571,7 +579,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// 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 (cap = /%([+\-*\/m&|\^=><])/g.exec(val)) { if (cap = /^%([+\-*\/m&|\^=><])/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
op = cap[1]; op = cap[1];
if (op === '=') op = '==='; if (op === '=') op = '===';
@ -582,7 +590,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// %A, %O // %A, %O
// logical AND and OR operations (for conditionals) // logical AND and OR operations (for conditionals)
if (cap = /%([AO])/g.exec(val)) { if (cap = /^%([AO])/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
op = cap[1]; op = cap[1];
code += op === ' A ' ? ' && ' : ' || '; code += op === ' A ' ? ' && ' : ' || ';
@ -591,7 +599,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// %! %~ // %! %~
// unary operations (logical and bit complement): push(op pop()) // unary operations (logical and bit complement): push(op pop())
if (cap = /%([!~])/g.exec(val)) { if (cap = /^%([!~])/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
op = cap[1]; op = cap[1];
code += 'stack.push(' + op + 'stack.pop())'; code += 'stack.push(' + op + 'stack.pop())';
@ -599,7 +607,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
} }
// %i add 1 to first two parameters (for ANSI terminals) // %i add 1 to first two parameters (for ANSI terminals)
if (cap = /%i/g.exec(val)) { if (cap = /^%i/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
code += '(params[0]++, params[1]++)'; code += '(params[0]++, params[1]++)';
continue; continue;
@ -620,27 +628,27 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// 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 (cap = /%\?/g.exec(val)) { if (cap = /^%\?/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
code += ';if ('; code += '"); if (';
continue; continue;
} }
if (cap = /%t/g.exec(val)) { if (cap = /^%t/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
code += ') {'; code += ') { out.push("';
continue; continue;
} }
if (cap = /%e/g.exec(val)) { if (cap = /^%e/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
code += '} else {'; code += '"); } else { out.push("';
continue; continue;
} }
if (cap = /%;/g.exec(val)) { if (cap = /^%;/g.exec(val)) {
val = val.substring(cap[0].length); val = val.substring(cap[0].length);
code += '}'; code += '"); } out.push("';
continue; continue;
} }
@ -675,6 +683,9 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
code += val[0]; code += val[0];
val = val.substring(1); val = val.substring(1);
} }
code += '"); return out.join("");';
break; break;
} }