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

Function parseInstruction

asm/parser.go:107–136  ·  view source on GitHub ↗
(line string)

Source from the content-addressed store, hash-verified

105}
106
107func parseInstruction(line string) (Instruction, error) {
108 tokens := INSTRUCTION.FindStringSubmatch(line)
109 tokens = stripEmptyGroups(tokens)
110 instructionName := tokens[1]
111
112 var operands string
113 if len(tokens) > 2 {
114 operands = tokens[2]
115 }
116
117 var instruction Instruction
118 var err error
119 switch instructionName {
120 case "ADD", "AND", "XOR", "OR", "CMP", "LD", "ST":
121 instruction, err = parseTwoRegisterInstruction(instructionName, operands)
122 case "SHR", "SHL", "NOT", "JR":
123 instruction, err = parseOneRegisterInstruction(instructionName, operands)
124 case "DATA":
125 instruction, err = parseDataInstruction(operands)
126 case "CLF":
127 instruction = CLF{}
128 case "OUT", "IN":
129 instruction, err = parseIOInstruction(instructionName, operands)
130 case "CALL", "JMP", "JMPZ", "JMPE", "JMPEZ", "JMPA", "JMPAZ", "JMPAE", "JMPAEZ", "JMPC", "JMPCZ", "JMPCE", "JMPCEZ", "JMPCA", "JMPCAZ", "JMPCAE", "JMPCAEZ":
131 instruction, err = parseLabelledJump(instructionName, operands)
132 default:
133 return nil, fmt.Errorf("unknown instruction name '%s'", instructionName)
134 }
135 return instruction, err
136}
137
138func parseLabelledJump(name string, operands string) (Instruction, error) {
139 arguments := LABEL_EXTRACTOR.FindStringSubmatch(operands)

Callers 1

ParseMethod · 0.85

Calls 6

stripEmptyGroupsFunction · 0.85
parseDataInstructionFunction · 0.85
parseIOInstructionFunction · 0.85
parseLabelledJumpFunction · 0.85

Tested by

no test coverage detected