-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathACL.java
More file actions
230 lines (201 loc) · 5.48 KB
/
ACL.java
File metadata and controls
230 lines (201 loc) · 5.48 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
//Advanced Computer Language interpreter
import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
public class ACL {
//reads source file and returns the code
static String getCode() {
try {
Scanner getName = new Scanner(System.in);
System.out.println("File to interpret?");
String fileName = getName.nextLine();
getName.close();
File file = new File(fileName);
Scanner fRead = new Scanner(file);
String code = "";
while (fRead.hasNextLine()) {
code += fRead.nextLine();
}
fRead.close();
return code;
} catch (FileNotFoundException e) {
System.out.println("FILE FAIL");
return "F";
}
}
//binary to decimal
static int toDec(String bin) {
int l = bin.length();
int j = l-1;
int x = 0;
int dec = 0;
while (x < l) {
dec += Math.pow(2, x) * ((int) bin.charAt(j) - 48);
x++; j--;
}
return dec;
}
//--------------------------------------------------------------
public static void main(String[] args) {
//declares necessary variables etc.
String code = getCode();
int l = code.length();
int i = 0; //code reader pointer;
ArrayList<Byte> memory = new ArrayList<Byte>(); //cell based, binary
memory.add((byte) 0); //make the memory have one cell at start
int ptr = 0; //memory pointer
int cells = 1; //cells in memory
String bOutput = ""; //binary output
String cOutput = ""; //character output
char c; //code command
Scanner input = new Scanner(System.in);
String f = ""; //for functions
//temporary variables
String t;
int x;
//--------------------------------------------------------------
//reads and executes the code
while (i < l) {
c = code.charAt(i);
switch (c) {
//pointer movement
case '0':
ptr = 0;
break;
case '1':
ptr++;
if (ptr == cells) {
memory.add((byte) 0);
cells++;
}
break;
case '2':
ptr--;
if (ptr < 0) {
ptr = cells - 1;
}
break;
//bit flip
case '3':
if (memory.get(ptr) == 1) {
memory.set(ptr,(byte) 0);
}
else {
memory.set(ptr,(byte) 1);
}
break;
case '4':
bOutput += memory.get(ptr);
break;
//if-else-endif
case '5':
if (memory.get(ptr) == 0) {
x = 1;
while (x != 0) {
i++;
if (code.charAt(i) == '5') {
x++;
}
else if (code.charAt(i) == '7' || code.charAt(i) == '8' || (x == 1 && code.charAt(i) == '6')) {
x--;
}
}
}
break;
//else
case '6':
x = 1;
while (x != 0) {
i++;
if (code.charAt(i) == '5') {
x++;
}
else if (code.charAt(i) == '7' || code.charAt(i) == '8') {
x--;
}
}
break;
case '7':
//endif command, does nothing on its own
break;
//loop
case '8':
if (memory.get(ptr) == 1) {
x = 1;
while (x != 0) {
i--;
if (code.charAt(i) == '7' || code.charAt(i) == '8') {
x++;
}
else if (code.charAt(i) == '5') {
x--;
}
}
}
break;
case '9':
memory.set(ptr, (byte) Math.round(Math.random()));
break;
//input
case 'A':
x = Integer.parseInt(input.nextLine());
if (x != 0 && x != 1) {
System.out.println(1111);
i = l;
break;
}
else {
memory.set(ptr,(byte) x);
break;
}
//binary output
case 'B':
System.out.println(bOutput);
bOutput = "";
break;
//integer and character output
case 'C':
if (bOutput.equals("")) {
System.out.println(cOutput);
cOutput = "";
}
else if (memory.get(ptr) == 1) {
cOutput += toDec(bOutput);
}
else {
cOutput += (char) toDec(bOutput);
}
bOutput = "";
break;
//declare function
case 'D':
i++;
f = "";
while (code.charAt(i) != 'D') {
f += code.charAt(i);
i++;
}
break;
//enter function into code
case 'E':
t = "";
for (x = 0; x < i+1; x++) {
t += code.charAt(x);
}
t += f;
for (x = i+1; x < l; x++) {
t += code.charAt(x);
}
code = t;
l = code.length();
break;
case 'F':
System.out.println(1111);
i = l;
}
i++;
}
input.close();
}
}