rem='';/*
@node "%~f0" %*
@goto :eof
:mode=javascript:
:: convert APL characters <=> multigraphs
:: `a `w `e `i `o `y `u `s `d `g `h `j `l `z `x `c `v `b `n <=> chars placed on keys
:: ⍺ ⍵ ∊ ⍳ ○ ↑ ↓ ⌈ ⌊ ∇ ∆ ∘ ⎕ ⊂ ⊃ ∩ ∪ ⊥ ⊤
:: `-> `<- `/\ `\/ `>< `* `<> `@ `== `# `/= `<= `>= `- `% <=> chars looking similar
:: → ← ∧ ∨ × × ⋄ ∘ ≡ ≢ ≠ ≤ ≥ ¯ ÷
:: `MUL `DIV `QUAD `IDX `INF <=> names
:: × ÷ ⎕ ⍳ ∞
:: `` ``O ``j ``* ``> ``~ <=> diaeresis
:: ¨ ⍥ ⍤ ⍣ ⍩ ⍨
:: `[] `['] `[=] `] `[/] `[<>] <=> quad
:: ⎕ ⍞ ⌸ ⌷ ⍁ ⌺
:: `() `(*) `(-) `(|) `(\) <=> circle
:: ○ ⍟ ⊖ ⌽ ⍉
:: `-/ `-\ `-, <=> hor. strike
:: ⌿ ⍀ ⍪
:: `~0 `~| <=> tilde
:: ⍬ ⍭
:: `|g `|h `|<- <=> ver. strike
:: ⍒ ⍋ ⍅
:: `_a `_w <=> underline
:: ⍶ ⍹
:: `_A_B_C_Z_ `_X_ `_Y_Y Y`_Y_ <=> underlined letters
:: ⒶⒷⒸⓏ Ⓧ ⓎY YⓎ
*/
const charmap={
'⍺':['a'],
'⍶':['_a'],
'⍵':['w'],
'⍹':['_w'],
'∊':['e'],
'⍷':['_e'],
'⍴':['r'],
'∼':['t', '~', 'NOT'],
'⍨':['`~'],
'⍭':['~|', '|~', '|t'],
'↑':['y', 'TAKE'],
'⍏':['-y'],
'↓':['u', 'DROP'],
'⍖':['-u'],
'⍳':['i', 'INDEX'],
'⍸':['_i'],
'○':['o', '()', 'CIRC'],
'⍥':['`o', '(")', '`()', '(``)'],
'⋆':['p'],
'⍣':['`*', '`p'],
'⍟':['(*)', '(p)'],
'⌈':['s', 'MAX', 'CEIL'],
'⌊':['d', 'MIN', 'FLOOR'],
'∇':['g', 'DEL', 'FN'],
'⍫':['~g'],
'⍢':['`g'],
'⍒':['|g', 'DESC'],
'∆':['h', 'DELTA'],
'⍙':['_h'],
'⍋':['|h', 'ASC'],
'∘':['j', '@', 'JOT'],
'⍤':['`j', '`@', 'RANK'],
'⎕':['l', '[]', 'QUAD'],
'⍞':["[']"],
'⌷':[']', 'SQUISH'],
'⍠':['[:]'],
'⌸':['[=]'],
'⍁':['[/]'],
'⍂':['[\\]'],
'⌺':['[<>]'],
'⍰':['[?]'],
'⌹':['[%]'],
'⌻':['[j]', '[@]'],
'⌼':['[o]', '[()]'],
'⍯':['[|=]', '[!=]', '[/=]'],
'⍃':['[<]'],
'⍄':['[>]'],
'⍌':['[V]'],
'⍓':['[^]'],
'⍇':['[<-]'],
'⍈':['[->]'],
'⍐':['[y]'],
'⍗':['[u]'],
'⍔':['[g]'],
'⍍':['[h]'],
'⊂':['z'],
'⊆':['_z'],
'⊃':['x'],
'⊇':['_x'],
'∩':['c'],
'⍝':['@c', 'REM', 'BULB'],
'⋔':['|c'],
'∪':['v'],
'⍦':['|v'],
'⊥':['b', '_|_', '|_', '_|', '_L'],
'⍎':['@b', '@_L', 'EVAL', 'EXEC'],
'⍊':['_b'],
'⊤':['T', 'n'],
'⍕':['@T', '@n', 'FMT', 'FORMAT'],
'⍡':['`T', '`n'],
'⍑':['-T', '-n', '_n', '_T'],
'⊢':['|-'],
'⊣':['-|'],
'¯':['-'],
'∞':['INF', '8'],
'⍬':['~0'],
'⌶':['I', 'IBEAM'],
'⍩':['`>', '>"'],
'«':['<<'],
'»':['>>'],
'≤':['<='],
'≥':['>='],
'≠':['|=', '!=', '/='],
'≡':['=='],
'≢':['#', '|==', '!=='],
'⍀':['-\\'],
'⌿':['-/'],
'×':['*', '><', 'MUL', 'X', '+'],
'÷':['/', '%', 'DIV'],
'←':['<-'],
'⍅':['<+', '|<-', '<|-'],
'→':['->', 'GOTO'],
'⍆':['+>', '|->', '-|>'],
'¨':['"', '`', '..', ':'],
'∵':['`.'],
'⌽':['(|)'],
'⍉':['(\\)'],
'⊖':['(-)'],
'∨':['OR', '\\/', 'V'],
'⍱':['NOR', '~\\/', '~V'],
'∧':['AND', '/\\', '^'],
'⍲':['NAND','~/\\', '~^'],
'√':['ROOT'],
'π':['PI'],
'λ':['LAMBDA', 'LAMDA'],
'χ':['AXIS', 'CHI'],
'⍮':['_;'],
'⍛':['_j'],
'⍜':['_o'],
'⍪':['-,'],
'⍚':['_<>'],
'⋄':['<>', ';'],
'⊙':['(.)'],
'⌾':['(j)', '(@)'],
//і т.д. ......
'`':[' ']
};
// Ⓐ...Ⓩ:
for(i='A', j='Ⓐ'; i<='Z';
i=String.fromCharCode(i.charCodeAt(0)+1),
j=String.fromCharCode(j.charCodeAt(0)+1))
charmap[j]=['_'+i+'_'];
//tranToAPL={'w':'⍵', '∘':'j', '∘':'@'...}
tranToAPL={};
tranlist=[];
for(i in charmap)
{
transl=charmap[i];
for(j in transl)
{
tranToAPL['`'+transl[j]]=i;
tranlist.push(transl[j]);
}
}
// згенерувати регекс для пошуку:
//tranlist - повний список усіх позначень
//відсортувати, щоб довші були першими:
tranlist.sort(function(a,b){return b.length-a.length});
//console.log(tranlist);
//заескейпити спецсимволи ().*?[]\
reToAPL='`(';
for(i in tranlist)
reToAPL+=tranlist[i].replace(/([\\\[\]\)\(\.\*\?\|\{\}\+])/g, '\\$1')+
(i<tranlist.length-1?'|':')|.');
//reToAPL=/`(LONGEST|MID|[1chr])|./ :
//console.log(reToAPL);
reToAPL=new RegExp(reToAPL, 'g');
tranFromAPL={};
for(i in charmap)
tranFromAPL[i]=charmap[i][0];
function pseudo2apl(s)
{
//розгорнути підкреслені групи літер `_A_B_C_=>`_A_`_B_`_C_.....
while(/`_[A-Z]_[A-Z]_/ .test(s))
s=s.replace(/`_([A-Z])_([A-Z])_/g, '`_$1_`_$2_');
//знайти й замінити:
s=s.match(reToAPL);
//console.log(s);
res=''
for(i in s)
if(repr=tranToAPL[s[i]])
res+=repr;
else res+=s[i];
return res;
}
function apl2pseudo(s)
{
res='';
for(i in s)
if(tr=charmap[s[i]])
res+='`'+tr[0];
else res+=s[i];
s=res
//`_A_`_B_ => `_A_B_....
while(/`(_[A-Z])+_`_[A-Z]_/ .test(s))
s=s.replace(/`((_[A-Z])+)_`_([A-Z])_/g, '`$1_$3_');
return s;
}
/* test
console.log(t=apl2pseudo('∘.×⍨⍳10 ⍝ ``Pythagorean table"'));
console.log(pseudo2apl(t));
console.log(pseudo2apl('`@c 2`MUL2`!=5'));
/* end test */
var toTranslit=false;
process.argv.slice(2).forEach(function(arg)
{
// -t --to-translit
if (arg==='-t'|arg==='--to-translit')
toTranslit=true;
// -u --to-unicode
else if (arg==='-u'|arg==='--to-unicode')
toTranslit=false;
});
translate=toTranslit? apl2pseudo: pseudo2apl;
processLine=function(s)
{
process.stdout.write(translate(s)+'\n');
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
var lingeringLine = "";
process.stdin.on('data', function(chunk)
{
lines = chunk.split("\n");
lines[0] = lingeringLine + lines[0];
lingeringLine = lines.pop();
lines.forEach(processLine);
});
process.stdin.on('end', function()
{
if(lingeringLine!=='')
processLine(lingeringLine);
});
// I/O.....