Abstract syntax tree and pretty-printing for Python. Works for Python 2 and 3. A lot of the data structures are inspired by the language-python package; I have chosen not to have language-python as a dependency of sifflet-lib, however, because it would be overkill and still allows to little control over pretty-printing of Python expressionsw.
- class PyPretty a where
- data PModule = PModule [PStatement]
- data PStatement
- = PReturn Expr
- | PImport String
- | PCondS Expr PStatement PStatement
- | PFun Symbol [Symbol] PStatement
- alterParens :: (Expr -> Expr) -> PStatement -> PStatement
- ret :: Expr -> PStatement
- condS :: Expr -> Expr -> Expr -> PStatement
- var :: String -> Expr
- ident :: String -> Symbol
- char :: Char -> Expr
- fun :: String -> [String] -> Expr -> PStatement
- operatorTable :: Map String Operator
Documentation
PyPretty Operator | |
PyPretty Expr | Expr as an instance of PyPretty. This instance is only for Exprs as Python exprs, for export to Python! It will conflict with the one in ToHaskell.hs (or Haskell.hs). The EOp case needs work to deal with precedences and avoid unnecessary parens. Note that this instance declaration is for *Python* Exprs. Haskell Exprs of course should not be pretty-printed the same way! |
PyPretty Symbol | |
PyPretty PStatement | |
PyPretty PModule |
Python module -- essentially a list of statements; should it also have a name?
data PStatement Source
Python statement
PReturn Expr | |
PImport String | import statement |
PCondS Expr PStatement PStatement | if condition action alt-action |
PFun Symbol [Symbol] PStatement | function name, formal parameters, body |
alterParens :: (Expr -> Expr) -> PStatement -> PStatementSource
ret :: Expr -> PStatementSource
Python return statement
operatorTable :: Map String OperatorSource
Binary operators Precedence levels are rather *informally* described in The Python Language Reference, http:docs.python.orgreference. I am adopting the infixr levels from Haskell, which seem to be consistent with Python, at least for the operators that Sifflet uses.
| Operator information Arithmetic operators: + and - have lower precedence than *, , /, % | Comparison operators have precedence lower than any arithmetic operator. Here, I've specified associative = False, because association doesn't even make sense (well, it does in Python but not in other languages); (a == b) == c is in general not well typed.