-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainfuck.k
268 lines (230 loc) · 6.67 KB
/
brainfuck.k
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
requires "syntax.k"
requires "types.k"
requires "data.k"
module BRAINFUCK
imports DOMAINS
imports BRAINFUCK-SYNTAX
imports BRAINFUCK-DATA
imports BRAINFUCK-TYPES
configuration
<bf>
<k> $PGM:Exp </k>
<ptr> 0:Int </ptr>
<mem> .Map </mem>
<env> DEFAULT </env>
<input> .List </input>
<output> .List </output>
</bf>
// sequence expressions
rule E1:Exp E2:Exp => E1 ~> E2 [macro]
// brainfuck commands to internal op representation
rule + => INCVAL [macro]
rule - => DECVAL [macro]
rule > => INCPTR [macro]
rule < => DECPTR [macro]
rule , => READ [macro]
rule .:Cmd => PRINT [macro]
rule [ ] => LOOP . [macro]
rule [ BODY ] => LOOP BODY [macro]
// halt takes an error code and consumes remaining operations
syntax KItem ::= "#halt" ErrorCode
rule <k> #halt ERROR ~> (_:Exp => .K) ...</k>
rule <k> #halt ERROR ~> (_:Op => .K) ...</k>
/// +
// increment an uninitialized memory cell
rule
<k> INCVAL => .K ...</k>
<ptr> PTR </ptr>
<mem> RHO:Map (.Map => PTR |-> 1) </mem>
requires notBool (PTR in_keys(RHO))
// wrapping increment
rule
<k> INCVAL => .K ...</k>
<ptr> PTR </ptr>
<mem>... PTR |-> (M => chop (inc M) (#intSize ENV)) ...</mem>
<env> ENV </env>
requires WrapWords < ENV >
// non-wrapping increment
rule
<k> INCVAL => .K ...</k>
<ptr> PTR </ptr>
<mem>... PTR |-> (M => inc M) ...</mem>
<env> ENV </env>
requires notBool WrapWords < ENV >
andBool inc M <Int #intSize ENV
// halt on overflow if non-wrapping
rule
<k> (.K => #halt WORD_OVERFLOW) ~> INCVAL ...</k>
<ptr> PTR </ptr>
<mem>... PTR |-> M ...</mem>
<env> ENV </env>
requires notBool WrapWords < ENV >
andBool inc M >=Int #intSize ENV
/// -
// wrapping decrement
rule
<k> DECVAL => .K ...</k>
<ptr> PTR </ptr>
<mem>... PTR |-> (M => chop (dec M) (#intSize ENV)) ...</mem>
<env> ENV </env>
requires WrapWords < ENV >
// wrapping decrement, uninitialized memory cell
rule
<k> DECVAL => .K ...</k>
<ptr> PTR </ptr>
<mem> RHO:Map (.Map => PTR |-> chop -1 (#intSize ENV)) </mem>
<env> ENV </env>
requires WrapWords < ENV >
andBool (notBool (PTR in_keys(RHO)))
// non-wrapping decrement
rule
<k> DECVAL => .K ...</k>
<ptr> PTR </ptr>
<mem>... PTR |-> (M => dec M) ...</mem>
<env> ENV </env>
requires notBool WrapWords < ENV >
andBool M >=Int 1
// halt on underflow if non-wrapping
rule
<k> (.K => #halt WORD_OVERFLOW) ~> DECVAL ...</k>
<ptr> PTR </ptr>
<mem>... PTR |-> M ...</mem>
<env> ENV </env>
requires notBool WrapWords < ENV >
andBool M <Int 1
// halt on decrement of uninitialized memory cell if non-wrapping
rule
<k> (.K => #halt WORD_OVERFLOW) ~> DECVAL ...</k>
<ptr> PTR </ptr>
<mem> RHO:Map </mem>
<env> ENV </env>
requires notBool WrapWords < ENV >
andBool (notBool (PTR in_keys(RHO)))
/// >
// wrapping pointer increment
rule
<k> INCPTR => .K ...</k>
<ptr> PTR => chop (inc PTR) (#memSize ENV) </ptr>
<env> ENV </env>
requires WrapMemory < ENV >
// non-wrapping pointer increment
rule
<k> INCPTR => .K ...</k>
<ptr> PTR => inc PTR </ptr>
<env> ENV </env>
requires notBool WrapMemory < ENV >
andBool inc PTR <Int #memSize ENV
// halt on memory overflow if non-wrapping
rule
<k> (.K => #halt MEMORY_OVERFLOW) ~> INCPTR ...</k>
<ptr> PTR </ptr>
<env> ENV </env>
requires notBool WrapMemory < ENV >
andBool inc PTR >=Int #memSize ENV
/// <
// wrapping pointer decrement
rule
<k> DECPTR => .K ...</k>
<ptr> PTR => chop (dec PTR) (#memSize ENV) </ptr>
<env> ENV </env>
requires WrapMemory < ENV >
// non-wrapping pointer decrement
rule
<k> DECPTR => .K ...</k>
<ptr> PTR => dec PTR </ptr>
<env> ENV </env>
requires notBool WrapMemory < ENV >
andBool dec PTR >=Int 0
// halt on memory underflow if non-wrapping
rule
<k> (.K => #halt MEMORY_OVERFLOW) ~> DECPTR ...</k>
<ptr> PTR => dec PTR </ptr>
<env> ENV </env>
requires notBool WrapMemory < ENV >
andBool dec PTR <Int 0
/// [ ... ]
// enter loop
rule
<k> LOOP OPS => OPS ~> LOOP OPS ...</k>
<ptr> PTR </ptr>
<mem>... PTR |-> M ...</mem>
requires M >Int 0
// continue
rule
<k> LOOP OPS => .K ...</k>
<ptr> PTR </ptr>
<mem>... PTR |-> M ...</mem>
requires M ==Int 0
// continue if uninitialized memory cell (uninitialized is zero)
rule
<k> LOOP OPS => .K ...</k>
<ptr> PTR </ptr>
<mem> RHO:Map </mem>
requires notBool (PTR in_keys(RHO))
// read
// - eof action
// - is eof
// - uninitialized mem cell
// - is within wordsize
/// ,
// read
rule
<k> READ => .K ...</k>
<ptr> PTR </ptr>
<mem>... PTR |-> (_ => ordHead IN ) ...</mem>
<env> ENV </env>
<input> ListItem(IN:String) => .List ...</input>
requires 0 <= ordHead IN < #intSize ENV
// read to an uninitialized memory cell
rule
<k> READ => .K ...</k>
<ptr> PTR </ptr>
<mem> RHO:Map (.Map => PTR |-> ordHead IN ) </mem>
<env> ENV </env>
<input> ListItem(IN) => .List ...</input>
requires notBool (PTR in_keys(RHO))
andBool 0 <= ordHead IN < #intSize ENV
// read from empty input - write EOFValue to cell
rule
<k> READ => .K ...</k>
<ptr> PTR </ptr>
<mem>... PTR |-> (_ => chop (#eofVal ENV) (#intSize ENV)) ...</mem>
<env> ENV </env>
<input> .List </input>
requires notBool (EOFNoOp < ENV >)
// read from empty input - write EOFValue to uninitialized memory cell
rule
<k> READ => .K ...</k>
<ptr> PTR </ptr>
<mem> RHO:Map (.Map => PTR |-> chop (#eofVal ENV) (#intSize ENV)) </mem>
<env> ENV </env>
<input> .List </input>
requires notBool (EOFNoOp < ENV >)
andBool (notBool (PTR in_keys(RHO)))
// read from empty input - EOF no-op
rule
<k> READ => .K ...</k>
<env> ENV </env>
<input> .List </input>
requires EOFNoOp < ENV >
// read a value larger than the wordsize
rule
<k> (.K => #halt WORD_OVERFLOW) ~> READ ...</k>
<env> ENV </env>
<input> ListItem(IN:String) ...</input>
requires notBool 0 <= ordHead IN < #intSize ENV
/// .
// print
rule
<k> PRINT => .K ...</k>
<ptr> PTR </ptr>
<mem>... PTR |-> M ...</mem>
<output>... .List => ListItem(chrChar(M)) </output>
// print an uninitialized memory cell
rule
<k> PRINT => .K ...</k>
<ptr> PTR </ptr>
<mem> RHO:Map </mem>
<output>... .List => ListItem(chrChar(0)) </output> //TODO
requires notBool (PTR in_keys(RHO))
endmodule