-
Notifications
You must be signed in to change notification settings - Fork 0
/
filska.py
604 lines (516 loc) · 15.4 KB
/
filska.py
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#
# file: filska.py
#
# Filska interpreter. Python 3.
#
# RTK, 05-Jul-2020
# Last update: 07-Feb-2022
#
################################################################
import sys
import time
import random
import signal
from math import fmod, ceil, floor, sqrt
from math import sin, cos, tan, log, exp, pow, asin, acos, atan
################################################################
# ctrlC
#
def ctrlC(sig, frame):
print()
sys.exit(0)
signal.signal(signal.SIGINT, ctrlC)
################################################################
# Filska
#
class Filska:
"""Implement a Filska machine and interpreter"""
#-----------------------------------------------------------
# Parse
#
def Parse(self, src):
"""
Parse Filska source code
"""
eoln = False
t = ""
for c in src:
if (eoln) and (c == '\n'):
eoln = False
elif (c == '"') and (not eoln):
eoln = True
elif (not eoln):
t += c
return " ".join(t.split()).upper().split()
#-----------------------------------------------------------
# ExtractProg
#
def ExtractProg(self, tokens, ks):
"""Extract a program"""
k = ks + 1
p = []
while (k < len(tokens)) and (tokens[k] != "}"):
p.append(tokens[k])
k += 1
self.prog[p[0]] = p[1:]
self.PC[p[0]] = 0
self.mem[p[0]] = 0.0
self.flags[p[0]] = [False, False, False, False]
return k
#-----------------------------------------------------------
# InitializeProg
#
def InitializeProg(self, tokens):
"""Scan the tokens to set up the program space"""
idx = 0
k = 0
while (k < len(tokens)):
if (tokens[k] == "{"):
k = self.ExtractProg(tokens,k)
else:
k += 1
#-----------------------------------------------------------
# Trace
#
def Trace(self):
"""Show the next instruction in trace mode"""
prog = self.prog[self.CP]
pc = self.PC[self.CP]
flags = self.flags[self.CP]
mem = self.mem[self.CP]
inst = prog[pc]
print("CP:%s,PC:%03d,X:%f,Y:%f,Z:%f,M:%f" % (self.CP, pc, self.X, self.Y, self.Z, mem), end="")
Z = 1 if (flags[0]) else 0
E = 1 if (flags[1]) else 0
L = 1 if (flags[2]) else 0
G = 1 if (flags[3]) else 0
print(",Z:%d,E:%d,L:%d,G:%d, %7s" % (Z,E,L,G, inst))
_ = input("<enter> or 'q' to quit: ").lower()
if (_ == "q"):
exit(0)
#-----------------------------------------------------------
# Instructions
#
def INC(self, rest, mem, flags):
mem += 1
flags[0] = False
if (mem == 0):
flags[0] = True
return mem, flags
def DEC(self, rest, mem, flags):
mem -= 1
flags[0] = False
if (mem == 0):
flags[0] = True
return mem, flags
def SET(self, rest, mem, flags):
try:
n = float(rest)
except:
n = 0.0
mem = n
flags[0] = False
if (mem == 0):
flags[0] = True
return mem, flags
def RND(self, rest, mem, flags):
mem = random.random()
return mem, flags
def HLT(self, rest, mem, flags):
print()
exit(0)
# Binary operators
# rest is "<dst>=<op1><op2>, e.g., y=mx"
#
def ops(self, rest, mem):
if (len(rest) != 4) or (rest[1] != "="):
raise ValueError("Illegal operands: %s" % rest)
dst = rest[0]
op0 = op1 = 0.0
if (rest[2] == "M"):
op0 = mem
elif (rest[2] == "X"):
op0 = self.X
elif (rest[2] == "Y"):
op0 = self.Y
elif (rest[2] == "Z"):
op0 = self.Z
if (rest[3] == "M"):
op1 = mem
elif (rest[3] == "X"):
op1 = self.X
elif (rest[3] == "Y"):
op1 = self.Y
elif (rest[3] == "Z"):
op1 = self.Z
return op0, op1, dst
def assign(self, ans, dst, mem, flags):
if (dst == "M"):
mem = ans
flags[0] = False
if (mem == 0):
flags[0] = True
elif (dst == "X"):
self.X = ans
elif (dst == "Y"):
self.Y = ans
else:
self.Z = ans
return mem, flags
def ADD(self, rest, mem, flags):
op0,op1,dst = self.ops(rest,mem)
ans = op0 + op1
return self.assign(ans, dst, mem, flags)
def SUB(self, rest, mem, flags):
op0,op1,dst = self.ops(rest,mem)
ans = op0 - op1
return self.assign(ans, dst, mem, flags)
def MUL(self, rest, mem, flags):
op0,op1,dst = self.ops(rest,mem)
ans = op0 * op1
return self.assign(ans, dst, mem, flags)
def DIV(self, rest, mem, flags):
op0,op1,dst = self.ops(rest,mem)
ans = op0 / op1
return self.assign(ans, dst, mem, flags)
def MOD(self, rest, mem, flags):
op0,op1,dst = self.ops(rest,mem)
ans = fmod(op0, op1)
return self.assign(ans, dst, mem, flags)
def POW(self, rest, mem, flags):
op0,op1,dst = self.ops(rest,mem)
ans = pow(op0, op1)
return self.assign(ans, dst, mem, flags)
# Unary operators -- memory only
def SQR(self, rest, mem, flags):
try:
mem = sqrt(mem)
except:
mem = 0.0 # real only
flags[0] = False
if (mem == 0.0):
flags[0] = True
return mem, flags
def NEG(self, rest, mem, flags):
mem = -mem
flags[0] = False
if (mem == 0.0):
flags[0] = True
return mem, flags
def SIN(self, rest, mem, flags):
mem = sin(mem)
flags[0] = False
if (mem == 0.0):
flags[0] = True
return mem, flags
def COS(self, rest, mem, flags):
mem = cos(mem)
flags[0] = False
if (mem == 0.0):
flags[0] = True
return mem, flags
def TAN(self, rest, mem, flags):
mem = tan(mem)
flags[0] = False
if (mem == 0.0):
flags[0] = True
return mem, flags
def ASN(self, rest, mem, flags):
mem = asin(mem)
flags[0] = False
if (mem == 0.0):
flags[0] = True
return mem, flags
def ACS(self, rest, mem, flags):
mem = acos(mem)
flags[0] = False
if (mem == 0.0):
flags[0] = True
return mem, flags
def ATN(self, rest, mem, flags):
mem = atan(mem)
flags[0] = False
if (mem == 0.0):
flags[0] = True
return mem, flags
def LOG(self, rest, mem, flags):
try:
mem = log(mem)
except:
mem = 0.0
flags[0] = False
if (mem == 0.0):
flags[0] = True
return mem, flags
def EXP(self, rest, mem, flags):
mem = exp(mem)
flags[0] = False
if (mem == 0.0):
flags[0] = True
return mem, flags
def FLR(self, rest, mem, flags):
mem = floor(mem)
flags[0] = False
if (mem == 0.0):
flags[0] = True
return mem, flags
def CEL(self, rest, mem, flags):
mem = ceil(mem)
flags[0] = False
if (mem == 0.0):
flags[0] = True
return mem, flags
def SWP(self, rest, mem, flags):
if (rest == "MX") or (rest == "XM"):
t = mem; mem = self.X; self.X = t
elif (rest == "MY") or (rest == "YM"):
t = mem; mem = self.Y; self.Y = t
elif (rest == "MZ") or (rest == "ZM"):
t = mem; mem = self.Z; self.Z = t
elif (rest == "XY") or (rest == "YX"):
t = self.X; self.X = self.Y; self.Y = t
elif (rest == "XZ") or (rest == "ZX"):
t = self.X; self.X = self.Z; self.Z = t
elif (rest == "YZ") or (rest == "ZY"):
t = self.Y; self.Y = self.Z; self.Z = t
else:
raise ValueError("Illegal SWP command")
flags[0] = False
if (mem == 0):
flags[0] = True
return mem, flags
def TMX(self, rest, mem, flags):
self.X = mem
return mem, flags
def TMY(self, rest, mem, flags):
self.Y = mem
return mem, flags
def TMZ(self, rest, mem, flags):
self.Z = mem
return mem, flags
def TXM(self, rest, mem, flags):
mem = self.X
if (mem == 0):
flags[0] = True
return mem, flags
def TYM(self, rest, mem, flags):
mem = self.Y
if (mem == 0):
flags[0] = True
return mem, flags
def TZM(self, rest, mem, flags):
mem =self.Z
if (mem == 0):
flags[0] = True
return mem, flags
def CHR(self, rest, mem, flags):
n = int(mem) & 255
if (n == 10) or (n == 13):
print()
else:
w = chr(n)
print("%s" % w, end="")
return mem, flags
def PRT(self, rest, mem, flags):
if (mem == int(mem)):
print("%d" % int(mem), end="")
else:
print("%0.10g" % mem, end="")
return mem, flags
def IPT(self, rest, mem, flags):
try:
t = input()
except:
print()
exit(0)
try:
mem = float(t)
except:
mem = 0.0
flags[0] = False
if (mem == 0.0):
flags[0] = True
return mem, flags
def CMP(self, rest, mem, flags):
if (rest == "X"):
n = self.X
elif (rest == "Y"):
n = self.Y
elif (rest == "Z"):
n = self.Z
else:
try:
n = float(rest)
except:
n = 0
# Z E L G
flags = [False, False, False, False]
if (mem < n):
flags[2] = True
if (mem > n):
flags[3] = True
if (n == mem):
flags[1] = True
if (mem == 0):
flags[0] = True
return mem, flags
def JMP(self, inst, pc, proglen):
pc = (pc+1) % proglen
cp = inst[4:]
return pc, cp
def JPR(self, inst, pc):
pc = 0
cp = inst[4:]
return pc, cp
def GTO(self, inst, pc, proglen):
try:
offset = int(inst[4:])
if (offset == 0):
offset = 1
except:
offset = 1
pc += offset
if (pc < 0):
pc = 0
if (pc >= proglen):
pc = proglen-1
return pc
def TST(self, inst, pc, proglen, flags):
if (inst[4] == "Z"):
v = flags[0]
elif (inst[4] == "E"):
v = flags[1]
elif (inst[4] == "L"):
v = flags[2]
elif (inst[4] == "G"):
v = flags[3]
elif (inst[4] == "N"):
v = (flags[0] == False)
if (v):
try:
offset = int(inst[6:])
if (offset == 0):
offset = 1
except:
offset = 1
pc += offset
if (pc < 0):
pc = 0
if (pc >= proglen):
pc = proglen-1
else:
pc = (pc+1) % proglen
return pc
#-----------------------------------------------------------
# Exec
#
def Exec(self, inst, proglen, mem, pc, flags):
"""Execute the instruction and return new state"""
cp = self.CP
if (inst[:3] == "JMP"):
pc, cp = self.JMP(inst, pc, proglen)
elif (inst[:3] == "JPR"):
pc, cp = self.JPR(inst, pc)
elif (inst[:3] == "GTO"):
pc = self.GTO(inst, pc, proglen)
elif (inst[:3] == "TST"):
pc = self.TST(inst, pc, proglen, flags)
else:
if (inst[:3] not in self.exe):
raise ValueError("Illegal instruction: %s" % inst)
rest = inst[4:]
mem, flags = self.exe[inst[:3]](rest, mem, flags)
pc = (pc+1) % proglen
return cp, pc, mem, flags
#-----------------------------------------------------------
# Execute
#
def Execute(self):
"""Execute one instruction"""
# Get the instruction and state
prog = self.prog[self.CP]
pc = self.PC[self.CP]
flags = self.flags[self.CP]
mem = self.mem[self.CP]
inst = prog[pc]
# Execute it
cp, pc, mem, flags = self.Exec(inst, len(prog), mem, pc, flags)
# Update the state
self.mem[self.CP] = mem
self.PC[self.CP] = pc
self.flags[self.CP] = flags
# Move to any new program if cp changed -- reset flags
if (self.CP != cp):
self.CP = cp
self.flags[cp] = [False, False, False, False]
#-----------------------------------------------------------
# Run
#
def Run(self):
"""Run the program in memory"""
if (self.trace):
self.Trace()
while (True):
self.Execute() # execute one instruction
if (self.trace): # trace, if set
self.Trace()
time.sleep(self.naptime) # take a nap
#-----------------------------------------------------------
# __init__
#
def __init__(self, src, trace=False):
"""Constructor"""
# Trace or not - console only
self.trace = trace
# Controls program execution speed
self.naptime = 0.00001 # seconds
# Set up the machine state
self.X = self.Y = self.Z = 0 # global registers
self.mem = {}
self.prog = {}
self.PC = {}
self.CP = "MAIN"
# Per program flags:
# Z,E,L,G
self.flags = {}
# Instructions
self.exe = {
"ADD": self.ADD, "SUB": self.SUB,
"MUL": self.MUL, "DIV": self.DIV,
"MOD": self.MOD, "INC": self.INC,
"DEC": self.DEC, "SIN": self.SIN,
"COS": self.COS, "TAN": self.TAN,
"LOG": self.LOG, "EXP": self.EXP,
"FLR": self.FLR, "CEL": self.CEL,
"RND": self.RND, "SWP": self.SWP,
"SET": self.SET, "CMP": self.CMP,
"NEG": self.NEG, "HLT": self.HLT,
"CHR": self.CHR, "PRT": self.PRT,
"IPT": self.IPT, "POW": self.POW,
"TMX": self.TMX, "TMY": self.TMY,
"TXM": self.TXM, "TYM": self.TYM,
"TZM": self.TZM, "TMZ": self.TMZ,
"SQR": self.SQR, "ASN": self.ASN,
"ACS": self.ACS, "ATN": self.ATN,
}
# Set up memory and program space
self.tokens = self.Parse(src)
self.InitializeProg(self.tokens)
################################################################
# main
#
def main():
"""Run a Filska program"""
if (len(sys.argv) == 1):
print()
print("filska <code> [-t]")
print()
print(" <code> - the Filska program to run (.filska)")
print(" -t - trace mode on")
print()
return
trace = False if (len(sys.argv) < 3) else True
app = Filska(open(sys.argv[1]).read(), trace=trace)
app.Run()
if (__name__ == "__main__"):
main()