sifflet-2.3.0: Simple, visual, functional language for learning about recursion.

Safe HaskellSafe
LanguageHaskell2010

Language.Sifflet.Export.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 where Source

Minimal complete definition

pyPretty

Instances

PyPretty Operator Source 
PyPretty Expr Source

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 Source 
PyPretty PStatement Source 
PyPretty PModule Source 

newtype 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 -> PStatement Source

Python return statement

condS :: Expr -> Expr -> Expr -> PStatement Source

Python if STATEMENT

var :: String -> Expr Source

Python variable

ident :: String -> Symbol Source

Python identifier

char :: Char -> Expr Source

Python character expression = string expression with one character

fun :: String -> [String] -> Expr -> PStatement Source

Defines function definition

operatorTable :: Map String Operator Source

Binary operators Precedence levels are rather *informally* described in The Python Language Reference, http://docs.python.org/reference/. 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.