#!/usr/bin/env python
# coding: utf-8

# In[ ]:


#! /usr/bin/env python
#  -*- coding: utf-8 -*-
#
# GUI module generated by PAGE version 5.4
#  in conjunction with Tcl version 8.6
#    Aug 17, 2020 10:54:44 PM -03  platform: Windows NT

import sys

try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

try:
    import ttk
    py3 = False
except ImportError:
    import tkinter.ttk as ttk
    py3 = True

import maquina_virtual_support

def vp_start_gui():
    '''Starting point when module is the main routine.'''
    global val, w, root
    root = tk.Tk()
    top = Toplevel1 (root)
    maquina_virtual_support.init(root, top)
    root.mainloop()

w = None
def create_Toplevel1(rt, *args, **kwargs):
    '''Starting point when module is imported by another module.
       Correct form of call: 'create_Toplevel1(root, *args, **kwargs)' .'''
    global w, w_win, root
    #rt = root
    root = rt
    w = tk.Toplevel (root)
    top = Toplevel1 (w)
    maquina_virtual_support.init(w, top, *args, **kwargs)
    return (w, top)

def destroy_Toplevel1():
    global w
    w.destroy()
    w = None

class Toplevel1:

    def __init__(self, top=None):
        '''This class configures and populates the toplevel window.
           top is the toplevel containing window.'''
        _bgcolor = '#d9d9d9'  # X11 color: 'gray85'
        _fgcolor = '#000000'  # X11 color: 'black'
        _compcolor = '#d9d9d9' # X11 color: 'gray85'
        _ana1color = '#d9d9d9' # X11 color: 'gray85'
        _ana2color = '#ececec' # Closest X11 color: 'gray92'
        
        self.COMENTARIOS_DICT = {
            "LDC":"k (Carregar constante): S:=s + 1 ; M [s]: = k",
            "LDV":"n (Carregar Valor): S:=s + 1 ; M[s]:=M[n]",
            "ADD":"M[s-1]:=M[s-1] + M[s]; s:=s - 1",
            "SUB":"M[s-1]:=M[s-1] - M[s]; s:=s - 1",
            "MULT":"M[s - 1] := M[s - 1] * M[s]; s := s - 1",
            "DIVI":"M[s - 1] := M[s - 1] div M[s]; s := s - 1",
            "INV":"M[s] := -M[s]",
            "AND":"se M[s - 1] = 1 e M[s] = 1 então M[s - 1] := 1 senão M[s - 1] := 0; s := s - 1",
            "OR":"se M[s - 1] = 1 ou M[s] = 1 então M[s - 1] := 1 senão M[s - 1] := 0; s := s - 1",
            "NEG":"M[s] := 1 - M[s]",
            "CME":"se M[s - 1] < M[s] então M[s - 1] := 1 senão M[s - 1] := 0; s := s - 1",
            "CMA":"se M[s - 1] > M[s] então M[s - 1] := 1 senão M[s - 1] := 0; s := s - 1",
            "CEQ":"se M[s - 1] = M[s] então M[s - 1] := 1 senão M[s - 1] := 0; s := s - 1",
            "CDIF":"se M[s - 1] ≠ M[s] então M[s - 1] := 1 senão M[s - 1] := 0; s := s - 1",
            "CMEQ":"se M[s - 1] ≤ M[s] então M[s - 1] := 1 senão M[s - 1] := 0; s := s - 1",
            "CMAQ":"se M[s - 1] ≥ M[s] então M[s - 1] := 1 senão M[s - 1] := 0; s := s - 1",
            "START":"S := -1",
            "HLT":"“Pára a execução da MVD”",
            "STR":"M[n] := M[s]; s := s - 1",
            "JMP":"i := t",
            "JMPF":"se M[s] = 0 então i := t senão i := i + 1; s := s - 1",
            "NULL":"(Nada)",
            "RD":"S := s + 1; M[s] := “próximo valor de entrada”",
            "PRN":"“Imprimir M[s]”; s := s - 1 ",
            "ALLOC":"m, n (Alocar memória): Para k := 0 até n - 1 faça { s := s + 1; M[s] := M[m + k] }",
            "DALLOC":"m, n (Desalocar memória): Para k := n - 1 até 0 faça { M[m + k] := M[s]; s := s - 1 }",
            "CALL":"t (Chamar procedimento ou função): S := s + 1; M[s] := i + 1; i := t",
            "RETURN":"(Retornar de procedimento): i := M[s]; s := s - 1"
        }

        top.geometry("1334x843+340+136")
        top.minsize(148, 1)
        top.maxsize(4804, 1325)
        top.resizable(1, 1)
        top.title("New Toplevel")
        top.configure(background="#d9d9d9")
        top.configure(highlightbackground="#d9d9d9")
        top.configure(highlightcolor="black")

        self.Frame1 = tk.Frame(top)
        self.Frame1.place(relx=0.007, rely=0.012, relheight=0.82, relwidth=0.987)

        self.Frame1.configure(relief='groove')
        self.Frame1.configure(borderwidth="2")
        self.Frame1.configure(relief="groove")
        self.Frame1.configure(background="#d9d9d9")
        self.Frame1.configure(highlightbackground="#d9d9d9")
        self.Frame1.configure(highlightcolor="black")

        self.Listbox4_2 = tk.Listbox(self.Frame1)
        self.Listbox4_2.place(relx=0.607, rely=0.651, relheight=0.308
                , relwidth=0.163)
        self.Listbox4_2.configure(background="white")
        self.Listbox4_2.configure(disabledforeground="#a3a3a3")
        self.Listbox4_2.configure(font="TkFixedFont")
        self.Listbox4_2.configure(foreground="#000000")
        self.Listbox4_2.configure(highlightbackground="#d9d9d9")
        self.Listbox4_2.configure(highlightcolor="black")
        self.Listbox4_2.configure(selectbackground="blue")
        self.Listbox4_2.configure(selectforeground="white")
        self.Listbox4_2.configure(takefocus="0")

        self.Label1 = tk.Label(self.Frame1)
        self.Label1.place(relx=0.023, rely=0.014, height=26, width=1003)
        self.Label1.configure(background="#d9d9d9")
        self.Label1.configure(disabledforeground="#a3a3a3")
        self.Label1.configure(foreground="#000000")
        self.Label1.configure(text='''Instruções a serem executadas pela MV''')
        self.Label1.configure(fg = "navy blue")
        self.Label1.configure(font = "Helvetica 16 bold")

        self.Label2 = tk.Label(self.Frame1)
        self.Label2.place(relx=0.812, rely=0.014, height=26, width=223)
        self.Label2.configure(background="#d9d9d9")
        self.Label2.configure(disabledforeground="#a3a3a3")
        self.Label2.configure(foreground="#000000")
        self.Label2.configure(text='''Conteúdo da pilha''')
        self.Label2.configure(fg = "navy blue")
        self.Label2.configure(font = "Helvetica 16 bold")

        self.Label3 = tk.Label(self.Frame1)
        self.Label3.place(relx=0.015, rely=0.608, height=26, width=351)
        self.Label3.configure(background="#d9d9d9")
        self.Label3.configure(disabledforeground="#a3a3a3")
        self.Label3.configure(foreground="#000000")
        self.Label3.configure(text='''Janela de entrada''')
        self.Label3.configure(fg = "navy blue")
        self.Label3.configure(font = "Helvetica 16 bold")

        self.Label4 = tk.Label(self.Frame1)
        self.Label4.place(relx=0.304, rely=0.608, height=26, width=353)
        self.Label4.configure(background="#d9d9d9")
        self.Label4.configure(disabledforeground="#a3a3a3")
        self.Label4.configure(foreground="#000000")
        self.Label4.configure(text='''Janela de saída''')
        self.Label4.configure(fg = "navy blue")
        self.Label4.configure(font = "Helvetica 16 bold")

        self.Label5 = tk.Label(self.Frame1)
        self.Label5.place(relx=0.607, rely=0.608, height=26, width=223)
        self.Label5.configure(background="#d9d9d9")
        self.Label5.configure(disabledforeground="#a3a3a3")
        self.Label5.configure(foreground="#000000")
        self.Label5.configure(text='''Break points''')
        self.Label5.configure(fg = "navy blue")
        self.Label5.configure(font = "Helvetica 16 bold")

        self.Listbox1 = tk.Listbox(self.Frame1)
        self.Listbox1.place(relx=0.023, rely=0.101, relheight=0.489
                , relwidth=0.056)
        self.Listbox1.configure(background="white")
        self.Listbox1.configure(disabledforeground="#a3a3a3")
        self.Listbox1.configure(font="TkFixedFont")
        self.Listbox1.configure(foreground="#000000")

        self.Listbox3 = tk.Listbox(self.Frame1)
        self.Listbox3.place(relx=0.076, rely=0.101, relheight=0.489
                , relwidth=0.087)
        self.Listbox3.configure(background="white")
        self.Listbox3.configure(disabledforeground="#a3a3a3")
        self.Listbox3.configure(font="TkFixedFont")
        self.Listbox3.configure(foreground="#000000")

        self.Listbox5 = tk.Listbox(self.Frame1)
        self.Listbox5.place(relx=0.159, rely=0.101, relheight=0.489
                , relwidth=0.094)
        self.Listbox5.configure(background="white")
        self.Listbox5.configure(cursor="fleur")
        self.Listbox5.configure(disabledforeground="#a3a3a3")
        self.Listbox5.configure(font="TkFixedFont")
        self.Listbox5.configure(foreground="#000000")

        self.Listbox6 = tk.Listbox(self.Frame1)
        self.Listbox6.place(relx=0.251, rely=0.101, relheight=0.488
                , relwidth=0.103)
        self.Listbox6.configure(background="white")
        self.Listbox6.configure(disabledforeground="#a3a3a3")
        self.Listbox6.configure(font="TkFixedFont")
        self.Listbox6.configure(foreground="#000000")

        self.Listbox7 = tk.Listbox(self.Frame1)
        self.Listbox7.place(relx=0.349, rely=0.101, relheight=0.489
                , relwidth=0.429)
        self.Listbox7.configure(background="white")
        self.Listbox7.configure(disabledforeground="#a3a3a3")
        self.Listbox7.configure(font="TkFixedFont")
        self.Listbox7.configure(foreground="#000000")

        self.Label6 = tk.Label(self.Frame1)
        self.Label6.place(relx=0.03, rely=0.058, height=26, width=42)
        self.Label6.configure(background="#d9d9d9")
        self.Label6.configure(disabledforeground="#a3a3a3")
        self.Label6.configure(foreground="#000000")
        self.Label6.configure(text='''I''')
        self.Label6.configure(fg="black")
        self.Label6.configure(font="Arial 12")

        self.Label7 = tk.Label(self.Frame1)
        self.Label7.place(relx=0.076, rely=0.058, height=26, width=112)
        self.Label7.configure(background="#d9d9d9")
        self.Label7.configure(disabledforeground="#a3a3a3")
        self.Label7.configure(foreground="#000000")
        self.Label7.configure(text='''Instrução''')
        self.Label7.configure(fg="black")
        self.Label7.configure(font="Arial 12")

        self.Label8 = tk.Label(self.Frame1)
        self.Label8.place(relx=0.159, rely=0.058, height=26, width=123)
        self.Label8.configure(background="#d9d9d9")
        self.Label8.configure(disabledforeground="#a3a3a3")
        self.Label8.configure(foreground="#000000")
        self.Label8.configure(text='''Atributo #1''')
        self.Label8.configure(fg="black")
        self.Label8.configure(font="Arial 12")

        self.Label9 = tk.Label(self.Frame1)
        self.Label9.place(relx=0.251, rely=0.058, height=26, width=131)
        self.Label9.configure(background="#d9d9d9")
        self.Label9.configure(disabledforeground="#a3a3a3")
        self.Label9.configure(foreground="#000000")
        self.Label9.configure(text='''Atributo #2''')
        self.Label9.configure(fg="black")
        self.Label9.configure(font="Arial 12")

        self.Label10 = tk.Label(self.Frame1)
        self.Label10.place(relx=0.349, rely=0.058, height=26, width=562)
        self.Label10.configure(background="#d9d9d9")
        self.Label10.configure(disabledforeground="#a3a3a3")
        self.Label10.configure(foreground="#000000")
        self.Label10.configure(text='''Comentário''')
        self.Label10.configure(fg="black")
        self.Label10.configure(font="Arial 12")

        self.Listbox2 = tk.Listbox(self.Frame1)
        self.Listbox2.place(relx=0.797, rely=0.101, relheight=0.489
                , relwidth=0.086)
        self.Listbox2.configure(background="white")
        self.Listbox2.configure(disabledforeground="#a3a3a3")
        self.Listbox2.configure(font="TkFixedFont")
        self.Listbox2.configure(foreground="#000000")

        self.Listbox8 = tk.Listbox(self.Frame1)
        self.Listbox8.place(relx=0.881, rely=0.101, relheight=0.489
                , relwidth=0.109)
        self.Listbox8.configure(background="white")
        self.Listbox8.configure(disabledforeground="#a3a3a3")
        self.Listbox8.configure(font="TkFixedFont")
        self.Listbox8.configure(foreground="#000000")

        self.Label11 = tk.Label(self.Frame1)
        self.Label11.place(relx=0.797, rely=0.058, height=26, width=112)
        self.Label11.configure(background="#d9d9d9")
        self.Label11.configure(disabledforeground="#a3a3a3")
        self.Label11.configure(foreground="#000000")
        self.Label11.configure(text='''Endereço''')
        self.Label11.configure(fg="black")
        self.Label11.configure(font="Arial 12")

        self.Label12 = tk.Label(self.Frame1)
        self.Label12.place(relx=0.881, rely=0.058, height=26, width=143)
        self.Label12.configure(background="#d9d9d9")
        self.Label12.configure(disabledforeground="#a3a3a3")
        self.Label12.configure(foreground="#000000")
        self.Label12.configure(text='''Valor''')
        self.Label12.configure(fg="black")
        self.Label12.configure(font="Arial 12")



        self.Listbox9 = tk.Listbox(self.Frame1)
        self.Listbox9.place(relx=0.023, rely=0.651, relheight=0.315
                , relwidth=0.261)
        self.Listbox9.configure(background="white")
        self.Listbox9.configure(disabledforeground="#a3a3a3")
        self.Listbox9.configure(font="TkFixedFont")
        self.Listbox9.configure(foreground="#000000")

        self.Listbox4 = tk.Listbox(self.Frame1)
        self.Listbox4.place(relx=0.304, rely=0.651, relheight=0.315
                , relwidth=0.27)
        self.Listbox4.configure(background="white")
        self.Listbox4.configure(disabledforeground="#a3a3a3")
        self.Listbox4.configure(font="TkFixedFont")
        self.Listbox4.configure(foreground="#000000")

        self.Button1 = tk.Button(top)
        self.Button1.place(relx=0.157, rely=0.854, height=43, width=176)
        self.Button1.configure(activebackground="#ececec")
        self.Button1.configure(activeforeground="#000000")
        self.Button1.configure(background="#d9d9d9")
        self.Button1.configure(disabledforeground="#a3a3a3")
        self.Button1.configure(foreground="#000000")
        self.Button1.configure(highlightbackground="#d9d9d9")
        self.Button1.configure(highlightcolor="black")
        self.Button1.configure(pady="0")
        self.Button1.configure(takefocus="0")
        self.Button1.configure(text='''DEBUG''')
        self.Button1.configure(fg="black")
        self.Button1.configure(font="Arial 12")

        self.menubar = tk.Menu(top,font="TkMenuFont",bg=_bgcolor,fg=_fgcolor)
        top.configure(menu = self.menubar)

        self.Button1_4 = tk.Button(top)
        self.Button1_4.place(relx=0.3, rely=0.854, height=43, width=176)
        self.Button1_4.configure(activebackground="#ececec")
        self.Button1_4.configure(activeforeground="#000000")
        self.Button1_4.configure(background="#d9d9d9")
        self.Button1_4.configure(disabledforeground="#a3a3a3")
        self.Button1_4.configure(foreground="#000000")
        self.Button1_4.configure(highlightbackground="#d9d9d9")
        self.Button1_4.configure(highlightcolor="black")
        self.Button1_4.configure(pady="0")
        self.Button1_4.configure(takefocus="0")
        self.Button1_4.configure(text='''RUN''')
        self.Button1_4.configure(fg="black")
        self.Button1_4.configure(font="Arial 12")

        self.Button2 = tk.Button(top)
        self.Button2.place(relx=0.015, rely=0.854, height=43, width=176)
        self.Button2.configure(activebackground="#ececec")
        self.Button2.configure(activeforeground="#000000")
        self.Button2.configure(background="#d9d9d9")
        self.Button2.configure(disabledforeground="#a3a3a3")
        self.Button2.configure(foreground="#000000")
        self.Button2.configure(highlightbackground="#d9d9d9")
        self.Button2.configure(highlightcolor="black")
        self.Button2.configure(pady="0")
        self.Button2.configure(text='''Carrega arquivo''')
        self.Button2.configure(command=self.carrega_arquivo)
        self.Button2.configure(fg="black")
        self.Button2.configure(font="Arial 12")
        
        self.Listbox1.bind("<Down>", self.down)
        self.Listbox1.bind("<Up>", self.up)
        self.Listbox1.bind("<<ListboxSelect>>", self.select)
        self.Listbox5.bind("<Down>", self.down)
        self.Listbox5.bind("<Up>", self.up)
        self.Listbox7.bind("<Down>", self.down)
        self.Listbox7.bind("<Up>", self.up)
        self.Listbox6.bind("<Down>", self.down)
        self.Listbox6.bind("<Up>", self.up)
        self.Listbox3.bind("<Down>", self.down)
        self.Listbox3.bind("<Up>", self.up)

    def select(self, event):
        widget = event.widget
        selection = widget.curselection()
        selection = selection[0]

    def up(self, event):
        self.Listbox1.yview_scroll(-1, "units")
        self.Listbox5.yview_scroll(-1, "units")
        self.Listbox7.yview_scroll(-1, "units")
        self.Listbox6.yview_scroll(-1, "units")
        self.Listbox3.yview_scroll(-1, "units")

    def down(self, event):
        self.Listbox1.yview_scroll(1, "units")
        self.Listbox5.yview_scroll(1, "units")
        self.Listbox7.yview_scroll(1, "units")
        self.Listbox6.yview_scroll(1, "units")
        self.Listbox3.yview_scroll(1, "units")

    def carrega_arquivo(self):
        with open("./dados/programaMV.txt", "r") as f:
            for i, item in enumerate(f.readlines()):
                #inserir índice
                self.Listbox1.insert(i, str(i))
                #pega só o comando
                item_splitado = item.split()
                comando = item_splitado[0]
                self.Listbox3.insert(i, str(comando))
                try:
                    self.Listbox7.insert(i, self.COMENTARIOS_DICT[str(comando)])
                except:
                    self.Listbox7.insert(i, "")
                #pega os parâmetros
                if len(item_splitado) > 1:
                    atributos = item_splitado[1].split(",")
                    self.Listbox5.insert(i, str(atributos[0]))
                    if len(atributos) > 1:
                        self.Listbox6.insert(i, str(atributos[1]))
                    else:
                        self.Listbox6.insert(i, "")
                else:
                    self.Listbox5.insert(i, "")
                    self.Listbox6.insert(i, "")

if __name__ == '__main__':
    vp_start_gui()

