Skip to content

Commit

Permalink
Minor refactory
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre Senna committed Sep 7, 2022
1 parent 63e5830 commit b9ec438
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 87 deletions.
8 changes: 4 additions & 4 deletions das/expression_hasher.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ def named_type_hash(name: str) -> str:
return ExpressionHasher._compute_hash(name)

@staticmethod
def atom_hash(named_type: str, atom_name: str) -> str:
def terminal_hash(named_type: str, terminal_name: str) -> str:
return ExpressionHasher._compute_hash(
ExpressionHasher.compound_separator.join([named_type, atom_name]))
ExpressionHasher.compound_separator.join([named_type, terminal_name]))

@staticmethod
def expression_hash(named_type_hash: str, elements: List[str]) -> str:
Expand Down Expand Up @@ -46,8 +46,8 @@ def named_type_hash(name: str) -> str:
return f"<Type: {name}>"

@staticmethod
def atom_hash(named_type: str, atom_name: str) -> str:
return f"<{named_type}: {atom_name}>"
def terminal_hash(named_type: str, terminal_name: str) -> str:
return f"<{named_type}: {terminal_name}>"

@staticmethod
def expression_hash(named_type_hash: str, elements: List[str]) -> str:
Expand Down
14 changes: 7 additions & 7 deletions das/metta_lex_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,32 +60,32 @@ def test_lexer():

"EXPRESSION_OPENNING",
"TYPE_DEFINITION_MARK",
"ATOM_NAME",
"TERMINAL_NAME",
"EXPRESSION_NAME",
"EXPRESSION_CLOSING",

"EXPRESSION_OPENNING",
"TYPE_DEFINITION_MARK",
"ATOM_NAME",
"TERMINAL_NAME",
"EXPRESSION_NAME",
"EXPRESSION_CLOSING",

"EXPRESSION_OPENNING",
"TYPE_DEFINITION_MARK",
"ATOM_NAME",
"TERMINAL_NAME",
"EXPRESSION_NAME",
"EXPRESSION_CLOSING",

"EXPRESSION_OPENNING",
"EXPRESSION_NAME",
"ATOM_NAME",
"TERMINAL_NAME",
"EXPRESSION_OPENNING",
"EXPRESSION_NAME",
"ATOM_NAME",
"TERMINAL_NAME",
"EXPRESSION_OPENNING",
"EXPRESSION_NAME",
"ATOM_NAME",
"ATOM_NAME",
"TERMINAL_NAME",
"TERMINAL_NAME",
"EXPRESSION_CLOSING",
"EXPRESSION_CLOSING",
"EXPRESSION_CLOSING",
Expand Down
58 changes: 29 additions & 29 deletions das/metta_yacc.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
EXPRESSION -> EXPRESSION_OPENNING LIST_OF_EXPRESSIONS EXPRESSION_CLOSING
| EXPRESSION_OPENNING TYPE_DEFINITION_MARK EXPRESSION_NAME TYPE_DESIGNATOR EXPRESSION_CLOSING
| EXPRESSION_NAME
| ATOM_NAME
| TERMINAL_NAME
"""

from dataclasses import dataclass
Expand All @@ -35,7 +35,7 @@ class Expression:
#AQUI : TODO: Implement non-ordered
toplevel: bool = False
ordered: bool = True
atom_name: Optional[str] = None
terminal_name: Optional[str] = None
named_type: Optional[str] = None
named_type_hash: Optional[str] = None
composite_type: Optional[List[Any]] = None
Expand All @@ -52,7 +52,7 @@ def p_START(self, p):
| EOF"""
self._revisit_pending_symbols()
missing_symbols = []
missing_symbols.extend([name for (name, expression) in self.pending_atom_names])
missing_symbols.extend([name for (name, expression) in self.pending_terminal_names])
missing_symbols.extend([name for (name, expression) in self.pending_expression_names])
missing_symbols.extend([type_designator for ((name, type_designator), expression) in self.pending_named_types])
if missing_symbols:
Expand Down Expand Up @@ -91,7 +91,7 @@ def p_TOP_LEVEL_EXPRESSION_expression(self, p):

