From 372be38613d5ee16d4fea9f73f3036e92f364a09 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Janouch?= Date: Thu, 20 Oct 2016 01:58:16 +0200 Subject: [PATCH] Get rid of machine.go --- machine.go | 61 ------------------------------------------------------ main.go | 58 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 57 insertions(+), 62 deletions(-) delete mode 100644 machine.go diff --git a/machine.go b/machine.go deleted file mode 100644 index c43ccae..0000000 --- a/machine.go +++ /dev/null @@ -1,61 +0,0 @@ -package main - -import ( - "errors" - "fmt" -) - -func Run(code []int16) error { - if len(code) != 100 { - return errors.New("Code must be exactly 100 mailboxes long") - } - - // for i, x := range code { - // fmt.Printf("%d: %d\n", i, x) - // } - // fmt.Println() - - var accumulator int16 = 0 - - pc := 0 - for pc >= 0 && pc < len(code) { - i := code[pc] - arg := i % 100 - pc++ - - switch i - arg { - case IHALT: - return nil - case IADD: - accumulator += code[arg] - case ISUBTRACT: - accumulator -= code[arg] - case ISTORE: - code[arg] = accumulator - case ILOAD: - accumulator = code[arg] - case IBRANCH: - pc = int(i % 100) - case IBRANCH_IF_ZERO: - if accumulator == 0 { - pc = int(arg) - } - case IBRANCH_IF_POSITIVE: - if accumulator > 0 { - pc = int(arg) - } - case IIO: - switch arg { - case IO_INPUT: - fmt.Printf("Input: ") - fmt.Scanf("%d\n", &accumulator) - case IO_OUTPUT: - fmt.Printf("Output: %d\n", accumulator) - } - default: - e := fmt.Sprintf("Unsupported instruction %d at %d", i, pc) - return errors.New(e) - } - } - return errors.New("Program counter ran away") -} diff --git a/main.go b/main.go index d08e114..61a7393 100644 --- a/main.go +++ b/main.go @@ -1,10 +1,66 @@ package main import ( + "errors" "fmt" "os" ) +func run(code []int16) error { + if len(code) != 100 { + return errors.New("Code must be exactly 100 mailboxes long") + } + + // for i, x := range code { + // fmt.Printf("%d: %d\n", i, x) + // } + // fmt.Println() + + var accumulator int16 = 0 + + pc := 0 + for pc >= 0 && pc < len(code) { + i := code[pc] + arg := i % 100 + pc++ + + switch i - arg { + case IHALT: + return nil + case IADD: + accumulator += code[arg] + case ISUBTRACT: + accumulator -= code[arg] + case ISTORE: + code[arg] = accumulator + case ILOAD: + accumulator = code[arg] + case IBRANCH: + pc = int(i % 100) + case IBRANCH_IF_ZERO: + if accumulator == 0 { + pc = int(arg) + } + case IBRANCH_IF_POSITIVE: + if accumulator > 0 { + pc = int(arg) + } + case IIO: + switch arg { + case IO_INPUT: + fmt.Printf("Input: ") + fmt.Scanf("%d\n", &accumulator) + case IO_OUTPUT: + fmt.Printf("Output: %d\n", accumulator) + } + default: + e := fmt.Sprintf("Unsupported instruction %d at %d", i, pc) + return errors.New(e) + } + } + return errors.New("Program counter ran away") +} + func main() { if len(os.Args) != 2 { fmt.Printf("usage: %s file\n", os.Args[0]) @@ -12,7 +68,7 @@ func main() { fmt.Printf("Cannot open file: %s\n", err) } else if code, err := Assemble(file); err != nil { fmt.Printf("Assembly failed: %s\n", err) - } else if err = Run(code); err != nil { + } else if err = run(code); err != nil { fmt.Printf("Runtime error: %s\n", err) } else { os.Exit(0)