Skip to content

Latest commit

 

History

History
50 lines (44 loc) · 1.3 KB

README.org

File metadata and controls

50 lines (44 loc) · 1.3 KB

Python AST generation / manipulation toolkit

>>> import ormsnack.tree
>>> \
... def foo(x: int) -> int:
...     return x + 23
>>> before_result = foo(234)
>>> before_foo_ast = ormsnack.tree.getast(foo)
>>> recomp = ormsnack.tree.compile_ast(before_foo_ast, foo.__name__)
>>> ormsnack.tree.run_sym(recomp, foo.__name__)(234)
257
>>>

The more high-level idea is to simplify AST nodes into a few categories (Right now there’s Statement, Block, Symbol and Literal). A few might follow. A top-level function will take any live object, transform to this simplified AST and use the Query DLS developed seperately in micropy.

You should be able to use the DSL to query for nodes in the simplified AST, e.g. like this using strict equal matching:

>>> from ormsnack import api
>>> \
... def foo(x: int) -> int:
...     return x + 23
>>> snack = api.snacka(foo)
>>> for node in snack == 'foo':
...     print(node)
<Block:foo(x)>
>>>

Or like this using regexp matching:

>>> from ormsnack import api
>>> \
... def foo(x: int) -> int:
...      "Docstring"
...      return x + 23
>>> snack = api.snacka(foo)
>>> for node in snack @ 'Docstring':
...     print(node)
<Literal:'Docstring':str>
>>>