Still working on it

This commit is contained in:
Brian Buller 2016-12-23 12:32:42 -06:00
parent 7c21111b12
commit 99e36b8912

View File

@ -51,18 +51,14 @@ func main() {
go readUserInput(eventChan)
go sendNoneEvent(eventChan)
curr = 0
// Find a set of instructions that look like:
// inc a
// dec c
// jnz c -2
// and convert them to:
// mlt a c a
// cpy 0 c
var ins []string
for curr < len(instructions) || done {
if debug {
lastInst := ins
// print state and wait for user to step
PrintState()
PrintInstructionState()
fmt.Println("Last Instruction:", lastInst)
//PrintInstructionState()
fmt.Println("(q): quit (space): pause/unpause")
ev := <-eventChan
if ev.Type == termbox.EventKey {
@ -74,8 +70,9 @@ func main() {
}
}
}
ins := strings.Fields(instructions[curr])
ins = strings.Fields(instructions[curr])
curr++
// Optimize multiplication
/*
// TODO:
> cpy b c
@ -84,9 +81,43 @@ func main() {
jnz c -2
dec d
jnz d -5
== mult b d a
and curr += 6
== mlt b d a
and curr += 5
*/
if ins[0] == "cpy" {
if strings.HasPrefix(instructions[curr+1], "inc") && strings.HasPrefix(instructions[curr+2], "dec") && strings.HasPrefix(instructions[curr+3], "jnz") && strings.HasPrefix(instructions[curr+4], "dec") && strings.HasPrefix(instructions[curr+5], "jnz") {
allGood := true
// The right instruction set, make sure the arguments match up
ins1 := strings.Fields(instructions[curr+1])
ins2 := strings.Fields(instructions[curr+2])
ins3 := strings.Fields(instructions[curr+3])
ins4 := strings.Fields(instructions[curr+4])
ins5 := strings.Fields(instructions[curr+5])
// Make sure all "c"s are correct
if ins[2] != ins2[1] && ins[2] != ins3[1] {
allGood = false
}
if allGood {
// Make sure all "b"s are correct
if ins[1] == ins1[1] || ins[1] == ins2[1] || ins[1] == ins4[1] {
allGood = false
}
}
if allGood {
// Make sure all "d"s are correct
if (ins[1] == ins4[1] || ins[2] == ins4[1]) || ins4[1] != ins5[1] {
allGood = false
}
}
if allGood {
// Go ahead and change the instruction
ins = []string{"mult", ins[1], ins4[1], ins1[1]}
curr += 5
}
}
}
switch ins[0] {
case "jnz":
// If we have a jnz c -2 it could be moving all of c into another register