| 1 | 1 |
new file mode 100644 |
| ... | ... |
@@ -0,0 +1,117 @@ |
| 1 |
+( brainfuck interpreter ) |
|
| 2 |
+ |
|
| 3 |
+%!~ { NEQk NIP }
|
|
| 4 |
+%DEC { #01 SUB }
|
|
| 5 |
+%DEC2 { #0001 SUB2 }
|
|
| 6 |
+%DECr { LITr 01 SUBr }
|
|
| 7 |
+%RTN { JMP2r }
|
|
| 8 |
+%HALT { #0101 #0e DEO2 }
|
|
| 9 |
+%EMIT { #18 DEO }
|
|
| 10 |
+ |
|
| 11 |
+%MEMORY { #8000 }
|
|
| 12 |
+ |
|
| 13 |
+|0000 |
|
| 14 |
+ |
|
| 15 |
+@pointer $2 |
|
| 16 |
+ |
|
| 17 |
+|0100 ( -> ) |
|
| 18 |
+ |
|
| 19 |
+ MEMORY .pointer STZ2 |
|
| 20 |
+ |
|
| 21 |
+ ;program |
|
| 22 |
+ &while |
|
| 23 |
+ LDAk ,op JSR |
|
| 24 |
+ INC2 LDAk ,&while JCN |
|
| 25 |
+ POP2 |
|
| 26 |
+ |
|
| 27 |
+ HALT |
|
| 28 |
+ |
|
| 29 |
+BRK |
|
| 30 |
+ |
|
| 31 |
+( |
|
| 32 |
+ > Move the pointer to the right |
|
| 33 |
+ < Move the pointer to the left |
|
| 34 |
+ + Increment the memory cell at the pointer |
|
| 35 |
+ - Decrement the memory cell at the pointer |
|
| 36 |
+ [ Jump past the matching ] if the cell at the pointer is 0 |
|
| 37 |
+ ] Jump back to the matching [ if the cell at the pointer is nonzero |
|
| 38 |
+ , Input a character and store it in the cell at the pointer |
|
| 39 |
+ . Output the character signified by the cell at the pointer ) |
|
| 40 |
+ |
|
| 41 |
+@op ( op -- ) |
|
| 42 |
+ |
|
| 43 |
+ LIT '> !~ ,&right JCN |
|
| 44 |
+ .pointer LDZ2k INC2 ROT STZ2 |
|
| 45 |
+ POP RTN &right |
|
| 46 |
+ LIT '< !~ ,&left JCN |
|
| 47 |
+ .pointer LDZ2k DEC2 ROT STZ2 |
|
| 48 |
+ POP RTN &left |
|
| 49 |
+ LIT '+ !~ ,&inc JCN |
|
| 50 |
+ .pointer LDZ2 STH2k LDA INC STH2r STA |
|
| 51 |
+ POP RTN &inc |
|
| 52 |
+ LIT '- !~ ,&dec JCN |
|
| 53 |
+ .pointer LDZ2 STH2k LDA DEC STH2r STA |
|
| 54 |
+ POP RTN &dec |
|
| 55 |
+ LIT '. !~ ,&emit JCN |
|
| 56 |
+ .pointer LDZ2 LDA EMIT |
|
| 57 |
+ POP RTN &emit |
|
| 58 |
+ LIT '[ !~ ,&next JCN |
|
| 59 |
+ POP ,goto-next JSR |
|
| 60 |
+ RTN &next |
|
| 61 |
+ LIT '] !~ ,&prev JCN |
|
| 62 |
+ POP ,goto-back JSR |
|
| 63 |
+ RTN &prev |
|
| 64 |
+ POP |
|
| 65 |
+ |
|
| 66 |
+RTN |
|
| 67 |
+ |
|
| 68 |
+@goto-next ( -- ) |
|
| 69 |
+ |
|
| 70 |
+ .pointer LDZ2 LDA #00 EQU JMP RTN |
|
| 71 |
+ |
|
| 72 |
+ ( depth ) LITr 00 |
|
| 73 |
+ INC2 |
|
| 74 |
+ &loop |
|
| 75 |
+ LDAk LIT '[ NEQ ,&no-depth JCN |
|
| 76 |
+ INCr |
|
| 77 |
+ &no-depth |
|
| 78 |
+ LDAk LIT '] NEQ ,&no-end JCN |
|
| 79 |
+ STHkr #00 EQU ,&end JCN |
|
| 80 |
+ DECr |
|
| 81 |
+ &no-end |
|
| 82 |
+ INC2 LDAk ,&loop JCN |
|
| 83 |
+ &end |
|
| 84 |
+ ( depth ) POPr |
|
| 85 |
+ |
|
| 86 |
+RTN |
|
| 87 |
+ |
|
| 88 |
+@goto-back ( -- ) |
|
| 89 |
+ |
|
| 90 |
+ .pointer LDZ2 LDA #00 NEQ JMP RTN |
|
| 91 |
+ |
|
| 92 |
+ ( depth ) LITr 00 |
|
| 93 |
+ DEC2 |
|
| 94 |
+ &loop |
|
| 95 |
+ LDAk LIT '] NEQ ,&no-depth JCN |
|
| 96 |
+ INCr |
|
| 97 |
+ &no-depth |
|
| 98 |
+ LDAk LIT '[ NEQ ,&no-end JCN |
|
| 99 |
+ STHkr #00 EQU ,&end JCN |
|
| 100 |
+ DECr |
|
| 101 |
+ &no-end |
|
| 102 |
+ DEC2 LDAk ,&loop JCN |
|
| 103 |
+ &end |
|
| 104 |
+ ( depth ) POPr |
|
| 105 |
+ |
|
| 106 |
+RTN |
|
| 107 |
+ |
|
| 108 |
+@program ( Hello World! ) |
|
| 109 |
+ |
|
| 110 |
+ 2b 2b 2b 2b 2b 2b 2b 2b 5b 3e 2b 2b 2b 2b 5b 3e |
|
| 111 |
+ 2b 2b 3e 2b 2b 2b 3e 2b 2b 2b 3e 2b 3c 3c 3c 3c |
|
| 112 |
+ 2d 5d 3e 2b 3e 2b 3e 2d 3e 3e 2b 5b 3c 5d 3c 2d |
|
| 113 |
+ 5d 3e 3e 2e 3e 2d 2d 2d 2e 2b 2b 2b 2b 2b 2b 2b |
|
| 114 |
+ 2e 2e 2b 2b 2b 2e 3e 3e 2e 3c 2d 2e 3c 2e 2b 2b |
|
| 115 |
+ 2b 2e 2d 2d 2d 2d 2d 2d 2e 2d 2d 2d 2d 2d 2d 2d |
|
| 116 |
+ 2d 2e 3e 3e 2b 2e 3e 2b 2b 2e 0a |
|
| 117 |
+ |