82 lines
2.3 KiB
Python
82 lines
2.3 KiB
Python
from .trinario import *
|
|
|
|
class Macchina:
|
|
|
|
def esegui(self, programma):
|
|
#Accumulatore
|
|
self.a = trin(0)
|
|
|
|
#Registri
|
|
self.d = [trin(0) for i in range(3**10)]
|
|
self.c = [trin(0) for i in range(3**10)]
|
|
|
|
#Puntatori
|
|
self.puntatore_c = trin(0)
|
|
self.puntatore_d = trin(0)
|
|
|
|
#Controllo lunghezza massima
|
|
if len(programma) > 3**10:
|
|
raise MemoryError("Memoria esaurita. Limite di 3**10 word superato.")
|
|
|
|
#Copia il programma nel registro c
|
|
for indice, word in enumerate(programma):
|
|
if word not in ("\n"," "):
|
|
self.c[indice] = trinord(word)
|
|
|
|
#Esecuzione del programma
|
|
while self.puntatore_c < len(programma):
|
|
|
|
#Determina l'istruzione da eseguire
|
|
istruzione = (self.c[self.puntatore_c.decimale] + self.puntatore_c) % 94
|
|
if istruzione == 4:
|
|
self.puntatore_c = self.d[self.puntatore_d.decimale]
|
|
elif istruzione == 5:
|
|
self.__stampa()
|
|
elif istruzione == 23:
|
|
self.a = trinord(input())
|
|
elif istruzione == 39:
|
|
self.__ruota()
|
|
self.a = self.d[self.puntatore_d.decimale]
|
|
elif istruzione == 40:
|
|
self.puntatore_d = self.d[self.puntatore_d.decimale]
|
|
elif istruzione == 62:
|
|
self.__pazza()
|
|
self.a = self.d[self.puntatore_d.decimale]
|
|
elif istruzione == 68:
|
|
self.__niente()
|
|
elif istruzione == 81:
|
|
self.__esci()
|
|
else:
|
|
self.__niente()
|
|
if 33 <= istruzione <= 126:
|
|
self.__traduci()
|
|
self.puntatore_c += 1
|
|
self.puntatore_d += 1
|
|
|
|
def __stampa(self):
|
|
print(trinchr(self.a), end="")
|
|
|
|
def __ruota(self):
|
|
#Ruota la word di un trit verso destra
|
|
self.d[self.puntatore_d.decimale] = trin(self.a[-1] + self.a[0:-1])
|
|
|
|
def __pazza(self):
|
|
#Operazione pazza
|
|
operazione = [[1,0,0],[1,0,2],[2,2,1]]
|
|
risultato = []
|
|
for i,j in zip(self.a, self.d[self.puntatore_d.decimale]):
|
|
risultato += operazione[i][j],
|
|
self.d[self.puntatore_d.decimale] = trin(risultato)
|
|
|
|
def __niente(self):
|
|
#Operazione nulla
|
|
pass
|
|
|
|
def __esci(self):
|
|
#Termina l'esecuzione
|
|
exit()
|
|
|
|
def __traduci(self):
|
|
#Traduce
|
|
trascrizione = str.maketrans('!"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~', '5z]&gqtyfr$(we4{WP)H-Zn,[%\\3dL+Q;>U!pJS72FhOA1CB6v^=I_0/8|jsb9m<.TVac`uY*MK\'X~xDl}REokN:#?G"i@')
|
|
self.c[self.puntatore_c.decimale] = trinord(str.translate(trinchr(self.c[self.puntatore_c.decimale]), trascrizione)) |