from os import system
from time import sleep
from js import setInterval, document
from pyodide import create_proxy
MEMORY_SIZE = 21
memory = [0]*MEMORY_SIZE
instruction = 0
a = 0
b = 0
c = 0
i = 0
j = 0
k = 0
x = 0
y = 0
z = 0
def reset(event):
global instruction
global a,i,j,k,x,y,z
global memory
memory[0] = 'a = 0'
memory[1] = 'i = 0'
memory[2] = 'j = 14'
memory[3] = 'k = 17'
memory[4] = 'x = memory[j]'
memory[5] = 'y = memory[k]'
memory[6] = 'z = x * y'
memory[7] = 'a = a + z'
memory[8] = 'i = i + 1'
memory[9] = 'j = j + 1'
memory[10] = 'k = k + 1 '
memory[11] = 'if i < 3: goto(4)'
memory[12] = 'memory[20] = a'
memory[13] = 'halt'
memory[14] = 0.1
memory[15] = 0.8
memory[16] = 0.2
memory[17] = 0.9
memory[18] = 0.1
memory[19] = 0.1
memory[20] = 0
instruction=0
a=i=j=k=x=y=z=0
print_computer()
def goto(cell):
global instruction
instruction = cell-1
def print_computer():
out = '╔═══ Processor ══════════════╗' + '
'
out += ' instruction: %d' % instruction + '
'
out += ' Short Term Memory' + '
'
out += ' a: %2.2f b: %2.2f c: %2.2f' % (a,b,c) + '
'
out += ' i: %4d j: %4d k: %4d' % (i,j,k) + '
'
out += ' x: %2.2f y: %2.2f z: %2.2f' % (x,y,z) + '
'
out += '╚════════════════════════════╝' + '
'
out += '╔═══ Memory ═════════════════╗' + '
'
for cell in range(len(memory)):
marker = "→ " if cell==instruction else " "
T = type(memory[cell])
if T == float:
formatted_cell = ("%2.2f" % memory[cell])
elif T==int:
formatted_cell = ("%d" % memory[cell])
else:
formatted_cell = memory[cell]
out += marker + "%2d" % cell + " : " + formatted_cell + '
'
out += '╚════════════════════════════╝' + '
'
pyscript.write('output', out)
def compute():
global instruction
if memory[instruction] != 'halt':
exec(memory[instruction], globals())
instruction = instruction + 1
print_computer()
reset(None)
_ = setInterval(create_proxy(compute), 1000)
document.getElementById("resetbutton").addEventListener("click", create_proxy(reset))