Commit 181554a6 authored by Brandon Warner's avatar Brandon Warner
Browse files

Started git repo for python z80/z180 asmbler. This is a work in progress.

parents
import argparse
import re
import pprint
VERBOSE = False
COMMENT_RE = re.compile("\\s*(.*);?.*")
LABEL_RE = re.compile("([A-Z_]*):.*")
INCLUDE_RE = re.compile("\\s*include '(.*)'")
LINE_RE = re.compile("\\s*(?P<label>[A-Z_]*):?\\s*(?P<code>[^;]+);?(?P<comment>.*)")
def get_all_code(input):
code_lines = []
input_lines = input.readlines()
for line in input_lines:
line = line.strip()
l = INCLUDE_RE.match(line)
if l:
if len(l.groups()) > 0 and len(l.groups()[0].strip()) > 0:
f = l.groups()[0].strip()
pprint.pprint(f)
with open(f, 'r') as fi:
for li in get_all_code(fi):
code_lines.append(li)
else:
code_lines.append(line)
return code_lines
def assemble(input, output):
LABELS = {}
code_lines = get_all_code(input)
if VERBOSE:
for l in code_lines:
#pprint.pprint(l)
pass
parsed_lines = []
for line in code_lines:
l = []
t = {}
i = 0
while i < len(line):
r = "code"
c = line[i]
if c in ":;\n":
if c == '\n':
if len(l) > 0:
t[r] = "".join(l).strip()
elif c == ":":
if len(l) > 0:
t["label"] = "".join(l).strip()
l = []
else:
pass #TODO throw error at empty label name
elif c == ";":
if len(l) > 0:
t["code"] = "".join(l).strip()
l = []
r = "comment"
else:
l.append(c)
i = i + 1
if "code" not in t.keys():
if len(l) > 0:
t["code"] = "".join(l).strip()
parsed_lines.append(t)
pprint.pprint(parsed_lines)
return
for line in code_lines:
line = line.strip()
if len(line) > 0:
if VERBOSE: pprint.pprint(line)
g = COMMENT_RE.match(line)
if g is not None:
g = g.groups()
if len(g) != 0 and len(g[0].strip()) > 0:
to_parse = g[0].strip();
if VERBOSE: pprint.pprint(to_parse)
l = LABEL_RE.match(to_parse)
if l is not None:
l = l.groups()
if len(l) != 0 and len(l[0].strip()) > 0:
LABELS[l[0].strip()] = -1
if VERBOSE: pprint.pprint(LABELS)
def main():
global VERBOSE
parser = argparse.ArgumentParser(description="z80/z180 asmbler in python v0.1-dev")
parser.add_argument("-i", "--input", help="Input file to assemble", required=True)
parser.add_argument("-o", "--output", help="Output file for assembled code", required=True)
parser.add_argument("-v", "--verbose", help="Verbose mode", action="store_true")
args = parser.parse_args()
if args.verbose:
VERBOSE = True
with open(args.input, 'r') as input:
with open(args.output, 'wb') as output:
assemble(input, output)
main()
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment