Mercurial > lcfOS
comparison python/ppci/layout.py @ 386:2a970e7270e2
Added repeat assembler macro
author | Windel Bouwman |
---|---|
date | Thu, 01 May 2014 17:40:59 +0200 |
parents | d056b552d3f4 |
children |
comparison
equal
deleted
inserted
replaced
385:d056b552d3f4 | 386:2a970e7270e2 |
---|---|
54 | 54 |
55 def __repr__(self): | 55 def __repr__(self): |
56 return 'Align({})'.format(self.alignment) | 56 return 'Align({})'.format(self.alignment) |
57 | 57 |
58 | 58 |
59 class SymbolDefinition(Input): | |
60 def __init__(self, symbol_name): | |
61 self.symbol_name = symbol_name | |
62 | |
63 def __repr__(self): | |
64 return 'Symbol define: {}'.format(self.symbol_name) | |
65 | |
66 | |
59 class LayoutLexer(BaseLexer): | 67 class LayoutLexer(BaseLexer): |
60 def __init__(self): | 68 def __init__(self): |
61 tok_spec = [ | 69 tok_spec = [ |
62 ('HEXNUMBER', r'0x[\da-fA-F]+', self.handle_number), | 70 ('HEXNUMBER', r'0x[\da-fA-F]+', self.handle_number), |
63 ('NUMBER', r'\d+', self.handle_number), | 71 ('NUMBER', r'\d+', self.handle_number), |
65 ('SKIP', r'[ \t\r\n]', None), | 73 ('SKIP', r'[ \t\r\n]', None), |
66 ('LEESTEKEN', r':=|[\.,=:\-+*\[\]/\(\)]|>=|<=|<>|>|<|}|{', lambda typ, val: (val, val)), | 74 ('LEESTEKEN', r':=|[\.,=:\-+*\[\]/\(\)]|>=|<=|<>|>|<|}|{', lambda typ, val: (val, val)), |
67 ('STRING', r"'.*?'", lambda typ, val: (typ, val[1:-1])), | 75 ('STRING', r"'.*?'", lambda typ, val: (typ, val[1:-1])), |
68 ] | 76 ] |
69 super().__init__(tok_spec) | 77 super().__init__(tok_spec) |
70 self.kws = ['MEMORY', 'ALIGN', 'LOCATION','SECTION','SIZE'] | 78 self.kws = ['MEMORY', 'ALIGN', 'LOCATION','SECTION','SIZE', 'DEFINESYMBOL'] |
71 | 79 |
72 def handle_id(self, typ, val): | 80 def handle_id(self, typ, val): |
73 if val in self.kws: | 81 if val in self.kws: |
74 typ = val | 82 typ = val |
75 return typ, val | 83 return typ, val |
88 g.add_one_or_more('mem', 'mem_list') | 96 g.add_one_or_more('mem', 'mem_list') |
89 g.add_production('mem', ['MEMORY', 'ID', 'LOCATION', '=', 'NUMBER', 'SIZE', '=', 'NUMBER', '{', 'input_list', '}'], self.handle_mem) | 97 g.add_production('mem', ['MEMORY', 'ID', 'LOCATION', '=', 'NUMBER', 'SIZE', '=', 'NUMBER', '{', 'input_list', '}'], self.handle_mem) |
90 g.add_one_or_more('input', 'input_list') | 98 g.add_one_or_more('input', 'input_list') |
91 g.add_production('input', ['ALIGN', '(', 'NUMBER', ')'], self.handle_align) | 99 g.add_production('input', ['ALIGN', '(', 'NUMBER', ')'], self.handle_align) |
92 g.add_production('input', ['SECTION', '(', 'ID', ')'], self.handle_section) | 100 g.add_production('input', ['SECTION', '(', 'ID', ')'], self.handle_section) |
101 g.add_production('input', ['DEFINESYMBOL', '(', 'ID', ')'], self.handle_defsym) | |
93 | 102 |
94 g.start_symbol = 'layout' | 103 g.start_symbol = 'layout' |
95 self.p = g.generate_parser() | 104 self.p = g.generate_parser() |
96 | 105 |
97 def parse(self, lexer, layout): | 106 def parse(self, lexer, layout): |
110 return Align(alignment.val) | 119 return Align(alignment.val) |
111 | 120 |
112 def handle_section(self, section_tag, lbrace, section_name, rbrace): | 121 def handle_section(self, section_tag, lbrace, section_name, rbrace): |
113 return Section(section_name.val) | 122 return Section(section_name.val) |
114 | 123 |
124 def handle_defsym(self, section_tag, lbrace, name, rbrace): | |
125 return SymbolDefinition(name.val) | |
126 | |
115 | 127 |
116 class LayoutLoader: | 128 class LayoutLoader: |
117 def __init__(self): | 129 def __init__(self): |
118 self.lexer = LayoutLexer() | 130 self.lexer = LayoutLexer() |
119 self.parser = LayoutParser(self.lexer.kws) | 131 self.parser = LayoutParser(self.lexer.kws) |