-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterpreter.cs
More file actions
161 lines (159 loc) · 6.47 KB
/
Interpreter.cs
File metadata and controls
161 lines (159 loc) · 6.47 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
namespace Interpreter{
class Translate{
static public void UpperCase(int input, bool appendToOutput = true){
List<string> translation = new List<string>(new string[]{" ", "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"});
try{
string output = translation[input];
if(appendToOutput){
Interpreter.AppendToOutput(output);
}else{
Funcs.Util.Text(output, ConsoleColor.White , false);
}
}
catch (System.Exception){
Funcs.Sys.Exit(1, "Error: Invalid input");
}
}
static public void LowerCase(int input, bool appendToOutput = true){
List<string> translation = new List<string>(new string[]{" ", "a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"});
try{
string output = translation[input];
if(appendToOutput){
Interpreter.AppendToOutput(output);
}else{
Funcs.Util.Text(output, ConsoleColor.White , false);
}
}
catch (System.Exception){
Funcs.Sys.Exit(1, "Error: Invalid input");
}
}
static public void SpecialChar(int input, bool appendToOutput = true){
List<string> translation = new List<string>(new string[]{"~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "-", "_", "+", "=", "`", "[", "]", "{", "}", "|", "\\", ";", ":", "\"", "'", "<", ">", ",", ".", "?", "/", "\n"});
try{
string output = translation[input];
if(appendToOutput){
Interpreter.AppendToOutput(output);
}else{
Funcs.Util.Text(output, ConsoleColor.White , false);
}
}
catch (System.Exception){
Funcs.Sys.Exit(1, "Error: Invalid input");
}
}
}
public class Interpreter{
static int pointer = 0;
static string output = "";
static public void Interpret(char param, Dictionary<int, int> storage, char[] paras, int index){
// Funcs.Util.Text(param.ToString()+"-"+index.ToString()+"-"+pointer.ToString());
if(param == '>'){
if(!storage.ContainsKey(pointer+1)){
storage.Add(pointer+1, 0);
}
pointer++;
}else if(param == '<'){
if(pointer == 0){
Funcs.Sys.Exit(1, "Error: Can't go into negetive memory");
}else{
pointer--;
}
}else if(param == '+'){
storage[pointer]++;
}else if(param == '-'){
storage[pointer]--;
}else if(param == '\''){
if(paras.Length < index+2){
Translate.LowerCase(storage[pointer]);
}else{
if(paras[index+1] == '\''){
Translate.LowerCase(storage[pointer], false);
Program.Program.SetI(index+1);
}else{
Translate.LowerCase(storage[pointer]);
}
}
}else if(param == '\"'){
if(paras.Length < index+2){
Translate.UpperCase(storage[pointer]);
}else{
if(paras[index+1] == '\"'){
Translate.UpperCase(storage[pointer], false);
Program.Program.SetI(index+1);
}else{
Translate.UpperCase(storage[pointer]);
}
}
}else if(param == ']'){
if(paras.Length < index+2){
output += storage[pointer];
}else{
if(paras[index+1] == ']'){
Funcs.Util.Text(storage[pointer].ToString());
Program.Program.SetI(index+1);
}else{
output += storage[pointer];
}
}
}else if(param == '['){
if(paras.Length < index+2){
Translate.SpecialChar(storage[pointer]);
}else{
if(paras[index+1] == '['){
Translate.SpecialChar(storage[pointer], false);
Program.Program.SetI(index+1);
}else{
Translate.SpecialChar(storage[pointer]);
}
}
}else if(param == '.'){
string? input = Console.ReadLine();
if(input == null){
Funcs.Sys.Exit(1, "Error: Invalid input");
}else{
try{
storage[pointer] += int.Parse(input);
}catch{
Funcs.Sys.Exit(1, "Error: Couldn't convert input to integer");
}
}
}else if(param == '{'){
int internalLoops = 0;
int loopEndIndex = 0;
int loopPointer = pointer;
for(int i = index; i < paras.Length; i++){
if(loopEndIndex != 0){
i = paras.Length;
break;
}
if(paras[i] == '{'){
internalLoops++;
}
if(paras[i] == '}'){
internalLoops--;
if(internalLoops == 0){
loopEndIndex = i;
}
}
}
char[] loopItems = new char[loopEndIndex - index];
for(int i = index+1; i < loopEndIndex; i++){
loopItems[i-(index+1)] = paras[i];
}
while(storage[loopPointer]!=0){
loopItems.ToList().ForEach(x => {
Interpret(x, storage, paras, index);
});
}
Program.Program.SetI(loopEndIndex);
}
}
static public void AppendToOutput(string input){
output += input;
}
static public string GetOutput(){
return output;
}
}
}