better code

This commit is contained in:
Christopher Jeffrey 2013-02-15 17:08:36 -06:00
parent 14df28fb35
commit c083164e33

View File

@ -288,89 +288,132 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// CSI Ps ; Ps r
// CSI ? Pm r
var code = 'var dyn = {}, stat = {}, stack = []; out.push("';
var code = 'var dyn = {}, stat = {}, stack = [], out = []; out.push("';
// man terminfo, around line 940
while (val) {
// '\e' -> ^[
val = val.replace(/\\e/gi, '\x1b');
if (cap = /\\e/gi.exec(val)) {
val = val.substring(cap[0].length);
code += '\x1b';
continue;
}
// '^A' -> ^A
val = val.replace(/\^(.)/gi, function(_, ch) { // case-insensitive?
if (cap = /\^(.)/gi.exec(val)) { // case-insensitive?
val = val.substring(cap[0].length);
ch = cap[1];
switch (ch) {
case '@':
return '\x00';
code += '\x00';
break;
case 'A':
return '\x01';
code += '\x01';
break;
case 'B':
return '\x02';
code += '\x02';
break;
case 'C':
return '\x03';
code += '\x03';
break;
case 'D':
return '\x04';
code += '\x04';
break;
case 'E':
return '\x05';
code += '\x05';
break;
case 'F':
return '\x06';
code += '\x06';
break;
case 'G':
return '\x07';
code += '\x07';
break;
case 'H':
return '\x08';
code += '\x08';
break;
case 'I':
return '\x09'; // \t
code += '\x09'; // \t
break;
case 'J':
return '\x0a'; // \n
code += '\x0a'; // \n
break;
case 'K':
return '\x0b';
code += '\x0b';
break;
case 'L':
return '\x0c';
code += '\x0c';
break;
case 'M':
return '\x0d';
code += '\x0d';
break;
case 'N':
return '\x0e';
code += '\x0e';
break;
case 'O':
return '\x0f';
code += '\x0f';
break;
case 'P':
return '\x10';
code += '\x10';
break;
case 'Q':
return '\x11';
code += '\x11';
break;
case 'R':
return '\x12';
code += '\x12';
break;
case 'S':
return '\x13';
code += '\x13';
break;
case 'T':
return '\x14';
code += '\x14';
break;
case 'U':
return '\x15';
code += '\x15';
break;
case 'V':
return '\x16';
code += '\x16';
break;
case 'W':
return '\x17';
code += '\x17';
break;
case 'X':
return '\x18';
code += '\x18';
break;
case 'Y':
return '\x19';
code += '\x19';
break;
case 'Z':
return '\x1a';
code += '\x1a';
break;
case '\\':
return '\x1c';
code += '\x1c';
break;
case '^':
return '\x1e';
code += '\x1e';
break;
case '_':
return '\x1f';
code += '\x1f';
break;
case '[':
return '\x1b';
code += '\x1b';
break;
case ']':
return '\x1d';
code += '\x1d';
break;
case '?':
return '\x7f';
code += '\x7f';
break;
}
continue;
}
});
// '\n' -> \n
// '\r' -> \r
// '\0' -> \200 (special case)
val = val.replace(/\\([nlrtbfs\^\\,:0])/g, function(_, ch) {
if (cap = /\\([nlrtbfs\^\\,:0])/g.exec(val)) {
val = val.substring(cap[0].length);
ch = cap[1];
switch (ch) {
case 'n':
return '\n';
@ -396,78 +439,91 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
//return '\0';
return '\200';
}
});
continue;
}
// 3 octal digits -> character
val = val.replace(/\\(\d\d\d)/g, function(_, ch) {
return String.fromCharCode(parseInt(ch, 8));
});
if (cap = /\\(\d\d\d)/g.exec(val)) {
val = val.substring(cap[0].length);
ch = cap[1];
code += String.fromCharCode(parseInt(ch, 8));
continue;
}
// $<5> -> padding
val = val.replace(/\$<(\d+)>(\*|\/)/g, function(_, ch, opt) {
// code += '';
// TODO
return '';
return Array(+ch + 1).join(' '); // "padding" characters?
});
if (cap = /\$<(\d+)>(\*|\/)/g.exec(val)) {
val = val.substring(cap[0].length);
ch = cap[1];
code += Array(+ch + 1).join(' '); // "padding" characters?
continue;
}
// man terminfo, around page 1034
// %% outputs `%'
val = val.replace(/%%/g, '%');
if (cap = /%%/g.exec(val)) {
val = val.substring(cap[0].length);
code += '%';
continue;
}
// %[[:]flags][width[.precision]][doxXs]
// as in printf, flags are [-+#] and space. Use a `:' to allow the
// next character to be a `-' flag, avoiding interpreting "%-" as an
// operator.
val = val.replace(/%(?:(:)?([\-+# ]+)?)(?:(\d+)(\.\d+)?)?([doxXs])?/g, function() {
// TODO
return '';
});
if (cap = /%(?:(:)?([\-+# ]+)?)(?:(\d+)(\.\d+)?)?([doxXs])?/g.exec(val)) {
val = val.substring(cap[0].length);
code += 'TODO';
continue;
}
// %c print pop() like %c in printf
val = val.replace(/%c/g, function() {
// code += 'out += stack.pop()'; // TODO: FORMAT
// TODO
return '';
});
if (cap = /%c/g.exec(val)) {
val = val.substring(cap[0].length);
code += 'stack.pop()'; // TODO: FORMAT
continue;
}
// %s print pop() like %s in printf
val = val.replace(/%s/g, function() {
// code += 'out += stack.pop()'; // TODO: FORMAT
// TODO
return '';
});
if (cap = /%s/g.exec(val)) {
val = val.substring(cap[0].length);
code += 'stack.pop()'; // TODO: FORMAT
continue;
}
// %p[1-9]
// push i'th parameter
val = val.replace(/%p([1-9])/g, function(_, i) {
// code += 'params[i]';
return params[i] || '';
});
if (cap = /%p([1-9])/g.exec(val)) {
val = val.substring(cap[0].length);
code += 'params[i]';
continue;
}
// %P[a-z]
// set dynamic variable [a-z] to pop()
val = val.replace(/%P([a-z])/g, function(_, v) {
// code += 'dyn[' + v + '] = stack.pop()';
// TODO
return '';
});
if (cap = /%P([a-z])/g.exec(val)) {
val = val.substring(cap[0].length);
v = cap[1];
code += 'dyn[' + v + '] = stack.pop()';
continue;
}
// %g[a-z]
// get dynamic variable [a-z] and push it
val = val.replace(/%g([a-z])/g, function(_, v) {
// code += '(stack.push(dyn[' + v + ']), data[' + v + '])';
// TODO
return '';
});
if (cap = /%g([a-z])/g.exec(val)) {
val = val.substring(cap[0].length);
v = cap[1];
code += '(stack.push(dyn[' + v + ']), dyn[' + v + '])';
continue;
}
// %P[A-Z]
// set static variable [a-z] to pop()
val = val.replace(/%P([A-Z])/g, function(_, v) {
// code += 'stat[' + v + '] = stack.pop()';
// TODO
return '';
});
if (cap = /%P([A-Z])/g.exec(val)) {
val = val.substring(cap[0].length);
v = cap[1];
code += 'stat[' + v + '] = stack.pop()';
continue;
}
// %g[A-Z]
// get static variable [a-z] and push it
@ -478,79 +534,76 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// documented in other implementations. Relying on it will adversely
// impact portability to other implementations.
val = val.replace(/%g([A-Z])/g, function(_, v) {
// TODO
return '';
});
if (cap = /%g([A-Z])/g.exec(val)) {
val = val.substring(cap[0].length);
v = cap[1];
code += 'stack.push(stat[' + v + '])';
continue;
}
// %'c' char constant c
val = val.replace(/%'(\w)'/g, function(_, ch) {
// code += '"' + ch + '"';
// TODO
return '';
});
if (cap = /%'(\w)'/g.exec(val)) {
val = val.substring(cap[0].length);
ch = cap[1];
code += '"' + ch + '"';
continue;
}
// %{nn}
// integer constant nn
val = val.replace(/%\{(\d+)\}/g, function(_, nn) {
// code += '(' + ch + ')';
// TODO
return '';
});
if (cap = /%\{(\d+)\}/g.exec(val)) {
val = val.substring(cap[0].length);
ch = cap[1];
code += '(' + ch + ')';
continue;
}
// %l push strlen(pop)
val = val.replace(/%l/g, function() {
// code += 'stack.push(stack.pop().length)';
// TODO
return '';
});
if (cap = /%l/g.exec(val)) {
val = val.substring(cap[0].length);
code += 'stack.push(stack.pop().length)';
continue;
}
// %+ %- %* %/ %m
// arithmetic (%m is mod): push(pop() op pop())
val = val.replace(/%([+\-*\/m])/g, function(_, op) {
// code += 'stack.push(stack.pop() ' + op + ' stack.pop())';
// TODO
return '';
});
// %& %| %^
// bit operations (AND, OR and exclusive-OR): push(pop() op pop())
val = val.replace(/%([&|\^])/g, function(_, op) {
// code += 'stack.push(stack.pop() ' + op + ' stack.pop())';
// TODO
return '';
});
// %= %> %<
// logical operations: push(pop() op pop())
val = val.replace(/%([=><])/g, function(_, op) {
// code += 'stack.push(stack.pop() ' + op + ' stack.pop())';
// TODO
return '';
});
if (cap = /%([+\-*\/m&|\^=><])/g.exec(val)) {
val = val.substring(cap[0].length);
op = cap[1];
if (op === '=') op = '===';
else if (op === 'm') op = '%';
code += 'stack.push(stack.pop() ' + op + ' stack.pop())';
continue;
}
// %A, %O
// logical AND and OR operations (for conditionals)
val = val.replace(/%([AO])/g, function(_, v) {
// code += v === ' A ' ? ' && ' : ' || ';
// TODO
return '';
});
if (cap = /%([AO])/g.exec(val)) {
val = val.substring(cap[0].length);
op = cap[1];
code += op === ' A ' ? ' && ' : ' || ';
continue;
}
// %! %~
// unary operations (logical and bit complement): push(op pop())
val = val.replace(/%([!~])/g, function(_, op) {
// code += 'stack.push(' + op + 'stack.pop())';
// TODO
return '';
});
if (cap = /%([!~])/g.exec(val)) {
val = val.substring(cap[0].length);
op = cap[1];
code += 'stack.push(' + op + 'stack.pop())';
continue;
}
// %i add 1 to first two parameters (for ANSI terminals)
val = val.replace(/%i/g, function(_, v) {
// code += '(params[0]++, params[1]++)';
// TODO
return '';
});
if (cap = /%i/g.exec(val)) {
val = val.substring(cap[0].length);
code += '(params[0]++, params[1]++)';
continue;
}
// %? expr %t thenpart %e elsepart %;
// This forms an if-then-else. The %e elsepart is optional. Usually
@ -567,36 +620,29 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// 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.
//val = val.replace(/%\?(.+?)%t(.+?)%e(.+?)%;/g, function(_, expr, thenpart, elsepart) {
// // TODO: Generate code:
// // code += ';if (' + parse(expr) + ') {' + out(thenpart) + '} else {' + out(elsepart) + '}';
// // TODO
// return '';
//});
if (cap = /%\?/g.exec(val)) {
val = val.substring(cap[0].length);
code += ';if (';
continue;
}
val = val.replace(/%\?/g, function(_, expr, thenpart, elsepart) {
// code += ';if (';
// TODO
return '';
});
if (cap = /%t/g.exec(val)) {
val = val.substring(cap[0].length);
code += ') {';
continue;
}
val = val.replace(/%t/g, function(_, expr, thenpart, elsepart) {
// code += ') {';
// TODO
return '';
});
if (cap = /%e/g.exec(val)) {
val = val.substring(cap[0].length);
code += '} else {';
continue;
}
val = val.replace(/%e/g, function(_, expr, thenpart, elsepart) {
// code += '} else {';
// TODO
return '';
});
val = val.replace(/%;/g, function(_, expr, thenpart, elsepart) {
// code += '}';
// TODO
return '';
});
if (cap = /%;/g.exec(val)) {
val = val.substring(cap[0].length);
code += '}';
continue;
}
// Binary operations are in postfix form with the operands in the usual
// order. That is, to get x-5 one would use "%gx%{5}%-". %P and %g vari
@ -626,10 +672,9 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// Then the same is done for the second parameter. More complex
// arithmetic is possible using the stack.
//val = val.replace(/%p(\d+)?/g, function(_, n) {
// return params[i++] || '';
//});
code += val[0];
val = val.substring(1);
}
break;
}