Leo_Ensa.tcl



#PROGRAMA QUE REPRESENTA UN LENGUAJE ENSAMBLADOR
#
#ELABORADO POR RAFAEL RODRIGUEZ OCHOA
#
# 12 DE JUNIO DE 1997


catch {destroy .a}
catch {destroy .b}
frame .a 
set renglon 0
set fin_programa 0
set n 0
set bandera 1
global data a b fileid fileName

frame .a.b 
#frame del nombre del archivo
frame .a.b.c
#frame del nombre del archivo y del text widget
label .a.b.c.label -text "Nombre del archivo:" -relief sunken -bg PapayaWhip\
	-width 17 -anchor w 
entry .a.b.c.entry -width 20 -textvariable fileName
button .a.b.c.button -text "Cargar" \
	-command { textLoadFile .a.b.c.text $fileName
			if { [array exists memoria] } {
				unset memoria
			}
			if { [file readable $fileName] } {
			 	set fileid [open $fileName "r"] 
			}
			bell
			set a 0
			set b 0
			set boton 0
			set i 1
			set cont 0
			while { $i == 1 } {
				gets $fileid data
				set memoria($cont) $data
				if {$data == ""} {
					set i 0 
				}
			incr cont
			}
		} -fg yellow -bg firebrick
pack .a.b.c.label .a.b.c.entry -side left
pack .a.b.c.button -side left -pady 5 -padx 10
bind .a.b.c.entry <Return> "
    textLoadFile .a.b.text \$fileName
    focus .a.string.entry
"
focus .a.b.c.entry
	-bg Blue3 -fg green4 -relief raised 

text .a.b.c.text -yscrollcommand ".a.b.c.scroll set" \
	-setgrid true -height 5 -width 20

scrollbar .a.b.c.scroll -command ".a.b.c.text yview"
pack .a.b.label -side top
pack .a.b.c.scroll -side right -fill y
pack .a.b.c.text -expand yes -fill both
pack .a.b -side top
pack .a.b.c -side top


frame .a.d
foreach i { f2 f3 f4 f5 } {
    frame .a.d.$i -bd 2
    entry .a.d.$i.entry -relief sunken -width 30
   label .a.d.$i.label 
   pack .a.d.$i.entry -side right
   pack .a.d.$i.label -side left
}


.a.d.f2.label config -text Entrada: -bg royalblue -fg black -relief ridge
.a.d.f5.label config -text Pila: -fg black  -bg yellow -relief ridge
.a.d.f3.label config -text Salida: -bg red -fg white -relief ridge
pack .a.d.f3 .a.d.f4 .a.d.f5 .a.d.f2 -side bottom -fill x

label .a.d.desp -text "Velocidad de despliegue del programa:" \
	-width 33 -anchor w -relief raised -bg SteelBlue
pack .a.d.desp -side left

entry .nada -width 0 -textvariable continua
pack .nada
	-command { avanza_auto }

proc avanza_auto { } {
  global fin_programa continua
  if { $fin_programa == 0 } then { 	
     avanza_instruccion 
  }
  after 100 { set continua "" }
  tkwait variable continua
  unset continua
  after 1000 avanza_auto
}

set boton 0
button .a.d.mas -text "Manual" -fg blue -bg green \
	-command { avanza_instruccion }

proc avanza_instruccion { } {
   global data boton memoria a b
   set data $memoria($boton)
   .a.d.f2.entry delete 0 end
   .a.d.f3.entry delete 0 end
   .a.d.f2.entry insert 0 [sin_direccion $data]		
   if { $a > 0 } {
	.a.d.f5.entry delete 0 end
	.a.d.f5.entry insert 0 $a
   }
   if { $a == 0 } {
	.a.d.f5.entry delete 0 end
   }
   if { $b > 0 } {
	.a.d.f4.entry delete 0 end
	.a.d.f4.entry insert 0 $b
   }
   if { $b == 0 } {
	.a.d.f4.entry delete 0 end
   }
   instruccion $data
   incr boton
}
	
pack .a.d.mas .a.d.auto -side left
pack .a.d -side left



pack .a -side left 
#FIN DEL PROGRAMA PRINCIPAL
#======================================================================

proc textLoadFile {w file} {
    set f [open $file]
    $w delete 1.0 end
    while {![eof $f]} {
	$w insert end [read $f 10000]
    }
    close $f
}

proc instruccion {renglon} {
	global accion donde cuanto a b boton bandera fileid
	set comando [split $renglon]
	set direccion [lindex $comando 0]
	set accion [lindex $comando 1]
	set donde [lindex $comando 2]
	set cuanto [lindex $comando 3]
	switch $accion {
		mover {set $donde $cuanto 
			return }  
		sumar { set variable [ letra $donde ]
				  set $variable [expr $a+$b]
				  return 
		}
		multiplicar { set variable [ letra $donde ]
				  set $variable [expr $a*$b]
				  return 
		}
		restar { set variable [ letra $donde ]
		         if { $variable	== "a" } {
					set $variable [expr $a-$b] }
			   if { $variable	== "b" } {
					set $variable [expr $b-$a] }
			   return
		}
		dividir { set variable [ letra $donde ]
		          if { $variable == "a" } {
					set $variable [expr $a/$b] }
			    if { $variable == "b" } {
					set $variable [expr $b/$a] }
			    return
		}

		decrementar { set variable [letra $donde]
				  set $variable [expr $[subst $variable]-1]
				  return
		}

		comparar { if { $a == $cuanto } {
			     	set bandera 0
			     }
			     if { $b == $cuanto } {
			     	set bandera 0
			     }

			     return
		}
				
		saltar { if { $bandera == 1 } {
				set boton [expr $donde - 1]
			   	return $boton } 
		}
		imprimir { if { $donde == "a" } {
					.a.d.f3.entry insert 0 $a
					return  }
			     if { $donde == "b" } {
					.a.d.f3.entry insert 0 $b
					return  }
		}
		alto { global fin_programa 
                   set fin_programa 1
                   .a.d.f3.entry insert 0 "Programa terminado"
			 bell
			 bell
			 return  }
	}
}


#envia la instruccion quitando la direccion
proc sin_direccion { con_direccion } {
	set no_dir [split $con_direccion]
	set no_direccion [lindex $no_dir 1]
	append no_direccion " [lindex $no_dir 2]" " [lindex $no_dir 3]"
	return $no_direccion
}

#Verifica que variable es.
proc letra { donde } {
	if { $donde == "a" } { 
		return a }
	if { $donde == "b" } { 
		return b }
}