예제 #1
0
def test_parse_line():
    a = Assembler()
    lines = iter(src.splitlines())
    assert a.parse_line(next(lines)) == (None, '.set', (
        'const',
        '123',
    ))
    assert a.parse_line(next(lines)) == (None, '.set', (
        'const_left',
        '976',
    ))
    assert a.parse_line(next(lines)) == None
    assert a.parse_line(next(lines)) == ('start', 'wait', ('42', ))
    assert a.parse_line(next(lines)) == (None, 'ld', (
        'r0',
        'r1',
        '0',
    ))
    assert a.parse_line(next(lines)) == (None, 'st', (
        'r0',
        'r1',
        '0',
    ))
    assert a.parse_line(next(lines)) == (None, 'halt', ())
    assert a.parse_line(next(lines)) == ('end', None, ())
    assert a.parse_line(next(lines)) == (
        None, '.data', ()
    )  # test left-aligned directive is not treated as label
예제 #2
0
def test_parse_line():
    a = Assembler()
    lines = src.splitlines()
    # note: line number = index + 1
    assert a.parse_line(lines[0]) == None
    assert a.parse_line(lines[1]) == ('start', 'wait', ('42', ))
    assert a.parse_line(lines[2]) == (None, 'ld', (
        'r0',
        'r1',
        '0',
    ))
    assert a.parse_line(lines[3]) == (None, 'st', (
        'r0',
        'r1',
        '0',
    ))
    assert a.parse_line(lines[4]) == (None, 'halt', ())
    assert a.parse_line(lines[5]) == ('end', None, ())
예제 #3
0
def test_parse_labels_correctly():
    """
    description of what defines a label
    https://sourceware.org/binutils/docs/as/Statements.html
    https://sourceware.org/binutils/docs/as/Labels.html
    """
    a = Assembler()
    assert a.parse_line('') is None
    assert a.parse_line('label: .set const, 42') == ('label', '.set', (
        'const',
        '42',
    ))
    assert a.parse_line('label:.set const, 42') == ('label', '.set', (
        'const',
        '42',
    ))
    assert a.parse_line('label:') == ('label', None, ())
    assert a.parse_line('    label:') == ('label', None, ())
    assert a.parse_line('    label:  ') == ('label', None, ())
    assert a.parse_line('nop  ') == (None, 'nop', ())
    assert a.parse_line('.set c, 1  ') == (None, '.set', (
        'c',
        '1',
    ))
    assert a.parse_line('invalid : nop') == (
        None, 'invalid', (': nop', ))  # no whitespace between label and colon
    assert a.parse_line('.string "hello world"') == (None, '.string',
                                                     ('"hello world"', ))
    assert a.parse_line('.string "hello : world"') == (None, '.string',
                                                       ('"hello : world"', )
                                                       )  # colon in string
    assert a.parse_line('label::') == ('label', ':', ())
    assert a.parse_line('label: :') == ('label', ':', ())
    assert a.parse_line('a_label:') == ('a_label', None, ())
    assert a.parse_line('$label:') == ('$label', None, ())
    assert a.parse_line('.label:') == ('.label', None, ())
    assert a.parse_line('&label:') == (None, '&label:', ()
                                       )  # & not a valid char in a label