-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.go
109 lines (86 loc) · 2.07 KB
/
parse.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
)
// parseRAMFile takes an io.Reader for a raw RAM file and returns
// the actual map to be used as the system RAM
func parseRAMFile(r io.Reader) (map[uint8]uint8, error) {
ram := initRAM()
sc := bufio.NewScanner(r)
for sc.Scan() {
if strings.TrimSpace(sc.Text()) == "" {
continue
}
addr, op, err := parseRAMLine(sc.Text())
if err != nil {
return ram, err
}
ram[addr] = op
}
if err := sc.Err(); err != nil {
return ram, err
}
return ram, nil
}
// parseRAMLine parses a whole line from the RAM file, e.g:
// 02: LDA 14
func parseRAMLine(l string) (uint8, uint8, error) {
p := strings.Split(l, ":")
if len(p) != 2 {
return 0, 0, fmt.Errorf("failed to parse ramfile line [%s]", l)
}
addr, err := strconv.Atoi(p[0])
if err != nil {
return 0, 0, fmt.Errorf("failed to parse address as number [%s]", p[0])
}
op, err := parseOp(strings.TrimSpace(p[1]))
if err != nil {
return 0, 0, err
}
return uint8(addr), op, nil
}
// parseOp parses and decodes the second portion of line from the
// RAM file. E.g:
// LDA 14
func parseOp(o string) (uint8, error) {
p := strings.Split(o, " ")
switch p[0] {
case "NOP":
return NOP, nil
case "LDA":
return parseOpWithArg(LDA, p)
case "ADD":
return parseOpWithArg(ADD, p)
case "JMP":
return parseOpWithArg(LDA, p)
case "OUT":
return OUT, nil
case "HALT":
return HALT, nil
default:
// parse as literal number
n, err := strconv.Atoi(p[0])
if err != nil {
return 0, fmt.Errorf("failed to parse literal number [%s]", o)
}
return uint8(n), nil
}
return 0, nil
}
// parseOpWithArg converts the split, textual representation of an op, e.g.:
// [LDA, 14]
// into the actual uint8 needed by the CPU
func parseOpWithArg(instr uint8, p []string) (uint8, error) {
if len(p) != 2 {
return 0, fmt.Errorf("%s without address %s", instructionNames[instr], p)
}
addr, err := strconv.Atoi(p[1])
if err != nil {
return 0, fmt.Errorf("%s address is non-number %s", instructionNames[instr], p)
}
return op(instr, uint8(addr)), nil
}