sifflet-lib-1.2: Library of modules shared by sifflet and its tests and its exporters.

Sifflet.Foreign.Python

Description

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.

Synopsis

Documentation

class PyPretty a whereSource

Instances

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 

data PModule Source

Python module -- essentially a list of statements; should it also have a name?

Constructors

PModule [PStatement] 

data PStatement Source

Python statement

Constructors

PReturn Expr 
PImport String

import statement

PCondS Expr PStatement PStatement

if condition action alt-action

PFun Symbol [Symbol] PStatement

function name, formal parameters, body

ret :: Expr -> PStatementSource

Python return statement

condS :: Expr -> Expr -> Expr -> PStatementSource

Python if STATEMENT

var :: String -> ExprSource

Python variable

ident :: String -> SymbolSource

Python identifier

char :: Char -> ExprSource

Python character expression = string expression with one character

fun :: String -> [String] -> Expr -> PStatementSource

Defines function definition

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.