diff options
| author | s-ol <s-ol@users.noreply.github.com> | 2020-05-27 21:10:29 +0000 |
|---|---|---|
| committer | s-ol <s-ol@users.noreply.github.com> | 2020-05-27 21:10:29 +0000 |
| commit | 132db77501f0399c413886c885b580f0070b54c0 (patch) | |
| tree | 37ce9a89e2ebce87be93b05727c8e06bee793aca /subx.py | |
| parent | add test.py (diff) | |
| download | subv-132db77501f0399c413886c885b580f0070b54c0.tar.gz subv-132db77501f0399c413886c885b580f0070b54c0.zip | |
output emulatable .elf
Diffstat (limited to 'subx.py')
| -rw-r--r-- | subx.py | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -0,0 +1,40 @@ +import re +white = re.compile('[ \t\.\n]+') +hex = re.compile('^(0x)?[0-9a-f]+$') + +def parse_part(part): + part = part.split('/') + if hex.match(part[0]): + part[0] = int(part[0], 16) + return tuple(part) + +def parse_instr(line): + parts = white.split(line) + parts = [parse_part(part) for part in parts if part != ''] + return parts + +def parse_segment(line): + parts = white.split(line) + return (parts[1], int(parts[2], 16)) + +def format_part(part): + if not isinstance(part[0], str): + part = ('{:02x}'.format(part[0]),) + part[1:] + return '/'.join(part) + +def format_instr(inst, comment=None): + packed = ' '.join(format_part(part) for part in inst) + if comment: + packed = packed + ' # ' + comment + return packed + +def clean(line): + return line.strip().split('#')[0] + +def classify(line): + if line.startswith('=='): # segment + return 'segment' + elif line.endswith(':'): # label + return 'label' + else: + return 'instr' |
