Пример #1
0
// 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
}
Пример #2
0
func TestCommaParse(t *testing.T) {
	testCaseList := []testCaseForTestCommaParse{
		// This is a comment line.
		{
			data:            "; This is a comment",
			expectedRemains: "; This is a comment",
			expectedOK:      false,
		},
		// This is a Comma and a Comma.
		{
			data: "MAIN			START",
			expectedRemains: "MAIN			START",
			expectedOK: false,
		},
		// A Comma must be starts without any sapce.
		{
			data:            "         OUT    BUF,LEN ",
			expectedRemains: "         OUT    BUF,LEN ",
			expectedOK:      false,
		},
		{
			data:            ", DATA1",
			expectedRemains: "DATA1",
			expectedOK:      true,
		},
		{
			data: "		,999 ; this is comment",
			expectedRemains: "999 ; this is comment",
			expectedOK:      true,
		},
	}
	for testIndex, testCase := range testCaseList {
		target := share.NewComma()
		testData := testCase.data
		expectedRemains := testCase.expectedRemains
		expectedOK := testCase.expectedOK

		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)
		}
	}

}
Пример #3
0
// Parse parser operands.
func (operands *LabelLabel) Parse(source string) (string, bool) {
	label1 := operand.NewLabel()
	remains, ok := label1.Parse(source)
	if !ok {
		return source, false
	}
	remains, ok = share.NewComma().Parse(remains)
	if !ok {
		return source, false
	}
	label2 := operand.NewLabel()
	remains, ok = label2.Parse(source)
	if !ok {
		return source, false
	}
	operands.Operands = []operand.Operand{label1, label2}
	return remains, true
}
Пример #4
0
// Parse parser operands.
func (operands *Constants) Parse(source string) (string, bool) {
	operands.Operands = []operand.Operand{}
	currentRemains := source
	for {
		constant := operand.NewConstant()
		remains, ok := constant.Parse(currentRemains)
		if !ok {
			if 0 < len(operands.Operands) {
				return currentRemains, true
			}
			return source, false
		}
		currentRemains, ok = share.NewComma().Parse(remains)
		if !ok {
			return remains, true
		}
	}
}
Пример #5
0
// 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
}