-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBFToCode.java
More file actions
103 lines (98 loc) · 2.25 KB
/
BFToCode.java
File metadata and controls
103 lines (98 loc) · 2.25 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
import java.util.*;
public class BFToCode
{
private TapeSeg<Integer> tape;
private ArrayList<Integer> output;
private boolean failed;
public BFToCode()
{
failed = false;
tape = new TapeSeg<Integer>(0, null, null);
output = new ArrayList<Integer>();
}
public void translate(String s)
{
try
{
while (s.length() != 0)
{
String command = s.substring(0,1);
s = s.substring(1);
if (command.equals("+"))
{
tape.setValue(tape.getValue() + 1);
}
else if (command.equals("-"))
{
if (tape.getValue() > 0)
tape.setValue(tape.getValue() - 1);
}
else if(command.equals("."))
{
//System.out.println(tape.get(pointer));
output.add(tape.getValue());
}
else if(command.equals(","))
{
//prompt for input
}
else if (command.equals(">"))
{
if (tape.getNext() == null)
{
tape.setNext(new TapeSeg<Integer>(0, null, tape));
tape = tape.getNext();
}
else
{
tape = tape.getNext();
}
}
else if (command.equals("<"))
{
if (tape.getPrev() == null)
{
tape.setPrev(new TapeSeg<Integer>(0, tape, null));
tape = tape.getPrev();
}
else
{
tape = tape.getPrev();
}
}
else if (command.equals("["))
{
int step = 0;
String toTranslate = "";
while (!command.equals("]"))
{
command = s.substring(0,1);
toTranslate += command;
s = s.substring(1);
}
toTranslate = toTranslate.substring(0, toTranslate.length() - 1);
while (tape.getValue() != 0 && !failed)
{
translate(toTranslate);
if (step > 1000)
{
failed = true;
}
step++;
}
}
}
}
//let it test its outputs even if invalid
catch (Exception e)
{}
}
public boolean failed()
{
return failed;
}
public ArrayList<Integer> getOutputs()
{
return output;
}
}