|
2 | 2 | from instruction import str_to_name
|
3 | 3 | from instruction import str_to_value
|
4 | 4 |
|
| 5 | +def error(msg): |
| 6 | + print("[!!] " + msg) |
| 7 | + |
5 | 8 | class Parser():
|
6 |
| - # Converts a string to and Instruction |
7 |
| - def line_to_instruction(self,s): |
8 |
| - |
9 |
| - # Conversion |
10 |
| - |
11 |
| - # remove endline |
12 |
| - words = s.replace("\n","") |
13 |
| - |
14 |
| - # shave off comments |
15 |
| - if ";" in words: |
16 |
| - words = words.split(";")[0] |
17 |
| - |
18 |
| - words = words.split(" ") |
19 |
| - |
20 |
| - # ignore lines with less than 2 words |
21 |
| - if len(words)<2: |
22 |
| - return None |
23 |
| - |
24 |
| - str_name = words[0].lower() |
25 |
| - str_value = words[1] |
26 |
| - num_name = str_to_name(str_name) |
27 |
| - num_value = str_to_value(str_value) |
28 |
| - |
29 |
| - # Error Checking |
30 |
| - if num_name is None: |
31 |
| - error("Invalid Instruction " + str_name) |
32 |
| - return None |
33 |
| - |
34 |
| - if num_value is None: |
35 |
| - error("Invalid Value " + str_value) |
36 |
| - return None |
37 |
| - |
38 |
| - return Instruction(num_name,num_value) |
39 |
| - |
40 |
| - # Converts a list of strings |
41 |
| - # to a list of Instructions |
42 |
| - def lines_to_assembly(self,lines): |
43 |
| - instr_list = [] |
44 |
| - for line in lines: |
45 |
| - instr = self.line_to_instruction(line) |
46 |
| - if instr is not None: |
47 |
| - instr_list.append(instr) |
48 |
| - return instr_list |
49 |
| - |
50 |
| - # Converts a list of instructions |
51 |
| - # to a list of bytes. |
52 |
| - def assembly_to_buffer(self,assembly): |
53 |
| - buf = [] |
54 |
| - for instr in assembly: |
55 |
| - name = chr(instr.name) |
56 |
| - value = chr(instr.value) |
57 |
| - buf.append(name) |
58 |
| - buf.append(value) |
59 |
| - return buf |
| 9 | + # Converts a string to and Instruction |
| 10 | + def line_to_instruction(self,s): |
| 11 | + |
| 12 | + # Conversion |
| 13 | + |
| 14 | + # remove endline |
| 15 | + words = s.replace("\n","") |
| 16 | + |
| 17 | + # shave off comments |
| 18 | + if ";" in words: |
| 19 | + words = words.split(";")[0] |
| 20 | + |
| 21 | + words = words.split(" ") |
| 22 | + |
| 23 | + # ignore lines with less than 2 words |
| 24 | + if len(words)<2: |
| 25 | + return None |
| 26 | + |
| 27 | + str_name = words[0].lower() |
| 28 | + str_value = words[1] |
| 29 | + num_name = str_to_name(str_name) |
| 30 | + num_value = str_to_value(str_value) |
| 31 | + |
| 32 | + # Error Checking |
| 33 | + if num_name is None: |
| 34 | + error("Invalid Instruction " + str_name) |
| 35 | + return None |
| 36 | + |
| 37 | + if num_value is None: |
| 38 | + error("Invalid Value " + str_value) |
| 39 | + return None |
| 40 | + |
| 41 | + return Instruction(num_name,num_value) |
| 42 | + |
| 43 | + def split_comma(self,lines): |
| 44 | + result = [] |
| 45 | + for line in lines: |
| 46 | + if ',' in line: |
| 47 | + split_line = line.split(",") |
| 48 | + for s in split_line: |
| 49 | + result.append(s) |
| 50 | + else: |
| 51 | + result.append(line) |
| 52 | + return result |
| 53 | + |
| 54 | + def remove_trailing_spaces(self,lines): |
| 55 | + result = [] |
| 56 | + for line in lines: |
| 57 | + while line[0]==" ": |
| 58 | + line = line[1:] |
| 59 | + result.append(line) |
| 60 | + return result |
| 61 | + |
| 62 | + def clean_lines(self, lines): |
| 63 | + result = [] |
| 64 | + for line in lines: |
| 65 | + if line[0]==";": |
| 66 | + continue |
| 67 | + |
| 68 | + line = line.replace("\n","") |
| 69 | + line = line.replace(" ","") |
| 70 | + |
| 71 | + if len(line)>1: |
| 72 | + result.append(line) |
| 73 | + return result |
| 74 | + |
| 75 | + # Converts a list of strings |
| 76 | + # to a list of Instructions |
| 77 | + def lines_to_assembly(self,lines): |
| 78 | + instr_list = [] |
| 79 | + print(lines) |
| 80 | + lines = self.split_comma(lines) |
| 81 | + lines = self.remove_trailing_spaces(lines) |
| 82 | + lines = self.clean_lines(lines) |
| 83 | + print(lines) |
| 84 | + for line in lines: |
| 85 | + instr = self.line_to_instruction(line) |
| 86 | + if instr is not None: |
| 87 | + instr_list.append(instr) |
| 88 | + |
| 89 | + return instr_list |
| 90 | + |
| 91 | + # Converts a list of instructions |
| 92 | + # to a list of bytes. |
| 93 | + def assembly_to_buffer(self,assembly): |
| 94 | + buf = [] |
| 95 | + for instr in assembly: |
| 96 | + name = chr(instr.name) |
| 97 | + value = chr(instr.value) |
| 98 | + buf.append(name) |
| 99 | + buf.append(value) |
| 100 | + return buf |
60 | 101 |
|
0 commit comments