-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimbre.py stacks in memory
406 lines (331 loc) · 13.2 KB
/
timbre.py stacks in memory
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# Timbre written in Python Robert Chapman III Jul 30, 2011
# stacks in memory - top in variable
MSIZE = 1000
DCELLS = 30
RCELLS = 30
LINELENGTH = 80
cellmask = 0xFFFFFFFF
def int2base(integer, base=10): # number converter
import string
if not integer: return '0'
sign = 1 if integer > 0 else -1
alphanum = string.digits + string.ascii_uppercase
nums = alphanum[:base]
res = ''
integer *= sign
while integer:
integer, mod = divmod(integer, base)
res += nums[mod]
return ('' if sign == 1 else '-') + res[::-1]
class Timbre(): # a Timbre interpreter
def __init__(self):
import sys
self.top = 0
self.rp0 = -1
self.sp0 = self.rp0 - RCELLS
self.memory = [0]*MSIZE
self.firstAddress = 0
self.basev = self.nextLocation()
self.dp = self.firstAddress
self.dictionary = {}
self.eol = '\n'
self.output = sys.stdout.write
self.reset()
def nextLocation(self): # allocation a location in dictionary
a = self.firstAddress
self.firstAddress += 1
return a
def reset(self): # ...- reset all data sets
self.sp = self.sp0
self.rp = self.rp0
self.dp = self.firstAddress
self.emptyDict()
self.decimal()
def emptyDict(self):
dict = {
#data stack
'dup': self.dup,
'drop': self.drop,
'swap': self.swap,
'over': self.over,
'?dup': self.qdup,
'sp!': self.spStore,
#return stack
'>r': self.tor,
'r>': self.rfrom,
'r': self.r,
#operations
'and': self.bitand,
'or': self.bitor,
'xor': self.xor,
'not': self.bitnot,
'shift': self.shift,
'negate': self.negate,
'+': self.plus,
'-': self.minus,
'/': self.slash,
'mod': self.mod,
'/mod': self.slashMod,
'*': self.star,
#compares
'=': self.equals,
'<': self.lessthan,
'>': self.greaterthan,
'u<': self.ulessthan,
'u>': self.ugreaterthan,
'abs': self.absv,
'min': self.minv,
'max': self.maxv,
#memory
'@': self.fetch,
'!': self.store,
'c@': self.cfetch,
'c!': self.cstore,
'+b': self.plusbits,
'-b': self.minusbits,
'cmove': self.cmove,
'fill': self.fill,
'erase': self.erase,
#dictionary
'here': self.here,
'allot': self.allot,
'c,': self.ccomma,
',': self.comma,
'find': self.find,
'execute': self.execute,
#output
'emit': self.emit,
'cr': self.cr,
'count': self.count,
'type': self.emits,
'base': self.base,
'hex': self.hexBase,
'decimal': self.decimal,
'.': self.dot,
'.r': self.dotr,
'.b': self.dotb,
'.d': self.dotd,
'.h': self.doth,
'.s': self.dots,
#tools
'words': self.words,
'reset': self.reset}
self.dictionary.clear()
for key in dict.keys(): self.dictionary[key] = dict[key]
# stack quarks
def drip(self):
self.dataStack[self.sp] = self.top
def dip(self):
self.top = self.dataStack[self.sp]
def nup(self):
self.sp -= 1
self.dataStack[self.sp] = self.dataStack[self.sp+1]
def nip(self):
self.sp += 1
def tuck(self):
self.sp -= 1
self.dataStack[self.sp] = self.top
def take(self):
self.top = self.dataStack[self.sp+1]
self.dataStack[self.sp+1] = self.dataStack[self.sp]
self.nip()
# data stack activities
def lit(self, n): # - n push a literal to the data stack
self.sp -= 1
self.dataStack[sp] = self.top
self.top = n
def drop(self): # n - throw away the top data stack item
self.dip()
self.nip()
def dup(self): # n - n n make a copy of the top data stack item
sp -= 1
self.drip()
def swap(self): # n m - m n swap top two items on the data stack
self.dataStack[self.sp], top = top, self.dataStack[self.sp]
def over(self): # n m - n m n copy 2nd data stack item to top of data stack
self.lit(self.dataStack[-2])
def qdup(self): # n - n n | - 0 duplicate top data stack item if not 0
if self.dataStack[-1]:
self.dataStack.append(self.dataStack[-1])
def spStore(self): # ... - empty the data stack
del(self.dataStack[:])
# return stack activities
def tor(self): # n - (R - n push the top item of the data stack onto the return stack
self.returnStack.append(self.dataStack.pop())
def rfrom(self): # - n (R n - move top item on return stack to data stack
self.dataStack.append(self.returnStack.pop())
def r(self): # - n (R n - n copy the top item of the return stack onto the data stack
self.dataStack.append(self.returnStack[-1])
# operations
def bitand(self): # n m - p bitwise AND top two data stack items and leave on top
tmp = self.dataStack.pop()
self.dataStack[-1] &= tmp
def bitor(self): # n m - p bitwise OR top two data stack items and leave on top
tmp = self.dataStack.pop()
self.dataStack[-1] |= tmp
def xor(self): # n m - p bitwise XOR top two data stack items and leave on top
tmp = self.dataStack.pop()
self.dataStack[-1] ^= tmp
def bitnot(self): # n - n' invert all bits on the top data stack item
self.dataStack[-1] = ~self.dataStack[-1]
def shift(self): # n m - p shift n by m bit left for minus and right for positive
m = self.dataStack.pop()
n = self.dataStack[-1]
self.dataStack[-1] = n << m if m > 0 else n >> -m
def negate(self): # n - -n complement of top data stack item
self.dataStack[-1] = -self.dataStack[-1]
def plus(self): # n m - p add top two data stack items and leave on top
tmp = self.dataStack.pop()
self.dataStack[-1] += tmp
def minus(self): # n m - p subtract top data stack item from next item and leave on top
tmp = self.dataStack.pop()
self.dataStack[-1] -= tmp
def slash(self): # n m - p divide next data stack item by top and leave on top
tmp = self.dataStack.pop()
self.dataStack[-1] /= tmp
def mod(self): # n m - p modulus next data stack item by top and leave on top
tmp = self.dataStack.pop()
self.dataStack[-1] %= tmp
def slashMod(self): # n m - q r return divide and modulus from top item into next item
t = self.dataStack[-1]
n = self.dataStack[-2]
self.dataStack[-1], self.dataStack[-2] = n/t, n%t
def star(self): # n m - p multiply next data stack item by top and leave on top
tmp = self.dataStack.pop()
self.dataStack[-1] *= tmp
# comparison
def equals(self): # n m - f leave a boolean on stack after equating top two data stack items
tmp = self.dataStack.pop()
self.dataStack[-1] = (self.dataStack[-1] == tmp)
def lessthan(self): # n m - f leave a boolean on stack indicating if next is less than top
tmp = self.dataStack.pop()
self.dataStack[-1] = (self.dataStack[-1] < tmp)
def greaterthan(self): # n m - f leave a boolean on stack indicating if next is greater than top
tmp = self.dataStack.pop()
self.dataStack[-1] = (self.dataStack[-1] > tmp)
def ulessthan(self): # n m - f leave a boolean on stack indicating if unsigned next is less than top
tmp = self.dataStack.pop() & cellmask
self.dataStack[-1] = ((self.dataStack[-1]&cellmask) < tmp)
def ugreaterthan(self): # n m - f leave a boolean on stack indicating if unsigned next is greater than top
tmp = self.dataStack.pop() & cellmask
self.dataStack[-1] = ((self.dataStack[-1]&cellmask) > tmp)
def absv(self): # n - n|-n top data stack item is made positive
self.dataStack[-1] = abs(self.dataStack[-1])
def minv(self): # n m - n|m leave minimum of top two stack items
tmp = self.dataStack.pop()
self.dataStack[-1] = min(self.dataStack[-1], tmp)
def maxv(self): # n m - n|m leave maximum of top two stack items
tmp = self.dataStack.pop()
self.dataStack[-1] = max(self.dataStack[-1], tmp)
# memory
def fetch(self): # a - n return contents of memory using top stack item as the address (processor sized)
self.dataStack[-1] = self.memory[self.dataStack[-1]]
def store(self): # n a - store next into memory using top as address (processor sized)
a = self.dataStack.pop()
self.memory[a] = self.dataStack.pop()
def cfetch(self): # a - c return contents of memory using top stack item as the address (8 bit)
self.dataStack[-1] = self.memory[self.dataStack[-1]] & 0xFF
def cstore(self): # c a - store next into memory using top as address (8 bit)
a = self.dataStack.pop()
self.memory[a] = self.dataStack.pop() & 0xFF
def plusbits(self): # b a - turn on b bits at address a: 0b10001 em +b
a = self.dataStack.pop()
self.memory[a] |= self.dataStack.pop()
def minusbits(self): # b a - turn off b bits at address a: 0b10001 em -b
a = self.dataStack.pop()
self.memory[a] &= ~self.dataStack.pop()
def cmove(self): # s d n -- move n bytes from s to d
n = self.dataStack.pop()
d = self.dataStack.pop()
s = self.dataStack.pop()
self.memory[d:d+n] = self.memory[s:s+n]
def fill(self): # d n p -- fill n bytes from s with x
p = self.dataStack.pop()
n = self.dataStack.pop()
d = self.dataStack.pop()
self.memory[d:d+n] = [p]*n
def erase(self): # s n - erase n bytes from s
s = self.dataStack.pop()
n = self.dataStack.pop()
self.memory[s:s+n] = [0]*n
# dictionary
def here(self): # - a return address of end of dictionary
self.lit(self.dp)
def allot(self): # n - reserve n bytes after end of dictionary
self.dp += self.dataStack.pop()
def ccomma(self): # c - allocate and 1 byte and put value in it
self.memory[self.dp] = self.dataStack.pop() & 0xFF
self.dp += 1
def comma(self): # n - allocate 1 cell and put n into it
self.memory[self.dp] = self.dataStack.pop()
self.dp += 1
def find(self, name=''): # - x return tick of given name
if not name:
name = self.dataStack.pop()
if name in self.dictionary.keys():
self.lit(self.dictionary[name])
else:
self.lit(False)
def execute(self): # x - use the top data stack item as a function call
self.dataStack.pop()()
# output
def emit(self): # c - ) send c to output device
self.output(chr(self.dataStack.pop()))
def cr(self): # send end of line to output device
self.output(self.eol)
def count(self): # a - a' c leave first character and incremented address on stack
top = self.dataStack[-1]
self.dataStack[-1] += 1
self.lit(self.memory[top])
def emits(self): # a n - output n characters starting at a
n = self.dataStack.pop()
a = self.dataStack.pop()
s = ''
for i in range(a, a+n):
s = s + chr(self.memory[i])
self.output(s)
def base(self): # - a return address of number radix
self.lit(self.basev)
def hexBase(self): # interpret all following numbers as hex
self.memory[self.basev] = 16
def decimal(self): # interpret all subsequent numbers as decimal
self.memory[self.basev] = 10
def dot(self): # n - print n in current number base
n = self.dataStack.pop()
s = ' '+int2base(n,self.memory[self.basev])
self.output(s)
def dotr(self): # m n - print m in right field of n digits
n = self.dataStack.pop()
m = self.dataStack.pop()
s = int2base(m,self.memory[self.basev])
self.output(s.rjust(n))
def dotb(self): # n - print n in binary
n = self.dataStack.pop() & cellmask
s = ''
while True:
s = ' ' + int2base(n&0xFF,2).zfill(8) + s
n = n >> 8
if n == 0: break
self.output(s)
def dotd(self): # n - print n in decimal
n = self.dataStack.pop()
s = ' '+int2base(n,10)
self.output(s)
def doth(self): # n - print n in hex
n = self.dataStack.pop() & cellmask
s = ' '+int2base(n,16)
self.output(s)
def dots(self): # print out data stackc
n = len(self.dataStack)
self.output('%i stack items: '%n)
if n > 10: n = 10
for i in range(-n, 0):
self.lit(self.dataStack[i])
self.dot()
# tools
def words(self, filter=''): # i:[pattern] list all words in dictionary
keys = self.dictionary.keys()
keys.sort()
for key in keys:
if key.find(filter) >= 0:
print key,