MCPcopy Create free account
hub / github.com/djhworld/simple-computer / Parse

Method Parse

asm/parser.go:32–67  ·  view source on GitHub ↗
(input io.Reader)

Source from the content-addressed store, hash-verified

30}
31
32func (p *Parser) Parse(input io.Reader) ([]Instruction, error) {
33 scanner := bufio.NewScanner(input)
34 instructions := []Instruction{}
35
36 for scanner.Scan() {
37 line := scanner.Text()
38 line = strings.TrimSpace(line)
39
40 if line == "" {
41 continue
42 }
43
44 if IS_DEFLABEL.MatchString(line) {
45 instructions = append(instructions, processLabel(line))
46 } else if IS_DEFSYMBOL.MatchString(line) {
47 if ins, err := parseDefSymbol(line); err == nil {
48 instructions = append(instructions, ins)
49 } else {
50 return nil, err
51 }
52 } else if INSTRUCTION.MatchString(line) {
53 if ins, err := parseInstruction(line); err == nil {
54 instructions = append(instructions, ins)
55 } else {
56 return nil, err
57 }
58 } else {
59 return nil, fmt.Errorf("unsupported/unparseable line: %s", line)
60 }
61
62 }
63 if err := scanner.Err(); err != nil {
64 return nil, err
65 }
66 return instructions, nil
67}
68
69func parseDefSymbol(line string) (Instruction, error) {
70 tokens := IS_DEFSYMBOL.FindStringSubmatch(line)

Callers 5

TestParseDefLabelFunction · 0.95
TestParseDefSymbolFunction · 0.95
testParseInstructionsFunction · 0.95
mainFunction · 0.95
mainFunction · 0.80

Calls 3

processLabelFunction · 0.85
parseDefSymbolFunction · 0.85
parseInstructionFunction · 0.85

Tested by 3

TestParseDefLabelFunction · 0.76
TestParseDefSymbolFunction · 0.76
testParseInstructionsFunction · 0.76