def test_process_let(self): generator = VMGenerator() generator.symbol_table.define('x', 'int', KIND_VAR) node = Node('letStatement', None, [ Node('keyword', 'let'), Node('identifier', 'x'), Node('symbol', '='), Node('expression', None, [ Node('term', None, [Node('integerConstant', '1')], 'single'), Node('symbol', '+'), Node('term', None, [Node('integerConstant', '2')], 'single'), Node('symbol', '*'), Node('term', None, [Node('identifier', 'x')], 'single') ]), Node('symbol', ';') ]) code = generator.process_let(node) self.assertEqual(code, [ 'push constant 1', 'push constant 2', 'add', 'push local 0', 'call Math.multiply 2', 'pop local 0' ])
def test_process_while(self): generator = VMGenerator() generator.current_class = 'Test' generator.symbol_table.define('x', 'int', KIND_FIELD) condition = Node('expression', None, [ Node('term', None, [Node('identifier', 'x')], 'single'), Node('symbol', '='), Node('term', None, [Node('integerConstant', '1')], 'single') ]) loop_body = Node('statements', None, [ Node('doStatement', None, [ Node('keyword', 'do'), Node('identifier', 'Memory'), Node('symbol', '.'), Node('identifier', 'test'), Node('symbol', '('), Node('expressionList', None, [ Node('expression', None, [ Node('term', None, [Node('integerConstant', '2')], 'single') ]) ], {'cnt': 1}), Node('symbol', ')'), Node('symbol', ';') ]) ]) node = Node('ifStatement', None, [ Node('keyword', 'while'), Node('symbol', '('), condition, Node('symbol', ')'), Node('symbol', '{'), loop_body, Node('symbol', '}') ]) code = generator.process_while(node) self.assertEqual(code, [ 'label Test1', 'push this 0', 'push constant 1', 'eq', 'not', 'if-goto Test2', 'push constant 2', 'call Memory.test 1', 'pop temp 0', 'goto Test1', 'label Test2' ])