forked from Maxoplata/StringToINTERCAL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringToINTERCAL.cs
More file actions
90 lines (67 loc) · 1.95 KB
/
StringToINTERCAL.cs
File metadata and controls
90 lines (67 loc) · 1.95 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
81
82
83
84
85
86
87
88
89
90
/**
* StringToINTERCAL.cs
*
* Converts a string to an INTERCAL script that will output said string.
* usage: csc StringToINTERCAL.cs && ./StringToINTERCAL your string here
*
* @author Maxamilian Demian
* @link https://www.maxodev.org
* @link https://github.com/Maxoplata/StringToINTERCAL
*/
using System;
class StringToINTERCAL {
private int politeCount = 0;
private string politeLine(string line) {
if (politeCount == 3) {
politeCount = 0;
return "PLEASE " + line + "\n";
}
politeCount++;
return "DO " + line + "\n";
}
private string leadingZeros(string dec) {
int count = dec.Length;
if (count < 8) {
dec = new string('0', (8 - count)) + dec;
}
return dec;
}
private string reverseString(string myString) {
char[] charArray = myString.ToCharArray();
Array.Reverse(charArray);
return new string(charArray);
}
public string convertToINTERCAL(string myString) {
// reset politeCount
politeCount = 0;
string ret = politeLine(",1 <- #" + myString.Length.ToString());
int lastCharLoc = 256;
for (int i = 0; i < myString.Length; i++) {
int charLoc = Convert.ToInt32(reverseString(leadingZeros(Convert.ToString((int) myString[i], 2))), 2);
int movePosition = 0;
if (charLoc < lastCharLoc) {
movePosition = (lastCharLoc - charLoc);
} else if (charLoc > lastCharLoc) {
movePosition = (256 - charLoc) + lastCharLoc;
}
lastCharLoc = lastCharLoc - movePosition;
if (lastCharLoc < 1) {
lastCharLoc = 256 + lastCharLoc;
}
ret = ret + politeLine(",1 SUB #" + (i + 1) + " <- #" + movePosition);
}
ret = ret + politeLine("READ OUT ,1");
ret = ret + politeLine("GIVE UP");
return ret;
}
}
class App {
static void Main(string[] args) {
// if we have arguments passed to the script
if (args.Length > 0) {
string inputString = String.Join(" ", args);
StringToINTERCAL myINTERCAL = new StringToINTERCAL();
Console.WriteLine(myINTERCAL.convertToINTERCAL(inputString));
}
}
}