diff --git a/hal/instructions.go b/hal/instructions.go index 72eea09..53c6808 100644 --- a/hal/instructions.go +++ b/hal/instructions.go @@ -12,7 +12,7 @@ type ProgrammedInstruction struct { func (pi *ProgrammedInstruction) Execute(module *Module) error { if pi.Instruction.ExecuteWithOperand != nil { - module.debug("Operand: %d", pi.Operand) + module.debug("Operand: %f", pi.Operand) return pi.Instruction.ExecuteWithOperand(module, pi.Operand) } else if pi.Instruction.Execute != nil { return pi.Instruction.Execute(module) diff --git a/hal/module.go b/hal/module.go index fbf4532..342b951 100644 --- a/hal/module.go +++ b/hal/module.go @@ -38,21 +38,27 @@ func (h *Module) debug(format string, args ...interface{}) { fmt.Printf("[DEBUG] "+format+"\n", args...) } -func (h *Module) Step() { +func (h *Module) Step() error { instruction := h.programStorage[h.programCounter()] h.increaseProgramCounter() if instruction == nil { - return + h.debug("Skip undefined instruction %d", h.programCounter()) + return nil } h.debug("Instruction: %s", instruction.Instruction.Name) h.debug("Accumulator before: %f", h.accumulator) - instruction.Execute(h) + if err := instruction.Execute(h); err != nil { + return err + } h.debug("Accumulator after: %f", h.accumulator) + return nil } func (h *Module) Run() error { for !h.isStopped && h.programCounter() <= maxInstructions { - h.Step() + if err := h.Step(); err != nil { + return err + } } if h.programCounter() > maxInstructions { return fmt.Errorf("module exceeded max instructions without being stopped") diff --git a/parser/program_parser.go b/parser/program_parser.go index 563cb86..24a456c 100644 --- a/parser/program_parser.go +++ b/parser/program_parser.go @@ -29,7 +29,7 @@ func ParseProgram(input []string) (hal.Program, error) { } func parseInstruction(args []string) (*hal.ProgrammedInstruction, error) { - if len(args) != 0 && len(args) != 1 { + if len(args) != 1 && len(args) != 2 { return nil, fmt.Errorf("invalid instruction args count (count = %d)", len(args)) } instruction := hal.FindInstructionByName(args[0])