def p_TOP_LEVEL_TYPE_DEFINITION(self, p):
"""TOP_LEVEL_TYPE_DEFINITION : EXPRESSION_OPENNING TYPE_DEFINITION_MARK EXPRESSION_NAME TYPE_DESIGNATOR EXPRESSION_CLOSING
| EXPRESSION_OPENNING TYPE_DEFINITION_MARK ATOM_NAME TYPE_DESIGNATOR EXPRESSION_CLOSING"""
| EXPRESSION_OPENNING TYPE_DEFINITION_MARK TERMINAL_NAME TYPE_DESIGNATOR EXPRESSION_CLOSING"""
if self.check_mode or not self.action_broker:
return
assert self.typedef_mark == p[2]
Expand Down Expand Up @@ -148,7 +148,7 @@ def p_EXPRESSION_sequence(self, p):

def p_EXPRESSION_type(self, p):
"""EXPRESSION : EXPRESSION_OPENNING TYPE_DEFINITION_MARK EXPRESSION_NAME TYPE_DESIGNATOR EXPRESSION_CLOSING
| EXPRESSION_OPENNING TYPE_DEFINITION_MARK ATOM_NAME TYPE_DESIGNATOR EXPRESSION_CLOSING"""
| EXPRESSION_OPENNING TYPE_DEFINITION_MARK TERMINAL_NAME TYPE_DESIGNATOR EXPRESSION_CLOSING"""
if self.check_mode or not self.action_broker:
return
assert self.typedef_mark == p[2]
Expand All @@ -166,12 +166,12 @@ def p_EXPRESSION_symbol(self, p):
expression = self._new_symbol(expression_name)
p[0] = expression

def p_EXPRESSION_atom(self, p):
"""EXPRESSION : ATOM_NAME"""
def p_EXPRESSION_terminal(self, p):
"""EXPRESSION : TERMINAL_NAME"""
if self.check_mode or not self.action_broker:
return
atom_name = p[1]
expression = self._new_atom(atom_name)
terminal_name = p[1]
expression = self._new_terminal(terminal_name)
p[0] = expression

def p_error(self, p):
Expand All @@ -190,23 +190,23 @@ def __init__(self, **kwargs):
self.parser = yacc.yacc(module=self, **kwargs)
self.check_mode = False
self.hasher = ExpressionHasher()
self.pending_atom_names = []
self.pending_terminal_names = []
self.pending_expression_names = []
self.pending_named_types = []
self.pending_expressions = []
self.named_types = {}
self.named_type_hash = {}
self.symbol_hash = {}
self.atom_hash = {}
self.terminal_hash = {}
self.parent_type = {}

def _get_atom_hash(self, named_type, atom_name):
key = (named_type, atom_name)
atom_hash = self.atom_hash.get(key, None)
if atom_hash is None:
atom_hash = self.hasher.atom_hash(*key)
self.atom_hash[key] = atom_hash
return atom_hash
def _get_terminal_hash(self, named_type, terminal_name):
key = (named_type, terminal_name)
terminal_hash = self.terminal_hash.get(key, None)
if terminal_hash is None:
terminal_hash = self.hasher.terminal_hash(*key)
self.terminal_hash[key] = terminal_hash
return terminal_hash

def _get_named_type_hash(self, named_type):
named_type_hash = self.named_type_hash.get(named_type, None)
Expand Down Expand Up @@ -261,19 +261,19 @@ def _typedef(self, name, type_designator, expression=None):
self.pending_named_types.append(((name, type_designator), expression))
return expression

def _new_atom(self, atom_name, expression=None):
def _new_terminal(self, terminal_name, expression=None):
if expression is None:
expression = Expression(atom_name=atom_name)
named_type = self.named_types.get(atom_name, None)
expression = Expression(terminal_name=terminal_name)
named_type = self.named_types.get(terminal_name, None)
if named_type:
named_type_hash = self._get_named_type_hash(named_type)
expression.named_type = named_type
expression.named_type_hash = named_type_hash
expression.composite_type = [named_type_hash]
expression.composite_type_hash = named_type_hash
expression.hash_code = self._get_atom_hash(named_type, atom_name)
expression.hash_code = self._get_terminal_hash(named_type, terminal_name)
else:
self.pending_atom_names.append((atom_name, expression))
self.pending_terminal_names.append((terminal_name, expression))
return expression

def _new_symbol(self, expression_name, expression=None):
Expand Down Expand Up @@ -301,11 +301,11 @@ def _revisit_pending_named_types(self):
dirty_flag = True
return dirty_flag

def _revisit_pending_atom_names(self):
pending = self.pending_atom_names
self.pending_atom_names = []
for (atom_name, expression) in pending:
modified_expression = self._new_atom(atom_name, expression)
def _revisit_pending_terminal_names(self):
pending = self.pending_terminal_names
self.pending_terminal_names = []
for (terminal_name, expression) in pending:
modified_expression = self._new_terminal(terminal_name, expression)

