-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcalfe.py
267 lines (219 loc) · 6.62 KB
/
calfe.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
#!/usr/bin/env python
# coding: utf-8
import re
import sys
import getopt
from collections import deque
# internal
EXIT = -1
DONE = -2
# instructions
# must be in sync with met.py
NOP = 0
CALL = 1
FALSE = 2
FFLAG = 3
TFLAG = 4
MATCH = 5
PRINT = 6
RETURN = 7
STOP = 8
THEN = 9
TRUE = 10
# stack
class Stack:
def __init__(self):
self.stack = []
def push(self, value):
self.stack.append(value)
def pop(self):
if (len(self.stack) > 0):
return self.stack.pop()
return EXIT
class Machine:
def __init__(self):
self.code = [] # deque()
self.stack = Stack()
self.tape = Stack()
self.pc = 0
self.FLAG = False # global flag
self.INPUT = deque() # [] # ""
self.inp = 0 # input pointer
self.OUTPUT = []
self.outp = 0 # output pointer
self.verbose = 0
def setcode(self, contents):
self.code = contents
def setinput(self, input):
self.INPUT = input
self.OUTPUT = []
def getoutput(self):
return ' '.join(self.OUTPUT)
# get value from address
# increase programcounter
def nextcode(self):
if (self.pc < len(self.code)):
c = self.code[self.pc]
self.pc = self.pc + 1
return c
return EXIT
# parse one op at a time
# then execute/interpret on op
def parse(self, op):
# call address and
# put current pc on stack
if (op == CALL):
address = self.nextcode()
self.tape.push(self.inp)
self.tape.push(self.outp)
self.stack.push(self.pc)
self.pc = address
# if flag is false, then jump address
# always consume address
elif (op == FALSE):
address = self.nextcode()
if (self.FLAG == False):
self.pc = address
arg = address
# set global FLAG to false
elif (op == FFLAG):
self.FLAG = False
# set global FLAG to true
elif (op == TFLAG):
self.FLAG = True
# check what's on input tape
# match item with tape
# put mathcing item on output
# advance input tape
elif (op == MATCH):
item = self.nextcode()
c = self.INPUT[self.inp]
if (c == '$'):
return DONE
if (c == item):
self.FLAG = True
self.inp = self.inp + 1
else:
self.FLAG = False
# print to output tape following item
elif (op == PRINT):
item = self.nextcode()
self.OUTPUT.insert(self.outp, item)
self.outp = self.outp + 1
# return from call
# pop address from stack
# jump to address
elif (op == RETURN):
# pop address, set pc
address = self.stack.pop()
self.pc = address
# pop the output pointer
self.outp = self.tape.pop()
# if flag is false then set input pointer
# from tape stack
# if true the just pop from stack, correct?
pointer = self.tape.pop()
if (self.FLAG == False):
self.inp = pointer
# explicit stop program and exit loop
elif (op == STOP):
return DONE
# match next item in input
elif (op == THEN):
item = self.nextcode()
c = self.INPUT[self.inp]
if (c == '$'):
return DONE
if (c == item):
if (self.FLAG == True):
self.inp = self.inp + 1
else:
self.FLAG = False
else:
self.FLAG = False
# if flag is then, then jump address
# always consume address
elif (op == TRUE):
address = self.nextcode()
if (self.FLAG == True):
self.pc = address
return
# at last a print out
def printout(self):
print("OUTPUT=", self.getoutput())
print("FLAG=", self.FLAG)
# running code
def run(self, contents, verbose):
self.verbose = verbose
self.setcode(contents)
while (True):
opcode = self.nextcode()
if (opcode == EXIT): # no more codes
return
endflag = self.parse(opcode)
if (endflag == DONE): # explicit stop
if verbose == 1:
self.printout()
return self.getoutput()
class Runner:
def __init__(self):
self.m = Machine()
self.contents = []
# loading files
def load(self, file):
content = []
with open(file, 'r') as f:
for line in f.readlines():
chars = line.strip().split(',')
# if not a digit assume a character for matching, assemble in array
content.extend([int(i) if i.isdigit() else i for i in chars])
f.close()
return content
# loading and saving
def readsample(self, infile, outfile, testfile, verbose):
if verbose == 1:
print("testfile=", testfile)
input = self.load(testfile) + ['$'] # end just in case
self.m.setinput(input)
if verbose == 1:
print("INPUT=", self.m.INPUT)
# processing input file = program
print("infile=", infile)
self.contents = self.load(infile)
# run, get output from program
out = self.m.run(self.contents, verbose)
# save the output
with open(outfile, 'w') as f:
f.write(out)
f.close()
# call with parsing of args to assembler
def main(argv):
inputfile = ''
outputfile = ''
testfile = ''
verbose = 0
try:
opts, args = getopt.getopt(argv, "vht:i:o:", ["tfile", "ifile=", "ofile="])
except getopt.GetoptError:
print('calfe.py -t <testfile> -i <inputfile> -o <outputfile>')
sys.exit(2)
for opt, arg in opts:
if opt == '-v':
verbose = 1
if opt == '-h':
print('usage: calfe.py -t <testfile> -i <inputfile> -o <outputfile>')
sys.exit()
elif opt in ("-t", "--tfile"):
testfile = arg
elif opt in ("-i", "--ifile"):
inputfile = arg
elif opt in ("-o", "--ofile"):
outputfile = arg
if verbose == 1:
print("running ..")
run = Runner()
run.readsample(inputfile, outputfile, testfile, verbose)
if verbose == 1:
print("done.")
if __name__ == "__main__":
main(sys.argv[1:])