def test_check_scoping(): st = SymbolTable() for val in xrange(0, 10): st.enter_scope() st.add('a', val) for val in xrange(9, -1, -1): assert st.find('a') == val st.leave_scope()
def test_simple(): st = SymbolTable() val = 1 st.add('a', val) eq_(st.find('a'), val) st.enter_scope() eq_(st.find('a'), val) st.leave_scope() eq_(st.find('a'), val)
def test_check_all_functions(): st = SymbolTable() foo = [1,2,3] bar = 'hi' st.add('a', foo) st.add('b', bar) eq_(st.find('a'), foo) eq_(st.find('b'), bar) st.remove('a') assert not st.find('a') assert not st.check_scope('a') eq_(st.check_scope('b'), bar) st.add('c', foo) with st.in_scope() as st: assert st.find('c') == foo assert st.find('b') == bar assert not st.check_scope('c') assert not st.check_scope('b') st.add('b', foo) assert st.find('b') == foo eq_(st.find('b'), bar)
def eval(self, st): func_type, func_args, func_body = st.get(self.value) if len(func_args) != len(self.children): raise ValueError('Unmatching sizes between defined function args' + f' and calling args ({len(func_args)} -- ' + f'{len(self.children)})') inner_st = SymbolTable(father=st) if func_type != const.VOID: inner_st.add(func_type, const.RETURN) for key, value in zip(func_args, self.children): key.eval(inner_st) # adds <key> to inner_st arg_type, arg_name = key.children # evals value with outter st inner_st.set(arg_name[0], value.eval(st)) ret_val = func_body.eval(inner_st) if func_type != const.VOID and ret_val is None: raise ValueError('Non void function is not returning a value') elif func_type == const.VOID: return None elif func_type != ret_val.type_: raise ValueError('Miss-matched type between function and return') return ret_val.value