Skip to content

Instantly share code, notes, and snippets.

@pony012
Created September 28, 2013 21:55
Show Gist options
  • Save pony012/6747125 to your computer and use it in GitHub Desktop.
Save pony012/6747125 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class Automata:
def __init__(self):
W = 39
E = 20
self.W = W
self.E = E
"""
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
[1 ,9 ,11,13,15,15,15,15,W,16, W, W, W, W, 20,W, 22,W ,33,33,W], 0
[W, 2, 4, 6, 8, 8, 8, 8, W, W, W, W, W, W, W, W, W, W, W, W, W], 1
[W, W, W, W, 3, 3, 3, 3, 3, W, W, W, W, 3, W, W, W, W, 3, 3, W], 2
[W, W, W, W, 3, 3, 3, 3, 3, W, W, W, W, 3, W, W, W, W, 3, 3, W], 3
[W, W, W, W, 5, 5, 5, W, W, W, W, W, W, W, W, W, W, W, W, W, W], 4
[W, W, W, W, 5, 5, 5, W, W, W, W, W, W, W, W, W, W, W, W, W, W], 5
[W, W, W, W, 7, 7, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W], 6
[W, W, W, W, 7, 7, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W], 7
[W, W, W, W, 8, 8, 8, 8, W, W, W, W, W, W, W, W, W, W, W, W, W], 8
[W, W, W, W, 10,10,10,10,10,W, W, W, W,10, W, W, W, W, 10,10,W], 9
[W, W, W, W, 10,10,10,10,10,W, W, W, W,10, W, W, W, W, 10,10,W], 10
[W, W, W, W, 12,12,12,W, W, W, W, W, W, W, W, W, W, W, W, W, W], 11
[W, W, W, W, 12,12,12,W, W, W, W, W, W, W, W, W, W, W, W, W, W], 12
[W, W, W, W, 14,14,W, W, W, W, W, W, W, W, W, W, W, W, W, W, W], 13
[W, W, W, W, 14,14,W, W, W, W, W, W, W, W, W, W, W, W, W, W, W], 14
[W, W, W, W,15,15,15,15, W,16, W, W, W, W, W, W, W, W, W, W, W], 15
[W, W, W, W, W, W, W, W, W, W,17,18,19, W,29,29, W, W, W, W, W], 16
[W, W, W, W, W, W, W, W, W, W, W, W, W, W,31,31, W, W, W, W, W], 17
[W, W, W, W, W, W, W, W, W, W, W, W,17, W, W, W, W, W, W, W, W], 18
[W, W, W, W, W, W, W, W, W, W, W, W, W,32, W, W, W, W, W, W, W], 19
[W, W, W, W, W,21,21,21, W, W, W, W, W, W, W, W, W, W, W, W, W], 20
[W, W, W, W,21,21,21,21, W,16, W, W, W, W, W, W, W, W, W, W, W], 21
[W, W, W, W,23,23,23,23, W, W, W, W, W, W, W, W, W, W, W,38, W], 22
[W, W, W, W,23,23,23,23, W,24, W, W, W, W, W, W, W, W, W, W, W], 23
[W, W, W, W, W, W, W, W, W, W,27,25,26, W, W, W, W, W, W, W, W], 24
[W, W, W, W, W, W, W, W, W, W, W, W,27, W, W, W, W, W, W, W, W], 25
[W, W, W, W, W, W, W, W, W, W, W, W, W,27, W, W, W, W, W, W, W], 26
[W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W,28, W, W, W], 27
[W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W], 28
[W, W, W, W, W, W, W, W, W, W,31,30, W, W, W, W, W, W, W, W, W], 29
[W, W, W, W, W, W, W, W, W, W, W, W,31, W, W, W, W, W, W, W, W], 30
[W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W], 31
[W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W], 32
[W, W, W, W, W, W, W, W, W,34, W, W, W, W, W, W, W, W, W, W, W], 33
[W, W, W, W, W, W, W, W, W, W,37,35,36, W, W, W, W, W, W, W, W], 34
[W, W, W, W, W, W, W, W, W, W, W, W,37, W, W, W, W, W, W, W, W], 35
[W, W, W, W, W, W, W, W, W, W, W, W, W,37, W, W, W, W, W, W, W], 36
[W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W], 37
[W, W, W, W, W, W, W, W, W,24, W, W, W, W, W, W, W, W, W, W, W], 38
[W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W] 39
"""
#Poner estados de error personalizados, por ejemplo en el 15, si no llega 0-9 o ',', mandar a un estado que diga Base numérica decimal inválida, caracteres válidos 0..9
self.tabla = [
[1 ,9 ,11,13,15,15,15,15,W,16, W, W, W, W, 20,W, 22,W ,33,33,W],
[W, 2, 4, 6, 8, 8, 8, 8, W, W, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W, 3, 3, 3, 3, 3, W, W, W, W, 3, W, W, W, W, 3, 3, W],
[W, W, W, W, 3, 3, 3, 3, 3, W, W, W, W, 3, W, W, W, W, 3, 3, W],
[W, W, W, W, 5, 5, 5, W, W, W, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W, 5, 5, 5, W, W, W, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W, 7, 7, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W, 7, 7, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W, 8, 8, 8, 8, W, W, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W, 10,10,10,10,10,W, W, W, W,10, W, W, W, W, 10,10,W],
[W, W, W, W, 10,10,10,10,10,W, W, W, W,10, W, W, W, W, 10,10,W],
[W, W, W, W, 12,12,12,W, W, W, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W, 12,12,12,W, W, W, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W, 14,14,W, W, W, W, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W, 14,14,W, W, W, W, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W,15,15,15,15, W,16, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W, W,17,18,19, W,29,29, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W, W, W, W, W, W,31,31, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W, W, W, W,17, W, W, W, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W, W, W, W, W,32, W, W, W, W, W, W, W],
[W, W, W, W, W,21,21,21, W, W, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W,21,21,21,21, W,16, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W,23,23,23,23, W, W, W, W, W, W, W, W, W, W, W,38, W],
[W, W, W, W,23,23,23,23, W,24, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W, W,27,25,26, W, W, W, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W, W, W, W,27, W, W, W, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W, W, W, W, W,27, W, W, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W,28, W, W, W],
[W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W, W,31,30, W, W, W, W, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W, W, W, W,31, W, W, W, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W,34, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W, W,37,35,36, W, W, W, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W, W, W, W,37, W, W, W, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W, W, W, W, W,37, W, W, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W,24, W, W, W, W, W, W, W, W, W, W, W],
[W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W, W]
]
def evaluarC(self, c):
c=c.upper()
if c=='#':
return 0
if c=='$':
return 1
if c=='@':
return 2
if c=='%':
return 3
if c=='0':
return 4
if c=='1':
return 5
if c>='2' and c <='7':
return 6
if c=='8' or c =='9':
return 7
if c =='E' or c=='F':
return 8
if c==',':
return 9
if c=='X' or c=='Y':
return 10
if c=='S':
return 11
if c=='P':
return 12
if c=='C':
return 13
if c=='-':
return 14
if c=='+':
return 15
if c=='[':
return 16
if c==']':
return 17
if c=='A' or c=='B':
return 18
if c=='D':
return 19
return self.E
def analizar(self, s):
estado = 0
valor = 0
#Inherente
if len(s)==0:
return "Inherente"
for c in s:
#print "Estado Actual = " + str(estado) + " Val = " +str(self.evaluarC(c))+ " C = " +c+ " Estado siguiente = " + str(self.tabla[estado][self.evaluarC(c)])
estado = self.tabla[estado][self.evaluarC(c)]
#Inmediatos (Falta validar los rangos)
if estado == 3:
return (int(s[2:],16),"Inmediato")
if estado == 5:
return (int(s[2:],8),"Inmediato")
if estado == 7:
return (int(s[2:],2),"Inmediato")
if estado == 8:
return (int(s[1:]),"Inmediato")
#Directo, extendido (Falta validar los rangos)
if estado == 10:
return (int(s[1:],16),"Directo|Extendido")
if estado == 12:
return (int(s[1:],8),"Directo|Extendido")
if estado == 14:
return (int(s[1:],2),"Directo|Extendido")
if estado == 15:
return (int(s),"Directo|Extendido")
#Indizados (Falta validar los rangos para determinar los bits)
if estado == 17 or estado == 32:
if s.find(',')!=0:
return (int(s[0:s.find(',')]),"IDX", s[s.find(',')+1:])
else:
return (0,"IDX",s[s.find(',')+1:])
#Indizado indirecto/Indizado de ac. indirecto
if estado == 28:
if s[1].upper()=='D':
return ("[D,IDX]",s[s.find(',')+1:s.find(']')])
else:
return (int(s[1:s.find(',')]),"[IDX2]",s[s.find(',')+1:s.find(']')])
#Indizado pre/post incremento/decremento
if estado == 31:
if s.find(',')!=0: #Podría validar que exita el caso "01,+sp"
valor = int(s[0:s.find(',')])
if valor > 0 and valor < 9:
if s[s.find(',')+1] == '+':
return (valor, "IDX", s[s.find(',')+2:] , "PreIncremento")
if s[s.find(',')+1] == '-':
return (valor, "IDX", s[s.find(',')+2:] , "PreDecremento")
if s[-1] == "+":
return (valor, "IDX", s[s.find(',')+1:-1] , "PostIncremento")
if s[-1] == "-":
return (valor, "IDX", s[s.find(',')+1:-1] , "PostDecremento")
#Indizado de acumulador
if estado ==37:
return (s[0], "IDX", s[s.find(',')+1:])
#Etiqueta (Relativo, Extendido)
tam = len(s)
band = True
if tam > 0:
if (s[0].islower() or s[0].isupper()) and tam <= 8:
for c in s[1:]:
if (not c.islower()) and (not c.isupper()) and (not c.isdigit()) and c != '_':
band = False
if band:
return (s,"REL|Extendido")
return "Error"
automata = Automata()
print automata.analizar("")
print automata.analizar("3")
print automata.analizar("@3")
print automata.analizar("%111")
print automata.analizar("$3")
print automata.analizar("300")
print automata.analizar("3444")
print automata.analizar("Et2_x")
print automata.analizar("$FFF")
print automata.analizar("#3")
print automata.analizar("#@3")
print automata.analizar("#%1111")
print automata.analizar("1,X")
print automata.analizar(",PC")
print automata.analizar("255,y")
print automata.analizar("32768,sp")
print automata.analizar("-10,Y")
print automata.analizar("-250,SP")
print automata.analizar("1,+SP")
print automata.analizar("7,X+")
print automata.analizar("5,-SP")
print automata.analizar("5,Y-")
print automata.analizar("A,pc")
print automata.analizar("254,X")
print automata.analizar("64444,Y")
print automata.analizar("[1,SP]")
print automata.analizar("[64444,PC]")
print automata.analizar("[D,SP]")
print automata.analizar("Et1_1")
print automata.analizar("ET1_3")
print automata.analizar("#8")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment