Last active
August 29, 2015 14:02
-
-
Save Sumolari/933c88654e2d36472708 to your computer and use it in GitHub Desktop.
Intérprete de código de máquina contador
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Memoria { | |
var r = Dictionary<String,Int>() | |
func suc( i:String ) -> Int { | |
self.r[ i ] = ( self.r[ i ]? ) ? self.r[ i ]! + 1 : 1 | |
return self.r[ i ]! | |
} | |
func pre( i:String ) -> Int { | |
let ret = ( self.r[ i ]? ) ? self.r[ i ]! - 1 : -1 | |
self.r[ i ] = ( self.r[ i ]? && self.r[ i ] > 0 ) ? self.r[ i ]! - 1: 0 | |
return ret | |
} | |
func val( i:String ) -> Int { | |
return ( self.r[ i ]? ) ? self.r[ i ]! : 0 | |
} | |
func set( i:String, k:Int ) { | |
self.r[ i ] = k | |
} | |
func print() { | |
for ( key, value ) in self.r { | |
println( "[\(key)]: \(value)" ) | |
} | |
} | |
} | |
protocol Instruccion { | |
func ejecutar( contador:Int, mem:Memoria ) -> Int | |
} | |
class InstruccionSuc : Instruccion { | |
var registro : String | |
init( r: String ) { | |
self.registro = r | |
} | |
func ejecutar( contador:Int, mem:Memoria ) -> Int { | |
mem.suc( self.registro ) | |
return contador + 1 | |
} | |
} | |
class InstruccionPre : Instruccion { | |
var registro : String | |
var salto: Int | |
init( r: String, s:Int ) { | |
self.registro = r | |
self.salto = s | |
} | |
func ejecutar( contador:Int, mem:Memoria ) -> Int { | |
let res = mem.pre( self.registro ) | |
if ( res < 0 ) { | |
return self.salto | |
} else { | |
return contador + 1 | |
} | |
} | |
} | |
class InstruccionGoto: Instruccion { | |
var salto: Int | |
init( s:Int ) { | |
self.salto = s | |
} | |
func ejecutar( contador:Int, mem:Memoria ) -> Int { | |
return self.salto | |
} | |
} | |
func suc( i:String ) -> InstruccionSuc { | |
return InstruccionSuc( r:i ) | |
} | |
func pre( i:String, k:Int ) -> InstruccionPre { | |
return InstruccionPre( r:i, s:k ) | |
} | |
func goto( k:Int ) -> InstruccionGoto { | |
return InstruccionGoto( s:k ) | |
} | |
func ejecutar( inicio:Dictionary<String,Int>, comandos:Array<Instruccion> ) -> Memoria { | |
var registros = Memoria() | |
for ( key, value ) in inicio { | |
registros.set( key, k: value ) | |
} | |
var c = 0; | |
while ( c < comandos.count ) { | |
c = comandos[ c ].ejecutar( c, mem: registros ) | |
} | |
registros.print() | |
return registros | |
} | |
// Código de una máquina contador que hace la operación copiar( i, j ) : [ i ] <- [ j ] | |
// let copiar : Array<Instruccion> = [ pre("j",4), suc("i"), suc("k"), goto(0), pre("k",7), suc("j"), goto(4) ] | |
let instrucciones : Array<Instruccion> = [ pre("j",4), suc("i"), suc("k"), goto(0), pre("k",7), suc("j"), goto(4) ] | |
ejecutar( [ "i": 0, "j": 20 ], instrucciones ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment