Today I implement a REALLY REALLY BASIC stack based virtual machine that does basic math.
s = []
def e(cmd):
global s
f={'add' : 's.append(s.pop()+s.pop())',
'dup' : 's.append(s[-1])',
'sub' : 's.append(s.pop()-s.pop())',
'mul' : 's.append(s.pop()*s.pop())',
'div' : 's.append(s.pop()/s.pop())',
'peak' : 'print s[-1]',
'out' : 'print s.pop()'
}[cmd]
exec(f)
s.append(5)
s.append(1)
e('add')
e('dup')
s.append(12)
e('mul')
e('sub')
e('peak')
e('out')
def e(cmd):
global s
f={'add' : 's.append(s.pop()+s.pop())',
'dup' : 's.append(s[-1])',
'sub' : 's.append(s.pop()-s.pop())',
'mul' : 's.append(s.pop()*s.pop())',
'div' : 's.append(s.pop()/s.pop())',
'peak' : 'print s[-1]',
'out' : 'print s.pop()'
}[cmd]
exec(f)
s.append(5)
s.append(1)
e('add')
e('dup')
s.append(12)
e('mul')
e('sub')
e('peak')
e('out')
The key of the dictionary is the instruction and the value defines what the instruction does.
0 comments:
Post a Comment