def _revisit_pending_expression_names(self):
pending = self.pending_expression_names
Expand All @@ -326,7 +326,7 @@ def _revisit_pending_expressions(self):
def _revisit_pending_symbols(self):
while self._revisit_pending_named_types():
pass
self._revisit_pending_atom_names()
self._revisit_pending_terminal_names()
self._revisit_pending_expression_names()
while self._revisit_pending_expressions():
pass
Expand Down
88 changes: 44 additions & 44 deletions das/metta_yacc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,47 +50,47 @@ def test_action_broker():
assert action_broker.count_expression == 1
assert action_broker.count_type == 8

def test_atom_hash():
def test_terminal_hash():

yacc_wrap = MettaYacc()
named_type1 = 'blah1'
atom_name1 = 'bleh1'
terminal_name1 = 'bleh1'
named_type2 = 'blah2'
atom_name2 = 'bleh2'

assert(len(yacc_wrap.atom_hash) == 0)

h = yacc_wrap._get_atom_hash(named_type1, atom_name1)
assert(len(yacc_wrap.atom_hash) == 1)
assert h == yacc_wrap._get_atom_hash(named_type1, atom_name1)
assert(len(yacc_wrap.atom_hash) == 1)

h = yacc_wrap._get_atom_hash(named_type2, atom_name2)
assert(len(yacc_wrap.atom_hash) == 2)
assert h == yacc_wrap._get_atom_hash(named_type2, atom_name2)
assert yacc_wrap._get_atom_hash(named_type1, atom_name1) \
!= yacc_wrap._get_atom_hash(named_type2, atom_name2)
assert(len(yacc_wrap.atom_hash) == 2)

h = yacc_wrap._get_atom_hash(named_type1, atom_name2)
assert(len(yacc_wrap.atom_hash) == 3)
assert h == yacc_wrap._get_atom_hash(named_type1, atom_name2)
assert yacc_wrap._get_atom_hash(named_type1, atom_name1) \
!= yacc_wrap._get_atom_hash(named_type1, atom_name2)
assert yacc_wrap._get_atom_hash(named_type2, atom_name2) \
!= yacc_wrap._get_atom_hash(named_type1, atom_name2)
assert(len(yacc_wrap.atom_hash) == 3)

h = yacc_wrap._get_atom_hash(named_type2, atom_name1)
assert(len(yacc_wrap.atom_hash) == 4)
assert h == yacc_wrap._get_atom_hash(named_type2, atom_name1)
assert yacc_wrap._get_atom_hash(named_type1, atom_name1) \
!= yacc_wrap._get_atom_hash(named_type2, atom_name1)
assert yacc_wrap._get_atom_hash(named_type2, atom_name2) \
!= yacc_wrap._get_atom_hash(named_type2, atom_name1)
assert yacc_wrap._get_atom_hash(named_type1, atom_name2) \
!= yacc_wrap._get_atom_hash(named_type2, atom_name1)
assert(len(yacc_wrap.atom_hash) == 4)
terminal_name2 = 'bleh2'

assert(len(yacc_wrap.terminal_hash) == 0)

h = yacc_wrap._get_terminal_hash(named_type1, terminal_name1)
assert(len(yacc_wrap.terminal_hash) == 1)
assert h == yacc_wrap._get_terminal_hash(named_type1, terminal_name1)
assert(len(yacc_wrap.terminal_hash) == 1)

h = yacc_wrap._get_terminal_hash(named_type2, terminal_name2)
assert(len(yacc_wrap.terminal_hash) == 2)
assert h == yacc_wrap._get_terminal_hash(named_type2, terminal_name2)
assert yacc_wrap._get_terminal_hash(named_type1, terminal_name1) \
!= yacc_wrap._get_terminal_hash(named_type2, terminal_name2)
assert(len(yacc_wrap.terminal_hash) == 2)

h = yacc_wrap._get_terminal_hash(named_type1, terminal_name2)
assert(len(yacc_wrap.terminal_hash) == 3)
assert h == yacc_wrap._get_terminal_hash(named_type1, terminal_name2)
assert yacc_wrap._get_terminal_hash(named_type1, terminal_name1) \
!= yacc_wrap._get_terminal_hash(named_type1, terminal_name2)
assert yacc_wrap._get_terminal_hash(named_type2, terminal_name2) \
!= yacc_wrap._get_terminal_hash(named_type1, terminal_name2)
assert(len(yacc_wrap.terminal_hash) == 3)

h = yacc_wrap._get_terminal_hash(named_type2, terminal_name1)
assert(len(yacc_wrap.terminal_hash) == 4)
assert h == yacc_wrap._get_terminal_hash(named_type2, terminal_name1)
assert yacc_wrap._get_terminal_hash(named_type1, terminal_name1) \
!= yacc_wrap._get_terminal_hash(named_type2, terminal_name1)
assert yacc_wrap._get_terminal_hash(named_type2, terminal_name2) \
!= yacc_wrap._get_terminal_hash(named_type2, terminal_name1)
assert yacc_wrap._get_terminal_hash(named_type1, terminal_name2) \
!= yacc_wrap._get_terminal_hash(named_type2, terminal_name1)
assert(len(yacc_wrap.terminal_hash) == 4)

def test_named_type_hash():

Expand Down Expand Up @@ -153,7 +153,7 @@ def test_nested_expression():

assert not composite1.toplevel
assert composite1.ordered
assert composite1.atom_name is None
assert composite1.terminal_name is None
assert composite1.named_type == 'Similarity'
assert composite1.named_type_hash == 'Similarity Hash'
assert composite1.composite_type == ['Typedef Similarity Type', 'Concept', 'Concept']
Expand All @@ -168,7 +168,7 @@ def test_nested_expression():
composite3 = yacc_wrap._nested_expression([expression1, composite1, composite2])
assert not composite3.toplevel
assert composite3.ordered
assert composite3.atom_name is None
assert composite3.terminal_name is None
assert composite3.named_type == 'Similarity'
assert composite3.named_type_hash == 'Similarity Hash'
assert composite3.composite_type == [
Expand All @@ -192,7 +192,7 @@ def test_typedef():
assert len(yacc_wrap.pending_named_types) == 1
assert not expression1.toplevel
assert expression1.ordered
assert expression1.atom_name is None
assert expression1.terminal_name is None
assert expression1.named_type is None
assert expression1.named_type_hash is None
assert expression1.composite_type is None
Expand All @@ -216,7 +216,7 @@ def test_typedef():
assert yacc_wrap.parent_type[h2] == h1
assert not expression2.toplevel
assert expression2.ordered
assert expression2.atom_name is None
assert expression2.terminal_name is None
assert expression2.named_type == ':'
assert expression2.named_type_hash is not None
assert expression2.composite_type == [h3, h1, h1]
Expand All @@ -233,7 +233,7 @@ def test_typedef():
assert yacc_wrap.parent_type[h4] == h1
assert not expression3.toplevel
assert expression3.ordered
assert expression3.atom_name is None
assert expression3.terminal_name is None
assert expression3.named_type == ':'
assert expression3.named_type_hash is not None
assert expression3.composite_type == [h3, h1, h1]
Expand Down Expand Up @@ -261,7 +261,7 @@ def test_typedef():
assert yacc_wrap.parent_type[h5] == h4
assert not expression5.toplevel
assert expression5.ordered
assert expression5.atom_name is None
assert expression5.terminal_name is None
assert expression5.named_type == ':'
assert expression5.named_type_hash is not None
assert expression5.composite_type == [h3, h4, h1]
Expand Down Expand Up @@ -367,7 +367,7 @@ def test_pending_types():
assert action_broker.count_expression == 1
assert action_broker.count_type == 9

def test_pending_atom_names():
def test_pending_terminal_names():

missing_type = """
(: Evaluation Type)
Expand Down
5 changes: 2 additions & 3 deletions das/my_metta_lex.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ def __init__(self, **kwargs):
'EXPRESSION_OPENNING',
'EXPRESSION_CLOSING',
'TYPE_DEFINITION_MARK',
'ATOM_NAME',
# AQUI TODO: change atom -> terminal
'TERMINAL_NAME',
'EXPRESSION_NAME',
'EOF',
] + list(self.reserved.values())
Expand All @@ -28,7 +27,7 @@ def __init__(self, **kwargs):
self.eof_handler = self.default_eof_handler
self.lexer.filename = ""

def t_ATOM_NAME(self, t):
def t_TERMINAL_NAME(self, t):
r'\"[^\"]+\"'
t.value = t.value[1:-1]
return t
Expand Down

0 comments on commit b9ec438

Please sign in to comment.