From b20e23d221ac418deb8fa495fa375f715511f953 Mon Sep 17 00:00:00 2001 From: Iván Ávalos Date: Wed, 16 Nov 2022 20:07:28 -0600 Subject: Se añade tabla de símbolos a la interfaz MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- compilador/lexer.py | 11 +++++-- compilador/main.py | 13 ++++++-- compilador/tabla.py | 17 ++++++++++ interfaz/main.py | 89 ++++++++++++++++++++++++++++++++++++----------------- 4 files changed, 97 insertions(+), 33 deletions(-) diff --git a/compilador/lexer.py b/compilador/lexer.py index 1952a16..ec7d0a4 100644 --- a/compilador/lexer.py +++ b/compilador/lexer.py @@ -33,7 +33,7 @@ class Selector(Enum): ENTERO = 5 class Lexer: - def __init__(self, data): + def __init__(self, data, input_file): self.tabla = TablaLex() self.numlinea = 1 self.selector = Selector.NINGUNO @@ -44,6 +44,9 @@ class Lexer: self.recol_ident = '' self.recol_entero = '' self.data = data + self.tabla_file = None + if input_file: + self.tabla_file = input_file + '.tab' def inicio(self): for l in self.data.splitlines(): @@ -56,7 +59,8 @@ class Lexer: self.numlinea += 1 # Imprimir tabla de símbolos - print (str(self.tabla)) + if self.tabla_file: + self.tabla.exportar(self.tabla_file) Parser(self.tabla).inicio() @@ -203,3 +207,6 @@ class Lexer: def insertar_tabla(self, token, nombre, valor): self.tabla.insertar(LexToken(token, nombre, valor, self.numlinea)) + + + diff --git a/compilador/main.py b/compilador/main.py index c27c107..186ca83 100644 --- a/compilador/main.py +++ b/compilador/main.py @@ -6,14 +6,16 @@ from lexer import * class Main: input_file = None output_file = None + output_table = False def print_help (self, arg0): - print("Uso: % s -i entrada.ñ -o salida.ñ" % arg0) + print("Uso: % s -i entrada.es -o salida.es" % arg0) + print(" % s -i entrada.es -o salida.es -t") print(" % s -h" % arg0) def main(self, argv): try: - opts, args = getopt.getopt(argv[1:], "hi:o:", ["input=", "output="]) + opts, args = getopt.getopt(argv[1:], "hi:o:t", ["input=", "output=", "table"]) except getopt.GetoptError as err: print(err) print_help(argv[0]); @@ -26,13 +28,18 @@ class Main: self.input_file = a elif o in ("-o", "--output"): self.output_file = a + elif o in ("-t", "--table"): + self.output_table = True else: assert False, "opción desconocida" if self.input_file and self.output_file: with open(self.input_file) as f: data = f.read() - Lexer(data).inicio() + if self.output_table: + Lexer(data, self.input_file).inicio() + else: + Lexer(data, None).inicio() if __name__ == "__main__": Main().main(sys.argv) diff --git a/compilador/tabla.py b/compilador/tabla.py index c8ca424..637b9a6 100644 --- a/compilador/tabla.py +++ b/compilador/tabla.py @@ -1,3 +1,4 @@ +import json, os from enum import Enum from dataclasses import dataclass from typing import Any @@ -63,6 +64,22 @@ class TablaLex: self.tabla[i] = tok return + def exportar(self, output_file): + data = [] + for t in self.tabla: + data.append({ + 'tipo': t.tipo, + 'nombre': t.nombre, + 'valor': str(t.valor), + 'numlinea': t.numlinea + }) + output = json.dumps(data) + if os.path.exists(output_file): + os.remove(output_file) + with open(output_file, 'w+') as f: + f.truncate(0) + f.write(output) + def __str__(self): output = "" for t in self.tabla: diff --git a/interfaz/main.py b/interfaz/main.py index eb8d437..9867e9e 100644 --- a/interfaz/main.py +++ b/interfaz/main.py @@ -1,4 +1,4 @@ -import gi, sys, subprocess +import gi, sys, subprocess, json gi.require_version('Gtk', '4.0') gi.require_version('Adw', '1') gi.require_version('GtkSource', '5') @@ -22,7 +22,8 @@ class MainWindow(Gtk.ApplicationWindow): self.crear_headerbar() self.crear_textview() - self.crear_paneles() + self.crear_area_mensajes() + self.crear_tabla_simbolos() def crear_headerbar(self): header = Gtk.HeaderBar() @@ -42,8 +43,6 @@ class MainWindow(Gtk.ApplicationWindow): self.run_button.set_icon_name('media-playback-start-symbolic') self.run_button.connect('clicked', self.correr) header.pack_end(self.run_button) - - return def crear_textview(self): scrolled_win = Gtk.ScrolledWindow() @@ -61,34 +60,38 @@ class MainWindow(Gtk.ApplicationWindow): scrolled_win.set_child(self.sourceview) - def crear_paneles(self): - # Área de mensajes - notebook1 = Gtk.Notebook() - self.grid.attach(notebook1, 0, 1, 2, 1) + def crear_area_mensajes(self): + notebook = Gtk.Notebook() + self.grid.attach(notebook, 0, 1, 2, 1) - scrolled_msg = Gtk.ScrolledWindow() - scrolled_msg.set_hexpand(True) - scrolled_msg.set_min_content_height(150) + scrolled = Gtk.ScrolledWindow() + scrolled.set_hexpand(True) + scrolled.set_min_content_height(150) self.msgbuf = GtkSource.Buffer() self.msgview = GtkSource.View.new_with_buffer(self.msgbuf) self.msgview.set_editable(False) - scrolled_msg.set_child(self.msgview) - notebook1.append_page(scrolled_msg, Gtk.Label.new('Mensajes')) - - # Tabla de símbolos - notebook2 = Gtk.Notebook() - self.grid.attach(notebook2, 1, 0, 1, 1) - - scrolled_tabla = Gtk.ScrolledWindow() - scrolled_tabla.set_vexpand(True) - scrolled_tabla.set_min_content_width(300) - - self.tablabuf = GtkSource.Buffer() - self.tablaview = GtkSource.View.new_with_buffer(self.tablabuf) - self.tablaview.set_editable(False) - scrolled_tabla.set_child(self.tablaview) - notebook2.append_page(scrolled_tabla, Gtk.Label.new('Símbolos')) + scrolled.set_child(self.msgview) + notebook.append_page(scrolled, Gtk.Label.new('Mensajes')) + + def crear_tabla_simbolos(self): + notebook = Gtk.Notebook() + self.grid.attach(notebook, 1, 0, 1, 1) + + scrolled = Gtk.ScrolledWindow() + scrolled.set_vexpand(True) + scrolled.set_min_content_width(300) + + self.tablagrid = Gtk.Grid() + self.tablagrid.set_vexpand(True) + self.tablagrid.set_row_spacing(8) + self.tablagrid.set_column_spacing(8) + self.tablagrid.set_margin_top(8) + self.tablagrid.set_margin_start(8) + self.tablagrid.set_margin_end(8) + self.tablagrid.set_margin_bottom (8) + scrolled.set_child(self.tablagrid) + notebook.append_page(scrolled, Gtk.Label.new('Símbolos')) def abrir_archivo(self, button): print('abrir_archivo()') @@ -123,11 +126,41 @@ class MainWindow(Gtk.ApplicationWindow): result = subprocess.run([ 'python', compilador_dir, '-i', self.input_file, - '-o', self.output_file + '-o', self.output_file, + '-t' ], stdout=subprocess.PIPE) output = result.stdout.decode('utf-8') self.msgbuf.set_text(output) + # Tabla de símbolos + with open(self.input_file + '.tab', 'r') as f: + data = f.read() + self.llenar_tabla(data) + + def llenar_tabla(self, data): + tabla = json.loads(data) + for i in range(4): + self.tablagrid.remove_column(0) + label_linea = Gtk.Label.new(None) + label_linea.set_markup('Línea') + label_nombre = Gtk.Label.new(None) + label_nombre.set_markup('Nombre') + label_valor = Gtk.Label.new(None) + label_valor.set_markup('Valor') + label_tipo = Gtk.Label.new(None) + label_tipo.set_markup('Tipo') + self.tablagrid.attach(label_linea, 0, 0, 1, 1) + self.tablagrid.attach(label_tipo, 1, 0, 1, 1) + self.tablagrid.attach(label_nombre, 2, 0, 1, 1) + self.tablagrid.attach(label_valor, 3, 0, 1, 1) + for i, t in enumerate(tabla): + row = i + 1 + self.tablagrid.attach(Gtk.Label.new(str(t['numlinea'])), 0, row, 1, 1) + self.tablagrid.attach(Gtk.Label.new(t['tipo']), 1, row, 1, 1) + self.tablagrid.attach(Gtk.Label.new(t['nombre']), 2, row, 1, 1) + self.tablagrid.attach(Gtk.Label.new(str(t['valor'])), 3, row, 1, 1) + + def on_activate(app): win = MainWindow() win.connect('destroy', Gtk.main_quit) -- cgit v1.2.3