Skip to content

Commit

Permalink
Added support for Atomese
Browse files Browse the repository at this point in the history
  • Loading branch information
Andre Senna committed Oct 11, 2022
1 parent 2976270 commit 88f8b02
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 25 deletions.
75 changes: 59 additions & 16 deletions das/atomese_yacc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from typing import List, Any, Optional
import ply.yacc as yacc
from das.atomese_lex import AtomeseLex
from das.metta_lex import BASIC_TYPE
from das.exceptions import AtomeseSyntaxError, UndefinedSymbolError
from das.expression_hasher import ExpressionHasher
from das.expression import Expression
Expand All @@ -35,54 +36,92 @@ def p_START(self, p):
| EOF
|"""
p[0] = 'SUCCESS'
print(f"p_START: {p[0]}")

def p_LIST_OF_TOP_LEVEL_ATOMS_base(self, p):
"""LIST_OF_TOP_LEVEL_ATOMS : TOP_LEVEL_ATOM"""
#if self.check_mode or not self.action_broker:
# return
p[0] = [p[1]]
print(f"p_LIST_OF_TOP_LEVEL_ATOMS_base: {p[0]}")
if self.check_mode or not self.action_broker:
return

def p_LIST_OF_TOP_LEVEL_ATOMS_recursion(self, p):
"""LIST_OF_TOP_LEVEL_ATOMS : LIST_OF_TOP_LEVEL_ATOMS TOP_LEVEL_ATOM"""
p[0] = [*p[1], p[2]]
print(f"p_LIST_OF_TOP_LEVEL_ATOMS_recursion: {p[0]}")

def p_TOP_LEVEL_ATOM(self, p):
"""TOP_LEVEL_ATOM : ATOM"""
atom = p[1]
p[0] = p[1]
print(f"p_TOP_LEVEL_ATOM: {p[0]}")
if self.check_mode or not self.action_broker:
return
if atom.elements is not None:
atom.toplevel = True
self.action_broker.new_top_level_expression(atom)

def p_ATOM_node(self, p):
"""ATOM : NODE"""
p[0] = p[1]
print(f"p_ATOM_node: {p[0]}")

def p_ATOM_link(self, p):
"""ATOM : LINK"""
p[0] = p[1]
print(f"p_ATOM_link: {p[0]}")

def p_NODE(self, p):
"""NODE : ATOM_OPENNING ATOM_TYPE NODE_NAME ATOM_CLOSING"""
p[0] = f"<{p[2]}: {p[3]}>"
print(f"p_NODE: {p[0]}")
if self.check_mode or not self.action_broker:
p[0] = f"<{p[2]}: {p[3]}>"
return
node_type = p[2]
node_name = p[3]
if node_type not in self.types:
self.types.add(node_type)
expression = self._typedef(node_type, BASIC_TYPE)
expression.toplevel = True
self.action_broker.new_top_level_typedef_expression(expression)
terminal_name = f"{node_type}:{node_name}"
if terminal_name not in self.nodes:
self.nodes.add(terminal_name)
expression = self._typedef(terminal_name, node_type)
expression.toplevel = True
self.action_broker.new_top_level_typedef_expression(expression)
expression = self._new_terminal(terminal_name)
self.action_broker.new_terminal(expression)
else:
expression = self._new_terminal(terminal_name)
p[0] = expression

def p_LINK(self, p):
"""LINK : ATOM_OPENNING ATOM_TYPE ATOM_LIST ATOM_CLOSING"""
p[0] = f"({p[2]} {p[3]})"
print(f"p_LINK: {p[0]}")
link_type = p[2]
targets = p[3]
if self.check_mode or not self.action_broker:
p[0] = f"<{p[2]}: {p[3]}>"
return
if link_type not in self.types:
self.types.add(link_type)
expression = self._typedef(link_type, BASIC_TYPE)
expression.toplevel = True
self.action_broker.new_top_level_typedef_expression(expression)
head_expression = self._new_symbol(link_type)
expression = self._nested_expression([head_expression, *targets])
p[0] = expression

def p_ATOM_LIST_base(self, p):
"""ATOM_LIST : ATOM"""
p[0] = [p[1]]
print(f"p_ATOM_LIST_base: {p[0]}")
atom = p[1]
p[0] = [atom]
if self.check_mode or not self.action_broker:
return
if atom.elements is not None:
self.action_broker.new_expression(atom)

def p_ATOM_LIST_recursion(self, p):
"""ATOM_LIST : ATOM_LIST ATOM"""
p[0] = [*p[1], p[2]]
print(f"p_ATOM_LIST_recursion: {p[0]}")
atom = p[2]
p[0] = [*p[1], atom]
if self.check_mode or not self.action_broker:
return
if atom.elements is not None:
self.action_broker.new_expression(atom)

def p_error(self, p):
error = f"Syntax error in line {self.lexer.lineno} " + \
Expand All @@ -96,3 +135,7 @@ def __init__(self, **kwargs):
self.lex_wrap = AtomeseLex()
super().setup()
self.parser = yacc.yacc(module=self)
self.types = set()
self.nodes = set()
named_type_hash = self._get_named_type_hash(BASIC_TYPE)
self.parent_type[named_type_hash] = named_type_hash
16 changes: 11 additions & 5 deletions das/atomese_yacc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,31 @@ def test_parser():
result = yacc_wrap.check(test_data)
assert result == "SUCCESS"

def _action_broker():
def test_action_broker():

action_broker = ActionBroker(test_data)
yacc_wrap = AtomeseYacc(action_broker=action_broker)
result = yacc_wrap.check(test_data)
assert result == "SUCCESS"
assert action_broker.count_terminal == 0
assert action_broker.count_nested_expression == 0
assert action_broker.count_toplevel_expression == 0
assert action_broker.count_type == 0

action_broker = ActionBroker()
yacc_wrap = AtomeseYacc(action_broker=action_broker)
result = yacc_wrap.parse(test_data)
assert result == "SUCCESS"
assert action_broker.count_toplevel_expression == 1
assert action_broker.count_type == 8
assert action_broker.count_terminal == 9
assert action_broker.count_nested_expression == 7
assert action_broker.count_toplevel_expression == 3
assert action_broker.count_type == 8 + action_broker.count_terminal

action_broker = ActionBroker(test_data)
yacc_wrap = AtomeseYacc(action_broker=action_broker)
result = yacc_wrap.parse_action_broker_input()
assert result == "SUCCESS"
assert action_broker.count_toplevel_expression == 1
assert action_broker.count_type == 8
assert action_broker.count_terminal == 9
assert action_broker.count_nested_expression == 7
assert action_broker.count_toplevel_expression == 3
assert action_broker.count_type == 8 + action_broker.count_terminal
1 change: 0 additions & 1 deletion das/metta_yacc.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ def p_TOP_LEVEL_EXPRESSION_DEFINITION(self, p):
sub_expressions = p[2]
expression = self._nested_expression(sub_expressions)
expression.toplevel = True
self.action_broker.update_line_number(self.lexer.lineno)
self.action_broker.new_top_level_expression(expression)
p[0] = expression

Expand Down
3 changes: 0 additions & 3 deletions das/parser_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ def new_top_level_typedef_expression(self, expression: Expression):
def __init__(self):
self.current_line_number = 1

def update_line_number(self, current_line_number: int):
self.current_line_number = current_line_number

class KnowledgeBaseFile(ParserActions):

def __init__(self, db: DBInterface, file_path: str, shared_data: SharedData):
Expand Down
2 changes: 2 additions & 0 deletions pytest
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
docker-compose exec app pytest das/metta_lex_test.py
docker-compose exec app pytest das/metta_yacc_test.py
docker-compose exec app pytest das/atomese_lex_test.py
docker-compose exec app pytest das/atomese_yacc_test.py
docker-compose exec app pytest das/distributed_atom_space_test.py
./empty-docker-up
docker-compose exec app python3 scripts/load_das.py
Expand Down

0 comments on commit 88f8b02

Please sign in to comment.