-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathVirtualMachine.py
358 lines (305 loc) · 12 KB
/
VirtualMachine.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
import sys #needed for sys.stdout.write
import math
import operator #allows operator.add, operator.mul etc
import Library # Must prefix function calls with "Library."
#from numpy.py import npv
def ltrim(*args):
mystring = args[0]
if len(args) == 1:
return mystring.lstrip()
elif len(args) == 2:
return mystring.lstrip(args[1])
elif len(args) >= 3:
if type(args[1] == type(1)):
#second parameter is nFlags
nFlags = args[1]
stripchars = ''.join(args[2:])
else:
# nFlags is ommitted
nFlags = 0
stripchars = ''.join(args[1:])
if nFlags == 0:
#case sensitive
return mystring.lstrip(stripchars)
else:
#case insensitive
return mystring.lower().lstrip(stripchars.lower())
def rtrim(*args):
mystring = args[0]
if len(args) == 1:
return mystring.rstrip()
elif len(args) == 2:
return mystring.rstrip(args[1])
elif len(args) >= 3:
if type(args[1] == type(1)):
#second parameter is nFlags
nFlags = args[1]
stripchars = ''.join(args[2:])
else:
# nFlags is omitted
nFlags = 0
stripchars = ''.join(args[1:])
if nFlags == 0:
#case sensitive
return mystring.rstrip(stripchars)
else:
#case insensitive
return mystring.lower().rstrip(stripchars.lower())
def alltrim(*args):
mystring = args[0]
if len(args) == 1:
return mystring.strip()
elif len(args) == 2:
return mystring.strip(args[1])
elif len(args) >= 3:
if type(args[1] == type(1)):
#second parameter is nFlags
nFlags = args[1]
stripchars = ''.join(args[2:])
else:
# nFlags is ommitted
nFlags = 0
stripchars = ''.join(args[1:])
if nFlags == 0:
#case sensitive
return mystring.strip(stripchars)
else:
#case insensitive
return mystring.lower().strip(stripchars.lower())
def left(my_string, num_chars):
return my_string[:num_chars]
def right(my_string, num_chars):
return my_string[len(my_string)-num_chars:]
def string(number, length=10, decimalplaces=0):
# equivalent of STR() in VFP
#this routine should round up but doesn't do any rounding at the moment
#this routine should padl with spaces if the specified "length" is greater than the length of the value
format_string = "{:."+str(decimalplaces)+"f}"
return left(format_string.format(number), length)
def substr(mystring, startingpos, numchars=-1):
startingpos = startingpos-1 #Python is zero indexed but VFP is 1 indexed
if numchars == -1:
# numchars has not been specified so return the whole string
return mystring[startingpos:]
else:
return mystring[startingpos:startingpos+numchars]
class VirtualMachine:
# Note any properties/members instantiated here will be shared by ALL INSTANCES of this class.
# See https://docs.python.org/3/tutorial/classes.html for details
def __init__(self):
#Instantiate the instance variable
self.code = "" #Code block to execute
self.ip = 0 # Instruction Pointer
## could we make these classes static or class members rather than instance
self.COMMAND_DICT = {
0x02: self.QuestionMark,
0x03: self.QuestionMarkDouble,
0x54: self.Assignment
}
self.OPERATOR_DICT = {
0x04: operator.mul,
0x05: operator.pow,
0x06: operator.add,
0x08: operator.sub,
0x0C: operator.truediv,
0x19: abs,
0x1C: ord,
0x20: chr,
0x3D: left,
0x3E: len,
0x40: lambda my_string: my_string.lower(), #lower
0x41: ltrim,
0x44: max,
0x46: min,
0x47: operator.mod,
0x52: right,
0x56: rtrim, # this seems to be a duplicate of 0x60
0x5A: string, ## STR() in VFP
0x5C: substr,
0x60: rtrim, # this seems to be a duplicate of 0x56
0x66: lambda my_string: my_string.upper(), #upper
#0x75: npv, ## needs install and import of numpy
0x9B: alltrim,
0xB1: lambda my_string, length: my_string.rjust(length), #padl - equivalent of right justify
0xB2: lambda my_string, length: my_string.rjust(length), #padr - equivalent of left justify
0xEA: self.extended
}
self.TERM_DICT = {
0xD9: self.ReadString,
0xE9: self.Int32,
0xF7: self.GetVariable,
0xF8: self.Int8,
0xF9: self.Int16,
0xFA: self.Double,
0xFC: self.Expression
}
self.locations = [] #List to hold variables (maximum 65536)
def extended(self, *params):
EXTENDED_FUNCTIONS = {
0x43: math.cos,
0x44: math.sin,
0x45: math.tan,
0x46: math.acos,
0x47: math.asin,
0x48: math.atan,
0x4C: math.exp
}
next_char = self.read_byte()
f = EXTENDED_FUNCTIONS[next_char]
return f(*params)
def interpret(self):
size_code_area = len(self.code)
while self.ip+3 < size_code_area:
self.codeline()
def codeline(self):
size_line = self.read_ushort() #Find out how long this line of code is
next_line = self.ip + size_line
self.command()
self.match(0xFE) ## end of code line
# self.ip = next_line - 2
def command(self):
# Can this be combined with Expression()?
opcode = self.read_byte()
if opcode in self.COMMAND_DICT:
command = self.COMMAND_DICT[opcode]
command()
else:
print("opcode " + str(opcode) + " 0x" + hex(opcode) + " is not recongised")
def consume(self, hexcode):
#Consume the expected character
num = self.code[self.ip]
if num == hexcode:
self.move_pointer(1)
else:
print("Position in code section: ", hex(self.ip))
print("Expected: ", hex(hexcode))
print("Got: ", hex(num))
def consumeCC(self):
#For an unknown reason a Double is sometimes followed by 0xCC which needs to be consumed if present
self.consume(0xCC)
def match(self, expected):
cur_pos = self.ip
got = self.code[self.ip]
self.move_pointer(1)
if got != expected:
print("File pos: ",cur_pos)
print("Expected: ", hex(expected))
print("Got: ", hex(got))
def move_pointer(self, numbytes):
self.ip = self.ip+numbytes
def read_byte(self):
mybyte = self.code[self.ip]
self.move_pointer(1)
return mybyte
def read_double(self):
mydouble = Library.double_to_float(self.code[self.ip:self.ip+8])
self.move_pointer(8)
return mydouble
def read_short(self):
myshort = Library.short_to_int(self.code[self.ip:self.ip+2])
self.move_pointer(2) #Move the ip on 2 bytes
return myshort
def read_ushort(self):
#mycode = self.code[self.ip:self.ip+2]
myshort = Library.ushort_to_int(self.code[self.ip:self.ip+2])
self.move_pointer(2) #Move the ip on 2 bytes
return myshort
def read_long(self):
mylong = Library.long_to_int(self.code[self.ip:self.ip+4])
self.move_pointer(4)
return mylong
def read_ulong(self):
mylong = Library.ulong_to_int(self.code[self.ip:self.ip+4])
self.move_pointer(4)
return mylong
## VFP Commands ##
def Assignment(self):
self.consume(0xF7)
location=self.read_ushort()
self.consume(0x10) ## not sure what this is for! Maybe data type?
next_byte = self.read_byte()
self.locations.insert(location, self.term(next_byte))
def Double(self):
#Doubles are stored using N(10.3) format the same as numeric fields in tables.
#The value is stored in IEEE 754 double-precision binary floating-point format: binary64
#see http://en.wikipedia.org/wiki/Double-precision_floating-point_format
digits = self.read_byte()
decimals = self.read_byte()
mydouble = self.read_double()
self.consumeCC() #For an unknown reason the double is sometimes followed by 0xCC which needs to be consumed if present
return mydouble
def Expression(self):
# Expression := Term [Term Operand] (BNF format)
stack = [] # initialise the stack
#loop for each element until end of expression
while True: ## Look <> "\xFD":
next_byte = self.read_byte()
if next_byte == 0xFD:
##print "End Expression"
break
if next_byte in self.OPERATOR_DICT:
#This is an OPERATOR opcode
f = self.OPERATOR_DICT[next_byte] ##Get function reference
params = []
# while len(stack) > 0:
# ## interate through the stack popping the last value into a parameter list
# a = stack.pop() ##POP value off the stack
# if a == 0x43: ##end of parameter list opcode
# break
# params.insert(0,a) ## Insert at start of parameter list (The stack is in reverse order to the parameter list)
## Pop the last two items off the stack into the params list (The stack is in reverse order to the parameter list)
if len(stack):
params.insert(0, stack.pop())
if len(stack):
item = stack.pop()
if item != 0x43:
params.insert(0, item)
stack.append(f(*params)) ##execute the function and push the result onto the stack
elif next_byte != 0x03: ## seems to sometimes have 0x03 in middle of expression for some reason!??
#push value onto stack
stack.append(self.term(next_byte))
#print "Pos before match 0xFD",progfile.tell()
#MatchNum(0xFD) #consume expression terminator
#pop and return last value off stack
return stack.pop()
def term(self, next_byte):
if next_byte in self.TERM_DICT:
func = self.TERM_DICT[next_byte]
return func()
else:
return next_byte
def GetVariable(self):
location = self.read_ushort()
return self.locations[location]
def Int8(self):
digits = self.read_byte()
return self.read_byte()
def Int16(self):
digits = self.read_byte()
return self.read_short()
def Int32(self):
digits = self.read_byte()
return self.read_long()
def QuestionMark(self):
#print("Question Mark")
sys.stdout.write('\n') ##writes new line
self.QuestionMarkDouble()
def QuestionMarkDouble(self):
#print("Double Question Mark")
self.move_pointer(2) #Skip the F8 03 bytes which are part of the 02 F8 03 code for the ? statement
number_params = self.read_byte()
## mytext = self.Expression() #.decode("ascii") #decodes bytes into a string
next_byte = self.read_byte()
mytext = self.term(next_byte)
sys.stdout.write(str(mytext))
def ReadBytes(self, numbytes):
mybytes=self.code[self.ip:self.ip+numbytes]
self.ip=self.ip+numbytes
return mybytes
def String(self):
string_length = self.read_ushort()
return self.ReadBytes(string_length)
def ReadString(self):
string_length = self.read_ushort()
bytestring = self.ReadBytes(string_length)
return bytestring.decode() # default encoding is 'ascii'