-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainfuck.bas
69 lines (66 loc) · 1.83 KB
/
brainfuck.bas
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
#INCLUDE "crt.bi"
DIM SHARED src AS STRING
DIM SHARED lenSrc AS INTEGER
DIM SHARED pc AS INTEGER
DIM SHARED vPtr AS INTEGER
DIM SHARED buffer(30000) AS UBYTE
DIM SHARED stack(30000) AS INTEGER
DIM SHARED stackPtr AS INTEGER
SUB get_source()
DIM AS STRING lineInput
DIM AS INTEGER fileNumber
DIM AS STRING filePath
IF COMMAND(1) <> "" THEN
filePath = COMMAND(1)
fileNumber = FREEFILE
OPEN filePath FOR INPUT AS #fileNumber
WHILE NOT EOF(fileNumber)
LINE INPUT #fileNumber, lineInput
src += lineInput
WEND
CLOSE #fileNumber
lenSrc = LEN(src)
ELSE
PRINT "Please provide the path to the Brainfuck source code file."
END
END If
END SUB
SUB interpret()
WHILE pc <= lenSrc
SELECT CASE MID(src, pc, 1)
CASE ">"
vPtr += 1
CASE "<"
vPtr -= 1
CASE "+"
buffer(vPtr) += 1
CASE "-"
buffer(vPtr) -= 1
CASE "."
PRINT CHR(buffer(vPtr));
CASE ","
buffer(vPtr) = ASC(INPUT(1))
CASE "["
IF buffer(vPtr) = 0 THEN
DIM level AS INTEGER = 1
DO
pc += 1
IF MID(src, pc, 1) = "[" THEN level += 1
IF MID(src, pc, 1) = "]" THEN level -= 1
LOOP UNTIL level = 0
ELSE
stack(stackPtr) = pc
stackPtr += 1
END IF
CASE "]"
IF buffer(vPtr) <> 0 THEN
pc = stack(stackPtr - 1)
ELSE
stackPtr -= 1
END IF
END SELECT
pc += 1
WEND
END SUB
get_source()
interpret()