forked from Maxoplata/StringToINTERCAL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringToINTERCAL.js
More file actions
80 lines (60 loc) · 1.63 KB
/
StringToINTERCAL.js
File metadata and controls
80 lines (60 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/**
* StringToINTERCAL.js
*
* Converts a string to an INTERCAL script that will output said string.
* usage: node StringToINTERCAL.js your string here
*
* @author Maxamilian Demian <max@maxdemian.com>
* @link https://www.maxodev.org
* @link https://github.com/Maxoplata/StringToINTERCAL
*/
// class definition
class StringToINTERCAL {
constructor() {
this.politeCount = 0
}
politeLine(line) {
if (this.politeCount === 3) {
this.politeCount = 0;
return `PLEASE ${line}\n`;
}
this.politeCount++;
return `DO ${line}\n`;
}
leadingZeros(dec) {
const count = dec.length;
if (count < 8) {
dec = '0'.repeat(8 - count) + dec;
}
return dec;
}
convertToINTERCAL(string) {
// reset politeCount
this.politeCount = 0;
let ret = this.politeLine(`,1 <- #${string.length}`);
let lastCharLoc = 256;
[...string].forEach((char, i) => {
const charLoc = parseInt(this.leadingZeros(char.charCodeAt(0).toString(2)).split('').reverse().join(''), 2)
let movePosition = 0;
if (charLoc < lastCharLoc) {
movePosition = (lastCharLoc - charLoc);
} else if (charLoc > lastCharLoc) {
movePosition = (256 - charLoc) + lastCharLoc;
}
lastCharLoc -= movePosition;
if (lastCharLoc < 1) {
lastCharLoc = 256 + lastCharLoc;
}
ret += this.politeLine(`,1 SUB #${(i + 1)} <- #${movePosition}`);
});
ret += this.politeLine('READ OUT ,1');
ret += this.politeLine('GIVE UP');
return ret;
}
}
// 1337 codez
if (process.argv.length > 2) {
const inputString = process.argv.splice(2).join(' ');
const myINTERCAL = new StringToINTERCAL();
console.log(myINTERCAL.convertToINTERCAL(inputString));
}