Fix some things

master
Jonas Franz 4 years ago
parent dda339123c
commit 38b5f34ff3
Signed by: JonasFranzDEV
GPG Key ID: 7293A220B7C38080
  1. 2
      hal/instructions.go
  2. 14
      hal/module.go
  3. 2
      parser/program_parser.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)

@ -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")

@ -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])

Loading…
Cancel
Save