refactoring
This commit is contained in:
parent
e73352b0b3
commit
86ffeee27c
|
@ -69,26 +69,28 @@ class Machine(input: Array[Long])
|
||||||
}
|
}
|
||||||
|
|
||||||
@scala.annotation.tailrec
|
@scala.annotation.tailrec
|
||||||
final def runUntilHalt(): Machine =
|
final def run(): Machine =
|
||||||
{
|
{
|
||||||
val tuple = OpCode99.parseInt(software(pointer).toInt)
|
val tuple = OpCode99.parseInt(software(pointer).toInt)
|
||||||
tuple match
|
tuple match
|
||||||
{
|
{
|
||||||
case (_, _, _, OpCode99) =>
|
case (_, _, _, OpCode99) =>
|
||||||
state = Finished
|
state = Finished
|
||||||
|
println("Machine finished")
|
||||||
this
|
this
|
||||||
|
|
||||||
case (_, _, m1, outputInstr: Output) =>
|
case (_, _, m1, outputInstr: Output) =>
|
||||||
outputStream.addOne(outputInstr.output(software, relative, software(pointer+1), m1))
|
outputStream.addOne(outputInstr.output(software, relative, software(pointer+1), m1))
|
||||||
pointer += outputInstr.length
|
pointer += outputInstr.length
|
||||||
state = Ready
|
state = Ready
|
||||||
runUntilHalt()
|
run()
|
||||||
|
|
||||||
case (_, _, m1, inputInstr: Input) =>
|
case (_, _, m1, inputInstr: Input) =>
|
||||||
//buffer is empty
|
//buffer is empty
|
||||||
if (inputStream.isEmpty)
|
if (inputStream.isEmpty)
|
||||||
{
|
{
|
||||||
state = Input
|
state = Input
|
||||||
|
println("Ran out of inputs")
|
||||||
this
|
this
|
||||||
}
|
}
|
||||||
// buffer contains inputs
|
// buffer contains inputs
|
||||||
|
@ -97,7 +99,7 @@ class Machine(input: Array[Long])
|
||||||
inputInstr.input(software, relative, software(pointer+1), m1, inputStream.remove(0))
|
inputInstr.input(software, relative, software(pointer+1), m1, inputStream.remove(0))
|
||||||
pointer += inputInstr.length
|
pointer += inputInstr.length
|
||||||
state = Ready
|
state = Ready
|
||||||
runUntilHalt()
|
run()
|
||||||
}
|
}
|
||||||
|
|
||||||
case (m3, m2, m1, instruction: Jump) =>
|
case (m3, m2, m1, instruction: Jump) =>
|
||||||
|
@ -105,49 +107,21 @@ class Machine(input: Array[Long])
|
||||||
if (bool) pointer = jmpPtr
|
if (bool) pointer = jmpPtr
|
||||||
else pointer += instruction.length
|
else pointer += instruction.length
|
||||||
state = Ready
|
state = Ready
|
||||||
runUntilHalt()
|
run()
|
||||||
|
|
||||||
case (m3, m2, m1, instruction: Action) =>
|
case (m3, m2, m1, instruction: Action) =>
|
||||||
instruction.exec(software, relative, software(pointer+1), software(pointer+2), software(pointer+3), m1, m2, m3)
|
instruction.exec(software, relative, software(pointer+1), software(pointer+2), software(pointer+3), m1, m2, m3)
|
||||||
pointer += instruction.length
|
pointer += instruction.length
|
||||||
state = Ready
|
state = Ready
|
||||||
runUntilHalt()
|
run()
|
||||||
|
|
||||||
case (_, _, m1, OpCode9) =>
|
case (_, _, m1, OpCode9) =>
|
||||||
relative += OpCode9.exec(software, relative, software(pointer+1), m1)
|
relative += OpCode9.exec(software, relative, software(pointer+1), m1)
|
||||||
pointer += OpCode9.length
|
pointer += OpCode9.length
|
||||||
state = Ready
|
state = Ready
|
||||||
runUntilHalt()
|
run()
|
||||||
|
|
||||||
case _ => throw new Exception("Something went wrong")
|
case _ => throw new Exception("Something went wrong")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@scala.annotation.tailrec
|
|
||||||
final def run(): Machine =
|
|
||||||
{
|
|
||||||
this.state match
|
|
||||||
{
|
|
||||||
case Ready =>
|
|
||||||
runUntilHalt()
|
|
||||||
run()
|
|
||||||
case Finished =>
|
|
||||||
println("Machine finished")
|
|
||||||
this
|
|
||||||
case Output =>
|
|
||||||
runUntilHalt()
|
|
||||||
run()
|
|
||||||
case Input =>
|
|
||||||
if (inputStream.isEmpty)
|
|
||||||
{
|
|
||||||
println("Ran out of inputs")
|
|
||||||
this
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
runUntilHalt()
|
|
||||||
run()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue