Skip to content

Commit

Permalink
Fixes #39 - object property symbols imported into namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Daniels committed Sep 22, 2015
1 parent a8d81a6 commit 33cad02
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 30 deletions.
4 changes: 2 additions & 2 deletions cacti/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def process_exports(self, module, stack_frame):
symbol_holders = module.private_table.symbol_holder_iter()
to_add = filter(lambda sh: sh[0] in exports, symbol_holders)
for s, h in to_add:
module.public_table.add_symbol(s, ConstantWrapperValueHolder(h))
module.export_table.add_symbol(s, ConstantWrapperValueHolder(h))

def __repr__(self):
return "{}('{}', {})".format(
Expand Down Expand Up @@ -225,7 +225,7 @@ def eval(self):
module_alias = ModuleAlias(module, *self.__only)
table.add_symbol(self.__alias, ConstantValueHolder(module_alias))
else:
symbol_holders = module.public_table.symbol_holder_iter()
symbol_holders = module.export_table.symbol_holder_iter()
if self.__only:
symbol_holders = filter(lambda e: e[0] in self.__only, symbol_holders)
for s, h in symbol_holders:
Expand Down
14 changes: 9 additions & 5 deletions cacti/lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,13 @@ def __init__(self, name, *, parent=None):
super().__init__(type_type, name, typeobj=module_type)

self.__parent = parent

self.__export_table = SymbolTable()
self.__private_table = SymbolTable()


@property
def export_table(self):
return self.__export_table

@property
def private_table(self):
return self.__private_table
Expand All @@ -223,10 +227,10 @@ def set_parent(self, parent):
self.__parent = parent

def __getitem__(self, symbol_name):
return self.public_table[symbol_name]
return self.export_table[symbol_name]

def __setitem__(self, symbol_name, symbol_value):
self.public_table[symbol_name] = symbol_value
self.export_table[symbol_name] = symbol_value

class ModuleAlias(TypeDefinition):
def __init__(self, module, *only):
Expand All @@ -239,7 +243,7 @@ def __init__(self, module, *only):
module_alias_type = get_type('ModuleAlias')
super().__init__(type_type, None, typeobj=module_alias_type)

symbol_holders = module.public_table.symbol_holder_iter()
symbol_holders = module.export_table.symbol_holder_iter()

if only:
symbol_holders = filter(lambda x: x[0] in only, symbol_holders)
Expand Down
2 changes: 1 addition & 1 deletion examples/import.cacti
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ print(F)
var c = abc.Something()
print(c.type)

print(name)
print(c)
24 changes: 12 additions & 12 deletions test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_process_exports_copies_exports(self):
c_cw = ('c', ConstantWrapperValueHolder(holder_c))
d_cw = ('d', ConstantWrapperValueHolder(holder_d))
cws = [a_cw, b_cw, c_cw, d_cw]
module_cws = list(module.public_table.symbol_holder_iter())
module_cws = list(module.export_table.symbol_holder_iter())
actual = list(map(lambda e: e in module_cws, cws))
assert [True, True, True, False] == actual

Expand All @@ -42,7 +42,7 @@ def test_exports_are_constant(self):
module_dec = ModuleDeclaration('test.module')
module_dec.process_exports(module, stack_frame)
with pytest.raises(ConstantValueError):
module.public_table['a'] = 5
module.export_table['a'] = 5

def test_exports_reflect_change_to_original(self):
stack_frame = StackFrame(make_object(), 'test.module')
Expand All @@ -53,9 +53,9 @@ def test_exports_reflect_change_to_original(self):
module.private_table.add_symbol('a', holder_a)
module_dec = ModuleDeclaration('test.module')
module_dec.process_exports(module, stack_frame)
value_before = module.public_table['a']
value_before = module.export_table['a']
module.private_table['a'] = 5
value_after = module.public_table['a']
value_after = module.export_table['a']
assert [value_before, value_after] == [1, 5]

def test_process_statement_results_copies_stack_frame_symbols(self):
Expand Down Expand Up @@ -148,10 +148,10 @@ def test_imports_all(self):
holder_c = ValueHolder(3)
holder_d = ValueHolder(4)
m = Module('test')
m.public_table.add_symbol('a', holder_a)
m.public_table.add_symbol('b', holder_b)
m.public_table.add_symbol('c', holder_c)
m.public_table.add_symbol('d', holder_d)
m.export_table.add_symbol('a', holder_a)
m.export_table.add_symbol('b', holder_b)
m.export_table.add_symbol('c', holder_c)
m.export_table.add_symbol('d', holder_d)
add_module(m)
impt = ImportStatement('test')
impt()
Expand All @@ -171,10 +171,10 @@ def test_imports_only(self):
holder_c = ValueHolder(3)
holder_d = ValueHolder(4)
m = Module('test')
m.public_table.add_symbol('a', holder_a)
m.public_table.add_symbol('b', holder_b)
m.public_table.add_symbol('c', holder_c)
m.public_table.add_symbol('d', holder_d)
m.export_table.add_symbol('a', holder_a)
m.export_table.add_symbol('b', holder_b)
m.export_table.add_symbol('c', holder_c)
m.export_table.add_symbol('d', holder_d)
add_module(m)
impt = ImportStatement('test', only=['a', 'c'])
impt()
Expand Down
26 changes: 16 additions & 10 deletions test/test_lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@ def test_parent_from_setter(self):
mc.set_parent(mp)
assert mc.parent is mp

def test_index_values_from_public_table(self):
def test_index_values_from_export_table(self):
m = Module('test')
m.private_table.add_symbol('a', ValueHolder(5))
m.public_table.add_symbol('a', ValueHolder(6))
m.export_table.add_symbol('a', ValueHolder(6))
assert [6, 5] == [m['a'], m.private_table['a']]

@pytest.mark.usefixtures('set_up_env')
Expand All @@ -96,10 +96,10 @@ def test_index_values_from_public_table(self):

def test_has_only_symbols_from_only_parameter(self):
m = Module('test')
m.public_table.add_symbol('a', ValueHolder(1))
m.public_table.add_symbol('b', ValueHolder(2))
m.public_table.add_symbol('c', ValueHolder(3))
m.public_table.add_symbol('d', ValueHolder(4))
m.export_table.add_symbol('a', ValueHolder(1))
m.export_table.add_symbol('b', ValueHolder(2))
m.export_table.add_symbol('c', ValueHolder(3))
m.export_table.add_symbol('d', ValueHolder(4))
ma = ModuleAlias(m, 'a', 'c')
symbols = ['a', 'b', 'c', 'd']
expected = [True, False, True, False]
Expand All @@ -108,16 +108,22 @@ def test_has_only_symbols_from_only_parameter(self):

def test_has_all_symbols_when_only_parameter_empty(self):
m = Module('test')
m.public_table.add_symbol('a', ValueHolder(1))
m.public_table.add_symbol('b', ValueHolder(2))
m.public_table.add_symbol('c', ValueHolder(3))
m.public_table.add_symbol('d', ValueHolder(4))
m.export_table.add_symbol('a', ValueHolder(1))
m.export_table.add_symbol('b', ValueHolder(2))
m.export_table.add_symbol('c', ValueHolder(3))
m.export_table.add_symbol('d', ValueHolder(4))
ma = ModuleAlias(m)
symbols = ['a', 'b', 'c', 'd']
expected = [True, True, True, True]
actual = list(map(lambda x: x in ma.public_table, symbols))
assert expected == actual

def test_index_from_public_table(self):
m = Module('test')
m.export_table.add_symbol('a', ValueHolder(1))
ma = ModuleAlias(m, 'a')
assert 1 == ma['a']

@pytest.mark.usefixtures('set_up_env')
class TestClassDefinition:
def dmy(self): pass
Expand Down

0 comments on commit 33cad02

Please sign in to comment.