// Parse parser operands. func (operands *RegisterAddressX) Parse(source string) (string, bool) { // R r := operand.NewRegister() remains, ok := r.Parse(source) if !ok { return source, false } // ,(Comma) remains, ok = share.NewComma().Parse(remains) if !ok { return source, false } // ADR adr := operand.NewAddress() remains, ok = adr.Parse(remains) if !ok { return source, false } // ,(Comma) remains, ok = share.NewComma().Parse(remains) if !ok { return source, false } // X x := operand.NewRegister() remains, ok = x.Parse(remains) if !ok { return source, false } operands.Operands = []operand.Operand{r, adr, x} return remains, true }
// Parse parser operands. func (operands *Register) Parse(source string) (string, bool) { // R r := operand.NewRegister() remains, ok := r.Parse(source) if !ok { return source, false } operands.Operands = []operand.Operand{r} return remains, true }
// Parse parser operands. func (operands *R1R2) Parse(source string) (string, bool) { // R1 r1 := operand.NewRegister() remains, ok := r1.Parse(source) if !ok { return source, false } // ,(Comma) remains, ok = share.NewComma().Parse(remains) if !ok { return source, false } // R2 r2 := operand.NewRegister() remains, ok = r2.Parse(remains) if !ok { return source, false } operands.Operands = []operand.Operand{r1, r2} return remains, true }
func TestOperandRegisterParse(t *testing.T) { testCaseList := []testCaseForTestOperandRegisterParse{ // This is a comment line. { data: "; This is a comment", expectedRemains: "; This is a comment", expectedOK: false, expectedOperandSingleValue: "", expectedHasValue: false, }, // This is a OperandRegister and a OperandRegister. { data: "MAIN START", expectedRemains: "MAIN START", expectedOK: false, expectedOperandSingleValue: "", expectedHasValue: false, }, // A OperandRegister must be starts without any sapce. { data: " OUT BUF,LEN ", expectedRemains: " OUT BUF,LEN ", expectedOK: false, expectedOperandSingleValue: "", expectedHasValue: false, }, { data: "GR0, DATA1", expectedRemains: ", DATA1", expectedOK: true, expectedOperandSingleValue: "GR0", expectedHasValue: true, }, { data: "GR1", expectedRemains: "", expectedOK: true, expectedOperandSingleValue: "GR1", expectedHasValue: true, }, { data: "GR1 ; this is comment", expectedRemains: "; this is comment", expectedOK: true, expectedOperandSingleValue: "GR1", expectedHasValue: true, }, } for testIndex, testCase := range testCaseList { target := operand.NewRegister() testData := testCase.data expectedRemains := testCase.expectedRemains expectedOK := testCase.expectedOK expectedOperandSingleValue := testCase.expectedOperandSingleValue expectedHasValue := testCase.expectedHasValue actualRemains, actualOK := target.Parse(testData) if actualRemains != expectedRemains { t.Errorf("testCase[%d]=%#v, expected=%#v, actual=%#v\n", testIndex, testData, expectedRemains, actualRemains) } if actualOK != expectedOK { t.Errorf("testCase[%d]=%#v, expected=%#v, actual=%#v\n", testIndex, testData, expectedOK, actualOK) } actualOperandSingleValue := target.SingleValue if actualOperandSingleValue != expectedOperandSingleValue { t.Errorf("testCase[%d]=%#v, expected=%#v, actual=%#v\n", testIndex, testData, expectedOperandSingleValue, actualOperandSingleValue) } actualHasValue := target.HasValue if actualHasValue != expectedHasValue { t.Errorf("testCase[%d]=%#v, expected=%#v, actual=%#v\n", testIndex, testData, expectedHasValue, actualHasValue) } } }