Browse code

(brainfuck.tal) Optimized

neauoire authored on 15/02/2022 18:24:11
Showing 1 changed files
... ...
@@ -1,57 +1,39 @@
1 1
 ( brainfuck interpreter )
2 2
 
3
-%!~ { NEQk NIP }
4 3
 %DEC { #01 SUB }
5 4
 %DEC2 { #0001 SUB2 }
6 5
 %DECr { LITr 01 SUBr }
7
-%RTN { JMP2r }
8 6
 %HALT { #0101 #0e DEO2 }
9 7
 %EMIT { #18 DEO }
10 8
 
11
-%MEMORY { #8000 }
12
-
13
-|0000
14
-
15
-@ptr $2
16
-
17 9
 |0100 ( -> )
18 10
 
19
-	MEMORY .ptr STZ2
20
-
11
+	;memory
21 12
 	;program
22 13
 	&while
23
-		LDAk ,op JSR
14
+		( Move the pointer to the right )
15
+		LDAk LIT '> NEQ ,&movr JCN [ SWP2 INC2 SWP2 ] &movr
16
+		( Move the pointer to the left )
17
+		LDAk LIT '< NEQ ,&movl JCN [ SWP2 DEC2 SWP2 ] &movl
18
+		( Increment the memory cell at the pointer )
19
+		LDAk LIT '+ NEQ ,&incr JCN [ OVR2 STH2k LDA INC STH2r STA ] &incr
20
+		( Decrement the memory cell at the pointer )
21
+		LDAk LIT '- NEQ ,&decr JCN [ OVR2 STH2k LDA DEC STH2r STA ] &decr
22
+		( Output the character signified by the cell at the pointer )
23
+		LDAk LIT '. NEQ ,&emit JCN [ OVR2 LDA EMIT ] &emit
24
+		( Jump past the matching ] if the cell at the pointer is 0 )
25
+		LDAk LIT '[ NEQ ,&next JCN [ ,goto-next JSR ] &next
26
+		( Jump back to the matching [ if the cell at the pointer is nonzero )
27
+		LDAk LIT '] NEQ ,&prev JCN [ ,goto-back JSR ] &prev
24 28
 		INC2 LDAk ,&while JCN
25 29
 	POP2
26
-
27 30
 	HALT
28 31
 	
29 32
 BRK
30 33
 
31
-@op ( op -- )
32
-
33
-	( Move the pointer to the right )
34
-	LIT '> !~ ,&movr JCN [ .ptr LDZ2k INC2 ROT STZ2 POP RTN ] &movr
35
-	( Move the pointer to the left )
36
-	LIT '< !~ ,&movl JCN [ .ptr LDZ2k DEC2 ROT STZ2 POP RTN ] &movl
37
-	( Increment the memory cell at the pointer )
38
-	LIT '+ !~ ,&incr JCN [ .ptr LDZ2 STH2k LDA INC STH2r STA POP RTN ] &incr
39
-	( Decrement the memory cell at the pointer )
40
-	LIT '- !~ ,&decr JCN [ .ptr LDZ2 STH2k LDA DEC STH2r STA POP RTN ] &decr
41
-	( Output the character signified by the cell at the pointer )
42
-	LIT '. !~ ,&emit JCN [ .ptr LDZ2 LDA EMIT POP RTN ] &emit
43
-	( Jump past the matching ] if the cell at the pointer is 0 )
44
-	LIT '[ !~ ,&next JCN [ POP  ,goto-next JSR RTN ] &next
45
-	( Jump back to the matching [ if the cell at the pointer is nonzero )
46
-	LIT '] !~ ,&prev JCN [ POP ,goto-back JSR RTN ] &prev
47
-	POP
48
-
49
-RTN
50
-
51 34
 @goto-next ( -- )
52 35
 
53
-	.ptr LDZ2 LDA #00 EQU JMP RTN
54
-
36
+	OVR2 LDA #00 EQU JMP JMP2r
55 37
 	( depth ) LITr 00
56 38
 	INC2
57 39
 	&loop
... ...
@@ -64,12 +46,11 @@ RTN
64 46
 	&end
65 47
 	( depth ) POPr
66 48
 
67
-RTN
49
+JMP2r
68 50
 
69 51
 @goto-back ( -- )
70 52
 
71
-	.ptr LDZ2 LDA #00 NEQ JMP RTN
72
-
53
+	OVR2 LDA #00 NEQ JMP JMP2r
73 54
 	( depth ) LITr 00
74 55
 	DEC2
75 56
 	&loop
... ...
@@ -82,11 +63,13 @@ RTN
82 63
 	&end
83 64
 	( depth ) POPr
84 65
 
85
-RTN
66
+JMP2r
86 67
 
87 68
 @program ( Hello World! )
88 69
 
89 70
 	"++++++++[>++++[>++>+++>+++>+<<<<
90 71
 	"-]>+>+>->>+[<]<-]>>.>---.+++++++
91 72
 	"..+++.>>.<-.<.+++.------.-------
92
-	"-.>>+.>++.
73
+	"-.>>+.>++. $1
74
+
75
+@memory
93 76
\ No newline at end of file