comparison python/libasm.py @ 194:b01429a5d695

Fixed test
author Windel Bouwman
date Wed, 29 May 2013 22:36:37 +0200
parents f091e7d70996
children 37ac6c016e0f
comparison
equal deleted inserted replaced
193:f091e7d70996 194:b01429a5d695
77 @property 77 @property
78 def Peak(self): 78 def Peak(self):
79 return self.curTok 79 return self.curTok
80 80
81 class Assembler: 81 class Assembler:
82 def handle_ins(self, id0):
83 self.ins = id0
84 def p_label(self, lname, cn):
85 self.label = lname
86
82 def __init__(self): 87 def __init__(self):
83 # Construct a parser given a grammar: 88 # Construct a parser given a grammar:
84 g = pyyacc.Grammar(['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', pyyacc.EPS]) 89 g = pyyacc.Grammar(['ID', 'NUMBER', ',', '[', ']', ':', '+', '-', pyyacc.EPS])
85 90
86 g.add_production('asmline', ['label', 'instruction', 'operands']) 91 g.add_production('asmline', ['label', 'instruction', 'operands'])
87 g.add_production('label', ['ID', ':']) 92 g.add_production('asmline', ['instruction', 'operands'])
88 g.add_production('label', [pyyacc.EPS]) # label is optional 93 g.add_production('label', ['ID', ':'], self.p_label)
89 g.add_production('instruction', ['ID']) 94 g.add_production('instruction', ['ID'], self.handle_ins)
90 g.add_production('operands', ['operand']) 95 g.add_production('operands', ['operand'])
91 g.add_production('operands', ['operands', ',', 'operand']) 96 g.add_production('operands', ['operands', ',', 'operand'])
92 g.add_production('operand', ['expression']) 97 g.add_production('operand', ['expression'])
93 g.add_production('operand', ['[', 'expression', ']']) 98 g.add_production('operand', ['[', 'expression', ']'])
94 g.add_production('expression', ['term']) 99 g.add_production('expression', ['term'])
100 g.add_production('factor', ['NUMBER']) 105 g.add_production('factor', ['NUMBER'])
101 # TODO: expand grammar 106 # TODO: expand grammar
102 g.start_symbol = 'asmline' 107 g.start_symbol = 'asmline'
103 108
104 self.p = g.genParser() 109 self.p = g.genParser()
110 def parse_line(self, line):
111 """ Parse line into asm AST """
112 tokens = tokenize(line)
113 self.p.parse(tokens)
114 aast = 1 # TODO
115 return aast
105 116
106 def assemble(self, asmsrc): 117 def assemble(self, asmsrc):
107 lxr = Lexer(asmsrc) 118 lxr = Lexer(asmsrc)
108 prsr = Parser(lxr) 119 prsr = Parser(lxr)
109 instructions = prsr.parse() 120 instructions = prsr.parse()
110 return instructions 121 return instructions
111 122
112 def assembleLine(self, line): 123 def assembleLine(self, line):
113 """ 124 """
114 Assemble a single source line. 125 Assemble a single source line.
115 Do not take newlines into account 126 Do not take newlines into account
116 """ 127 """
117 tokens = tokenize(line) 128 aast = self.parseLine(line)
118 self.p.parse(tokens) 129 self.assemble_aast(aast)
119 130
120 def assembleAst(self, at): 131 def assemble_aast(self, at):
121 """ Assemble a parsed asm line """ 132 """ Assemble a parsed asm line """
122 pass 133 pass
123 134
124 135