Getting a bit closer.
Chris Pressey
9 years ago
101 | 101 |
return (ord(s[0]), s[1:])
|
102 | 102 |
|
103 | 103 |
|
|
104 |
def scan_line_number(s):
|
|
105 |
acc = []
|
|
106 |
while s and s[0].isdigit():
|
|
107 |
acc.append(s[0])
|
|
108 |
s = s[1:]
|
|
109 |
return (int(''.join(acc)), s)
|
|
110 |
|
|
111 |
|
104 | 112 |
def tokenize(s):
|
105 | |
# TODO: strip the line number off the front, return as an int
|
|
113 |
(line_number, s) = scan_line_number(s)
|
106 | 114 |
bytes = []
|
107 | 115 |
while s:
|
108 | 116 |
(byte, s) = scan(s)
|
109 | 117 |
bytes.append(byte)
|
110 | |
return (999, bytes)
|
|
118 |
return (line_number, bytes)
|
|
119 |
|
|
120 |
|
|
121 |
def write_word(f, word):
|
|
122 |
"""f being a file-like object, word being an integer 0-65535"""
|
|
123 |
low = word & 255
|
|
124 |
high = (word >> 8) & 255
|
|
125 |
f.write(chr(low) + chr(high))
|
111 | 126 |
|
112 | 127 |
|
113 | 128 |
class TokenizedLine(object):
|
|
123 | 138 |
|
124 | 139 |
def write_to(self, f):
|
125 | 140 |
"""f being a file-like object"""
|
126 | |
assert self.next_addr is not None
|
127 | |
# TODO: write line number in binary
|
128 | |
# TODO: write next_addr in binary
|
|
141 |
#assert self.next_addr is not None
|
|
142 |
write_word(f, 0) # TODO make this the next_addr
|
|
143 |
write_word(f, self.line_number)
|
129 | 144 |
for byte in self.bytes:
|
130 | 145 |
f.write(chr(byte))
|
131 | 146 |
f.write(chr(0))
|
|
148 | 163 |
|
149 | 164 |
outfile = sys.stdout
|
150 | 165 |
|
151 | |
sys.write(outfile, chr(1) + chr(8)) # TODO: write helper for this
|
|
166 |
write_word(outfile, start_addr)
|
152 | 167 |
|
153 | 168 |
for tokenized_line in tokenized_lines:
|
154 | 169 |
tokenized_line.write_to(outfile)
|