-
Notifications
You must be signed in to change notification settings - Fork 0
/
instruction.go
52 lines (44 loc) · 1.12 KB
/
instruction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package machine
import (
"fmt"
"strconv"
"strings"
)
type Instruction struct {
Opcode Opcode
Arg int
}
func ParseProgram(program []string) ([]Instruction, error) {
instructions := make([]Instruction, len(program))
for i, instruction := range program {
parsedInstruction, err := parseInstruction(instruction)
if err != nil {
return nil, fmt.Errorf("machine: parsing instruction %q: %w", instruction, err)
}
instructions[i] = *parsedInstruction
}
return instructions, nil
}
func parseInstruction(in string) (*Instruction, error) {
tokens := strings.SplitN(in, " ", 2)
if len(tokens) != 2 {
return nil, fmt.Errorf("machine: got %d tokens, expected 2", len(tokens))
}
opcode, ok := ops[tokens[0]]
if !ok {
return nil, fmt.Errorf("machine: unknown opcode: %q", tokens[0])
}
arg, err := strconv.Atoi(tokens[1])
if err != nil {
return nil, fmt.Errorf("machine: parsing argument %q: %w", tokens[1], err)
}
return &Instruction{
Opcode: opcode,
Arg: arg,
}, nil
}
func CloneProgram(program []Instruction) []Instruction {
ret := make([]Instruction, len(program))
copy(ret, program)
return ret
}