Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Issue 655 solution #658

Merged
merged 10 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions python/hyperon/stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ def match_(self, atom):



@register_atoms
def text_ops():
@register_atoms(pass_metta=True)
def text_ops(run_context):
"""Add text operators

repr: convert Atom to string.
Expand All @@ -108,7 +108,7 @@ def text_ops():

reprAtom = OperationAtom('repr', lambda a: [ValueAtom(repr(a))],
['Atom', 'String'], unwrap=False)
parseAtom = OperationAtom('parse', lambda s: [ValueAtom(SExprParser(str(s)[1:-1]).parse(Tokenizer()))],
parseAtom = OperationAtom('parse', lambda s: [SExprParser(str(s)[1:-1]).parse(run_context.tokenizer())],
['String', 'Atom'], unwrap=False)
stringToCharsAtom = OperationAtom('stringToChars', lambda s: [E(*[ValueAtom(Char(c)) for c in str(s)[1:-1]])],
['String', 'Atom'], unwrap=False)
Expand Down
17 changes: 15 additions & 2 deletions python/tests/test_stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,23 @@ def test_text_ops(self):

# Check that (parse "(my atom)") == (my atom)
self.assertEqualMettaRunnerResults(metta.run("!(parse \"(my atom)\")"),
[[ValueAtom(E(S("my"), S("atom")))]])
[[E(S("my"), S("atom"))]])

#unstable renaming of variables causes random failures of the test
#self.assertEqualMettaRunnerResults(metta.run('!(parse "$X")'),
# [[(V("X"))]])

self.assertEqualMettaRunnerResults(metta.run('!(parse "\\"A\\"")'),
[[(ValueAtom("A"))]])

#self.assertEqualMettaRunnerResults(metta.run('!(parse "(func (Cons $x (Cons $xs $xss))) ")'),
# [[E(S("func"), E(S("Cons"), V("x"), E(S("Cons"), V("xs"), V("xss"))))]])

self.assertEqualMettaRunnerResults(metta.run('!(parse "(A 2 \'S\')")'),
[[E(S("A"), ValueAtom(2), ValueAtom(Char("S")))]])

# Check that (stringToChars "ABC") == ('A' 'B' 'C')
self.assertEqualMettaRunnerResults(metta.run("!(stringToChars \"ABC\")"),
self.assertEqualMettaRunnerResults(metta.run('!(stringToChars "ABC")'),
[[E(ValueAtom(Char("A")), ValueAtom(Char("B")), ValueAtom(Char("C")))]])

# Check that (charsToString ('A' 'B' 'C')) == "ABC"
Expand Down
Loading