-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkbf.3
192 lines (190 loc) · 3.93 KB
/
kbf.3
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
.\" $OpenBSD: Exp $
.\"
.\" Copyright (c) 2015 Tristan Le Guern <[email protected]>
.\"
.\" Permission to use, copy, modify, and distribute this software for any
.\" purpose with or without fee is hereby granted, provided that the above
.\" copyright notice and this permission notice appear in all copies.
.\"
.\" THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
.\" WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
.\" MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
.\" ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
.\" WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
.\" ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
.\" OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
.\"
.Dd $Mdocdate: January 18 2015 $
.Dt KBF 3 PRM
.Os
.Sh NAME
.Nm kbf
.Nd Brainfuck interpreter written in Korn shell
.Sh SYNOPSIS
.\" .In kbf.sh
.Fn init
.Fn kbf
.Fo move
.Fa "$index"
.Fc
.Fo cell8
.Fa "$value"
.Fc
.Fo cell16
.Fa "$value"
.Fc
.Fo cell24
.Fa "$value"
.Fc
.Fo cell32s
.Fa "$value"
.Fc
.Fo cell32u
.Fa "$value"
.Fc
.Fo cell64s
.Fa "$value"
.Fc
.Fn output
.Fn input
.Fn nextzero
.Fn prevzero
.Fn stats
.Sh DESCRIPTION
The
.Fn init
function defines the global variables
.Va $instructions ,
.Va $tape ,
.Va $instidx
and
.Va $tapeidx ,
which are respectively the
.Sy Instructions Array ,
the
.Sy Memory Tape ,
the
.Sy Instruction Index
and the
.Sy Tape Index .
It also defines two shorthands variables
.Va $array
and
.Va $cell
which respectively contains the name of either
.Fn arrayksh ,
.Fn arraybash
or
.Fn arrayzsh
and
.Fn cell8 ,
.Fn cell16 ,
.Fn cell24 ,
.Fn cell32s ,
.Fn cell32u ,
or
.Fn cell64s .
.Pp
The
.Fn move
function moves the
.Sy Tape pointer
by the amount of bytes contained in
.Va $index .
This number can be positive or negative. It is not possible to move
bellow the position zero or after the position
.Va $tflag .
.Pp
The
.Fn cell8 ,
.Fn cell16 ,
.Fn cell24 ,
.Fn cell32s ,
.Fn cell32u ,
or
.Fn cell64s
functions increase or decrease the value under the
.Sy Tape Pointer ,
by the amount given as the parameter
.Va $value .
This value can be positive or negative. It is possible to give a large
value that will cause an overflow, also called wrap in Brainfuck
terminology. Underflows are also possible. The value at which a cell will
wrap depend on the function used. The special value 0 is used to reset the
cell value directly at 0.
.Pp
The
.Fn output
function writes the
.Tn ASCII
representation of the byte under the
.Sy Tape Pointer .
.Pp
The
.Fn input
functions writes the value of the
.Tn ASCII
byte read from stdin in the
cell under the
.Sy Tape Pointer .
.Pp
The
.Fn nextzero
function moves the
.Sy Tape Pointer
to the next cell on the right with a value of zero. If none are found,
it moves it to the end of the
.Sy Memory Tape .
.Pp
The
.Fn prevzero
function moves the
.Sy Tape Pointer
to the next cell on the left with a value of zero. If none are found,
it moves it to the beginning of the
.Sy Memory Tape .
.Pp
The
.Fn stats
function writes statistics about the programme, such as the number of
cells used, the number of instructions executed and the state of the
.Sy Memory Tape .
.Pp
The
.Fn kbf
function executes the instructions contained in the
.Sy Instructions Array
with the help of the
.Sy Instructions Pointer .
.Sh INTERMEDIARY REPRESENTATION
The
.Nm
utility is able to transform some parts of the programme into new operators,
for performance purpose :
.Bl -tag -width Ds
.It 0
Replace the clear loop, [-].
.It >>
Replace the next zero loop, [>].
.It <<
Replace the prev zero loop, [<].
.El
.Sh EXAMPLES
This example runs a simple multiplication loop on a
.Sy Memory Tape
already initialized:
.Bd -literal -offset indent
\&. ./kbf.sh
init
$array instructions "+ + + [ > + + + < - ]"
$array tape 5 2
kbf
stats
.Ed
.Sh SEE ALSO
.Xr kbf 1
.Sh AUTHORS
The
.Nm
utility was written by
.An Tristan Le Guern Aq Mt [email protected] .