-
Notifications
You must be signed in to change notification settings - Fork 1
/
StringToINTERCAL.ts
82 lines (61 loc) · 1.78 KB
/
StringToINTERCAL.ts
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
81
82
/**
* StringToINTERCAL.ts
*
* Converts a string to an INTERCAL script that will output said string.
* usage: ts-node StringToINTERCAL.ts your string here
*
* @author Maxamilian Demian <max@maxdemian.com>
* @link https://www.maxodev.org
* @link https://github.com/Maxoplata/StringToINTERCAL
*/
// class definition
class StringToINTERCAL {
private politeCount: number;
constructor() {
this.politeCount = 0
}
politeLine(line: string): string {
if (this.politeCount === 3) {
this.politeCount = 0;
return `PLEASE ${line}\n`;
}
this.politeCount++;
return `DO ${line}\n`;
}
leadingZeros(dec: string): string {
const count = dec.length;
if (count < 8) {
dec = '0'.repeat(8 - count) + dec;
}
return dec;
}
convertToINTERCAL(string: string): string {
// reset politeCount
this.politeCount = 0;
let ret: string = this.politeLine(`,1 <- #${string.length}`);
let lastCharLoc: number = 256;
[...string].forEach((char: string, i: number) => {
const charLoc: number = parseInt(this.leadingZeros(char.charCodeAt(0).toString(2)).split('').reverse().join(''), 2)
let movePosition: number = 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: string = process.argv.splice(2).join(' ');
const myINTERCAL: StringToINTERCAL = new StringToINTERCAL();
console.log(myINTERCAL.convertToINTERCAL(inputString));
}