aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorIván Ávalos <avalos@disroot.org>2022-11-06 23:13:27 -0600
committerIván Ávalos <avalos@disroot.org>2022-11-06 23:13:27 -0600
commit36aebb9be7903b9461d332e72102e4b4beb39ba6 (patch)
tree61bd56d6a12daf290ec0b1528f174eed2717548f
parentcea3e027ad4f70d4420e3a81bcb438537e9bebbf (diff)
downloadjavanol-36aebb9be7903b9461d332e72102e4b4beb39ba6.tar.gz
javanol-36aebb9be7903b9461d332e72102e4b4beb39ba6.tar.bz2
javanol-36aebb9be7903b9461d332e72102e4b4beb39ba6.zip
Se añaden componentes a la UI y se implementan partes del lexer
-rw-r--r--lexer.py109
1 files changed, 109 insertions, 0 deletions
diff --git a/lexer.py b/lexer.py
index 7383180..49d75df 100644
--- a/lexer.py
+++ b/lexer.py
@@ -25,6 +25,114 @@ t_string_lit = r'"[[:print]]*"'
def inicio_lexer(data):
tabla = TablaLex()
+
+ es_string = False
+ es_caracter = False
+ es_comentario = False
+ selector = ''
+ recol_string = ''
+ recol_caracter = ''
+ recol_comentario = ''
+ recol_operador = ''
+
+ for c in data + "\n":
+ print (c + ' (' + selector + ')')
+
+ if selector == '':
+ # Entradas a tokens
+ if c == '"':
+ selector = 'STRING_LIT'
+ continue
+ elif c == '\'':
+ selector = 'CHAR_LIT'
+ continue
+ elif c == '/':
+ recol_comentario = '/'
+ elif (c == '=' or c == '+' or c == '-' or c == '&' or c == '|') and len(recol_operador) == 0:
+ recol_operador = c
+ continue
+ elif c == '!' or c == '<' or c == '>':
+ recol_operador = c
+ continue
+ elif c == '{':
+ tabla.insertar(LexToken('{', None, None, 1))
+ elif c == '}':
+ tabla.insertar(LexToken('}', None, None, 1))
+ elif c == '(':
+ tabla.insertar(LexToken('(', None, None, 1))
+ elif c == ')':
+ tabla.insertar(LexToken(')', None, None, 1))
+ elif c == ',':
+ tabla.insertar(LexToken(',', None, None, 1))
+ elif c == ';':
+ tabla.insertar(LexToken(';', None, None, 1))
+ elif c == '*':
+ tabla.insertar(LexToken('*', None, None, 1))
+
+ # Apertura de comentario
+ if recol_comentario == '/' and c == '*':
+ selector = 'COMMENT'
+ recol_comentario = ''
+ continue
+
+ # Apertura de operador compuesto
+ if len(recol_operador) > 0:
+ rc = recol_operador + c
+ if (rc == '>=' or rc == '<=' or rc == '==' or rc == '!=' or
+ rc == '&&' or rc == '||' or rc == '++' or rc == '--'):
+ # Operador compuesto
+ tabla.insertar(LexToken(rc, None, None, 1))
+ recol_operador = ''
+ continue
+ else:
+ # Operador simple
+ tabla.insertar(LexToken(recol_operador, None, None, 1))
+ recol_operador = ''
+
+ # Cadenas de texto
+ if selector == 'STRING_LIT':
+ if c == '"':
+ tabla.insertar(LexToken('STRING_LIT', None, recol_string, 1))
+ selector = ''
+ recol_string = ''
+ else:
+ recol_string += c
+ continue
+
+ # Caracteres
+ if selector == 'CHAR_LIT':
+ if len(recol_caracter) > 1:
+ print ('Error: más de un caracter en una literal de caracter')
+ break
+ if c == '\'':
+ if len(recol_caracter) == 0:
+ print ('Error: literal de caracter vacía')
+ break
+ tabla.insertar(LexToken('CHAR_LIT', None, recol_caracter, 1))
+ selector = ''
+ recol_caracter = ''
+ continue
+ else:
+ recol_caracter += c
+
+ # Comentarios
+ if selector == 'COMMENT':
+ if c == '*':
+ recol_comentario = c
+ continue
+ elif recol_comentario == '*':
+ if c == '/':
+ selector = ''
+ recol_comentario = ''
+ continue
+ else:
+ recol_comentario = ''
+ continue
+
+ print (str(tabla))
+
+ '''
+ tabla = TablaLex()
# booleano ivan = verdadero
tabla.insertar(LexToken('BOOLEAN', None, None, 1))
tabla.insertar(LexToken('IDENT', 'ivan', None, 1))
@@ -39,3 +147,4 @@ def inicio_lexer(data):
tabla.actualizar('ivan', ident)
ident = tabla.buscar('ivan')
print (str(ident))
+ '''