git @ Cat's Eye Technologies hatoucan / 5566f2c
Getting a bit closer. Chris Pressey 9 years ago
2 changed file(s) with 22 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
0 10 print "hello"
101101 return (ord(s[0]), s[1:])
102102
103103
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
104112 def tokenize(s):
105 # TODO: strip the line number off the front, return as an int
113 (line_number, s) = scan_line_number(s)
106114 bytes = []
107115 while s:
108116 (byte, s) = scan(s)
109117 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))
111126
112127
113128 class TokenizedLine(object):
123138
124139 def write_to(self, f):
125140 """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)
129144 for byte in self.bytes:
130145 f.write(chr(byte))
131146 f.write(chr(0))
148163
149164 outfile = sys.stdout
150165
151 sys.write(outfile, chr(1) + chr(8)) # TODO: write helper for this
166 write_word(outfile, start_addr)
152167
153168 for tokenized_line in tokenized_lines:
154169 tokenized_line.write_to(outfile)