@@ -25,6 +25,24 @@ impl EggSmolError {
2525 }
2626}
2727
28+ // Convert a Python Variant object into a rust variable, by getting the attributes
29+ fn get_variant ( obj : & PyAny ) -> PyResult < egg_smol:: ast:: Variant > {
30+ // TODO: Is there a way to do this more automatically?
31+ Ok ( egg_smol:: ast:: Variant {
32+ name : obj
33+ . getattr ( pyo3:: intern!( obj. py( ) , "name" ) ) ?
34+ . extract :: < String > ( ) ?
35+ . into ( ) ,
36+ cost : obj. getattr ( pyo3:: intern!( obj. py( ) , "cost" ) ) ?. extract ( ) ?,
37+ types : obj
38+ . getattr ( pyo3:: intern!( obj. py( ) , "types" ) ) ?
39+ . extract :: < Vec < String > > ( ) ?
40+ . into_iter ( )
41+ . map ( |x| x. into ( ) )
42+ . collect ( ) ,
43+ } )
44+ }
45+
2846#[ pymethods]
2947impl EGraph {
3048 #[ new]
@@ -34,6 +52,28 @@ impl EGraph {
3452 }
3553 }
3654
55+ /// declare_sort($self, name)
56+ /// --
57+ ///
58+ /// Declare a new sort with the given name.
59+ fn declare_sort ( & mut self , name : & str ) -> PyResult < ( ) > {
60+ // TODO: Should the name be a symbol? If so, how should we expose that
61+ // to Python?
62+ self . egraph
63+ . declare_sort ( name)
64+ . map_err ( |e| PyErr :: new :: < EggSmolError , _ > ( e. to_string ( ) ) )
65+ }
66+
67+ fn declare_constructor (
68+ & mut self ,
69+ #[ pyo3( from_py_with = "get_variant" ) ] variant : egg_smol:: ast:: Variant ,
70+ sort : & str ,
71+ ) -> PyResult < ( ) > {
72+ self . egraph
73+ . declare_constructor ( variant, sort)
74+ . map_err ( |e| PyErr :: new :: < EggSmolError , _ > ( e. to_string ( ) ) )
75+ }
76+
3777 /// parse_and_run_program($self, input)
3878 /// --
3979 ///
0 commit comments