-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainfuck.csb
201 lines (197 loc) · 2.39 KB
/
brainfuck.csb
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
#Brainfuck interpreter
#NOTE: The interpreter is VERY slow and doesn't have debug features.
0->DimZ
10->Z
Z->Dim
#Cells
1000->I
#Stack
10->Y
0->A
#Array location
1->B
#Program size
0->C
#Cursor location
#X
1->D
#Y
1->E
#Cell pointer
0->F
#Loop buffer
0->G
#Loop stack pointer
1->H
#Cleans input
For 0->X To 10:GetKey:Next
Lbl 0
While A=0
GetKey->A
WhileEnd
If B=Z:Then
Z+1->Z
Z->DimZ
IfEnd
A->Z[B]
#+
If A=77:Then
Locate D,E,"+"
IfEnd
#-
If A=67:Then
Locate D,E,"-"
IfEnd
#> [x]
If A=24:Then
Locate D,E,"*"
IfEnd
#< [÷]
If A=87:Then
Locate D,E,"/"
IfEnd
#( [[]
If A=43:Then
Locate D,E,"["
IfEnd
#) []]
If A=44:Then
Locate D,E,"]"
IfEnd
#Input [,]
If A=45:Then
Locate D,E,","
IfEnd
#Output [.]
If A=26:Then
Locate D,E,"."
IfEnd
# [DEL]
If A=34:Then
0->Z[B]
B-2->B
D-1->D
If D=0:Then
If E>1:Then
E-1->E
16->D
Else
1->D
IfEnd
IfEnd
Locate D,E," "
IfEnd
If A=77 Or A=67 Or A=24 Or A=87 Or A=43 Or A=44 Or A=26 Or A=45:Then
D+1->D
#Line wrap
If D=16:Then
1->D
E+1->E
IfEnd
#New page
If E=4 And D=15:Then
Cls
1->D
1->E
IfEnd
IfEnd
B+1->B
If A!=47:Then
0->A
Goto 0
IfEnd
Cls
#Program size
Z->C
#Cell pointer
C+1->F
#Program size+Cells+Loop stack
Z+I+Y->Z
Z->DimZ
#Readin the program
For 1->B To C
Lbl 2
If Z[B]=43:Then
If G=0:Then
B->G
Else
G->Z[C+I+H]
B->G
H+1->H
IfEnd
If C+I+H=Z:Then
#Error: Too many nested loops
-1->H
Goto 1
IfEnd
IfEnd
If Z[B]=44:Then
If G=0:Then
#Missing [
0->H
Goto 1
IfEnd
If Z[F]=0:Then
If Z[C+I+1]=0:Then
0->G
1->H
Else
Z[C+I+H-1]->G
0->Z[C+I+H-1]
H-1->H
IfEnd
Else
G+1->B
Goto 2
IfEnd
IfEnd
If Z[B]=77:Then
Z[F]+1->Z[F]
IfEnd
If Z[B]=67:Then
Z[F]-1->Z[F]
IfEnd
If Z[B]=24:Then
F+1->F
If F>=C+I:Then
Goto 1
IfEnd
IfEnd
If Z[B]=87:Then
If F=C:Then
#Stop the overwriting of the program
Goto 1
Else
F-1->F
IfEnd
IfEnd
If Z[B]=26:Then
Z[F]@
IfEnd
If Z[B]=45:Then
?->Z[F]
IfEnd
Next
0->Z
Z->DimZ
Stop
Lbl 1
#Error handling
Cls
Locate 1,1,"ERROR"
#Missing ]
If H=-1:Then
Locate 1,2,")? L:"
#Location of the error
Locate 7,2,B
IfEnd
#Missing [
If H=0:Then
Locate 1,2,"(? L:"
Locate 7,2,B
IfEnd
#Location of the character
Locate 1,3,F
Stop
#Var:A B C D E F G H I Y Z Z[]
#Goto: 0 1 2